Beispiel #1
0
    def dataReceived(self, data):
        self.buffer += data
        self.totalData += len(data)

        parseBuffer = DataBuffer(self.buffer)
        while parseBuffer.lenLeft() > 0:
            packetType = ord(parseBuffer.read(1))

            #logging.debug("packet 0x%x" % (packetType,))
            try:
                format = PACKET_FORMATS[packetType]
            except KeyError:
                logging.error("invalid packet type - 0x%x %r %r" %
                              (packetType, len(self.buffer), self.buffer))
                logging.error("last 0x%x" % (self.lastPacket, ))
                #self.transport.loseConnection()
                return

            try:
                #format = PACKET_FORMATS[PACKET_NEW]
                parts = list(format.decode(parseBuffer) or [])
            except IncompleteDataError:
                break
                #pass
            self.buffer = parseBuffer.peek()

            #interesting benchmark stuffs
            #self.packetCounts[packetType] += 1
            #self.counter += 1
            #if self.counter % 1000 == 0:
            #    d = time.time()-self.lastCountTime
            #    logging.debug("1000 in %r - %r packets/s - %r kB/s" % (d, 1000/d, self.totalData/1000/d))
            #    logging.debug(repr(self.packetCounts.most_common(5)))
            #    self.lastCountTime = time.time()
            #    self.totalData = 0
            #    self.packetCounts.clear()

            self.lastPacket = packetType

            for handler in self.packetHandlers[packetType]:
                try:
                    ret = handler(parts)
                    #logging.info(parts)
                    if ret == False:
                        return
                except Exception as ex:
                    logging.error("Exception in handling packet 0x%02x:" %
                                  (packetType, ))
                    logging.exception(ex)
Beispiel #2
0
 def dataReceived(self, data):
     self.buffer += data
     self.totalData += len(data)
     
     parseBuffer = DataBuffer(self.buffer)
     while parseBuffer.lenLeft() > 0:
         packetType = ord(parseBuffer.read(1))
         
         #logging.debug("packet 0x%x" % (packetType,))
         try:
             format = PACKET_FORMATS[packetType]
         except KeyError:
             logging.error("invalid packet type - 0x%x %r %r" % (packetType,
                 len(self.buffer), self.buffer))
             logging.error("last 0x%x" % (self.lastPacket,))
             #self.transport.loseConnection()
             return
         
         try:
             #format = PACKET_FORMATS[PACKET_NEW]
             parts = list(format.decode(parseBuffer) or [])
         except IncompleteDataError:
             break
             #pass
         self.buffer = parseBuffer.peek()
         
         
         #interesting benchmark stuffs
         #self.packetCounts[packetType] += 1
         #self.counter += 1
         #if self.counter % 1000 == 0:
         #    d = time.time()-self.lastCountTime
         #    logging.debug("1000 in %r - %r packets/s - %r kB/s" % (d, 1000/d, self.totalData/1000/d))
         #    logging.debug(repr(self.packetCounts.most_common(5)))
         #    self.lastCountTime = time.time()
         #    self.totalData = 0
         #    self.packetCounts.clear()
         
         self.lastPacket = packetType
         
         for handler in self.packetHandlers[packetType]:
             try:
                 ret = handler(parts)
                 #logging.info(parts)
                 if ret == False:
                     return
             except Exception as ex:
                 logging.error("Exception in handling packet 0x%02x:" % (packetType,))
                 logging.exception(ex)
Beispiel #3
0
    def dataReceived(self, data):
        self.buffer += data

        parseBuffer = DataBuffer(self.buffer)
        while parseBuffer.lenLeft() > 0:
            packetType = ord(parseBuffer.read(1))

            #logging.debug("packet 0x%x" % (packetType,))
            try:
                format = PACKET_FORMATS[packetType]
            except KeyError:
                logging.error("invalid packet type - 0x%x %r %r" %
                              (packetType, len(self.buffer), self.buffer))

                self.transport.loseConnection()
                return

            try:
                parts = list(format.decode(parseBuffer) or [])
            except IncompleteDataError:
                break
            self.buffer = parseBuffer.peek()

            self.counter += 1

            #TODO: send the keepalive at some set interval
            if self.counter % 300 == 0:
                self.sendPacked(PACKET_KEEPALIVE)

            for handler in self.packetHandlers[packetType]:
                try:
                    ret = handler(parts)
                    if ret == False:
                        return
                except Exception as ex:
                    log.err(ex)
Beispiel #4
0
 def dataReceived(self, data):
     self.buffer += data
     
     parseBuffer = DataBuffer(self.buffer)
     while parseBuffer.lenLeft() > 0:
         packetType = ord(parseBuffer.read(1))
         
         #logging.debug("packet 0x%x" % (packetType,))
         try:
             format = PACKET_FORMATS[packetType]
         except KeyError:
             logging.error("invalid packet type - 0x%x %r %r" % (packetType,
                 len(self.buffer), self.buffer))
             
             self.transport.loseConnection()
             return
         
         try:
             parts = list(format.decode(parseBuffer) or [])
         except IncompleteDataError:
             break
         self.buffer = parseBuffer.peek()
         
         self.counter += 1
         
         #TODO: send the keepalive at some set interval
         if self.counter % 300 == 0:
             self.sendPacked(PACKET_KEEPALIVE)
         
         for handler in self.packetHandlers[packetType]:
             try:
                 ret = handler(parts)
                 if ret == False:
                     return
             except Exception as ex:
                 log.err(ex)