Ejemplo n.º 1
0
    def __processPacket(self, packet):
        client = packet.recv_from

        if self.debug:
            log.info("Received packet %s from %s:%u" %
                     (packet, client.host, client.port))

        # Send an ack if needed
        if not packet.skippable: self.__sendAck(packet)

        # Is is a special packet (ack / ping / poing) ?
        if packet.type == Packet.PACKET_ACK:
            client.processAck(packet)
            return None
        if packet.type == Packet.PACKET_PING:
            client.processPing(packet)
            return None
        if packet.type == Packet.PACKET_PONG:
            client.processPong(packet)
            return None

        # This packet is already received ? Drop it!
        if client.alreadyReceived(packet.id):
            if self.debug:
                log.warning("Drop packet %u (already received)" % packet.id)
            return None

        client.receivePacket(packet)

        # Returns the new packet
        return packet
Ejemplo n.º 2
0
    def connect(self, host, port):
        """ Connect to host:port """
        max_connection = 50
    
        self.__addr = (host, port,)
        if self.__is_server:
            if self.verbose:
                log.info("Run server at %s:%u (tcp)" % (self.host, self.port))
            thread.start_new_thread( self.__waiter.runThread, (port,max_connection,))
        else:
            if self.verbose:
                log.info("Connect to server %s:%u" % (self.host, self.port))
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                s.connect(self.__addr)
            except socket.error, err:
                if err[0]==111:
                    if self.verbose:
                        log.warning("Fail to connect to server %s:%u" % (self.host, self.port))
                    if self.on_connection_fails:
                        self.on_connection_fails()
                    return
                raise

            client = TCP_Client(self, self.__addr, socket=s)
            self.__server = client
            self.__clients_sema.acquire()
            self.__clients[client.addr] = client
            self.__clients_sema.release()
Ejemplo n.º 3
0
 def lostConnection(self):
     if self.verbose:
         log.warning("Lost connection to %s:%u!" % (self.host, self.port))
     if self.__socket_open:
         self.__socket.close()
         self.__socket_open = False
     if self.on_lost_connection: self.on_lost_connection()
     self.stop()
Ejemplo n.º 4
0
 def processConnection(self, ioclient, version, signature):
     self.launchEvent("happyboom", "signature", ioclient, signature)
     features = ""
     for name in self.features:
         try:
             feature = self.protocol.getFeature(name)
             features = features + "%c" % feature.id
         except ProtocolException, msg:
             log.warning("Protocol exception: %s" % msg)
             pass
def run(Client, arg):
    from happyboom.common.log import log
    
    client = Client(arg)
    try:
        client.start()
    except KeyboardInterrupt:
        log.warning("Program interrupted (CTRL+C).")
        pass
    client.stop()
Ejemplo n.º 6
0
 def processConnection(self, ioclient, version, signature):
     self.launchEvent("happyboom", "signature", ioclient, signature)
     features = ""
     for name in self.features:
         try:
             feature = self.protocol.getFeature(name)
             features = features + "%c" % feature.id
         except ProtocolException, msg:
             log.warning("Protocol exception: %s" % msg)
             pass
Ejemplo n.º 7
0
def run(Client, arg):
    from happyboom.common.log import log

    client = Client(arg)
    try:
        client.start()
    except KeyboardInterrupt:
        log.warning("Program interrupted (CTRL+C).")
        pass
    client.stop()
Ejemplo n.º 8
0
    def __processRecvData(self, data):
        # If no data, connection is lost
        if len(data)==0:
            if self.io.verbose:
                log.warning("Client %s lost connection with server!" % self)
            self.disconnect()
            return None

        # Call user event if needed
        if self.on_receive != None: self.on_receive(data)
        return data
Ejemplo n.º 9
0
    def __processRecvData(self, data):
        # If no data, connection is lost
        if len(data) == 0:
            if self.io.verbose:
                log.warning("Client %s lost connection with server!" % self)
            self.disconnect()
            return None

        # Call user event if needed
        if self.on_receive != None: self.on_receive(data)
        return data
 def onClientConnection(self, ioclient, version, signature):
     # TODO: Case where signature != "" ??? (reconnection)
     if self.__verbose: log.info("Client %s try to connect : check version." % ioclient)
     server_version = self.__protocol.version
     if version == server_version:
         if self.__verbose: log.info("Client %s try to connect: version ok." % ioclient)
         signature = self.generateSignature(ioclient)
         self.launchEvent("happyboom", "connection", ioclient, server_version, signature)
     else:    
         if self.__verbose: log.warning("Client %s try to connect: wrong version (%s)." % version)
         self.launchEvent("happyboom", "closeConnection", ioclient, u"Wrong server version (%s VS %s)" % (version, serveur_version))
Ejemplo n.º 11
0
 def __processData(self, client, data):
     while data != "":
         packet = Packet()
         packet.recv_from = client
         data = packet.unpack(data)
         if not packet.isValid():
             if self.debug:
                 log.warning("Received buggy network packet from %s!" % client)
             return
         if self.debug:
             log.info("Received %s:%u => \"%s\"" % (client.host, client.port, packet.data))
         if self.on_new_packet: self.on_new_packet(packet)
Ejemplo n.º 12
0
    def processPong(self, packet):
        """ Process pong.
        @type packet: C{L{Packet<io.Packet>}}
        """
        id = self.__getPingId(packet.data)
        if id == None:
            if self.debug:
                log.warning("Received invalid udp ping packet!")
            return

        # Received too late ?
        if not self.__sent_ping.has_key(id): return

        # Remove ping from the list
        del self.__sent_ping[id]
Ejemplo n.º 13
0
    def processPong(self, packet):
        """ Process pong.
        @type packet: C{L{Packet<io.Packet>}}
        """
        id = self.__getPingId(packet.data)
        if id == None:
            if self.debug:
                log.warning("Received invalid udp ping packet!")
            return

        # Received too late ?
        if not self.__sent_ping.has_key(id): return

        # Remove ping from the list
        del self.__sent_ping[id]
Ejemplo n.º 14
0
 def onClientConnection(self, ioclient, version, signature):
     # TODO: Case where signature != "" ??? (reconnection)
     if self.__verbose:
         log.info("Client %s try to connect : check version." % ioclient)
     server_version = self.__protocol.version
     if version == server_version:
         if self.__verbose:
             log.info("Client %s try to connect: version ok." % ioclient)
         signature = self.generateSignature(ioclient)
         self.launchEvent("happyboom", "connection", ioclient,
                          server_version, signature)
     else:
         if self.__verbose:
             log.warning("Client %s try to connect: wrong version (%s)." %
                         version)
         self.launchEvent(
             "happyboom", "closeConnection", ioclient,
             u"Wrong server version (%s VS %s)" %
             (version, serveur_version))
Ejemplo n.º 15
0
    def __processRecvData(self, data, addr):
        if self.__is_server:
            self.__clients_sema.acquire()
            if addr not in self.__clients:
                client = UDP_Client(self, addr)
                self.__clients[addr] = client
                self.__clients_sema.release()
                if self.verbose:
                    log.info("New client: %s:%u." % (addr[0], addr[1]))
                client.send_ping = True
                if self.on_client_connect != None:
                    self.on_client_connect(client)
            else:
                client = self.__clients[addr]
                self.__clients_sema.release()
        else:
            # Drop packets which doesn't come from server
            if self.__server.addr != addr:
                if self.debug:
                    log.warning(
                        "Drop packet from %s:%u (it isn't the server address)."
                        % (addr[0], addr[1]))
                return None
            client = self.__server

        # Call user event if needed
        if self.on_receive != None: self.on_receive(data)

        # Decode data to normal packet (unpack)
        packet = Packet()
        packet.unpack(data)
        if not packet.isValid():
            if self.debug:
                log.warning("Drop invalid network packet from %s" %
                            (data, client))
            return None

        # Return packet
        packet.recv_from = client
        return self.__processPacket(packet)
Ejemplo n.º 16
0
 def processPacket(self, packet):
     """ Processes incomming network packets (converts and launches local event).
     @param packet: incomming network packet.
     @type packet: C{net.io.packet.Packet}
     """
     ptype, data = self.unpackPacketType(packet.data)
     
     # Choose process function
     if ptype in self._unpackFunc:
         data = self._unpackFunc[ptype] (packet.recv_from, data)
     else:
         log.warning("ProtocoleWarning: received unexpected packet type %s." % ptype)
     if len(data) != 0:
         log.warning("ProtocolWarning: Received a message with an unexpected length.")
         log.warning(u"Rest: [%s]." % data)
Ejemplo n.º 17
0
    def unpack(self, binary_data):
        """ Fill attributes from a binary data packet
        @parameter binary_data: Binary datas which comes from network.
        @type C{binary}
        """
        if binary_data == None: return
        self.__valid = False

        if Packet.use_tcp:
            # Read data len
            format = "!I"
            size = struct.calcsize(format)
            if len(binary_data) < size:
                log.warning("Wrong network packet size from %s!" %
                            self.recv_from)
                return None
            data = struct.unpack(format, binary_data[:size])
            data_len = data[0]
            binary_data = binary_data[size:]
        else:
            # Read skippable, id, data len
            format = "!BBII"
            size = struct.calcsize(format)
            if len(binary_data) < size:
                log.warning("Wrong network packet size from %s!" %
                            self.recv_from)
                return None
            data = struct.unpack(format, binary_data[:size])
            self.type = data[0]
            self.skippable = (data[1] == 1)
            self.id = data[2]
            data_len = data[3]
            binary_data = binary_data[size:]

        # Read data
        if 0 < data_len:
            format = "!%us" % (data_len)
            size = struct.calcsize(format)
            if len(binary_data) < size:
                log.warning("Wrong network packet size from %s!" %
                            self.recv_from)
                return None
            data = struct.unpack(format, binary_data[:size])
            self.__data = data[0]
        else:
            self.__data = None
        self.__valid = True
        return binary_data[size:]
Ejemplo n.º 18
0
    def processPacket(self, packet):
        """ Processes incomming network packets (converts and launches local event).
        @param packet: incomming network packet.
        @type packet: C{net.io.packet.Packet}
        """
        ptype, data = self.unpackPacketType(packet.data)

        # Choose process function
        if ptype in self._unpackFunc:
            data = self._unpackFunc[ptype](packet.recv_from, data)
        else:
            log.warning(
                "ProtocoleWarning: received unexpected packet type %s." %
                ptype)
        if len(data) != 0:
            log.warning(
                "ProtocolWarning: Received a message with an unexpected length."
            )
            log.warning(u"Rest: [%s]." % data)
Ejemplo n.º 19
0
 def onLostConnection(self):
     """ Handler called on losting network connection. """
     log.warning("[HAPPYBOOM] Lost connection with server.")
     self.launchEvent("happyboom", "stop")
Ejemplo n.º 20
0
 def messageReceived(self, msg):
     if self.__debug:
         log.warning("Unhandled message : %s -- %s" %(type(self), msg))
Ejemplo n.º 21
0
 def onLostConnection(self):
     """ Handler called on losting network connection. """
     log.warning("[HAPPYBOOM] Lost connection with server.")
     self.launchEvent("happyboom", "stop")
Ejemplo n.º 22
0
 def messageReceived(self, msg):
     if self.__debug:
         log.warning("Unhandled message : %s -- %s" % (type(self), msg))
Ejemplo n.º 23
0
 def __lostClient(self, client):
     if not client.addr in self.__clients: return
     client = self.__clients[client.addr]
     if self.verbose:
         log.warning("Lost connection with client %s !" % client)
     self.disconnectClient(client)