Sha256: 3101da5404bd1196cb1d44d70bdd5f789c9632e624924f30f0ba4964783d388f

Contents?: true

Size: 810 Bytes

Versions: 1

Compression:

Stored size: 810 Bytes

Contents

require 'socket'
require 'sanford-protocol'

module AndSon

  class Connection < Struct.new(:host, :port)
    module NoRequest
      def self.to_s; "[?]"; end
    end

    def open
      protocol_connection = Sanford::Protocol::Connection.new(tcp_socket)
      yield protocol_connection if block_given?
    ensure
      protocol_connection.close rescue false
    end

    private

    # TCP_NODELAY is set to disable buffering. In the case of Sanford
    # communication, we have all the information we need to send up front and
    # are closing the connection, so it doesn't need to buffer.
    # See http://linux.die.net/man/7/tcp

    def tcp_socket
      TCPSocket.new(host, port).tap do |socket|
        socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
and-son-0.1.0 lib/and-son/connection.rb