Ejemplo n.º 1
0
 def handlePacket(self, packet):
     if packet.split:
         self.handleSplit(packet)
         return
     id = packet.buffer[0]
     dataPacket = None
     pk = None
     sendPacket = None
     if id < 0x80:
         if self.state == self.status["Connecting"]:
             if id == PacketIdentifiers.ConnectionRequest:
                 dataPacket = ConnectionRequest()
                 dataPacket.buffer = packet.buffer
                 dataPacket.decode()
                 pk = ConnectionRequestAccepted()
                 pk.clientAddress = self.address
                 pk.systemIndex = 0
                 pk.requestTime = dataPacket.time
                 pk.time = Binary.flipLongEndianness(int(timeNow(
                 ))) if Binary.ENDIANNESS == Binary.LITTLE_ENDIAN else int(
                     timeNow())
                 pk.encode()
                 sendPacket = EncapsulatedPacket()
                 sendPacket.reliability = 0
                 sendPacket.buffer = pk.buffer
                 self.addToQueue(sendPacket, self.priority["Immediate"])
             elif id == PacketIdentifiers.NewIncomingConnection:
                 dataPacket = NewIncomingConnection()
                 dataPacket.buffer = packet.buffer
                 dataPacket.decode()
                 serverPort = self.server.socket.address.port
                 if dataPacket.address.port == serverPort:
                     self.state = status["Connected"]
                     self.server.interface.onOpenConnection(self)
         elif id == PacketIdentifiers.DisconnectNotification:
             self.disconnect('client disconnect')
         elif id == PacketIdentifiers.ConnectedPing:
             dataPacket = ConnectedPing()
             dataPacket.buffer = packet.buffer
             dataPacket.decode()
             pk = ConnectedPong()
             pk.pingTime = dataPacket.time
             pk.pongTime = Binary.flipLongEndianness(int(timeNow(
             ))) if Binary.ENDIANNESS == Binary.LITTLE_ENDIAN else int(
                 timeNow())
             pk.encode()
             sendPacket = EncapsulatedPacket()
             sendPacket.reliability = 0
             sendPacket.buffer = pk.buffer
             self.addToQueue(sendPacket)
     elif self.state == self.status["Connected"]:
         self.interface.onEncapsulated(packet, self.address)
Ejemplo n.º 2
0
 def addEncapsulatedToQueue(self, packet, flags=priority["Normal"]):
     if Reliability.isReliable(packet.reliability):
         packet.messageIndex = self.messageIndex
         self.messageIndex += 1
         if packet.reliability == Reliability.reliableOrdered:
             packet.orderIndex = self.channelIndex[packet.orderChannel]
             self.channelIndex[packet.orderChannel] += 1
     if packet.getTotalLength() > self.mtuSize:
         buffers = {}
         i = 0
         splitIndex = 0
         while i < len(packet.buffer):
             buffers[splitIndex] = packet.buffer[i:i + self.mtuSize]
             splitIndex += 1
             i += self.mtuSize
         for index, buffer in buffers.items():
             pk = EncapsulatedPacket()
             pk.splitId = self.splitId
             pk.splitCount = len(buffers)
             pk.reliability = packet.reliability
             pk.splitIndex = index
             pk.buffer = buffer
             if index != 0:
                 pk.messageIndex = self.messageIndex
                 self.messageIndex += 1
             if pk.reliability == Reliability.reliableOrdered:
                 pk.orderChannel = packet.orderChannel
                 pk.orderIndex = packet.orderIndex
             self.addToQueue(pk, flags)
         self.splitId += 1
     else:
         self.addToQueue(packet, flags)
Ejemplo n.º 3
0
 def addEncapsulatedToQueue(self, packet, flags = priority["Normal"]):
     packet.needAck = flags & 0b00001000
     if packet.needAck > 0:
         self.needAck[packet.identifierACK] = []
     if 2 <= packet.reliability <= 7:
         packet.messageIndex = self.messageIndex
         self.messageIndex += 1
         if packet.reliability == 3:
             packet.orderIndex = self.channelIndex[packet.orderChannel]
             self.channelIndex[packet.orderChannel] += 1
     if packet.getTotalLength() + 4 > self.mtuSize:
         buffers = []
         for i in range(0, len(packet.buffer), self.mtuSize - 34):
             buffers.append(packet.buffer[i:i - (self.mtuSize - 34)])
         self.splitId += 1
         splitId = self.splitId % 65536
         for count, buffer in enumerate(buffers):
             pk = EncapsulatedPacket()
             pk.splitId = splitId
             pk.split = True
             pk.splitCount = len(buffers)
             pk.reliability = packet.reliability
             pk.splitIndex = count
             pk.buffer = buffer
             if count > 0:
                 pk.messageIndex = self.messageIndex
                 self.messageIndex += 1
             else:
                 pk.messageIndex = packet.messageIndex
             if pk.reliability == 3:
                 pk.orderChannel = packet.orderChannel
                 pk.orderIndex = packet.orderIndex
             self.addToQueue(pk, flags | self.priority["Immediate"])
     else:
         self.addToQueue(packet, flags)
Ejemplo n.º 4
0
 def sendDataPacket(self, packet):
     pk = BatchPacket()
     pk.addPacket(packet)
     pk.encode()
     sendPacket = EncapsulatedPacket()
     sendPacket.reliability = 0
     sendPacket.buffer = pk.buffer
     self.connection.addEncapsulatedToQueue(sendPacket)
Ejemplo n.º 5
0
 def handleSplit(self, packet):
     if packet.splitId in self.splitPackets:
         value = self.splitPackets[packet.splitId]
         value[packet.splitIndex] = packet
         self.splitPackets[packet.splitId] = value
     else:
         self.splitPackets[packet.splitId] = {packet.splitIndex: packet}
     localSplits = self.splitPackets[packet.splitId]
     if len(localSplits) == packet.splitCount:
         pk = EncapsulatedPacket()
         for count, packet in enumerate(localSplits):
             BinaryStream.put(packet.buffer)
         del self.splitPackets[packet.splitId]
         pk.buffer = BinaryStream.buffer
         self.receivePacket(pk)
Ejemplo n.º 6
0
 def handleSplit(self, packet):
     if packet.splitId in self.splitPackets:
         value = self.splitPackets[packet.splitId]
         value[packet.splitIndex] = packet
         self.splitPackets[packet.splitId] = value
     else:
         self.splitPackets[packet.splitId] = {packet.splitIndex: packet}
     localSplits = self.splitPackets[packet.splitId]
     if len(localSplits) == packet.splitCount:
         pk = EncapsulatedPacket()
         stream = BinaryStream()
         for index, value in localSplits.items():
             stream.put(value.buffer)
         del self.splitPackets[packet.splitId]
         pk.buffer = stream.buffer
         self.receivePacket(pk)