|
7 | 7 | import datetime |
8 | 8 |
|
9 | 9 | import construct |
| 10 | +import twisted |
10 | 11 | from twisted.internet import reactor |
11 | 12 | from twisted.internet.error import CannotListenError |
12 | 13 | from twisted.internet.protocol import ClientFactory, ServerFactory, Protocol, connectionDone, DatagramProtocol |
@@ -432,19 +433,24 @@ def connectionLost(self, reason=connectionDone): |
432 | 433 | x = build_packet(packets.Packets.CLIENT_DISCONNECT, |
433 | 434 | packets.client_disconnect().build(Container(data=0))) |
434 | 435 |
|
435 | | - if self.player is not None: |
436 | | - if self.protocol is not None: |
| 436 | + if hasattr(self, "protocol") and self.protocol is not None: |
| 437 | + if self.player is not None: |
437 | 438 | self.client_disconnect(x) |
438 | 439 | self.player.logged_in = False |
439 | 440 | self.player.protocol = None |
440 | | - self.client_protocol.transport.write(x) |
441 | | - except: |
442 | | - logger.error("Couldn't disconnect protocol.") |
| 441 | + if self.client_protocol is not None: |
| 442 | + self.client_protocol.transport.write(x) |
| 443 | + del self.client_protocol |
| 444 | + except twisted.internet.error.ConnectionDone: |
| 445 | + logger.debug("Connection was closed cleanly.") |
| 446 | + except Exception as e: |
| 447 | + logger.error("Couldn't disconnect protocol. %s", str(e)) |
443 | 448 | finally: |
444 | 449 | self.die() |
445 | 450 |
|
446 | 451 | def die(self): |
447 | | - self.transport.abortConnection() |
| 452 | + if self.player.protocol is not None: |
| 453 | + self.transport.abortConnection() |
448 | 454 | self.factory.protocols.pop(self.id) |
449 | 455 | try: |
450 | 456 | self.client_protocol.transport.abortConnection() |
|
0 commit comments