Class: Fluent::PluginHelper::Server::EventHandler::UDPServer

Inherits:
Coolio::IO
  • Object
show all
Defined in:
lib/fluent/plugin_helper/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock, max_bytes, flags, close_socket, log, under_plugin_development, &callback) ⇒ UDPServer

Returns a new instance of UDPServer.

Raises:

  • (ArgumentError)


521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/fluent/plugin_helper/server.rb', line 521

def initialize(sock, max_bytes, flags, close_socket, log, under_plugin_development, &callback)
  raise ArgumentError, "socket must be a UDPSocket: sock = #{sock}" unless sock.is_a?(UDPSocket)

  super(sock)

  @sock = sock
  @max_bytes = max_bytes
  @flags = flags
  @close_socket = close_socket
  @log = log
  @under_plugin_development = under_plugin_development
  @callback = callback

  on_readable_impl = case @callback.arity
                     when 1 then :on_readable_without_sock
                     when 2 then :on_readable_with_sock
                     else
                       raise "BUG: callback block must have 1 or 2 arguments"
                     end
  self.define_singleton_method(:on_readable, method(on_readable_impl))
end

Instance Attribute Details

#close_after_write_complete=(value) ⇒ Object (writeonly)

dummy for consistent method call in callbacks



519
520
521
# File 'lib/fluent/plugin_helper/server.rb', line 519

def close_after_write_complete=(value)
  @close_after_write_complete = value
end

Instance Method Details

#on_readable_with_sockObject



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/fluent/plugin_helper/server.rb', line 560

def on_readable_with_sock
  begin
    data, addr = @sock.recvfrom(@max_bytes)
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINTR, Errno::ECONNRESET, IOError, Errno::EBADF
    return
  rescue Errno::EMSGSIZE
    # Windows ONLY: This happens when the data size is larger than `@max_bytes`.
    @log.info "A received data was ignored since it was too large."
    return
  end
  @callback.call(data, UDPCallbackSocket.new(@sock, addr, close_socket: @close_socket))
rescue => e
  @log.error "unexpected error in processing UDP data", error: e
  @log.error_backtrace
  raise if @under_plugin_development
end

#on_readable_without_sockObject



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/fluent/plugin_helper/server.rb', line 543

def on_readable_without_sock
  begin
    data = @sock.recv(@max_bytes, @flags)
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINTR, Errno::ECONNRESET, IOError, Errno::EBADF
    return
  rescue Errno::EMSGSIZE
    # Windows ONLY: This happens when the data size is larger than `@max_bytes`.
    @log.info "A received data was ignored since it was too large."
    return
  end
  @callback.call(data)
rescue => e
  @log.error "unexpected error in processing UDP data", error: e
  @log.error_backtrace
  raise if @under_plugin_development
end