Skip to content

Commit

Permalink
support.endpoint: allow waiting for incoming connection.
Browse files Browse the repository at this point in the history
Some protocols start with the server sending some data as soon as the
client connects. If such data is not sent, the client will not proceed
with its first message.

When send is called before a client is connected, it is immediately
discarded. To wait for data sent by the client, a server implementation
can call recv_wait. This commit makes recv_wait return after a client
connects, too.
  • Loading branch information
neuschaefer authored and whitequark committed Jul 13, 2024
1 parent 553160c commit c8fa37e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions software/glasgow/support/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def connection_made(self, transport):
self._log(logging.INFO, "closing old connection")
self._transport.close()
self._new_transport = transport
self.data_received(b"")

def connection_lost(self, exc):
peername = self._transport.get_extra_info("peername")
Expand Down
18 changes: 18 additions & 0 deletions software/tests/support/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,21 @@ async def do_test_tcp(self):
def test_tcp(self):
asyncio.get_event_loop().run_until_complete(
self.do_test_tcp())

async def do_test_server_banner(self):
sock = ("tcp", "localhost", 2345)
endp = await ServerEndpoint("test_server_banner", logging.getLogger(__name__), sock)

async def endpoint_task():
await endp.recv_wait()
await endp.send(b"Hello")
asyncio.create_task(endpoint_task())

conn_rd, _ = await asyncio.open_connection(*sock[1:])
r = await conn_rd.read(5)
self.assertEqual(r, b"Hello")

def test_server_banner(self):
logging.basicConfig(level=logging.TRACE)
asyncio.get_event_loop().run_until_complete(
self.do_test_server_banner())

0 comments on commit c8fa37e

Please sign in to comment.