From cd56e91057864c86a92da59458a10ce219ff95a3 Mon Sep 17 00:00:00 2001 From: Loudrob Date: Sun, 12 Jul 2015 12:48:58 -0400 Subject: [PATCH] New NetMessenger --- toontown/ai/ToontownAIRepository.py | 1 + toontown/distributed/ShardStatusReceiver.py | 5 ++- toontown/distributed/ToontownDistrictAI.py | 15 +++----- .../distributed/ToontownDistrictStatsAI.py | 6 ++-- .../distributed/ToontownInternalRepository.py | 25 ++++++++++--- .../distributed/ToontownNetMessengerAI.py | 35 +++++++++++++++++++ toontown/rpc/ToontownRPCHandler.py | 4 +-- toontown/suit/SuitInvasionManagerAI.py | 12 +++---- toontown/uberdog/ToontownUberRepository.py | 1 + 9 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 toontown/distributed/ToontownNetMessengerAI.py diff --git a/toontown/ai/ToontownAIRepository.py b/toontown/ai/ToontownAIRepository.py index 62b13812..1435a642 100755 --- a/toontown/ai/ToontownAIRepository.py +++ b/toontown/ai/ToontownAIRepository.py @@ -178,6 +178,7 @@ class ToontownAIRepository(ToontownInternalRepository): self.cogHeadquarters.append(BossbotHQAI.BossbotHQAI(self)) def handleConnected(self): + ToontownInternalRepository.handleConnected(self) self.districtId = self.allocateChannel() self.notify.info('Creating ToontownDistrictAI(%d)...' % self.districtId) self.distributedDistrict = ToontownDistrictAI(self) diff --git a/toontown/distributed/ShardStatusReceiver.py b/toontown/distributed/ShardStatusReceiver.py index e7c01afd..e297cdbb 100755 --- a/toontown/distributed/ShardStatusReceiver.py +++ b/toontown/distributed/ShardStatusReceiver.py @@ -5,10 +5,9 @@ class ShardStatusReceiver: self.shards = {} # Accept the shardStatus event: - self.air.netMessenger.accept('shardStatus', self, self.handleShardStatus) + self.air.accept('shardStatus', self.handleShardStatus) - # Query the status of any existing shards: - self.air.netMessenger.send('queryShardStatus') + self.air.sendNetEvent('queryShardStatus') def handleShardStatus(self, channel, status): self.shards.setdefault(channel, {}).update(status) diff --git a/toontown/distributed/ToontownDistrictAI.py b/toontown/distributed/ToontownDistrictAI.py index a98cb931..aff9e313 100755 --- a/toontown/distributed/ToontownDistrictAI.py +++ b/toontown/distributed/ToontownDistrictAI.py @@ -14,7 +14,7 @@ class ToontownDistrictAI(DistributedDistrictAI): # We want to handle shard status queries so that a ShardStatusReceiver # being created after we're generated will know where we're at: - self.air.netMessenger.accept('queryShardStatus', self, self.handleShardStatusQuery) + self.air.accept('shardStatus', self.handleShardStatusQuery) # Send a shard status update with the information we have: status = { @@ -22,12 +22,7 @@ class ToontownDistrictAI(DistributedDistrictAI): 'name': self.name, 'created': int(time.time()) } - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) - - # Add a post remove shard status update in-case we go down: - status = {'available': False} - datagram = self.air.netMessenger.prepare('shardStatus', [self.air.ourChannel, status]) - self.air.addPostRemove(datagram) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) def handleShardStatusQuery(self): # Send a shard status update with the information we have: @@ -36,18 +31,18 @@ class ToontownDistrictAI(DistributedDistrictAI): 'name': self.name, 'created': int(time.time()) } - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) def setName(self, name): DistributedDistrictAI.setName(self, name) # Send a shard status update containing our name: status = {'name': name} - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) def setAvailable(self, available): DistributedDistrictAI.setAvailable(self, available) # Send a shard status update containing our availability: status = {'available': bool(available)} - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) diff --git a/toontown/distributed/ToontownDistrictStatsAI.py b/toontown/distributed/ToontownDistrictStatsAI.py index 15a615b1..60d71fae 100755 --- a/toontown/distributed/ToontownDistrictStatsAI.py +++ b/toontown/distributed/ToontownDistrictStatsAI.py @@ -13,12 +13,12 @@ class ToontownDistrictStatsAI(DistributedObjectAI): # We want to handle shard status queries so that a ShardStatusReceiver # being created after we're generated will know where we're at: - self.air.netMessenger.accept('queryShardStatus', self, self.handleShardStatusQuery) + self.air.accept('shardStatus', self.handleShardStatusQuery) def handleShardStatusQuery(self): # Send a shard status update containing our population: status = {'population': self.avatarCount} - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sentNetEvent('shardStatus', [self.air.ourChannel, status]) def setDistrictId(self, districtId): self.districtId = districtId @@ -38,7 +38,7 @@ class ToontownDistrictStatsAI(DistributedObjectAI): # Send a shard status update containing our population: status = {'population': self.avatarCount} - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) def d_setAvatarCount(self, avatarCount): self.sendUpdate('setAvatarCount', [avatarCount]) diff --git a/toontown/distributed/ToontownInternalRepository.py b/toontown/distributed/ToontownInternalRepository.py index 084809ad..49ad9269 100755 --- a/toontown/distributed/ToontownInternalRepository.py +++ b/toontown/distributed/ToontownInternalRepository.py @@ -1,5 +1,6 @@ from direct.distributed.AstronInternalRepository import AstronInternalRepository from otp.distributed.OtpDoGlobals import * +from toontown.distributed.ToontownNetMessengerAI import ToontownNetMessengerAI class ToontownInternalRepository(AstronInternalRepository): GameGlobalsId = OTP_DO_ID_TOONTOWN @@ -10,11 +11,25 @@ class ToontownInternalRepository(AstronInternalRepository): AstronInternalRepository.__init__( self, baseChannel, serverId=serverId, dcFileNames=dcFileNames, dcSuffix=dcSuffix, connectMethod=connectMethod, threadedNet=threadedNet) - - self.netMessenger.register(0, 'shardStatus') - self.netMessenger.register(1, 'queryShardStatus') - self.netMessenger.register(2, 'startInvasion') - self.netMessenger.register(3, 'stopInvasion') + + def handleConnected(self): + self.__messenger = ToontownNetMessengerAI(self) + + def sendNetEvent(self, message, sentArgs=[]): + self.__messenger.send(message, sentArgs) + + def addExitEvent(self, message): + dg = self.__messenger.prepare(message) + self.addPostRemove(dg) + + def handleDatagram(self, di): + msgType = self.getMsgType() + + if msgType == self.__messenger.msgType: + self.__messenger.handle(msgType, di) + return + + AstronInternalRepository.handleDatagram(self, di) def getAvatarIdFromSender(self): return self.getMsgSender() & 0xFFFFFFFF diff --git a/toontown/distributed/ToontownNetMessengerAI.py b/toontown/distributed/ToontownNetMessengerAI.py new file mode 100644 index 00000000..81ef7ff8 --- /dev/null +++ b/toontown/distributed/ToontownNetMessengerAI.py @@ -0,0 +1,35 @@ +from direct.directnotify import DirectNotifyGlobal +from direct.distributed.PyDatagram import PyDatagram +import cPickle, zlib + +class ToontownNetMessengerAI: + """ + This works very much like the NetMessenger class except that + this is much simpler and makes much more sense. + """ + notify = DirectNotifyGlobal.directNotify.newCategory('ToontownNetMessengerAI') + + def __init__(self, air, msgChannel=40000, msgType=54321): + self.air = air + self.air.registerForChannel(msgChannel) + self.msgChannel = msgChannel + self.msgType = msgType + + def prepare(self, message, sentArgs=[]): + dg = PyDatagram() + dg.addServerHeader(self.msgChannel, self.air.ourChannel, self.msgType) + dg.addString(message) + dg.addString(zlib.compress(cPickle.dumps(sentArgs))) + return dg + + def send(self, message, sentArgs=[]): + self.notify.debug('sendNetEvent: %s %r' % (message, sentArgs)) + dg = self.prepare(message, sentArgs) + self.air.send(dg) + + def handle(self, msgType, di): + message = di.getString() + data = zlib.decompress(di.getString()) + sentArgs = cPickle.loads(data) + messenger.send(message, sentArgs) + \ No newline at end of file diff --git a/toontown/rpc/ToontownRPCHandler.py b/toontown/rpc/ToontownRPCHandler.py index cf246871..0f98f723 100755 --- a/toontown/rpc/ToontownRPCHandler.py +++ b/toontown/rpc/ToontownRPCHandler.py @@ -716,7 +716,7 @@ class ToontownRPCHandler(ToontownRPCHandlerBase): = Extra invasion flags. = The invasion type. """ - self.air.netMessenger.send( + self.air.sendNetEvent( 'startInvasion', [shardId, suitDeptIndex, suitTypeIndex, flags, type]) @@ -730,7 +730,7 @@ class ToontownRPCHandler(ToontownRPCHandlerBase): [int shardId] = The ID of the shard that is running the invasion to be terminated. """ - self.air.netMessenger.send('stopInvasion', [shardId]) + self.air.sendNetEvent('stopInvasion', [shardId]) # --- NAME APPROVAL --- diff --git a/toontown/suit/SuitInvasionManagerAI.py b/toontown/suit/SuitInvasionManagerAI.py index cf1a595b..055bf96e 100755 --- a/toontown/suit/SuitInvasionManagerAI.py +++ b/toontown/suit/SuitInvasionManagerAI.py @@ -18,14 +18,14 @@ class SuitInvasionManagerAI: self.suitTypeIndex = None self.flags = 0 - self.air.netMessenger.accept( - 'startInvasion', self, self.handleStartInvasion) - self.air.netMessenger.accept( - 'stopInvasion', self, self.handleStopInvasion) + self.air.accept( + 'startInvasion', self.handleStartInvasion) + self.air.accept( + 'stopInvasion', self.handleStopInvasion) # We want to handle shard status queries so that a ShardStatusReceiver # being created after we're created will know where we're at: - self.air.netMessenger.accept('queryShardStatus', self, self.sendInvasionStatus) + self.air.accept('queryShardStatus', self.sendInvasionStatus) self.sendInvasionStatus() @@ -224,4 +224,4 @@ class SuitInvasionManagerAI: } else: status = {'invasion': None} - self.air.netMessenger.send('shardStatus', [self.air.ourChannel, status]) + self.air.sendNetEvent('shardStatus', [self.air.ourChannel, status]) diff --git a/toontown/uberdog/ToontownUberRepository.py b/toontown/uberdog/ToontownUberRepository.py index 720f10c0..8d3464b0 100755 --- a/toontown/uberdog/ToontownUberRepository.py +++ b/toontown/uberdog/ToontownUberRepository.py @@ -29,6 +29,7 @@ class ToontownUberRepository(ToontownInternalRepository): self.notify.setInfo(True) def handleConnected(self): + ToontownInternalRepository.handleConnected(self) rootObj = DistributedDirectoryAI(self) rootObj.generateWithRequiredAndId(self.getGameDoId(), 0, 0)