From 06d75ece6452a33f11afd7e5ec3dd1f916c8583f Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 6 Mar 2023 16:19:36 -0400 Subject: TProtocol: Match both `parse_packet()`, and `run_task()` --- src/pso/TProtocol.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) (limited to 'src/pso/TProtocol.cpp') diff --git a/src/pso/TProtocol.cpp b/src/pso/TProtocol.cpp index b0be8ee..d30df7d 100644 --- a/src/pso/TProtocol.cpp +++ b/src/pso/TProtocol.cpp @@ -14,18 +14,84 @@ OBJECT_NAME(TProtocol); -void TProtocol::run_task() { +packet *server_packet = nullptr; + +void copy_packet(struct packet *pkt) { } -int TProtocol::send(u8 *data, size_t size) { - return TTcpSocket::send(data, size); +int TProtocol::handle_command(struct packet *pkt) { + } -int handle_command(packet *pkt) { +void TProtocol::parse_packet() { + // This is needed to match, either that or cast the sizeof to an int. + const int max_packet_size = sizeof(struct packet); + while (!is_empty()) { + m_packet.bytes[m_packet_offset++] = next(); + if (m_packet_offset == sizeof(packet_header)) { + if (m_is_encrypted) { + m_recv_crypt.encrypt(&m_packet.packet.header, sizeof(packet_header)); + } + } + // NOTE: The `const int &` is required here to match. + // Convert packet size to native endian. + const int &packet_size = m_packet_size = to_be_uint16_t(m_packet.packet.header.size); + // Is the packet size invalid? + if (packet_size > max_packet_size || !packet_size) { + m_packet_size = 0; + m_packet_offset = 0; + m_is_invalid_packet = true; + break; + // Do we have the rest of the packet yet? + } else if (m_packet_size > 0 && m_packet_offset >= m_packet_size) { + // Decrypt it if necessary. + if (m_is_encrypted) { + m_recv_crypt.encrypt(&m_packet.packet.data, m_packet_size-sizeof(packet_header)); + } + struct packet *pkt = &m_packet; + m_packet_size = 0; + m_packet_offset = 0; + if (m_handle_pings_only) { + if (pkt->packet.header.command == 0x1d) { + if (!handle_command(pkt)) { + continue; + } else { + break; + } + } else { + copy_packet(pkt); + } + } else if (handle_command(pkt)) { + break; + } + } + } } +short TProtocol::send(u8 *data, size_t size) { + return TTcpSocket::send(data, size); +} + +void TProtocol::run_task() { + TTcpSocket::some_stub(); + some_stub(); + if (m_is_invalid_packet) { + close(); + m_is_encrypted = 0; + m_connected = 0; + m_packet_offset = 0; + m_packet_size = 0; + } else if (!m_buffer_cleared) { + server_packet = &m_packet; + parse_packet(); + recv(); + } +} + +void TProtocol::some_stub() {}; + TProtocol::~TProtocol() { } @@ -56,7 +122,7 @@ TProtocol::TProtocol(TObject *parent, u16 sub_version, int language, char *seria m_connected = 0; m_joined_game = 0; m_has_meet_user_settings = 0; - m_buffer_ready = 0; + m_handle_pings_only = false; m_entry_count = 0; m_unused = 0; m_lobby_list_count = 0; -- cgit v1.2.3-13-gbd6f