Class: Cosmos::TcpipClientStream

Inherits:
TcpipSocketStream show all
Defined in:
lib/cosmos/streams/tcpip_client_stream.rb

Overview

Data Stream which reads and writes to TCPIP sockets. This class creates the actual sockets based on the constructor parameters. The rest of the interface is implemented by the super class TcpipSocketStream.

Constant Summary

Constants inherited from TcpipSocketStream

Cosmos::TcpipSocketStream::FAST_READ

Instance Attribute Summary

Attributes inherited from TcpipSocketStream

#write_socket

Instance Method Summary collapse

Methods inherited from TcpipSocketStream

#connected?, #disconnect, #read, #read_nonblock, #write

Methods inherited from Stream

#connected?, #disconnect, #read, #read_nonblock, #write

Constructor Details

#initialize(hostname, write_port, read_port, write_timeout, read_timeout, connect_timeout = 5.0) ⇒ TcpipClientStream

Returns a new instance of TcpipClientStream.

Parameters:

  • hostname (String)

    The host to connect to

  • write_port (Integer|nil)

    The port to write. Pass nil to make this a read only stream.

  • read_port (Integer|nil)

    The port to read. Pass nil to make this a write only stream.

  • write_timeout (Float|nil)

    Number of seconds to wait for the write to complete or nil to block until the socket is ready to write.

  • read_timeout (Float|nil)

    Number of seconds to wait for the read to complete or nil to block until the socket is ready to read.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cosmos/streams/tcpip_client_stream.rb', line 38

def initialize(hostname, write_port, read_port, write_timeout, read_timeout, connect_timeout = 5.0)
  @hostname = hostname
  if @hostname.to_s.upcase == 'LOCALHOST'
    @hostname = '127.0.0.1'
  end
  @write_port = ConfigParser.handle_nil(write_port)
  @write_port = Integer(write_port) if @write_port
  @read_port  = ConfigParser.handle_nil(read_port)
  @read_port  = Integer(read_port) if @read_port

  @write_addr = nil
  @read_addr = nil
  begin
    @write_addr = Socket.pack_sockaddr_in(@write_port, @hostname) if @write_port
    @read_addr = Socket.pack_sockaddr_in(@read_port, @hostname) if @read_port
  rescue => error
    if /getaddrinfo/.match?(error.message)
      raise "Invalid hostname: #{@hostname}"
    else
      raise error
    end
  end

  write_socket = nil
  if @write_addr
    write_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    write_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  end

  read_socket = nil
  if @read_addr
    if @write_port != @read_port
      read_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      read_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
    else
      read_socket = write_socket
    end
  end

  @connect_timeout = ConfigParser.handle_nil(connect_timeout)
  @connect_timeout = @connect_timeout.to_f if @connect_timeout

  super(write_socket, read_socket, write_timeout, read_timeout)
end

Instance Method Details

#connectObject

Connect the socket(s)



84
85
86
87
88
# File 'lib/cosmos/streams/tcpip_client_stream.rb', line 84

def connect
  connect_nonblock(@write_socket, @write_addr) if @write_socket
  connect_nonblock(@read_socket, @read_addr) if @read_socket and @read_socket != @write_socket
  super()
end