def main(self): self.commCondition.acquire() while self.switch: if len( self.packQueue ) <= 0: self.commCondition.wait() ( pid, code, package ) = self.packQueue.pop() Logger.info('received head from %s : [%d, %2x]' % ( self.address, pid, code ) ) Logger.debug('received package from %s : '%self.address, package) #检查权限 if not self.packager.codeAuthorized( code, self.myAuth ): Logger.warning( '%s do not allow to response the package %d'%( self.address, code ) ) continue #解析包 data = self.packager.parsePackage( self.mainThreadName, code, package ) packInfo = self.packager.codeFindPackage( code ) #发送回应包 if packInfo['ExistReply'] == 1: respPackage = self.packager.genPackage( self.mainThreadName, 'Response', pid ) self.send( 'Response', respPackage ) func = getattr( self, packInfo['Name'] ) unitNum = packInfo['StructLabel'].split( ',' ).__len__() data = [data[i : i + unitNum] for i in range( 0, len( data ), unitNum )] func( data ) self.commCondition.release()
def send(self, packName, package): if not self.packager.nameAuthorized( packName, self.myAuth ): Logger.warning( '%s do not allow to send %s package'%( self.address, packName ) ) return super( OuterService, self ).send( package ) if self.packager.existsReply( packName ): self.pid += 1 if self.respSurplusTime>0: self.respSurplusTime += self.timeout self.respWaitCount += 1 else: self.chkCondition.acquire() self.respSurplusTime += self.timeout self.respWaitCount += 1 self.chkCondition.notify() self.chkCondition.release()