Class: OpenC3::TcpipSocketStream

Inherits:
Stream show all
Defined in:
lib/openc3/streams/tcpip_socket_stream.rb

Overview

Data Stream which reads and writes from Tcpip Sockets.

Direct Known Subclasses

TcpipClientStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(write_socket, read_socket, write_timeout, read_timeout) ⇒ TcpipSocketStream

Returns a new instance of TcpipSocketStream.

Parameters:

  • write_socket (Socket)

    Socket to write

  • read_socket (Socket)

    Socket to read

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 39

def initialize(write_socket, read_socket, write_timeout, read_timeout)
  super()

  @write_socket  = write_socket
  @read_socket   = read_socket
  @write_timeout = ConfigParser.handle_nil(write_timeout)
  if @write_timeout
    @write_timeout = @write_timeout.to_f
  else
    Logger.warn("Warning: To avoid interface lock, write_timeout can not be nil. Setting to 10 seconds.")
    @write_timeout = 10.0
  end
  @read_timeout  = ConfigParser.handle_nil(read_timeout)
  @read_timeout  = @read_timeout.to_f if @read_timeout

  # Mutex on write is needed to protect from commands coming in from more
  # than one tool
  @write_mutex = Mutex.new
  @pipe_reader, @pipe_writer = IO.pipe
  @connected = false
end

Instance Attribute Details

#write_socketObject (readonly)

Returns the value of attribute write_socket.



32
33
34
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 32

def write_socket
  @write_socket
end

Instance Method Details

#connectObject

Connect the stream



146
147
148
149
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 146

def connect
  # If called directly this class is acting as a server and does not need to connect the sockets
  @connected = true
end

#connected?Boolean

Returns Whether the sockets are connected.

Returns:

  • (Boolean)

    Whether the sockets are connected



152
153
154
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 152

def connected?
  @connected
end

#disconnectObject

Disconnect by closing the sockets



157
158
159
160
161
162
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 157

def disconnect
  OpenC3.close_socket(@write_socket)
  OpenC3.close_socket(@read_socket)
  @pipe_writer.write('.')
  @connected = false
end

#readString

Returns a binary string of data from the socket

Returns:

  • (String)

    Returns a binary string of data from the socket



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
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 62

def read
  raise "Attempt to read from write only stream" unless @read_socket

  # No read mutex is needed because reads happen serially
  begin
    while true # Loop until we get some data
      data = @read_socket.read_nonblock(65535, exception: false)
      raise EOFError, 'end of file reached' unless data

      if data == :wait_readable
        # Wait for the socket to be ready for reading or for the timeout
        begin
          result = IO.fast_select([@read_socket, @pipe_reader], nil, nil, @read_timeout)
          # If select returns something it means the socket is now available for
          # reading so retry the read. If it returns nil it means we timed out.
          # If the pipe is present that means we closed the socket
          if result
            if result.include?(@pipe_reader)
              raise IOError
            else
              next
            end
          else
            raise Timeout::Error, "Read Timeout"
          end
        rescue IOError, Errno::ENOTSOCK
          # These can happen with the socket being closed while waiting on select
          data = ''
        end
      end
      break
    end
  rescue Errno::ECONNRESET, Errno::ECONNABORTED, IOError, Errno::ENOTSOCK
    data = ''
  end
  data
end

#read_nonblockString

Returns a binary string of data from the socket. Always returns immediately

Returns:

  • (String)

    Returns a binary string of data from the socket. Always returns immediately



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 101

def read_nonblock
  # No read mutex is needed because reads happen serially
  begin
    data = @read_socket.read_nonblock(65535, exception: false)
    raise EOFError, 'end of file reached' unless data
    data = '' if data == :wait_readable
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNRESET, Errno::ECONNABORTED, IOError
    data = ''
  end
  data
end

#set_option(option_name, option_values) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 164

def set_option(option_name, option_values)
  option_name_upcase = option_name.upcase

  case option_name_upcase
  when 'KEEPALIVE'
    @write_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
    @read_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
  when 'KEEPCNT'
    @write_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, Integer(option_values[0]))
    @read_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, Integer(option_values[0]))
  when 'KEEPIDLE'
    @write_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, Integer(option_values[0]))
    @read_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, Integer(option_values[0]))
  when 'KEEPINTVL'
    @write_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, Integer(option_values[0]))
    @read_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, Integer(option_values[0]))
  end
end

#write(data) ⇒ Object

Parameters:

  • data (String)

    A binary string of data to write to the socket



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openc3/streams/tcpip_socket_stream.rb', line 114

def write(data)
  raise "Attempt to write to read only stream" unless @write_socket

  @write_mutex.synchronize do
    num_bytes_to_send = data.length
    total_bytes_sent = 0
    bytes_sent = 0
    data_to_send = data

    loop do
      begin
        bytes_sent = @write_socket.write_nonblock(data_to_send)
      rescue Errno::EAGAIN, Errno::EWOULDBLOCK
        # Wait for the socket to be ready for writing or for the timeout
        result = IO.fast_select(nil, [@write_socket], nil, @write_timeout)
        # If select returns something it means the socket is now available for
        # writing so retry the write. If it returns nil it means we timed out.
        if result
          retry
        else
          raise Timeout::Error, "Write Timeout"
        end
      end
      total_bytes_sent += bytes_sent
      break if total_bytes_sent >= num_bytes_to_send

      data_to_send = data[total_bytes_sent..-1]
    end
  end
end