Module: Contrast::Api::Communication::Socket
- Included in:
- TcpSocket, UnixSocket
- Defined in:
- lib/contrast/api/communication/socket.rb
Overview
Behavior common to all sockets used to communicate with the Contrast Service.
Constant Summary collapse
- SOCKET_MONITOR =
Monitor.new
Instance Method Summary collapse
-
#new_socket ⇒ Object
Override this method to return a socket.
-
#send_marshaled(marshaled) ⇒ String
Send a message across the socket and read back the response.
Instance Method Details
#new_socket ⇒ Object
Override this method to return a socket. Should be interface compatible with TCPSocket, UNIXSocket, etc.
39 40 41 |
# File 'lib/contrast/api/communication/socket.rb', line 39 def new_socket raise NoMethodError, 'This is abstract, override it.' end |
#send_marshaled(marshaled) ⇒ String
Send a message across the socket and read back the response.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/contrast/api/communication/socket.rb', line 17 def send_marshaled marshaled SOCKET_MONITOR.synchronize do socket = new_socket # write message length len = marshaled.length socket.write([len].pack(Contrast::Api::ENCODING_STRING)) # write the entire message socket.write(marshaled) socket.close_write # read the response length len = socket.read(4).unpack1(Contrast::Api::ENCODING_STRING) # read expected response to end socket.read(len) end end |