def __readNextObjectFromUnzipped(self): '''Read, and return, the next object from the unzipped input buffer.''' unpacked = self.__unpackUnzippedInput(self.__unzippedInput[0:5]) pattern = re.compile("^(..)(.{8})$") matched = pattern.match(unpacked).groups() # Ping or pong -- just get these out of the way # (and log them for good measure) if matched is not None and (matched[0] == "03" or matched[0] == "04"): unzippedObject = self.__unzippedInput[0:5] self.__unzippedOutput += unzippedObject # Determine what type of packet this is if matched[0] == "03": objectType = "Ping" elif matched[0] == "04": objectType = "Pong" else: objectType = "ClearContext" self.log.debug("Received %s (%d)" % (objectType, int(matched[1], 16)), level=7) self.__unzippedInput = self.__unzippedInput[5:] self.__flushUnzippedOutput() return None objectSize = int(matched[1], 16) prefix = self.__unzippedInput[0:5] objectData = self.__unzippedInput[5:objectSize + 5] self.__unzippedInput = self.__unzippedInput[objectSize + 5:] # Conver the object to a plist and return it return Plist.convert(objectData)
def injectObjectToOutputStream(self, obj): '''Inject the given object into the output stream of this connection. This effectively sends the object to the foward destination connection for this connection. * obj -- The object to inject into the output stream ''' refId = obj.get("refId") if refId is not None and len(refId) > 0: # If the refIds have changed than this is a new session if self.__blockRestOfSession and self.__lastRefId != refId: self.__blockRestOfSession = False self.__setRefId(refId) # Convert the object to a binary plist objectData = Plist.toBinary(obj, self.__logger) # Recalculate the size in case the object gets modified. If new size is # zero, then remove the object from the stream entirely objLen = len(objectData) self.log.debug("Forwarding object [%s] to %s, len: %d" % \ (obj.get('class'), self.__connectionManager.getForwardName(self.__direction), objLen), level=5) if objLen > 0: # Get the header containing the length of the object, and pad # zeros to the left to make it ten digits long hexStr = '%X' % (0x0200000000 + objLen) hexStr = hexStr.rjust(10, '0') # Get the prefix by converting the hex length into binary prefix = unhexlify(hexStr) self.__unzippedOutput += prefix + objectData self.__flushUnzippedOutput()