Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

Commit

Permalink
Simplified disconnection logic. I believe this *should* fix the playe…
Browse files Browse the repository at this point in the history
…r full message.
  • Loading branch information
AMorporkian committed Feb 3, 2014
1 parent 4d092d6 commit d826215
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
9 changes: 6 additions & 3 deletions plugins/announcer_plugin/announcer_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from base_plugin import BasePlugin
from packets import connect_response


class Announcer(BasePlugin):
Expand All @@ -13,8 +14,10 @@ def activate(self):

def after_connect_response(self, data):
try:
self.factory.broadcast(
self.protocol.player.colored_name(self.config.colors) + " joined.", 0, "", "Announcer")
c = connect_response().parse(data.data)
if c.success:
self.factory.broadcast(
self.protocol.player.colored_name(self.config.colors) + " joined.", 0, "", "Announcer")
except AttributeError:
self.logger.debug("Attribute error in after_connect_response.")
return
Expand All @@ -23,7 +26,7 @@ def after_connect_response(self, data):
raise

def on_client_disconnect(self, data):
if not self.protocol.player.logged_in:
if self.protocol.player is not None:
self.factory.broadcast(self.protocol.player.colored_name(self.config.colors) + " left.", 0,
"", "Announcer")

2 changes: 1 addition & 1 deletion plugins/core/player_manager/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def after_world_start(self, data):
self.protocol.player.on_ship = True

def on_client_disconnect(self, player):
if self.protocol.player.logged_in:
if self.protocol.player is not None and self.protocol.player.logged_in:
self.logger.info("Player disconnected: %s", self.protocol.player.name)
self.protocol.player.logged_in = False

Expand Down
32 changes: 17 additions & 15 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def connectionMade(self):
self.plugin_manager = self.factory.plugin_manager
self.packet_stream = PacketStream(self)
self.packet_stream.direction = packets.Direction.CLIENT
logger.info("Connection established from IP: %s" % self.transport.getPeer().host)
logger.info("Connection established from IP: %s", self.transport.getPeer().host)
reactor.connectTCP(self.config.upstream_hostname, self.config.upstream_port,
StarboundClientFactory(self), timeout=self.config.server_connect_timeout)

Expand Down Expand Up @@ -402,7 +402,8 @@ def send_chat_message(self, text, channel=0, world='', name=''):
for line in lines:
self.send_chat_message(line)
return
logger.trace("Calling send_chat_message from player %s on channel %d on world '%s' with reported username of %s with message: %s", self.player.name, channel, world, name, text)
if self.player is not None:
logger.trace("Calling send_chat_message from player %s on channel %d on world '%s' with reported username of %s with message: %s", self.player.name, channel, world, name, text)
chat_data = packets.chat_received().build(Container(chat_channel=channel,
world=world,
client_id=0,
Expand All @@ -429,27 +430,28 @@ def connectionLost(self, reason=connectionDone):
:return: None
"""
try:
x = build_packet(packets.Packets.CLIENT_DISCONNECT,
if self.client_protocol is not None:
x = build_packet(packets.Packets.CLIENT_DISCONNECT,
packets.client_disconnect().build(Container(data=0)))

if self.player is not None:
if self.protocol is None:
if self.player is not None and self.player.logged_in:
self.client_disconnect(x)
self.player.logged_in = False
self.player.protocol = None
self.client_protocol.transport.write(x)
self.player = None
self.client_protocol.transport.write(x)
self.client_protocol.transport.abortConnection()
except:
logger.error("Couldn't disconnect protocol.")
finally:
self.die()
try:
self.factory.protocols.pop(self.id)
except:
self.logger.trace("Protocol was not in factory list. This should not happen.")
finally:
logger.info("Lost connection from IP: %s", self.transport.getPeer().host)
self.transport.abortConnection()

def die(self):
self.transport.abortConnection()
self.factory.protocols.pop(self.id)
try:
self.client_protocol.transport.abortConnection()
except AttributeError:
pass
self.connectionLost()

def connectionFailed(self, *args, **kwargs):
self.connectionLost()
Expand Down

0 comments on commit d826215

Please sign in to comment.