Ejemplo n.º 1
0
    def construct(self, obj, recipients=None, new=True):
        # recipients is needed to send replicas to new participants
        if recipients is None:
            recipients = self._participants

        if new:
            self._network_ids[obj] = self._current_network_id
            self._current_network_id += 1

        out = BitStream()
        out.write(c_ubyte(Message.ReplicaManagerConstruction))
        out.write(c_bit(True))
        out.write(c_ushort(self._network_ids[obj]))
        out.write(obj.write_construction())

        for recipient in recipients:
            self._server.send(out, recipient)
Ejemplo n.º 2
0
 def on_connection_request(self, data, address):
     packet_password = data
     if self.incoming_password == packet_password:
         response = BitStream()
         response.write(c_ubyte(Message.ConnectionRequestAccepted))
         response.write(socket.inet_aton(address[0]))
         response.write(c_ushort(address[1]))
         response.write(
             bytes(2)
         )  # Connection index, seems like this was right out ignored in RakNet
         response.write(socket.inet_aton(self._address[0]))
         response.write(c_ushort(self._address[1]))
         self.send(response,
                   address,
                   reliability=PacketReliability.Reliable)
     else:
         raise NotImplementedError
Ejemplo n.º 3
0
    def _send_loop(self):
        for message_number, resend_data in self._resends.items():
            resend_time, packet = resend_data
            if resend_time > time.time():
                continue

            if self._packets_sent >= self._cwnd:
                break
            self._packets_sent += 1
            log.info("actually resending %i", message_number)

            data, reliability, ordering_index, split_packet_id, split_packet_index, split_packet_count = packet
            self._send_packet(data, message_number, reliability,
                              ordering_index, split_packet_id,
                              split_packet_index, split_packet_count)
            if reliability == PacketReliability.Reliable or reliability == PacketReliability.ReliableOrdered:
                self._resends[message_number] = time.time() + self._rto, packet

        while self._sends:
            if self._packets_sent >= self._cwnd:
                break
            packet = self._sends.pop(0)
            self._packets_sent += 1

            data, reliability, ordering_index, split_packet_id, split_packet_index, split_packet_count = packet
            message_number = self._send_message_number_index
            self._send_message_number_index += 1

            self._send_packet(data, message_number, reliability,
                              ordering_index, split_packet_id,
                              split_packet_index, split_packet_count)

            if reliability == PacketReliability.Reliable or reliability == PacketReliability.ReliableOrdered:
                self._resends[message_number] = time.time() + self._rto, packet

        if self._acks:
            out = BitStream()
            out.write(c_bit(True))
            out.write(c_uint(self._remote_system_time))
            out.write(self._acks.serialize())
            self._acks.clear()
            self._transport.sendto(out, self._address)

        if not self.stop:
            asyncio.get_event_loop().call_later(0.03, self._send_loop)
Ejemplo n.º 4
0
    def on_packet(self, data, address):
        self.log_packet(data, address, received=True)

        handlers = self.handlers.get(self.packet_id(data), ())
        origin_handlers = [
            i for i in handlers if i[1] is None or i[1] == address
        ]
        if not origin_handlers:
            log.warning("No handlers for the previously received message")

        data = self.handler_data(data)
        for handler_tuple in origin_handlers:
            handler, origin_filter = handler_tuple
            stream = BitStream(data)
            if asyncio.iscoroutinefunction(handler):
                asyncio.ensure_future(handler(stream, address))
            else:
                handler(stream, address)
Ejemplo n.º 5
0
    def _send_packet(self, data, message_number, reliability, ordering_index,
                     split_packet_id, split_packet_index, split_packet_count):
        out = BitStream()
        out.write(c_bit(bool(self._acks)))
        if self._acks:
            out.write(c_uint(self._remote_system_time))
            out.write(self._acks.serialize())
            self._acks.clear()

        assert len(out) + ReliabilityLayer.packet_header_length(
            reliability, split_packet_id is not None) + len(data) < 1492

        has_remote_system_time = True
        out.write(c_bit(has_remote_system_time))
        out.write(c_uint(int(time.perf_counter() * 1000)))

        out.write(c_uint(message_number))

        out.write_bits(reliability, 3)

        if reliability in (PacketReliability.UnreliableSequenced,
                           PacketReliability.ReliableOrdered):
            out.write_bits(
                0,
                5)  # ordering_channel, no one ever uses anything else than 0
            out.write(c_uint(ordering_index))

        is_split_packet = split_packet_id is not None
        out.write(c_bit(is_split_packet))
        if is_split_packet:
            out.write(c_ushort(split_packet_id))
            out.write(c_uint(split_packet_index), compressed=True)
            out.write(c_uint(split_packet_count), compressed=True)
        out.write(c_ushort(len(data) * 8), compressed=True)
        out.align_write()
        out.write(data)

        assert len(out) < 1492  # maximum packet size handled by raknet
        self._transport.sendto(out, self._address)
Ejemplo n.º 6
0
	def handle_datagram(self, datagram):
		stream = BitStream(datagram)
		if self.handle_datagram_header(stream):
			return # Acks only packet
		# There can be multiple packets in one datagram
		yield from self.parse_packets(stream)
Ejemplo n.º 7
0
 def setUp(self):
     self.stream = BitStream()