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