Skip to content

Commit

Permalink
2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andiricum2 committed Sep 22, 2024
1 parent 209275f commit e52c95c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions pieraknet/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def handle_connection_requests(self, frame):
ConnectionRequestAcceptedPacket = ConnectionRequestHandler.handle(frame.body, self.server, self)

#Create a frame set with answer
frameSetPacket = FrameSetPacket().create_frame_set_packet(ConnectionRequestAcceptedPacket, flags=0x64)
frameSetPacket = FrameSetPacket(server=self.server).create_frame_set_packet(ConnectionRequestAcceptedPacket, self.client_sequence_number, flags=0x64)

#Encode frame set
buffer = Buffer()
frameSetPacket.encode(connection=self, buffer=buffer)
frameSetPacket.encode(buffer=buffer)

#Send frame set
self.send_data(buffer.getvalue())
Expand All @@ -82,10 +82,10 @@ def process_online_ping(self, frame):

OnlinePongPacket = OnlinePingHandler.handle(OnlinePing(frame.body), self.server, self)

frameSetPacket = FrameSetPacket().create_frame_set_packet(OnlinePongPacket, flags=0x00)
frameSetPacket = FrameSetPacket(server=self.server).create_frame_set_packet(OnlinePongPacket, self.client_sequence_number, flags=0x00)

buffer = Buffer()
frameSetPacket.encode(connection=self, buffer=buffer)
frameSetPacket.encode(buffer=buffer)

self.send_data(buffer.getvalue())
self.server.logger.info(f"We have just sent an Online Pong to {self.address}")
Expand Down
6 changes: 3 additions & 3 deletions pieraknet/handlers/ack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def handle(data, server, connection):
connection.recovery_queue.pop(sequence_number, None)

@staticmethod
def send_ack(server, connection, sequence_number, order_channel):
def send_ack(server, connection, sequence_number):
ack_packet = Ack()

ack_packet.sequence_numbers = [sequence_number]

ack_packet.encode()

server.send(ack_packet, connection.address)
server.send(ack_packet.getvalue(), connection.address)

server.logger.debug(f"Sent ACK for sequence number {sequence_number} on order channel {order_channel}")
server.logger.debug(f"Sent ACK for sequence number {sequence_number}")
3 changes: 2 additions & 1 deletion pieraknet/handlers/established_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pieraknet.protocol_info import ProtocolInfo
from pieraknet.handlers.ack import AckHandler

class EstablishedConnectionHandler:
@staticmethod
Expand All @@ -7,7 +8,7 @@ def handle(frame, server, connection):

server.logger.debug("Established Connection Packet:")
server.logger.debug(f"- Packet Type: {packet_type}")

if packet_type == ProtocolInfo.ONLINE_PING:
connection.process_online_ping(frame)
elif packet_type == ProtocolInfo.GAME_PACKET:
Expand Down
1 change: 1 addition & 0 deletions pieraknet/handlers/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ def handle(frame, server, connection):
if not connection.connected:
connection.handle_connection_requests(frame)
else:
connection.handle_connection_requests(frame)
connection.handle_established_connection(frame)
2 changes: 2 additions & 0 deletions pieraknet/handlers/frame_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pieraknet.buffer import Buffer
from pieraknet.handlers.frame import FrameHandler
from pieraknet.handlers.fragmented_frame import FragmentedFrameHandler
from pieraknet.handlers.ack import AckHandler

class FrameSetHandler:
@staticmethod
Expand All @@ -18,6 +19,7 @@ def handle(data, server, connection):

if incoming_sequence_number not in connection.client_sequence_numbers:
connection.client_sequence_numbers.append(incoming_sequence_number)
AckHandler.send_ack(server=server, connection=connection, sequence_number=incoming_sequence_number)
connection.ack_queue.append(incoming_sequence_number)
connection.handle_packet_loss(incoming_sequence_number)
connection.client_sequence_number = incoming_sequence_number
Expand Down
7 changes: 1 addition & 6 deletions pieraknet/packets/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def decode(self, buffer: Buffer):
self.body = buffer.read(body_length)
# print(f"Read Body (Length {body_length} bytes): {self.body}")

def encode(self, connection, buffer: Buffer):
def encode(self, buffer: Buffer):
if not isinstance(self.flags, int) or not isinstance(self.length_in_bits, int):
raise TypeError("flags and length_in_bits must be integers")

Expand Down Expand Up @@ -115,11 +115,6 @@ def encode(self, connection, buffer: Buffer):
buffer.write_byte(self.order_channel)
# print(f"Written Ordered Frame Index: {self.ordered_frame_index}")
# print(f"Written Order Channel: {self.order_channel}")

if reliability_type in {5, 6, 7}:
# Send acknowledgement
# print(f"Sending ACK for frame {self.ordered_frame_index} on order channel {self.order_channel}")
AckHandler.send_ack(self.server, connection, self.ordered_frame_index, self.order_channel)

if is_fragmented:
buffer.write_int(self.compound_size)
Expand Down
11 changes: 6 additions & 5 deletions pieraknet/packets/frame_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def decode(self, buffer: Buffer):
self.frames.append(frame)
# print(f"Read Frame: {frame}")

def encode(self, connection, buffer: Buffer):
def encode(self, buffer: Buffer):
# Escribir el ID del paquete
buffer.write_byte(self.packet_id) # Su valor debe ser 0x80
# print(f"Written Packet ID: {self.packet_id}")
Expand All @@ -67,23 +67,24 @@ def encode(self, connection, buffer: Buffer):
# Codificar cada frame y escribirlo en el buffer
for frame in self.frames:
frame_buffer = Buffer() # Crear un buffer separado para cada frame
frame.encode(connection, frame_buffer) # Codificar el frame
frame.encode(frame_buffer) # Codificar el frame
buffer.write(frame_buffer.getvalue()) # Escribir el frame codificado en el buffer general
# print(f"Written Frame: {frame}")

# Retornar el valor codificado del buffer
return buffer.getvalue()

def create_frame_set_packet(self, body, flags=0):
def create_frame_set_packet(self, body, client_sequence_number, flags=0):
# print(f"create_frame_set_packet Flags (hex): {hex(flags)}")
# print(f"create_frame_set_packet Flags (dec): {flags}")
# Crear un nuevo frame con el cuerpo provisto
self.sequence_number = client_sequence_number
frame = Frame(self.server)
frame.flags = flags
frame.length_in_bits = len(body) * 8
frame.body = body

# Añadir el objeto Frame a la lista de frames
self.frames.append(frame)

return self

0 comments on commit e52c95c

Please sign in to comment.