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)


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/fluent/plugin_helper/server.rb', line 502

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



500
501
502
# File 'lib/fluent/plugin_helper/server.rb', line 500

def close_after_write_complete=(value)
  @close_after_write_complete = value
end

Instance Method Details

#on_readable_with_sockObject



537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/fluent/plugin_helper/server.rb', line 537

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
  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



524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/fluent/plugin_helper/server.rb', line 524

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
  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