New NetMessenger

This commit is contained in:
Loudrob 2015-07-12 12:48:58 -04:00
parent 3c8eb1c848
commit cd56e91057
9 changed files with 75 additions and 29 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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])

View file

@ -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])

View file

@ -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

View file

@ -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)

View file

@ -716,7 +716,7 @@ class ToontownRPCHandler(ToontownRPCHandlerBase):
<int flags> = Extra invasion flags.
<int type> = 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 ---

View file

@ -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])

View file

@ -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)