bool network_to_native(Packet_Head<0x0063> *native, NetPacket_Head<0x0063> network) { bool rv = true; rv &= network_to_native(&native->magic_packet_id, network.magic_packet_id); rv &= network_to_native(&native->magic_packet_length, network.magic_packet_length); return rv; }
RecvResult recv_vpacket(Session *s, Packet_Head<id>& head, std::vector<Packet_Repeat<id>>& repeat) { static_assert(id == Packet_Head<id>::PACKET_ID, "Packet_Head<id>::PACKET_ID"); static_assert(headsize == sizeof(NetPacket_Head<id>), "NetPacket_Head<id>"); static_assert(id == Packet_Repeat<id>::PACKET_ID, "Packet_Repeat<id>::PACKET_ID"); static_assert(repeatsize == sizeof(NetPacket_Repeat<id>), "NetPacket_Repeat<id>"); NetPacket_Head<id> net_head; std::vector<NetPacket_Repeat<id>> net_repeat; RecvResult rv = net_recv_vpacket(s, net_head, net_repeat); if (rv == RecvResult::Complete) { if (!network_to_native(&head, net_head)) return RecvResult::Error; assert (head.magic_packet_id == Packet_Head<id>::PACKET_ID); repeat.resize(net_repeat.size()); for (size_t i = 0; i < net_repeat.size(); ++i) { if (!network_to_native(&repeat[i], net_repeat[i])) return RecvResult::Error; } } return rv; }
bool network_to_native(Packet_Fixed<0x006a> *native, NetPacket_Fixed<0x006a> network) { bool rv = true; rv &= network_to_native(&native->magic_packet_id, network.magic_packet_id); rv &= network_to_native(&native->error_code, network.error_code); rv &= network_to_native(&native->error_message, network.error_message); return rv; }
bool network_to_native(Packet_Fixed<0x0064> *native, NetPacket_Fixed<0x0064> network) { bool rv = true; rv &= network_to_native(&native->magic_packet_id, network.magic_packet_id); rv &= network_to_native(&native->unknown, network.unknown); rv &= network_to_native(&native->account_name, network.account_name); rv &= network_to_native(&native->account_pass, network.account_pass); rv &= network_to_native(&native->version_2_flags, network.version_2_flags); return rv; }
RecvResult net_recv_opacket(Session *s, NetPacket_Head<id>& head, bool *has_opt, NetPacket_Option<id>& opt) { bool ok = packet_fetch(s, 0, reinterpret_cast<Byte *>(&head), sizeof(NetPacket_Head<id>)); if (ok) { Packet_Head<id> nat; if (!network_to_native(&nat, head)) return RecvResult::Error; if (packet_avail(s) < nat.magic_packet_length) return RecvResult::Incomplete; if (nat.magic_packet_length < sizeof(NetPacket_Head<id>)) return RecvResult::Error; size_t bytes_repeat = nat.magic_packet_length - sizeof(NetPacket_Head<id>); if (bytes_repeat % sizeof(NetPacket_Option<id>)) return RecvResult::Error; size_t has_opt_pls = bytes_repeat / sizeof(NetPacket_Option<id>); if (has_opt_pls > 1) return RecvResult::Error; *has_opt = has_opt_pls; if (!*has_opt || packet_fetch(s, sizeof(NetPacket_Head<id>), reinterpret_cast<Byte *>(&opt), sizeof(NetPacket_Option<id>))) { packet_discard(s, nat.magic_packet_length); return RecvResult::Complete; } return RecvResult::Incomplete; } return RecvResult::Incomplete; }
RecvResult net_recv_vpacket(Session *s, NetPacket_Head<id>& head, std::vector<NetPacket_Repeat<id>>& repeat) { bool ok = packet_fetch(s, 0, reinterpret_cast<Byte *>(&head), sizeof(NetPacket_Head<id>)); if (ok) { Packet_Head<id> nat; if (!network_to_native(&nat, head)) return RecvResult::Error; if (packet_avail(s) < nat.magic_packet_length) return RecvResult::Incomplete; if (nat.magic_packet_length < sizeof(NetPacket_Head<id>)) return RecvResult::Error; size_t bytes_repeat = nat.magic_packet_length - sizeof(NetPacket_Head<id>); if (bytes_repeat % sizeof(NetPacket_Repeat<id>)) return RecvResult::Error; repeat.resize(bytes_repeat / sizeof(NetPacket_Repeat<id>)); if (packet_fetch(s, sizeof(NetPacket_Head<id>), reinterpret_cast<Byte *>(repeat.data()), bytes_repeat)) { packet_discard(s, nat.magic_packet_length); return RecvResult::Complete; } return RecvResult::Incomplete; } return RecvResult::Incomplete; }
friend bool network_to_native(GmLevel *native, Little16 network) { uint16_t tmp; bool rv = network_to_native(&tmp, network); native->bits = tmp; return rv; }
bool network_to_native(Packet_Head<0x0069> *native, NetPacket_Head<0x0069> network) { bool rv = true; rv &= network_to_native(&native->magic_packet_id, network.magic_packet_id); rv &= network_to_native(&native->magic_packet_length, network.magic_packet_length); rv &= network_to_native(&native->login_id1, network.login_id1); rv &= network_to_native(&native->account_id, network.account_id); rv &= network_to_native(&native->login_id2, network.login_id2); rv &= network_to_native(&native->unused, network.unused); rv &= network_to_native(&native->last_login_string, network.last_login_string); rv &= network_to_native(&native->unused2, network.unused2); rv &= network_to_native(&native->sex, network.sex); return rv; }
RecvResult recv_opacket(Session *s, Packet_Head<id>& head, bool *has_opt, Packet_Option<id>& opt) { static_assert(id == Packet_Head<id>::PACKET_ID, "Packet_Head<id>::PACKET_ID"); static_assert(headsize == sizeof(NetPacket_Head<id>), "NetPacket_Head<id>"); static_assert(id == Packet_Option<id>::PACKET_ID, "Packet_Option<id>::PACKET_ID"); static_assert(optsize == sizeof(NetPacket_Option<id>), "NetPacket_Option<id>"); NetPacket_Head<id> net_head; NetPacket_Option<id> net_opt; RecvResult rv = net_recv_opacket(s, net_head, has_opt, net_opt); if (rv == RecvResult::Complete) { if (!network_to_native(&head, net_head)) return RecvResult::Error; assert (head.magic_packet_id == Packet_Head<id>::PACKET_ID); if (*has_opt) { if (!network_to_native(&opt, net_opt)) return RecvResult::Error; } } return rv; }
RecvResult recv_fpacket(Session *s, Packet_Fixed<id>& fixed) { static_assert(id == Packet_Fixed<id>::PACKET_ID, "Packet_Fixed<id>::PACKET_ID"); static_assert(size == sizeof(NetPacket_Fixed<id>), "NetPacket_Fixed<id>"); NetPacket_Fixed<id> net_fixed; RecvResult rv = net_recv_fpacket(s, net_fixed); if (rv == RecvResult::Complete) { if (!network_to_native(&fixed, net_fixed)) return RecvResult::Error; assert (fixed.magic_packet_id == Packet_Fixed<id>::PACKET_ID); } return rv; }
bool network_to_native(Packet_Repeat<0x0069> *native, NetPacket_Repeat<0x0069> network) { bool rv = true; rv &= network_to_native(&native->ip, network.ip); rv &= network_to_native(&native->port, network.port); rv &= network_to_native(&native->server_name, network.server_name); rv &= network_to_native(&native->users, network.users); rv &= network_to_native(&native->maintenance, network.maintenance); rv &= network_to_native(&native->is_new, network.is_new); return rv; }
RecvResult recv_vpacket(Session *s, Packet_Head<id>& head, AString& repeat) { static_assert(id == Packet_Head<id>::PACKET_ID, "Packet_Head<id>::PACKET_ID"); static_assert(headsize == sizeof(NetPacket_Head<id>), "NetPacket_Head<id>"); static_assert(id == Packet_Repeat<id>::PACKET_ID, "Packet_Repeat<id>::PACKET_ID"); static_assert(repeatsize == sizeof(NetPacket_Repeat<id>), "NetPacket_Repeat<id>"); static_assert(repeatsize == 1, "repeatsize"); NetPacket_Head<id> net_head; std::vector<NetPacket_Repeat<id>> net_repeat; RecvResult rv = net_recv_vpacket(s, net_head, net_repeat); assert (head.magic_packet_id == Packet_Head<id>::PACKET_ID); if (rv == RecvResult::Complete) { if (!network_to_native(&head, net_head)) return RecvResult::Error; // reinterpret_cast is needed to correctly handle an empty vector const char *begin = sign_cast<const char *>(net_repeat.data()); const char *end = begin + net_repeat.size(); end = std::find(begin, end, '\0'); repeat = XString(begin, end, nullptr); } return rv; }
RecvResult recv_ppacket(Session *s, Packet_Payload<id>& payload) { static_assert(id == Packet_Payload<id>::PACKET_ID, "Packet_Payload<id>::PACKET_ID"); NetPacket_Payload<id> net_payload; RecvResult rv = net_recv_ppacket(s, net_payload); if (rv == RecvResult::Complete) { if (!network_to_native(&payload, net_payload)) return RecvResult::Error; assert (payload.magic_packet_id == Packet_Payload<id>::PACKET_ID); if (id == 0x8000) { // 0x8000 is special if (packet_avail(s) < payload.magic_packet_length) return RecvResult::Incomplete; payload.magic_packet_length = 4; return RecvResult::Complete; } if (payload.magic_packet_length != sizeof(net_payload)) return RecvResult::Error; } return rv; }
friend bool network_to_native(GmLevel *native, Little32 network) { return network_to_native(&native->bits, network); }
bool network_to_native(Packet_Repeat<0x0063> *native, NetPacket_Repeat<0x0063> network) { bool rv = true; rv &= network_to_native(&native->c, network.c); return rv; }