|
7 | 7 | import datetime |
8 | 8 |
|
9 | 9 | import construct |
10 | | -import twisted |
11 | 10 | from twisted.internet import reactor |
12 | 11 | from twisted.internet.error import CannotListenError |
13 | 12 | from twisted.internet.protocol import ClientFactory, ServerFactory, Protocol, connectionDone, DatagramProtocol |
@@ -110,7 +109,7 @@ def connectionMade(self): |
110 | 109 | self.plugin_manager = self.factory.plugin_manager |
111 | 110 | self.packet_stream = PacketStream(self) |
112 | 111 | self.packet_stream.direction = packets.Direction.CLIENT |
113 | | - logger.info("Connection established from IP: %s" % self.transport.getPeer().host) |
| 112 | + logger.info("Connection established from IP: %s", self.transport.getPeer().host) |
114 | 113 | reactor.connectTCP(self.config.upstream_hostname, self.config.upstream_port, |
115 | 114 | StarboundClientFactory(self), timeout=self.config.server_connect_timeout) |
116 | 115 |
|
@@ -431,32 +430,28 @@ def connectionLost(self, reason=connectionDone): |
431 | 430 | :return: None |
432 | 431 | """ |
433 | 432 | try: |
434 | | - x = build_packet(packets.Packets.CLIENT_DISCONNECT, |
| 433 | + if self.client_protocol is not None: |
| 434 | + x = build_packet(packets.Packets.CLIENT_DISCONNECT, |
435 | 435 | packets.client_disconnect().build(Container(data=0))) |
436 | | - |
437 | | - if hasattr(self, "protocol") and self.protocol is not None: |
438 | | - if self.player is not None: |
| 436 | + if self.player is not None and self.player.logged_in: |
439 | 437 | self.client_disconnect(x) |
440 | | - self.player.logged_in = False |
441 | 438 | self.player.protocol = None |
442 | | - if self.client_protocol is not None: |
| 439 | + self.player = None |
443 | 440 | self.client_protocol.transport.write(x) |
444 | | - del self.client_protocol |
445 | | - except twisted.internet.error.ConnectionDone: |
446 | | - logger.debug("Connection was closed cleanly.") |
447 | | - except Exception as e: |
448 | | - logger.error("Couldn't disconnect protocol. %s", str(e)) |
| 441 | + self.client_protocol.transport.abortConnection() |
| 442 | + except: |
| 443 | + logger.error("Couldn't disconnect protocol.") |
449 | 444 | finally: |
450 | | - self.die() |
| 445 | + try: |
| 446 | + self.factory.protocols.pop(self.id) |
| 447 | + except: |
| 448 | + self.logger.trace("Protocol was not in factory list. This should not happen.") |
| 449 | + finally: |
| 450 | + logger.info("Lost connection from IP: %s", self.transport.getPeer().host) |
| 451 | + self.transport.abortConnection() |
451 | 452 |
|
452 | 453 | def die(self): |
453 | | - if self.player is not None and self.player.protocol is not None: |
454 | | - self.transport.abortConnection() |
455 | | - self.factory.protocols.pop(self.id) |
456 | | - try: |
457 | | - self.client_protocol.transport.abortConnection() |
458 | | - except AttributeError: |
459 | | - pass |
| 454 | + self.connectionLost() |
460 | 455 |
|
461 | 456 | def connectionFailed(self, *args, **kwargs): |
462 | 457 | self.connectionLost() |
|
0 commit comments