Module: NewBridge::TcpFramed
- Defined in:
- lib/new_bridge/tcp_framed.rb
Overview
Minimal TCP client/server for framed byte payloads (Phase 0).
Defined Under Namespace
Classes: Error
Class Method Summary collapse
- .send_receive(host, port, payload) ⇒ Object
-
.with_echo_server(host: '127.0.0.1') ⇒ Object
Yields bound port; server echoes one frame then closes.
Class Method Details
.send_receive(host, port, payload) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/new_bridge/tcp_framed.rb', line 34 def send_receive(host, port, payload) sock = TCPSocket.new(host, port) begin Framing.write_frame(sock, payload) Framing.read_frame(sock) ensure sock.close end end |
.with_echo_server(host: '127.0.0.1') ⇒ Object
Yields bound port; server echoes one frame then closes.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/new_bridge/tcp_framed.rb', line 14 def with_echo_server(host: '127.0.0.1') server = TCPServer.new(host, 0) port = server.addr[1] thread = Thread.new do client = server.accept begin payload = Framing.read_frame(client) Framing.write_frame(client, payload) ensure client.close rescue nil end ensure server.close rescue nil end yield port ensure thread&.join(5) server&.close rescue nil end |