Class: OpenC3::TcpipClientStream
- Inherits:
-
TcpipSocketStream
- Object
- Stream
- TcpipSocketStream
- OpenC3::TcpipClientStream
- Defined in:
- lib/openc3/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.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from TcpipSocketStream
Instance Method Summary collapse
-
#connect ⇒ Object
Connect the socket(s).
-
#initialize(hostname, write_port, read_port, write_timeout, read_timeout, connect_timeout = 5.0) ⇒ TcpipClientStream
constructor
A new instance of TcpipClientStream.
Methods inherited from TcpipSocketStream
#connected?, #disconnect, #read, #read_nonblock, #set_option, #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.
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 82 83 84 85 86 87 |
# File 'lib/openc3/streams/tcpip_client_stream.rb', line 44 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.) 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
#connect ⇒ Object
Connect the socket(s)
90 91 92 93 94 |
# File 'lib/openc3/streams/tcpip_client_stream.rb', line 90 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 |