Class: OpenC3::TcpipClientStream

Inherits:
TcpipSocketStream show all
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

WebSocketClientStream

Instance Attribute Summary

Attributes inherited from TcpipSocketStream

#write_socket

Instance Method Summary collapse

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.

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)

    Seconds to wait before aborting writes

  • read_timeout (Float|nil)

    Seconds to wait before aborting reads. Pass nil to block until the read is complete.

  • connect_timeout (Float|nil) (defaults to: 5.0)

    Seconds to wait before aborting connect. Pass nil to block until the connection is complete.



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



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