Пример #1
0
 def write(self, reader):
     reader.write(MAGIC)
     size = len(self.items)
     start = 8 + size * 4
     reader.writeInt(size + 1, True, False)
     values = ByteWriter()
     for index, item in enumerate(self.items):
         value_offset = values.tell()
         values.writeString(item.value)
         offset = value_offset + start
         reader.writeInt(offset | (item.type << 24), True, False)
     reader.write(str(values))
Пример #2
0
 def write(self, reader):
     reader.write(MAGIC)
     size = len(self.items)
     start = 8 + size * 4
     reader.writeInt(size + 1, True, False)
     values = ByteWriter()
     for item in self.items:
         value_offset = values.tell()
         values.writeString(item.value)
         offset = value_offset + start
         reader.writeInt(offset | (item.type << 24), True, False)
     reader.write(str(values))
Пример #3
0
 def send_contained(self,
                    contained,
                    unsequenced=False,
                    sender=None,
                    team=None,
                    save=False,
                    rule=None):
     if unsequenced:
         flags = enet.PACKET_FLAG_UNSEQUENCED
     else:
         flags = enet.PACKET_FLAG_RELIABLE
     writer = ByteWriter()
     contained.write(writer)
     data = bytes(writer)
     packet = enet.Packet(data, flags)
     for player in self.connections.values():
         if player is sender or player.player_id is None:
             continue
         if team is not None and player.team is not team:
             continue
         if rule is not None and rule(player) == False:
             continue
         if player.saved_loaders is not None:
             if save:
                 player.saved_loaders.append(data)
         else:
             player.peer.send(0, packet)
Пример #4
0
 def loader_received(self, packet):
     is_contained = hasattr(packet, 'data') and packet.id != MapData.id
     if is_contained:
         data = packet.data
         contained = load_server_packet(data)
     spam_class = contained.__class__ if is_contained else packet.__class__
     is_spammy = spam_class in self.spammy
     if is_spammy:
         self.spammy[spam_class] += 1
     else:
         message = None
         spammed = [spam.__name__ + (' x%s' % recv if recv > 1 else '')
             for spam, recv in self.spammy.items() if recv]
         if len(spammed):
             message = 'received ' + ', '.join(spammed)
             print message
             for spam in self.spammy:
                 self.spammy[spam] = 0            
         if is_contained:
             print contained
             print '    ', hexify(data)
     if packet.id == ConnectionResponse.id:
         self.connection_id = packet.connection_id
         self.unique = packet.unique
         self.connected = True
         print 'connected', self.connection_id, self.unique
     elif is_contained:
         if packet.id == SizedData.id:
             pass
             # reactor.callLater(1.0, self.send_join, team = 0, weapon = 0)
             #reactor.callLater(4.0, self.send_join, 1, 1)
             # open('testy.vxl', 'wb').write(str(self.map))
         # data = packet.data
         # if data.dataLeft():
             # raw_input('not completely parsed')
         # print contained.id
         if contained.id == loaders.MapStart.id:
             print 'map size:', contained.size
             self.map_size = contained.size
             self.map = ByteWriter()
         elif contained.id == loaders.MapChunk.id:
             self.map.write(contained.data)
             if len(self.map) == self.map_size:
                 print 'done!', len(self.map)
         # newdata = ByteWriter()
         # contained.write(newdata)
         # if contained.id != loaders.PlayerData.id:
             # if str(data) != str(newdata):
                 # print hexify(data)
                 # print hexify(newdata)
                 # raw_input('incorrect save func')
     elif packet.id == Ack.id:
         pass
     elif packet.id == Ping.id:
         pass
     elif packet.id == Packet10.id:
         print 'received packet10'
     else:
         print 'received:', packet
         raw_input('unknown packet')
Пример #5
0
    def __init__(self, protocol):
        BaseConnection.__init__(self)
        self.protocol = protocol
        self.auth_val = random.randint(0, 0xFFFF)
        self.map = ByteWriter()
        self.connections = MultikeyDict()
        self.spammy = {
            Ping: 0,
            loaders.MapChunk: 0,
            loaders.OrientationData: 0,
            loaders.PositionData: 0,
            loaders.InputData: 0
        }

        connect_request = ConnectionRequest()
        connect_request.auth_val = self.auth_val
        connect_request.client = True
        connect_request.version = self.get_version()
        self.send_loader(connect_request, False, 255)
Пример #6
0
 def send_contained(self, contained, sequence=False):
     if self.disconnected:
         return
     if sequence:
         flags = enet.PACKET_FLAG_UNSEQUENCED
     else:
         flags = enet.PACKET_FLAG_RELIABLE
     data = ByteWriter()
     contained.write(data)
     packet = enet.Packet(str(data), flags)
     self.peer.send(0, packet)
Пример #7
0
 def send_join(self, team=-1, weapon=-1):
     print 'joining team %s' % team
     loader = SizedData()
     data = ByteWriter()
     join = loaders.JoinTeam()
     join.name = 'flotothelo'
     join.team = team
     join.weapon = weapon
     join.write(data)
     loader.data = data
     self.send_loader(loader, True)
Пример #8
0
 def __init__(self, protocol):
     BaseConnection.__init__(self)
     self.protocol = protocol
     self.auth_val = random.randint(0, 0xFFFF)
     self.map = ByteWriter()
     self.connections = MultikeyDict()
     self.spammy = {Ping : 0, loaders.MapChunk : 0, 
         loaders.OrientationData : 0,
         loaders.PositionData : 0, loaders.InputData : 0}
     
     connect_request = ConnectionRequest()
     connect_request.auth_val = self.auth_val
     connect_request.client = True
     connect_request.version = self.get_version()
     self.send_loader(connect_request, False, 255)
Пример #9
0
    def broadcast_contained(self,
                            contained,
                            unsequenced=False,
                            sender=None,
                            team=None,
                            save=False,
                            rule=None):
        """send a Contained `Loader` to all or a selection of connected
        players

        Parameters:
            contained: the `Loader` object to send
            unsequenced: set the enet ``UNSEQUENCED`` flag on this packet
            sender: if set to a connection object, do not send this packet to
                that player, as they are the sender.
            team: if set to a team, only send the packet to that team
            save: if the player has not downloaded the map yet, save this
                packet and send it when the map transfer has completed
            rule: if set to a callable, this function is called with the player
                as parameter to determine if a given player should receive the
                packet
        """
        if unsequenced:
            flags = enet.PACKET_FLAG_UNSEQUENCED
        else:
            flags = enet.PACKET_FLAG_RELIABLE
        writer = ByteWriter()
        contained.write(writer)
        data = bytes(writer)
        packet = enet.Packet(data, flags)
        for player in self.connections.values():
            if player is sender or player.player_id is None:
                continue
            if team is not None and player.team is not team:
                continue
            if rule is not None and not rule(player):
                continue
            if player.saved_loaders is not None:
                if save:
                    player.saved_loaders.append(data)
            else:
                player.peer.send(0, packet)
Пример #10
0
 def loader_received(self, packet):
     is_contained = hasattr(packet, 'data') and packet.id != MapData.id
     if is_contained:
         data = packet.data
         contained = load_server_packet(data)
     spam_class = contained.__class__ if is_contained else packet.__class__
     is_spammy = spam_class in self.spammy
     if is_spammy:
         self.spammy[spam_class] += 1
     else:
         message = None
         spammed = [
             spam.__name__ + (' x%s' % recv if recv > 1 else '')
             for spam, recv in self.spammy.items() if recv
         ]
         if len(spammed):
             message = 'received ' + ', '.join(spammed)
             print message
             for spam in self.spammy:
                 self.spammy[spam] = 0
         if is_contained:
             print contained
             print '    ', hexify(data)
     if packet.id == ConnectionResponse.id:
         self.connection_id = packet.connection_id
         self.unique = packet.unique
         self.connected = True
         print 'connected', self.connection_id, self.unique
     elif is_contained:
         if packet.id == SizedData.id:
             pass
             # reactor.callLater(1.0, self.send_join, team = 0, weapon = 0)
             #reactor.callLater(4.0, self.send_join, 1, 1)
             # open('testy.vxl', 'wb').write(str(self.map))
         # data = packet.data
         # if data.dataLeft():
         # raw_input('not completely parsed')
         # print contained.id
         if contained.id == loaders.MapStart.id:
             print 'map size:', contained.size
             self.map_size = contained.size
             self.map = ByteWriter()
         elif contained.id == loaders.MapChunk.id:
             self.map.write(contained.data)
             if len(self.map) == self.map_size:
                 print 'done!', len(self.map)
         # newdata = ByteWriter()
         # contained.write(newdata)
         # if contained.id != loaders.PlayerData.id:
         # if str(data) != str(newdata):
         # print hexify(data)
         # print hexify(newdata)
         # raw_input('incorrect save func')
     elif packet.id == Ack.id:
         pass
     elif packet.id == Ping.id:
         pass
     elif packet.id == Packet10.id:
         print 'received packet10'
     else:
         print 'received:', packet
         raw_input('unknown packet')
Пример #11
0
class ClientConnection(BaseConnection):
    protocol = None
    displayed_id = False

    def __init__(self, protocol):
        BaseConnection.__init__(self)
        self.protocol = protocol
        self.auth_val = random.randint(0, 0xFFFF)
        self.map = ByteWriter()
        self.connections = MultikeyDict()
        self.spammy = {
            Ping: 0,
            loaders.MapChunk: 0,
            loaders.OrientationData: 0,
            loaders.PositionData: 0,
            loaders.InputData: 0
        }

        connect_request = ConnectionRequest()
        connect_request.auth_val = self.auth_val
        connect_request.client = True
        connect_request.version = self.get_version()
        self.send_loader(connect_request, False, 255)

    def get_version(self):
        return crc32(open('../data/client.exe', 'rb').read())

    def send_join(self, team=-1, weapon=-1):
        print 'joining team %s' % team
        loader = SizedData()
        data = ByteWriter()
        join = loaders.JoinTeam()
        join.name = 'flotothelo'
        join.team = team
        join.weapon = weapon
        join.write(data)
        loader.data = data
        self.send_loader(loader, True)

    def packet_received(self, packet):
        if not self.displayed_id:
            print 'server id:', packet.connection_id
            self.displayed_id = True
        BaseConnection.packet_received(self, packet)

    def loader_received(self, packet):
        is_contained = hasattr(packet, 'data') and packet.id != MapData.id
        if is_contained:
            data = packet.data
            contained = load_server_packet(data)
        spam_class = contained.__class__ if is_contained else packet.__class__
        is_spammy = spam_class in self.spammy
        if is_spammy:
            self.spammy[spam_class] += 1
        else:
            message = None
            spammed = [
                spam.__name__ + (' x%s' % recv if recv > 1 else '')
                for spam, recv in self.spammy.items() if recv
            ]
            if len(spammed):
                message = 'received ' + ', '.join(spammed)
                print message
                for spam in self.spammy:
                    self.spammy[spam] = 0
            if is_contained:
                print contained
                print '    ', hexify(data)
        if packet.id == ConnectionResponse.id:
            self.connection_id = packet.connection_id
            self.unique = packet.unique
            self.connected = True
            print 'connected', self.connection_id, self.unique
        elif is_contained:
            if packet.id == SizedData.id:
                pass
                # reactor.callLater(1.0, self.send_join, team = 0, weapon = 0)
                #reactor.callLater(4.0, self.send_join, 1, 1)
                # open('testy.vxl', 'wb').write(str(self.map))
            # data = packet.data
            # if data.dataLeft():
            # raw_input('not completely parsed')
            # print contained.id
            if contained.id == loaders.MapStart.id:
                print 'map size:', contained.size
                self.map_size = contained.size
                self.map = ByteWriter()
            elif contained.id == loaders.MapChunk.id:
                self.map.write(contained.data)
                if len(self.map) == self.map_size:
                    print 'done!', len(self.map)
            # newdata = ByteWriter()
            # contained.write(newdata)
            # if contained.id != loaders.PlayerData.id:
            # if str(data) != str(newdata):
            # print hexify(data)
            # print hexify(newdata)
            # raw_input('incorrect save func')
        elif packet.id == Ack.id:
            pass
        elif packet.id == Ping.id:
            pass
        elif packet.id == Packet10.id:
            print 'received packet10'
        else:
            print 'received:', packet
            raw_input('unknown packet')

    def send_data(self, data):
        self.protocol.transport.write(data)
Пример #12
0
 def generate(self):
     reader = ByteWriter()
     self.write(reader)
     return reader
Пример #13
0
class ClientConnection(BaseConnection):
    protocol = None
    displayed_id = False
    
    def __init__(self, protocol):
        BaseConnection.__init__(self)
        self.protocol = protocol
        self.auth_val = random.randint(0, 0xFFFF)
        self.map = ByteWriter()
        self.connections = MultikeyDict()
        self.spammy = {Ping : 0, loaders.MapChunk : 0, 
            loaders.OrientationData : 0,
            loaders.PositionData : 0, loaders.InputData : 0}
        
        connect_request = ConnectionRequest()
        connect_request.auth_val = self.auth_val
        connect_request.client = True
        connect_request.version = self.get_version()
        self.send_loader(connect_request, False, 255)
    
    def get_version(self):
        return crc32(open('../data/client.exe', 'rb').read())
    
    def send_join(self, team = -1, weapon = -1):
        print 'joining team %s' % team
        loader = SizedData()
        data = ByteWriter()
        join = loaders.JoinTeam()
        join.name = 'flotothelo'
        join.team = team
        join.weapon = weapon
        join.write(data)
        loader.data = data
        self.send_loader(loader, True)
    
    def packet_received(self, packet):
        if not self.displayed_id:
            print 'server id:', packet.connection_id
            self.displayed_id = True
        BaseConnection.packet_received(self, packet)
    
    def loader_received(self, packet):
        is_contained = hasattr(packet, 'data') and packet.id != MapData.id
        if is_contained:
            data = packet.data
            contained = load_server_packet(data)
        spam_class = contained.__class__ if is_contained else packet.__class__
        is_spammy = spam_class in self.spammy
        if is_spammy:
            self.spammy[spam_class] += 1
        else:
            message = None
            spammed = [spam.__name__ + (' x%s' % recv if recv > 1 else '')
                for spam, recv in self.spammy.items() if recv]
            if len(spammed):
                message = 'received ' + ', '.join(spammed)
                print message
                for spam in self.spammy:
                    self.spammy[spam] = 0            
            if is_contained:
                print contained
                print '    ', hexify(data)
        if packet.id == ConnectionResponse.id:
            self.connection_id = packet.connection_id
            self.unique = packet.unique
            self.connected = True
            print 'connected', self.connection_id, self.unique
        elif is_contained:
            if packet.id == SizedData.id:
                pass
                # reactor.callLater(1.0, self.send_join, team = 0, weapon = 0)
                #reactor.callLater(4.0, self.send_join, 1, 1)
                # open('testy.vxl', 'wb').write(str(self.map))
            # data = packet.data
            # if data.dataLeft():
                # raw_input('not completely parsed')
            # print contained.id
            if contained.id == loaders.MapStart.id:
                print 'map size:', contained.size
                self.map_size = contained.size
                self.map = ByteWriter()
            elif contained.id == loaders.MapChunk.id:
                self.map.write(contained.data)
                if len(self.map) == self.map_size:
                    print 'done!', len(self.map)
            # newdata = ByteWriter()
            # contained.write(newdata)
            # if contained.id != loaders.PlayerData.id:
                # if str(data) != str(newdata):
                    # print hexify(data)
                    # print hexify(newdata)
                    # raw_input('incorrect save func')
        elif packet.id == Ack.id:
            pass
        elif packet.id == Ping.id:
            pass
        elif packet.id == Packet10.id:
            print 'received packet10'
        else:
            print 'received:', packet
            raw_input('unknown packet')
    
    def send_data(self, data):
        self.protocol.transport.write(data)