| 588 | sock.close() |
| 589 | |
| 590 | class ServerProto(asyncio.Protocol): |
| 591 | def __init__(self, on_con, on_con_lost, on_got_hello): |
| 592 | self.on_con = on_con |
| 593 | self.on_con_lost = on_con_lost |
| 594 | self.on_got_hello = on_got_hello |
| 595 | self.data = b'' |
| 596 | self.transport = None |
| 597 | |
| 598 | def connection_made(self, tr): |
| 599 | self.transport = tr |
| 600 | self.on_con.set_result(tr) |
| 601 | |
| 602 | def replace_transport(self, tr): |
| 603 | self.transport = tr |
| 604 | |
| 605 | def data_received(self, data): |
| 606 | self.data += data |
| 607 | if len(self.data) >= len(HELLO_MSG): |
| 608 | self.on_got_hello.set_result(None) |
| 609 | |
| 610 | def connection_lost(self, exc): |
| 611 | self.transport = None |
| 612 | if exc is None: |
| 613 | self.on_con_lost.set_result(None) |
| 614 | else: |
| 615 | self.on_con_lost.set_exception(exc) |
| 616 | |
| 617 | async def main(proto, on_con, on_con_lost, on_got_hello): |
| 618 | tr = await on_con |