Pol  Revision:cb584c9
msghandl.cpp
Go to the documentation of this file.
1 #include "msghandl.h"
2 
3 #include "../../clib/clib_endian.h"
4 #include "../../clib/logfacility.h"
5 #include "../../clib/passert.h"
6 #include "../../clib/rawtypes.h"
7 #include "../globals/network.h"
8 #include "../pktboth.h"
9 #include "../pktbothid.h"
10 #include "../pktin.h"
11 #include "../pktinid.h"
12 #include "../pktout.h"
13 #include "../pktoutid.h"
14 #include "client.h"
15 
16 namespace Pol
17 {
18 namespace Network
19 {
20 void PacketRegistry::handle_msg( unsigned char msgid, Client* client, void* msg )
21 {
22  MSG_HANDLER msghandler = find_handler( msgid, client );
23  msghandler.func( client, msg );
24 }
25 
26 MSG_HANDLER PacketRegistry::find_handler( unsigned char msgid, const Client* client )
27 {
28  if ( client->might_use_v2_handler() && PacketRegistry::msglen_v2( msgid ) )
29  return Core::networkManager.handler_v2[msgid];
30 
31  return Core::networkManager.handler[msgid];
32 }
33 
35  PacketVersion version /*= PacketVersion::V1*/ )
36 {
37  if ( version == PacketVersion::V1 )
38  return Core::networkManager.handler[msgid].func;
39 
40  return Core::networkManager.handler_v2[msgid].func;
41 }
42 
43 int PacketRegistry::msglen( unsigned char msgid )
44 {
45  return Core::networkManager.handler[msgid].msglen;
46 }
47 
48 int PacketRegistry::msglen_v2( unsigned char msgid )
49 {
50  return Core::networkManager.handler_v2[msgid].msglen;
51 }
52 
53 void PacketRegistry::set_handler( unsigned char msgid, int len, PktHandlerFunc func,
54  PacketVersion version /*= PacketVersion::V1*/ )
55 {
56  passert( len != 0 );
57 
58  if ( version == PacketVersion::V1 )
59  {
60  Core::networkManager.handler[msgid].func = func;
61  Core::networkManager.handler[msgid].msglen = len;
62  }
63  else
64  {
65  Core::networkManager.handler_v2[msgid].func = func;
66  Core::networkManager.handler_v2[msgid].msglen = len;
67  }
68 }
69 
70 bool PacketRegistry::is_defined( unsigned char msgid )
71 {
72  return Core::networkManager.handler[msgid].msglen ||
73  Core::networkManager.handler_v2[msgid].msglen;
74 }
75 
77 {
78  u16 msglen = cfBEu16( msg->msglen );
79 
80  if ( msglen > sizeof *msg ) // sanity-check the message.
81  return;
82  if ( msg->data[msglen - offsetof( Core::PKTIN_12, data ) - 1] !=
83  '\0' ) // the string must be null-terminated.
84  return;
85 
86  if ( Core::networkManager.ext_handler_table[msg->submsgtype].func )
87  Core::networkManager.ext_handler_table[msg->submsgtype].func( client, msg );
88 }
89 
91  void ( *func )( Client* client, Core::PKTIN_12* msg ) )
92 {
93  if ( Core::networkManager.ext_handler_table[submsgtype].func )
94  {
95  ERROR_PRINT.Format( "Extended Message Handler {} (0x{:X}) multiply defined.\n" )
96  << submsgtype << submsgtype;
97  throw std::runtime_error( "Extended Message Handler multiply defined." );
98  }
99  Core::networkManager.ext_handler_table[submsgtype].func = func;
100 }
101 
102 // Preprocessor macros for registering message handlers:
103 // example:
104 // MESSAGE_HANDLER( PKTBI_2C, handle_resurrect_menu );
105 //
106 // will register the fixed-length packet 2C with callback "handle_ressurect_menu"
107 #define MESSAGE_HANDLER( type, func ) \
108  PacketRegistry::set_handler( type##_ID, sizeof( type ), \
109  reinterpret_cast<Network::PktHandlerFunc>( func ), \
110  Network::PacketVersion::V1 )
111 
112 #define MESSAGE_HANDLER_VARLEN( type, func ) \
113  PacketRegistry::set_handler( type##_ID, MSGLEN_2BYTELEN_DATA, (Network::PktHandlerFunc)func, \
114  Network::PacketVersion::V1 )
115 
116 #define MESSAGE_HANDLER_V2( type, func ) \
117  PacketRegistry::set_handler( type##_ID, sizeof( type ), \
118  reinterpret_cast<Network::PktHandlerFunc>( func ), \
119  Network::PacketVersion::V2 )
120 
121 #define MESSAGE_HANDLER_VARLEN_V2( type, func ) \
122  PacketRegistry::set_handler( type##_ID, MSGLEN_2BYTELEN_DATA, (Network::PktHandlerFunc)func, \
123  Network::PacketVersion::V2 )
124 
126 {
127  using namespace Core;
128  MESSAGE_HANDLER( PKTIN_00, ClientCreateChar );
129  MESSAGE_HANDLER( PKTIN_02, handle_walk );
132  MESSAGE_HANDLER( PKTIN_06, doubleclick );
133  MESSAGE_HANDLER( PKTIN_07, get_item );
134  MESSAGE_HANDLER( PKTIN_08_V1, drop_item );
135  MESSAGE_HANDLER_V2( PKTIN_08_V2, drop_item_v2 );
136  MESSAGE_HANDLER( PKTIN_09, handle_singleclick );
138  MESSAGE_HANDLER( PKTIN_13, equip_item );
139  MESSAGE_HANDLER( PKTBI_22_SYNC, handle_resync_request );
141  MESSAGE_HANDLER( PKTIN_34, srequest );
142  MESSAGE_HANDLER( PKTBI_3A_LOCKS, handle_skill_lock );
144  MESSAGE_HANDLER( PKTBI_56, handle_map_pin );
145  MESSAGE_HANDLER( PKTIN_5D, char_select );
150  MESSAGE_HANDLER( PKTBI_72, handle_mode_set );
151  MESSAGE_HANDLER( PKTBI_73, handle_keep_alive );
152  MESSAGE_HANDLER( PKTIN_75, handle_rename_char );
154  MESSAGE_HANDLER( PKTIN_80, loginserver_login );
156  MESSAGE_HANDLER( PKTIN_8D, ClientCreateCharKR );
157  MESSAGE_HANDLER( PKTIN_91, login2 );
160  MESSAGE_HANDLER( PKTBI_98_IN, handle_allnames );
162  MESSAGE_HANDLER( PKTIN_9B, handle_help );
164  MESSAGE_HANDLER( PKTIN_A0, select_server );
165  MESSAGE_HANDLER( PKTIN_A4, handle_A4 );
166  MESSAGE_HANDLER( PKTIN_A7, handle_get_tip );
170  MESSAGE_HANDLER( PKTIN_B5, handle_msg_B5 );
173  MESSAGE_HANDLER( PKTBI_BB, handle_msg_BB );
177  MESSAGE_HANDLER( PKTOUT_C4, handle_unknown_C4 );
181  MESSAGE_HANDLER( PKTIN_D9, handle_D9 );
184  MESSAGE_HANDLER( PKTIN_EF, handle_ef_seed );
186 
188 }
189 
191 {
192  using namespace Core;
198 }
199 }
200 }
void equip_item(Network::Client *client, PKTIN_13 *msg)
Definition: eqpitem.cpp:30
void ClientCreateChar(Network::Client *client, PKTIN_00 *msg)
Definition: create.cpp:201
void handle_target_cursor(Network::Client *client, PKTBI_6C *msg)
Definition: target.cpp:37
void handle_attack(Network::Client *client, Core::PKTIN_05 *msg)
Definition: attack.cpp:28
void handle_secure_trade_msg(Network::Client *client, PKTBI_6F *msg)
Definition: dropitem.cpp:1057
void(* PktHandlerFunc)(Client *client, void *msg)
Definition: msghandl.h:29
void handle_msg_BB(Client *client, PKTBI_BB *)
Definition: miscmsg.cpp:207
void char_select(Network::Client *client, PKTIN_5D *msg)
Definition: pol.cpp:385
void handle_mode_set(Client *client, PKTBI_72 *msg)
Definition: miscmsg.cpp:87
void open_book_handler(Client *client, PKTBI_93 *msg)
Definition: uomod2.cpp:2434
std::array< Network::MSG_HANDLER, 256 > handler_v2
Definition: network.h:87
static int msglen(unsigned char msgid)
Definition: msghandl.cpp:43
void handle_char_profile_request(Client *client, PKTBI_B8_IN *msg)
Definition: miscmsg.cpp:151
void handle_msg_BF(Client *client, PKTBI_BF *msg)
Definition: miscmsg.cpp:306
void handle_open_spellbook(Network::Client *client, PKTIN_12 *msg)
Definition: spells.cpp:357
PktHandlerFunc func
Definition: msghandl.h:34
static MSG_HANDLER find_handler(unsigned char msgid, const Client *client)
Definition: msghandl.cpp:26
void handle_get_tip(Network::Client *client, PKTIN_A7 *msg)
Definition: tips.cpp:59
void gumpbutton_handler(Client *client, PKTIN_B1 *msg)
Definition: uomod2.cpp:1421
void handle_delete_character(Network::Client *client, PKTIN_83 *msg)
Definition: login.cpp:486
void handle_map_pin(Network::Client *client, PKTBI_56 *msg)
Definition: umap.cpp:358
void loginserver_login(Network::Client *client, PKTIN_80 *msg)
Definition: login.cpp:106
void login2(Network::Client *client, PKTIN_91 *msg)
Definition: login.cpp:388
static bool is_defined(unsigned char msgid)
Definition: msghandl.cpp:70
static void handle_msg(unsigned char msgid, Client *client, void *data)
Definition: msghandl.cpp:20
void handle_keep_alive(Network::Client *client, PKTBI_73 *msg)
Definition: pol.cpp:500
void get_item(Network::Client *client, PKTIN_07 *msg)
Definition: getitem.cpp:58
void KR_Verifier_Response(Network::Client *, PKTIN_E4 *)
Definition: login.cpp:517
void handle_prompt(Network::Client *client, PKTBI_9A *msg)
Definition: getmsg.cpp:41
void handle_ef_seed(Client *client, PKTIN_EF *msg)
Definition: miscmsg.cpp:471
unsigned short u16
Definition: rawtypes.h:26
void handle_client_version(Client *client, PKTBI_BD *msg)
Definition: miscmsg.cpp:212
void handle_e1_clienttype(Client *client, PKTIN_E1 *msg)
Definition: miscmsg.cpp:508
void handle_help(Network::Client *client, PKTIN_9B *)
Definition: help.cpp:20
static int msglen_v2(unsigned char msgid)
Definition: msghandl.cpp:48
void handle_allnames(Client *client, PKTBI_98_IN *msg)
Definition: miscmsg.cpp:428
static PktHandlerFunc get_callback(unsigned char msgid, PacketVersion version=PacketVersion::V1)
Definition: msghandl.cpp:34
void drop_item(Network::Client *client, PKTIN_08_V1 *msg)
Definition: dropitem.cpp:792
void srequest(Network::Client *client, PKTIN_34 *msg)
Definition: irequest.cpp:145
static void set_handler(unsigned char msgid, int len, PktHandlerFunc func, PacketVersion version=PacketVersion::V1)
Definition: msghandl.cpp:53
NetworkManager networkManager
Definition: network.cpp:28
void handle_cast_spell(Network::Client *client, PKTIN_12 *msg)
Definition: spells.cpp:349
void UnicodeSpeechHandler(Network::Client *client, PKTIN_AD *msgin)
Definition: speech.cpp:432
static void set_extended_handler(UEXTMSGID submsgtype, void(*func)(Client *client, Core::PKTIN_12 *msg))
Definition: msghandl.cpp:90
void handle_resync_request(Network::Client *client, PKTBI_22_SYNC *msg)
Definition: pol.cpp:483
void handle_bulletin_boards(Client *client, PKTBI_71 *)
Definition: miscmsg.cpp:82
void doubleclick(Network::Client *client, PKTIN_06 *msg)
Definition: dblclick.cpp:78
#define passert(exp)
Definition: passert.h:62
#define cfBEu16(x)
Definition: clib_endian.h:44
void handle_rename_char(Client *client, PKTIN_75 *msg)
Definition: miscmsg.cpp:107
void handle_textentry(Client *client, PKTIN_AC *msg)
Definition: uomod2.cpp:1623
void handle_A4(Network::Client *, PKTIN_A4 *)
Definition: login.cpp:225
void handle_menu_selection(Network::Client *client, PKTIN_7D *msg)
Definition: menusel.cpp:22
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
std::array< Network::ExtMsgHandler, 256 > ext_handler_table
Definition: network.h:89
#define MESSAGE_HANDLER(type, func)
Definition: msghandl.cpp:107
void handle_unicode_prompt(Client *client, Core::PKTBI_C2 *msg)
Definition: unimod.cpp:48
void buyhandler(Client *client, PKTBI_3B *msg)
Definition: uomod2.cpp:548
bool might_use_v2_handler() const
Definition: client.h:311
std::array< Network::MSG_HANDLER, 256 > handler
Definition: network.h:81
void handle_singleclick(Network::Client *client, PKTIN_09 *msg)
Definition: sngclick.cpp:126
static void handle_extended_cmd(Client *client, Core::PKTIN_12 *msg)
Definition: msghandl.cpp:76
void handle_action(Network::Client *client, PKTIN_12 *cmd)
Definition: bowsalut.cpp:247
void handle_skill_lock(Network::Client *client, PKTBI_3A_LOCKS *msg)
Definition: irequest.cpp:105
void handle_msg_B5(Client *client, PKTIN_B5 *)
Definition: miscmsg.cpp:146
void handle_request_tooltip(Network::Client *client, PKTIN_B6 *msgin)
Definition: tooltips.cpp:39
#define MESSAGE_HANDLER_VARLEN(type, func)
Definition: msghandl.cpp:112
u8 data[300]
Definition: pktin.h:185
#define MESSAGE_HANDLER_V2(type, func)
Definition: msghandl.cpp:116
static void initialize_msg_handlers()
Definition: msghandl.cpp:125
void SpeechHandler(Network::Client *client, PKTIN_03 *mymsg)
Definition: speech.cpp:177
void drop_item_v2(Network::Client *client, PKTIN_08_V2 *msg)
Definition: dropitem.cpp:861
#define ERROR_PRINT
Definition: logfacility.h:230
void handle_use_skill(Network::Client *client, PKTIN_12 *msg)
Definition: useskill.cpp:30
void handle_walk(Network::Client *client, PKTIN_02 *msg02)
Definition: movement.cpp:137
void sellhandler(Client *client, PKTIN_9F *msg)
Definition: uomod2.cpp:890
void handle_update_range_change(Client *client, PKTBI_C8 *)
Definition: miscmsg.cpp:423
void handle_selcolor(Client *client, PKTBI_95 *msg)
Definition: uomod2.cpp:2140
void ClientCreateCharKR(Network::Client *client, PKTIN_8D *msg)
Definition: create.cpp:595
void handle_D9(Network::Client *client, PKTIN_D9 *msg)
Definition: login.cpp:227
void read_book_page_handler(Client *client, PKTBI_66 *msg)
Definition: uomod2.cpp:2333
void handle_unknown_C4(Client *client, PKTOUT_C4 *)
Definition: miscmsg.cpp:418
Definition: berror.cpp:12
static void initialize_extended_handlers()
Definition: msghandl.cpp:190
void handle_se_object_list(Client *client, PKTBI_D6_IN *msgin)
Definition: miscmsg.cpp:455
void handle_aos_commands(Client *client, PKTBI_D7 *msg)
Definition: miscmsg.cpp:529
void handle_resurrect_menu(Client *client, PKTBI_2C *msg)
Definition: uomod2.cpp:2095
void select_server(Network::Client *client, PKTIN_A0 *msg)
Definition: login.cpp:260
void ClientCreateChar70160(Network::Client *client, PKTIN_F8 *msg)
Definition: create.cpp:953