Class: UnixSocks::TCPSocketServer

Inherits:
Object
  • Object
show all
Includes:
ServerShared
Defined in:
lib/unix_socks/tcp_socket_server.rb

Overview

Provides TCP socket-based communication for inter-process messaging.

This class enables sending and receiving messages over TCP connections, supporting both client and server functionality for network-based communication.

Examples:

server = UnixSocks::TCPSocketServer.new(hostname: 'localhost', port: 8080)
server.receive { |message| puts message.inspect }
server.transmit({ message: 'hello' })

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ServerShared

#to_uri, #transmit_with_response

Constructor Details

#initialize(hostname: 'localhost', port:) ⇒ TCPSocketServer

Initializes a new UnixSocks::TCPSocketServer instance.

Sets up the server with the specified hostname and port for TCP communication.

Parameters:

  • hostname (String) (defaults to: 'localhost')

    The hostname to bind to, defaults to ‘localhost’

  • port (Integer)

    The port number to bind to



21
22
23
# File 'lib/unix_socks/tcp_socket_server.rb', line 21

def initialize(hostname: 'localhost', port:)
  @hostname, @port = hostname, port
end

Instance Attribute Details

#hostnameString (readonly)

Returns the hostname associated with this TCP socket server instance.

Returns:

  • (String)

    The hostname that the server binds to for TCP communication.



28
29
30
# File 'lib/unix_socks/tcp_socket_server.rb', line 28

def hostname
  @hostname
end

#portInteger (readonly)

Returns the port number associated with this TCP socket server instance.

Returns:

  • (Integer)

    The port number that the server binds to for TCP communication.



33
34
35
# File 'lib/unix_socks/tcp_socket_server.rb', line 33

def port
  @port
end

Instance Method Details

#receive(force: nil) {|UnixSocks::Message| ... } ⇒ Object

Receives messages from clients connected to the TCP socket.

This method binds to the configured hostname and port, listens for incoming connections, and processes messages from connected clients. It accepts a single connection at a time and handles each message by parsing it from JSON and yielding it to the provided block. The socket connection is closed after processing each message.

Parameters:

  • force (nil) (defaults to: nil)

    This parameter is accepted for interface compatibility but is unused in the TCP implementation.

Yields:

Raises:

  • (Errno::EADDRINUSE)

    If the address is already in use.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/unix_socks/tcp_socket_server.rb', line 77

def receive(force: nil, &block)
  block or raise ArgumentError, 'require &block argument to receive messages'
  Addrinfo.tcp(@hostname, @port).bind do |server|
    server.listen(1)
    loop do
      socket, = server.accept
      message = pop_message(socket) and block.(message)
      socket.close
    end
  end
rescue Errno::EADDRINUSE => e
  raise UnixSocks::ServerError.mark(e)
end

#receive_in_background(force: nil) {|UnixSocks::Message| ... } ⇒ Thread

Runs the message receiver in a background thread to prevent blocking.

This method starts a new thread that continuously listens for incoming messages from connected clients. The server socket is created in the background, allowing the main execution flow to continue without waiting for messages.

Parameters:

  • force (nil) (defaults to: nil)

    This parameter is accepted for interface compatibility but is unused in the TCP implementation.

Yields:

Returns:

  • (Thread)

    The background thread running the receiver



103
104
105
106
107
108
109
# File 'lib/unix_socks/tcp_socket_server.rb', line 103

def receive_in_background(force: nil, &block)
  Thread.new do
    receive(&block)
  rescue Errno::EADDRINUSE => e
    raise UnixSocks::ServerError.mark(e)
  end
end

#to_urlString

Returns the URL representation of the TCP socket server configuration.

This method constructs and returns a URL string in the format “tcp://hostname:port” that represents the TCP socket server’s address and port configuration.

Returns:

  • (String)

    A URL string in the format “tcp://hostname:port”



42
43
44
# File 'lib/unix_socks/tcp_socket_server.rb', line 42

def to_url
  "tcp://#@hostname:#@port"
end

#transmit(message, close: false) ⇒ TCPSocket

Sends a message over a TCP connection to the configured hostname and port.

This method establishes a TCP socket connection to the specified hostname and port, serializes the provided message to JSON format, and writes it to the socket. The socket connection is returned after the message is sent.

Parameters:

  • message (Object)

    The message to be sent, which will be converted to JSON

  • close (TrueClass, FalseClass) (defaults to: false)

    Whether to close the socket after sending

Returns:

  • (TCPSocket)

    The socket connection that was used to transmit the message



56
57
58
59
60
61
62
# File 'lib/unix_socks/tcp_socket_server.rb', line 56

def transmit(message, close: false)
  socket = TCPSocket.new(@hostname, @port)
  socket.puts JSON(message)
  socket
ensure
  close and socket&.close
end