Sha256: fd63032764e8d77d5a7992cadd575f452268be20252f830df4c1fa42ed97c20b

Contents?: true

Size: 1.54 KB

Versions: 14

Compression:

Stored size: 1.54 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Api
    module Communication
      # Behavior common to all sockets used
      # to communicate with the Contrast Service.
      module Socket
        SOCKET_MONITOR = Monitor.new

        # Send a message across the socket and read back the response.
        # @param marshaled [String] some marshaled form of data to be sent across
        #   the socket. Typically an encoded form of Contrast::Api::Dtm::Message.
        # @return [String] some marshalled form of data returned by the socket.
        #   Typically an encoded form of Contrast::Api::Settings::AgentSettings.
        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

        # Override this method to return a socket.
        # Should be interface compatible with TCPSocket, UNIXSocket, etc.
        def new_socket
          raise NoMethodError, 'This is abstract, override it.'
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
contrast-agent-4.4.1 lib/contrast/api/communication/socket.rb
contrast-agent-4.4.0 lib/contrast/api/communication/socket.rb
contrast-agent-4.3.2 lib/contrast/api/communication/socket.rb
contrast-agent-4.3.1 lib/contrast/api/communication/socket.rb
contrast-agent-4.3.0 lib/contrast/api/communication/socket.rb
contrast-agent-4.2.0 lib/contrast/api/communication/socket.rb
contrast-agent-4.1.0 lib/contrast/api/communication/socket.rb
contrast-agent-4.0.0 lib/contrast/api/communication/socket.rb
contrast-agent-3.16.0 lib/contrast/api/communication/socket.rb
contrast-agent-3.15.0 lib/contrast/api/communication/socket.rb
contrast-agent-3.14.0 lib/contrast/api/communication/socket.rb
contrast-agent-3.13.2 lib/contrast/api/communication/socket.rb
contrast-agent-3.13.1 lib/contrast/api/communication/socket.rb
contrast-agent-3.13.0 lib/contrast/api/communication/socket.rb