示例#1
0
        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(2048)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1],
                                   data[:50])
                    count_incoming_packet(data)

                    if data[:4] == 'ping':
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:4] == 'pong':
                        self.ee.emit('on_pong_message', (data, addr))

                    elif data[:15] == 'send_relay_ping':
                        self.ee.emit('on_send_relay_ping', (data, addr))

                    elif data[:10] == 'relay_ping':
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto(
                            'send_relay_pong %s %s' % (sender, recipient),
                            (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' %
                                              (sender, recipient))

                    elif data[:15] == 'send_relay_pong':
                        self.ee.emit('on_send_relay_pong', (data, addr))

                    elif data[:9] == 'heartbeat':
                        self.log.debug('We just received a heartbeat.')

                    elif data[:7] == 'relayto':
                        self.log.debug('Relay To Packet')
                        self.ee.emit('on_relayto', data)

                    elif data[:6] == 'relay ':
                        self.log.debug('Relay Packet')
                        self.ee.emit('on_message', (data, addr))

                    else:
                        self.ee.emit('on_message', (data, addr))

                except socket.timeout as e:
                    err = e.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass
示例#2
0
        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(PEERLISTENER_RECV_FROM_BUFFER_SIZE)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1], data[:50])
                    count_incoming_packet(data)

                    if data[:MSG_PING_ID_SIZE] == MSG_PING_ID:
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:MSG_PONG_ID_SIZE] == MSG_PONG_ID:
                        self.event_emitter.emit('on_pong_message', (data, addr))

                    elif data[:MSG_SEND_RELAY_PING_ID_SIZE] == MSG_SEND_RELAY_PING_ID:
                        self.event_emitter.emit('on_send_relay_ping', (data, addr))

                    elif data[:MSG_RELAY_PING_ID_SIZE] == MSG_RELAY_PING_ID:
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto('send_relay_pong %s %s' % (sender, recipient), (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' % (sender, recipient))

                    elif data[:MSG_SEND_RELAY_PONG_ID_SIZE] == MSG_SEND_RELAY_PONG_ID:
                        self.event_emitter.emit('on_send_relay_pong', (data, addr))

                    elif data[:MSG_HEARTBEAT_ID_SIZE] == MSG_HEARTBEAT_ID:
                        self.log.debug('We just received a heartbeat.')

                    elif data[:MSG_RELAYTO_ID_SIZE] == MSG_RELAYTO_ID:
                        self.log.debug('Relay To Packet')
                        self.event_emitter.emit('on_relayto', data)

                    elif data[:MSG_RELAY_ID_SIZE] == MSG_RELAY_ID:
                        self.log.debug('Relay Packet')
                        self.event_emitter.emit('on_message', (data, addr))

                    else:
                        self.event_emitter.emit('on_message', (data, addr))

                except socket.timeout as exc:
                    err = exc.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass
示例#3
0
        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(2048)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1], data[:50])
                    count_incoming_packet(data)

                    if data[:4] == 'ping':
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:4] == 'pong':
                        self.ee.emit('on_pong_message', (data, addr))

                    elif data[:15] == 'send_relay_ping':
                        self.ee.emit('on_send_relay_ping', (data, addr))

                    elif data[:10] == 'relay_ping':
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto('send_relay_pong %s %s' % (sender, recipient), (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' % (sender, recipient))

                    elif data[:15] == 'send_relay_pong':
                        self.ee.emit('on_send_relay_pong', (data, addr))

                    elif data[:9] == 'heartbeat':
                        self.log.debug('We just received a heartbeat.')

                    elif data[:7] == 'relayto':
                        self.log.debug('Relay To Packet')
                        self.ee.emit('on_relayto', data)

                    elif data[:6] == 'relay ':
                        self.log.debug('Relay Packet')
                        self.ee.emit('on_message', (data, addr))

                    else:
                        self.ee.emit('on_message', (data, addr))

                except socket.timeout as e:
                    err = e.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass
示例#4
0
 def receive(self, packet):
     if packet._acknowledgement:
         self._sender.verify_acknowledgement(packet._sequence_number)
     else:
         self._receiver.receive(packet)
     count_incoming_packet(packet)
示例#5
0
        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(
                        PEERLISTENER_RECV_FROM_BUFFER_SIZE)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1],
                                   data[:50])
                    count_incoming_packet(data)

                    if data[:MSG_PING_ID_SIZE] == MSG_PING_ID:
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:MSG_PONG_ID_SIZE] == MSG_PONG_ID:
                        self.event_emitter.emit('on_pong_message',
                                                (data, addr))

                    elif data[:
                              MSG_SEND_RELAY_PING_ID_SIZE] == MSG_SEND_RELAY_PING_ID:
                        self.event_emitter.emit('on_send_relay_ping',
                                                (data, addr))

                    elif data[:MSG_RELAY_PING_ID_SIZE] == MSG_RELAY_PING_ID:
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto(
                            'send_relay_pong %s %s' % (sender, recipient),
                            (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' %
                                              (sender, recipient))

                    elif data[:
                              MSG_SEND_RELAY_PONG_ID_SIZE] == MSG_SEND_RELAY_PONG_ID:
                        self.event_emitter.emit('on_send_relay_pong',
                                                (data, addr))

                    elif data[:MSG_HEARTBEAT_ID_SIZE] == MSG_HEARTBEAT_ID:
                        self.log.debug('We just received a heartbeat.')

                    elif data[:MSG_RELAYTO_ID_SIZE] == MSG_RELAYTO_ID:
                        self.log.debug('Relay To Packet')
                        self.event_emitter.emit('on_relayto', data)

                    elif data[:MSG_RELAY_ID_SIZE] == MSG_RELAY_ID:
                        self.log.debug('Relay Packet')
                        self.event_emitter.emit('on_message', (data, addr))

                    else:
                        self.event_emitter.emit('on_message', (data, addr))

                except socket.timeout as exc:
                    err = exc.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass