Class: UnixSocks::TCPSocketServer
- Inherits:
-
Object
- Object
- UnixSocks::TCPSocketServer
- 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.
Instance Attribute Summary collapse
-
#hostname ⇒ String
readonly
Returns the hostname associated with this TCP socket server instance.
-
#port ⇒ Integer
readonly
Returns the port number associated with this TCP socket server instance.
Instance Method Summary collapse
-
#initialize(hostname: 'localhost', port:) ⇒ TCPSocketServer
constructor
Initializes a new UnixSocks::TCPSocketServer instance.
-
#receive(force: nil) {|UnixSocks::Message| ... } ⇒ Object
Receives messages from clients connected to the TCP socket.
-
#receive_in_background(force: nil) {|UnixSocks::Message| ... } ⇒ Thread
Runs the message receiver in a background thread to prevent blocking.
-
#to_url ⇒ String
Returns the URL representation of the TCP socket server configuration.
-
#transmit(message, close: false) ⇒ TCPSocket
Sends a message over a TCP connection to the configured hostname and port.
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.
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
#hostname ⇒ String (readonly)
Returns the hostname associated with this TCP socket server instance.
28 29 30 |
# File 'lib/unix_socks/tcp_socket_server.rb', line 28 def hostname @hostname end |
#port ⇒ Integer (readonly)
Returns the port number associated with this TCP socket server instance.
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.
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 = (socket) and block.() 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.
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_url ⇒ String
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.
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.
56 57 58 59 60 61 62 |
# File 'lib/unix_socks/tcp_socket_server.rb', line 56 def transmit(, close: false) socket = TCPSocket.new(@hostname, @port) socket.puts JSON() socket ensure close and socket&.close end |