class Backport::Adapter

The application interface between Backport servers and clients.

Public Class Methods

new(output, remote = {}) click to toggle source

@param output [IO] @param remote [Hash{Symbol => String, Integer}]

# File lib/backport/adapter.rb, line 7
def initialize output, remote = {}
  # Store internal data in a singleton method to avoid instance variable
  # collisions in custom adapters
  data = {
    out: output,
    remote: remote,
    closed: false
  }
  define_singleton_method :_data do
    data
  end
end

Public Instance Methods

close() click to toggle source

Close the client connection.

@note The adapter sets closed? to true and runs the closing callback.

The server is responsible for implementation details like closing the
client's socket.

@return [void]

# File lib/backport/adapter.rb, line 80
def close
  return if closed?
  _data[:closed] = true
  _data[:on_close].call unless _data[:on_close].nil?
  closing
end
closed?() click to toggle source
# File lib/backport/adapter.rb, line 69
def closed?
  _data[:closed] ||= false
end
closing() click to toggle source

A callback triggered when a client connection is closing. Subclasses and/or modules should override this method to provide their own functionality.

@return [void]

# File lib/backport/adapter.rb, line 41
def closing; end
opening() click to toggle source

A callback triggered when a client connection is opening. Subclasses and/or modules should override this method to provide their own functionality.

@return [void]

# File lib/backport/adapter.rb, line 34
def opening; end
receiving(data) click to toggle source

A callback triggered when the server receives data from the client. Subclasses and/or modules should override this method to provide their own functionality.

@param data [String] @return [void]

# File lib/backport/adapter.rb, line 49
def receiving(data); end
remote() click to toggle source

A hash of information about the client connection. The data can vary based on the transport, e.g., :hostname and :address for TCP connections or :filename for file streams.

@return [Hash{Symbol => String, Integer}]

# File lib/backport/adapter.rb, line 25
def remote
  _data[:remote]
end
write(data) click to toggle source

Send data to the client.

@param data [String] @return [void]

# File lib/backport/adapter.rb, line 55
def write data
  _data[:out].write data
  _data[:out].flush
end
write_line(data) click to toggle source

Send a line of data to the client.

@param data [String] @return [void]

# File lib/backport/adapter.rb, line 64
def write_line data
  _data[:out].puts data
  _data[:out].flush
end