netmsgr.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // netmsgr.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 05/21/97 MJR Started.
  23. //
  24. // 05/25/97 JMI Integrated CBufQ's; finished NetMsg union; and filled in
  25. // CNetMsgr functions.
  26. //
  27. // 05/25/97 JMI Made pumping in Update() loop until conditions dictate that
  28. // no more data can be received/sent without blocking during
  29. // the current Update().
  30. //
  31. // 05/26/97 MJR Moved some functions from here to .cpp file.
  32. // Took out "friend" stuff, made two members public instead.
  33. //
  34. // 05/26/97 JMI Added 1 to NetMsgr::Chat::Len.
  35. //
  36. // 06/11/97 JMI Added FINISH_REALM and FINISHED_REALM net messages.
  37. //
  38. // 06/14/97 MJR Removed LOAD_REALM and FINISHED_REALM messages and
  39. // modified START_GAME message.
  40. //
  41. // 06/15/97 MJR Added new reason for denying a join request.
  42. //
  43. // 08/13/97 MJR Added difficulty to startGame message.
  44. // MJR Added support for getting/putting mac addresses.
  45. //
  46. // 08/15/97 MJR Fixed bug in how prototype was being read & written.
  47. //
  48. // 08/18/97 MJR Added "ChangeReq" and "Changed" messages.
  49. // Added more parameters to JoinReq and Joined messages.
  50. //
  51. // 08/23/97 MJR Added Reset().
  52. //
  53. // 09/01/97 MJR Lots of changes as part of overall network overhaul.
  54. //
  55. // 09/02/97 MJR Tested and tuned alot, and fixed a bunch of bugs. Now
  56. // appears to be very stable.
  57. //
  58. // 09/07/97 MJR Added "Proceed" message and renamed some error types.
  59. //
  60. // 09/09/97 MJR Changed Connect() so it returns the error that
  61. // RSocket::Open() returned (if there is an error).
  62. //
  63. // 11/20/97 JMI Added sCoopLevels & sCoopMode to SetupGame and
  64. // StartGame.
  65. //
  66. // 11/24/97 JMI Upped MinVersionNum and CurVersionNum to 0x0002 for PCs and
  67. // 0x1002 for Macs. Also, added MacVersionBit (0x1000).
  68. // Also, changed VersionMismatchError to
  69. // ServerVersionMismatchError and added
  70. // ClientVersionMismatchError, ServerPlatformMismatchError,
  71. // and ClientPlatformMismatchError.
  72. // Also, added ulParam for a generic parameter for messages
  73. // (currently used by Client/ServerVersionMismatchError to
  74. // communicate the other machine's Postal net version number).
  75. //
  76. // 10/11/99 JMI Upped net version number to 3. This causes the version 3
  77. // _client_ to reject connections from server versions 1 & 2.
  78. // Client versions 1 & 2 will reject connections to version 3
  79. // servers. This works this way due to issues explained in
  80. // NetClient.cpp in the comment in the function Update() in
  81. // the case for handling LOGIN_ACCEPT when in
  82. // WaitForLoginResponse state.
  83. //
  84. ////////////////////////////////////////////////////////////////////////////////
  85. #ifndef NETMSGR_H
  86. #define NETMSGR_H
  87. #include "RSPiX.h"
  88. #include "net.h"
  89. #include "socket.h"
  90. #include "BufQ.h"
  91. #include "input.h"
  92. ////////////////////////////////////////////////////////////////////////////////
  93. // Function makes it easy to get socket address from buffer queue
  94. ////////////////////////////////////////////////////////////////////////////////
  95. inline void GetSocketAddress(
  96. CBufQ* pBuf,
  97. RSocket::Address* paddress)
  98. {
  99. // Stuff that's common to all sockets
  100. long lTmp;
  101. pBuf->Get(&lTmp);
  102. paddress->prototype = (RSocket::ProtoType)lTmp;
  103. pBuf->Get(&paddress->lAddressLen);
  104. // The rest is protocol-dependant
  105. switch(paddress->prototype)
  106. {
  107. case RSocket::TCPIP:
  108. {
  109. RProtocolBSDIP::AddressIP* p = (RProtocolBSDIP::AddressIP*)paddress;
  110. pBuf->Get(&p->address.sin_family);
  111. // Don't byte-swap these!!! They are always in network order on all systems!
  112. pBuf->Get((U8*)&p->address.sin_port, sizeof(p->address.sin_port));
  113. pBuf->Get((U8*)&p->address.sin_addr, sizeof(p->address.sin_addr));
  114. pBuf->Get((U8*)&p->address.sin_zero, sizeof(p->address.sin_zero));
  115. }
  116. break;
  117. default:
  118. TRACE("PutSocketAddress(): Unknown protocol!\n");
  119. break;
  120. }
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. // Function makes it easy to put socket address to buffer queue
  124. ////////////////////////////////////////////////////////////////////////////////
  125. inline void PutSocketAddress(
  126. CBufQ* pBuf,
  127. RSocket::Address* paddress)
  128. {
  129. // The RSocket::Address is a bit tricky, and different for each protocol
  130. pBuf->Put((long)paddress->prototype);
  131. pBuf->Put(paddress->lAddressLen);
  132. switch(paddress->prototype)
  133. {
  134. case RSocket::TCPIP:
  135. {
  136. RProtocolBSDIP::AddressIP* p = (RProtocolBSDIP::AddressIP*)paddress;
  137. pBuf->Put(p->address.sin_family);
  138. // Don't let these byte-swap!!! They are always in network order on all systems!
  139. pBuf->Put((U8*)&p->address.sin_port, sizeof(p->address.sin_port));
  140. pBuf->Put((U8*)&p->address.sin_addr, sizeof(p->address.sin_addr));
  141. pBuf->Put((U8*)&p->address.sin_zero, sizeof(p->address.sin_zero));
  142. }
  143. break;
  144. default:
  145. TRACE("PutSocketAddress(): Unknown protocol!\n");
  146. break;
  147. }
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. //
  151. // Define a net message
  152. //
  153. ////////////////////////////////////////////////////////////////////////////////
  154. class NetMsg
  155. {
  156. // // forward declaration
  157. // class CNetComm;
  158. //------------------------------------------------------------------------------
  159. // Types, enums, etc.
  160. //------------------------------------------------------------------------------
  161. public:
  162. // Message types. Do NOT change the order without making the same changes
  163. // to the CNetMsgr static array of info corersponding to these values!!!
  164. typedef enum
  165. {
  166. NOTHING, // 0
  167. STAT, // 1
  168. ERR, // 2
  169. LOGIN, // 3
  170. LOGIN_ACCEPT, // 4
  171. LOGIN_DENY, // 5
  172. LOGOUT, // 6
  173. JOIN_REQ, // 7
  174. JOIN_ACCEPT, // 8
  175. JOIN_DENY, // 9
  176. JOINED, // 10
  177. CHANGE_REQ, // 11
  178. CHANGED, // 12
  179. DROP_REQ, // 13
  180. DROPPED, // 14
  181. DROP_ACK, // 15
  182. INPUT_REQ, // 16
  183. INPUT_DATA, // 17
  184. INPUT_MARK, // 18
  185. CHAT_REQ, // 19
  186. CHAT, // 20
  187. SETUP_GAME, // 21
  188. START_GAME, // 22
  189. ABORT_GAME, // 23
  190. READY_REALM, // 24
  191. BAD_REALM, // 25
  192. START_REALM, // 26
  193. HALT_REALM, // 27
  194. NEXT_REALM, // 28
  195. PROGRESS_REALM, // 29
  196. PROCEED, // 30
  197. PING, // 31
  198. RAND, // 32
  199. // Total number of messages (must be the last value)
  200. NumMessages
  201. };
  202. // Reasons client can't join
  203. typedef enum
  204. {
  205. TooManyPlayers,
  206. BandwidthTooLow,
  207. GameAlreadyStarted
  208. };
  209. // Reasons why game was aborted
  210. typedef enum
  211. {
  212. UserAbortedGame,
  213. ErrorAbortedGame
  214. };
  215. // Reasons for errors
  216. typedef enum
  217. {
  218. NoError,
  219. ReceiveError,
  220. InQFullError,
  221. OutQFullError,
  222. SendError,
  223. InQReadError,
  224. OutQWriteError,
  225. ConnectionError,
  226. TimeoutError,
  227. ListenError,
  228. CantConnectError,
  229. ConnectTimeoutError,
  230. ServerVersionMismatchError,
  231. LoginDeniedError,
  232. JoinDeniedError,
  233. CantOpenPeerSocketError,
  234. ClientVersionMismatchError,
  235. ServerPlatformMismatchError,
  236. ClientPlatformMismatchError,
  237. // Keep this as last value
  238. NumErrors
  239. } Error;
  240. // Reasons for status
  241. typedef enum
  242. {
  243. NoStatus,
  244. Opened,
  245. Connected,
  246. LoginAccepted,
  247. JoinAccepted,
  248. // Keep this as last value
  249. NumStatii
  250. } Status;
  251. //------------------------------------------------------------------------------
  252. // Typedefs for each of the message structs.
  253. //
  254. // Aside from some no-longer-valid historical reasons why these started out as
  255. // structs instead of classes, there are still some reasons why they remain
  256. // structs instead of classes. With a union of classes, we can declare the
  257. // "container" struct (NetMsg) and change it into any message we like by simply
  258. // changing the message type. With a class, if we declare the base class, we
  259. // only get enough data for the base class itself. We could probably declare
  260. // a union of classes, but that would then include memory for virtual function
  261. // pointers (really??), and I feel less comfortable casting classes back and
  262. // forth into other classes than I do with structs. Not GREAT reasons, but
  263. // that's why it is what it is.
  264. //------------------------------------------------------------------------------
  265. typedef struct Nothing
  266. {
  267. enum { Size = 1 };
  268. unsigned char ucType; // Message type
  269. long lSize; // Size of message for variable-sized messages
  270. // This is not an integral part of the Nothing
  271. // message, but rather it's a way for us to
  272. // generically access the size member of a
  273. // variable-sized message. For those messages,
  274. // the size always follows the type.
  275. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  276. {
  277. TRACE("ERROR! NOTHING message is not intended to be transmitted!\n");
  278. ASSERT(0);
  279. }
  280. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  281. {
  282. TRACE("ERROR! NOTHING message is not intended to be transmitted!\n");
  283. ASSERT(0);
  284. }
  285. } Nothing;
  286. typedef struct Stat
  287. {
  288. enum { Size = 1 + sizeof(Status) };
  289. unsigned char ucType; // Message type
  290. Status status; // status value
  291. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  292. {
  293. TRACE("ERROR! STAT message is not intended to be transmitted!\n");
  294. ASSERT(0);
  295. }
  296. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  297. {
  298. TRACE("ERROR! STAT message is not intended to be transmitted!\n");
  299. ASSERT(0);
  300. }
  301. } Stat;
  302. typedef struct Err
  303. {
  304. enum { Size = 1 + sizeof(Error) + sizeof(ULONG) };
  305. unsigned char ucType; // Message type
  306. Error error; // error value
  307. ULONG ulParam; // Miscellaneous param for errors.
  308. // For *VersionMismatchError, used
  309. // for other system's version for
  310. // error reportage.
  311. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  312. {
  313. TRACE("ERROR! ERR message is not intended to be transmitted!\n");
  314. ASSERT(0);
  315. }
  316. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  317. {
  318. TRACE("ERROR! ERR message is not intended to be transmitted!\n");
  319. ASSERT(0);
  320. }
  321. } Err;
  322. // client tells server it wants to login
  323. typedef struct Login
  324. {
  325. enum { Size = 9 };
  326. unsigned char ucType; // Message type
  327. unsigned long ulMagic; // Magic number
  328. unsigned long ulVersion; // Version number
  329. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  330. {
  331. pBuf->Get(&pmsg->msg.login.ucType);
  332. pBuf->Get(&pmsg->msg.login.ulMagic);
  333. pBuf->Get(&pmsg->msg.login.ulVersion);
  334. }
  335. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  336. {
  337. pBuf->Put(&pmsg->msg.login.ucType);
  338. pBuf->Put(&pmsg->msg.login.ulMagic);
  339. pBuf->Put(&pmsg->msg.login.ulVersion);
  340. }
  341. } Login;
  342. // server tells client it may login
  343. typedef struct LoginAccept
  344. {
  345. enum { Size = 10 };
  346. unsigned char ucType; // Message type
  347. Net::ID idAssigned; // Assigned ID
  348. unsigned long ulMagic; // Magic number
  349. unsigned long ulVersion; // Version number
  350. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  351. {
  352. pBuf->Get(&pmsg->msg.loginAccept.ucType);
  353. pBuf->Get(&pmsg->msg.loginAccept.idAssigned);
  354. pBuf->Get(&pmsg->msg.loginAccept.ulMagic);
  355. pBuf->Get(&pmsg->msg.loginAccept.ulVersion);
  356. }
  357. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  358. {
  359. pBuf->Put(&pmsg->msg.loginAccept.ucType);
  360. pBuf->Put(&pmsg->msg.loginAccept.idAssigned);
  361. pBuf->Put(&pmsg->msg.loginAccept.ulMagic);
  362. pBuf->Put(&pmsg->msg.loginAccept.ulVersion);
  363. }
  364. } LoginAccept;
  365. // server tells client it may not login
  366. typedef struct LoginDeny
  367. {
  368. enum { Size = 1 };
  369. unsigned char ucType; // Message type
  370. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  371. {
  372. pBuf->Get(&pmsg->msg.loginDeny.ucType);
  373. }
  374. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  375. {
  376. pBuf->Put(&pmsg->msg.loginDeny.ucType);
  377. }
  378. } LoginDeny;
  379. // client tells server it is logging out
  380. typedef struct Logout
  381. {
  382. enum { Size = 1 };
  383. unsigned char ucType; // Message type
  384. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  385. {
  386. pBuf->Get(&pmsg->msg.logout.ucType);
  387. }
  388. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  389. {
  390. pBuf->Put(&pmsg->msg.logout.ucType);
  391. }
  392. } Logout;
  393. // client tells server it wants to join
  394. typedef struct JoinReq
  395. {
  396. enum { Size = 1 + Net::MaxPlayerNameSize + 1 + 1 + 2 };
  397. unsigned char ucType; // Message type
  398. char acName[Net::MaxPlayerNameSize];// Name
  399. unsigned char ucColor; // Color number
  400. unsigned char ucTeam; // Team number
  401. short sBandwidth; // Bandwidth
  402. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  403. {
  404. pBuf->Get(&pmsg->msg.joinReq.ucType);
  405. pBuf->Get(pmsg->msg.joinReq.acName, sizeof(pmsg->msg.joinReq.acName));
  406. pBuf->Get(&pmsg->msg.joinReq.ucColor);
  407. pBuf->Get(&pmsg->msg.joinReq.ucTeam);
  408. pBuf->Get(&pmsg->msg.joinReq.sBandwidth);
  409. }
  410. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  411. {
  412. pBuf->Put(&pmsg->msg.joinReq.ucType);
  413. pBuf->Put(pmsg->msg.joinReq.acName, sizeof(pmsg->msg.joinReq.acName));
  414. pBuf->Put(&pmsg->msg.joinReq.ucColor);
  415. pBuf->Put(&pmsg->msg.joinReq.ucTeam);
  416. pBuf->Put(&pmsg->msg.joinReq.sBandwidth);
  417. }
  418. } JoinReq;
  419. // server tells client it can join
  420. typedef struct JoinAccept
  421. {
  422. enum { Size = 1 };
  423. unsigned char ucType; // Message type
  424. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  425. {
  426. pBuf->Get(&pmsg->msg.joinAccept.ucType);
  427. }
  428. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  429. {
  430. pBuf->Put(&pmsg->msg.joinAccept.ucType);
  431. }
  432. } JoinAccept;
  433. // server tells client it cannot join
  434. typedef struct JoinDeny
  435. {
  436. enum { Size = 2 };
  437. unsigned char ucType; // Message type
  438. unsigned char ucReason; // Reason for denail
  439. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  440. {
  441. pBuf->Get(&pmsg->msg.joinDeny.ucType);
  442. pBuf->Get(&pmsg->msg.joinDeny.ucReason);
  443. }
  444. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  445. {
  446. pBuf->Put(&pmsg->msg.joinDeny.ucType);
  447. pBuf->Put(&pmsg->msg.joinDeny.ucReason);
  448. }
  449. } JoinDeny;
  450. // server tells client about another client (or about itself!)
  451. typedef struct Joined
  452. {
  453. enum { Size = 1 + 1 + sizeof(RSocket::Address) + Net::MaxPlayerNameSize + 1 + 1 + 2 };
  454. unsigned char ucType; // Message type
  455. Net::ID id; // ID
  456. RSocket::Address address; // Address
  457. char acName[Net::MaxPlayerNameSize];// Name
  458. unsigned char ucColor; // Color number
  459. unsigned char ucTeam; // Team number
  460. short sBandwidth; // Bandwidth
  461. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  462. {
  463. pBuf->Get(&pmsg->msg.joined.ucType);
  464. pBuf->Get(&pmsg->msg.joined.id);
  465. GetSocketAddress(pBuf, &pmsg->msg.joined.address);
  466. pBuf->Get(pmsg->msg.joined.acName, sizeof(pmsg->msg.joined.acName));
  467. pBuf->Get(&pmsg->msg.joined.ucColor);
  468. pBuf->Get(&pmsg->msg.joined.ucTeam);
  469. pBuf->Get(&pmsg->msg.joined.sBandwidth);
  470. }
  471. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  472. {
  473. pBuf->Put(&pmsg->msg.joined.ucType);
  474. pBuf->Put(&pmsg->msg.joined.id);
  475. PutSocketAddress(pBuf, &pmsg->msg.joined.address);
  476. pBuf->Put(pmsg->msg.joined.acName, sizeof(pmsg->msg.joined.acName));
  477. pBuf->Put(&pmsg->msg.joined.ucColor);
  478. pBuf->Put(&pmsg->msg.joined.ucTeam);
  479. pBuf->Put(&pmsg->msg.joined.sBandwidth);
  480. }
  481. } Joined;
  482. // client tells server it wants to change its info
  483. typedef struct ChangeReq
  484. {
  485. enum { Size = 1 + Net::MaxPlayerNameSize + 1 + 1 };
  486. unsigned char ucType; // Message type
  487. char acName[Net::MaxPlayerNameSize];// Name
  488. unsigned char ucColor; // Color number
  489. unsigned char ucTeam; // Team number
  490. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  491. {
  492. pBuf->Get(&pmsg->msg.changeReq.ucType);
  493. pBuf->Get(pmsg->msg.changeReq.acName, sizeof(pmsg->msg.changeReq.acName));
  494. pBuf->Get(&pmsg->msg.changeReq.ucColor);
  495. pBuf->Get(&pmsg->msg.changeReq.ucTeam);
  496. }
  497. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  498. {
  499. pBuf->Put(&pmsg->msg.changeReq.ucType);
  500. pBuf->Put(pmsg->msg.changeReq.acName, sizeof(pmsg->msg.changeReq.acName));
  501. pBuf->Put(&pmsg->msg.changeReq.ucColor);
  502. pBuf->Put(&pmsg->msg.changeReq.ucTeam);
  503. }
  504. } ChangeReq;
  505. // server tells client about a client's change
  506. typedef struct Changed
  507. {
  508. enum { Size = 1 + 1 + Net::MaxPlayerNameSize + 1 + 1 };
  509. unsigned char ucType; // Message type
  510. Net::ID id; // ID
  511. char acName[Net::MaxPlayerNameSize];// Name
  512. unsigned char ucColor; // Color number
  513. unsigned char ucTeam; // Team number
  514. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  515. {
  516. pBuf->Get(&pmsg->msg.changed.ucType);
  517. pBuf->Get(&pmsg->msg.changed.id);
  518. pBuf->Get(pmsg->msg.changed.acName, sizeof(pmsg->msg.changed.acName));
  519. pBuf->Get(&pmsg->msg.changed.ucColor);
  520. pBuf->Get(&pmsg->msg.changed.ucTeam);
  521. }
  522. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  523. {
  524. pBuf->Put(&pmsg->msg.changed.ucType);
  525. pBuf->Put(&pmsg->msg.changed.id);
  526. pBuf->Put(pmsg->msg.changed.acName, sizeof(pmsg->msg.changed.acName));
  527. pBuf->Put(&pmsg->msg.changed.ucColor);
  528. pBuf->Put(&pmsg->msg.changed.ucTeam);
  529. }
  530. } Changed;
  531. // client tells server it wants to drop
  532. typedef struct DropReq
  533. {
  534. enum { Size = 1 };
  535. unsigned char ucType; // Message type
  536. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  537. {
  538. pBuf->Get(&pmsg->msg.dropReq.ucType);
  539. }
  540. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  541. {
  542. pBuf->Put(&pmsg->msg.dropReq.ucType);
  543. }
  544. } DropReq;
  545. // Server ---> Client
  546. //
  547. // When the server sends one of these before the game has started,
  548. // sContext will be -1, which indicates that no response is
  549. // required. All the clients simply drop that player.
  550. //
  551. // When the server sends one of these during a game, sContext
  552. // will be >= 0. It is used when multiple players are being dropped
  553. // at the same time. All further drop messages related to a particular
  554. // drop context must contain this context number. The server and the
  555. // clients all pause their games throughout a drop context. Clients
  556. // must respond to this message with a DropAck.
  557. typedef struct Dropped
  558. {
  559. enum { Size = 1 + 1 + 2 };
  560. unsigned char ucType; // Message type
  561. Net::ID id; // Dropee's ID
  562. short sContext; // Context or -1 if none
  563. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  564. {
  565. pBuf->Get(&pmsg->msg.dropped.ucType);
  566. pBuf->Get(&pmsg->msg.dropped.id);
  567. pBuf->Get(&pmsg->msg.dropped.sContext);
  568. }
  569. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  570. {
  571. pBuf->Put(&pmsg->msg.dropped.ucType);
  572. pBuf->Put(&pmsg->msg.dropped.id);
  573. pBuf->Put(&pmsg->msg.dropped.sContext);
  574. }
  575. } Dropped;
  576. // Client ---> Server
  577. //
  578. // When clients receive a Dropped message with a valid drop
  579. // context (not -1), they pause their game and respond with this
  580. // message, including the drop context within the message so the
  581. // server knows which context the message belongs to. This message
  582. // tells the server what the client's client's last known
  583. // input sequence from the dropped player is, along with what
  584. // the client's current frame number is.
  585. //
  586. // The server uses these responses to determine which client knows
  587. // the most about the dropee and which client is furthest ahead
  588. // in terms of frames. From that, it can also determine how much of
  589. // the dropee's input data to send to each of the clients so that
  590. // they all have the same information.
  591. typedef struct DropAck
  592. {
  593. enum { Size = 1 + sizeof(Net::SEQ) + sizeof(Net::SEQ) };
  594. unsigned char ucType; // Message type
  595. Net::SEQ seqLastDropeeInput; // Client's last input seq from dropee
  596. Net::SEQ seqLastDoneFrame; // Client's last frame that was done
  597. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  598. {
  599. pBuf->Get(&pmsg->msg.dropAck.ucType);
  600. pBuf->Get(&pmsg->msg.dropAck.seqLastDropeeInput);
  601. pBuf->Get(&pmsg->msg.dropAck.seqLastDoneFrame);
  602. }
  603. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  604. {
  605. pBuf->Put(&pmsg->msg.dropAck.ucType);
  606. pBuf->Put(&pmsg->msg.dropAck.seqLastDropeeInput);
  607. pBuf->Put(&pmsg->msg.dropAck.seqLastDoneFrame);
  608. }
  609. } DropAck;
  610. // Server ---> Client
  611. //
  612. // The server sends these to a client to request that the client
  613. // supply the range of input data for the specified net ID.
  614. //
  615. // Previous to this message, the server would have learned
  616. // what range of inputs the client had available by way of
  617. // the DropAck that the client sent to the server.
  618. typedef struct InputReq
  619. {
  620. enum { Size = 1 + 1 + sizeof(Net::SEQ) + 2 };
  621. unsigned char ucType; // Message type
  622. Net::ID id; // ID whose input is being requested
  623. Net::SEQ seqStart; // Startng seq of range
  624. short sNum; // Number of seq's in range
  625. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  626. {
  627. pBuf->Get(&pmsg->msg.inputReq.ucType);
  628. pBuf->Get(&pmsg->msg.inputReq.id);
  629. pBuf->Get(&pmsg->msg.inputReq.seqStart);
  630. pBuf->Get(&pmsg->msg.inputReq.sNum);
  631. }
  632. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  633. {
  634. pBuf->Put(&pmsg->msg.inputReq.ucType);
  635. pBuf->Put(&pmsg->msg.inputReq.id);
  636. pBuf->Put(&pmsg->msg.inputReq.seqStart);
  637. pBuf->Put(&pmsg->msg.inputReq.sNum);
  638. }
  639. } InputReq;
  640. // Client <---> Server
  641. // Variable sized message!
  642. //
  643. // The client sends this in response to the server's InputReq.
  644. typedef struct InputData
  645. {
  646. enum { Size = -1 }; // -1 indicates variable-sized message
  647. unsigned char ucType; // Message type
  648. long lSize; // Message size (must follow type!)
  649. Net::ID id; // ID whose input is being sent
  650. Net::SEQ seqStart; // Starting seq of range
  651. short sNum; // Number of seq's in range
  652. UINPUT* pInputs; // Pointer used to read/write actual input data
  653. U8* pFrameTimes; // Pointer to read/write actual frame time data *SPA
  654. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  655. {
  656. pBuf->Get(&pmsg->msg.inputData.ucType);
  657. pBuf->Get(&pmsg->msg.inputData.lSize);
  658. pBuf->Get(&pmsg->msg.inputData.id);
  659. pBuf->Get(&pmsg->msg.inputData.seqStart);
  660. pBuf->Get(&pmsg->msg.inputData.sNum);
  661. // Allocate buffer for variable-sized data
  662. pmsg->msg.inputData.pInputs = (UINPUT*)AllocVar(pmsg, (long)pmsg->msg.inputData.sNum * sizeof(UINPUT));
  663. // Get the variable-sized data
  664. pBuf->Get(pmsg->msg.inputData.pInputs, (long)(pmsg->msg.inputData.sNum));
  665. }
  666. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  667. {
  668. // Calculate message size
  669. pmsg->msg.inputData.lSize = 1 + 4 + 1 + sizeof(Net::SEQ) + 2 + ((long)(pmsg->msg.inputData.sNum) * sizeof(UINPUT));
  670. // Write message
  671. pBuf->Put(&pmsg->msg.inputData.ucType);
  672. pBuf->Put(&pmsg->msg.inputData.lSize);
  673. pBuf->Put(&pmsg->msg.inputData.id);
  674. pBuf->Put(&pmsg->msg.inputData.seqStart);
  675. pBuf->Put(&pmsg->msg.inputData.sNum);
  676. pBuf->Put(pmsg->msg.inputData.pInputs, (long)(pmsg->msg.inputData.sNum));
  677. }
  678. } InputData;
  679. // Server ---> Client
  680. //
  681. // The server sends these to a client to tell the client
  682. // to mark the specified client's input data as being
  683. // invalid after the specified seq.
  684. typedef struct InputMark
  685. {
  686. enum { Size = 1 + 1 + sizeof(Net::SEQ) };
  687. unsigned char ucType; // Message type
  688. Net::ID id; // ID whose input is being requested
  689. Net::SEQ seqMark; // Seq to mark
  690. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  691. {
  692. pBuf->Get(&pmsg->msg.inputMark.ucType);
  693. pBuf->Get(&pmsg->msg.inputMark.id);
  694. pBuf->Get(&pmsg->msg.inputMark.seqMark);
  695. }
  696. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  697. {
  698. pBuf->Put(&pmsg->msg.inputMark.ucType);
  699. pBuf->Put(&pmsg->msg.inputMark.id);
  700. pBuf->Put(&pmsg->msg.inputMark.seqMark);
  701. }
  702. } InputMark;
  703. // client tells server it wants to chat
  704. typedef struct ChatReq
  705. {
  706. enum { Size = 1 + 2 + Net::MaxChatSize };
  707. unsigned char ucType; // Message type
  708. U16 u16Mask; // Who will get this chat text
  709. char acText[Net::MaxChatSize]; // Chat text
  710. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  711. {
  712. pBuf->Get(&pmsg->msg.chatReq.ucType);
  713. pBuf->Get(&pmsg->msg.chatReq.u16Mask);
  714. pBuf->Get(pmsg->msg.chatReq.acText, sizeof(pmsg->msg.chatReq.acText));
  715. }
  716. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  717. {
  718. pBuf->Put(&pmsg->msg.chatReq.ucType);
  719. pBuf->Put(&pmsg->msg.chatReq.u16Mask);
  720. pBuf->Put(pmsg->msg.chatReq.acText, sizeof(pmsg->msg.chatReq.acText));
  721. }
  722. } ChatReq;
  723. // server tells specified clients the chat message
  724. typedef struct Chat
  725. {
  726. enum { Size = 1 + 1 + Net::MaxChatSize };
  727. unsigned char ucType; // Message type
  728. unsigned char id; // Who said this
  729. char acText[Net::MaxChatSize]; // Chat text
  730. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  731. {
  732. pBuf->Get(&pmsg->msg.chat.ucType);
  733. pBuf->Get(&pmsg->msg.chat.id);
  734. pBuf->Get(pmsg->msg.chat.acText, sizeof(pmsg->msg.chat.acText));
  735. }
  736. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  737. {
  738. pBuf->Put(&pmsg->msg.chat.ucType);
  739. pBuf->Put(&pmsg->msg.chat.id);
  740. pBuf->Put(pmsg->msg.chat.acText, sizeof(pmsg->msg.chat.acText));
  741. }
  742. } Chat;
  743. // server tells clients how to setup game
  744. typedef struct SetupGame
  745. {
  746. enum { Size = 1 + 2 + Net::MaxRealmNameSize + 2 + 2 + 2 + 2 + 2 + 2 };
  747. unsigned char ucType; // Message type
  748. short sRealmNum; // Starting realm number or -1 to use name
  749. char acRealmFile[Net::MaxRealmNameSize];// Name of realm file to load
  750. short sDifficulty; // Difficulty level
  751. short sRejuvenate;
  752. short sTimeLimit;
  753. short sKillLimit;
  754. short sCoopLevels; // Non-zero for cooperative levels, zero for deathmatch levels.
  755. short sCoopMode; // Non-zero for cooperative mode, zero for deathmatch mode.
  756. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  757. {
  758. pBuf->Get(&pmsg->msg.setupGame.ucType);
  759. pBuf->Get(&pmsg->msg.setupGame.sRealmNum);
  760. pBuf->Get(pmsg->msg.setupGame.acRealmFile, sizeof(pmsg->msg.setupGame.acRealmFile));
  761. pBuf->Get(&pmsg->msg.setupGame.sDifficulty);
  762. pBuf->Get(&pmsg->msg.setupGame.sRejuvenate);
  763. pBuf->Get(&pmsg->msg.setupGame.sTimeLimit);
  764. pBuf->Get(&pmsg->msg.setupGame.sKillLimit);
  765. pBuf->Get(&pmsg->msg.setupGame.sCoopLevels);
  766. pBuf->Get(&pmsg->msg.setupGame.sCoopMode);
  767. }
  768. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  769. {
  770. pBuf->Put(&pmsg->msg.setupGame.ucType);
  771. pBuf->Put(&pmsg->msg.setupGame.sRealmNum);
  772. pBuf->Put(pmsg->msg.setupGame.acRealmFile, sizeof(pmsg->msg.setupGame.acRealmFile));
  773. pBuf->Put(&pmsg->msg.setupGame.sDifficulty);
  774. pBuf->Put(&pmsg->msg.setupGame.sRejuvenate);
  775. pBuf->Put(&pmsg->msg.setupGame.sTimeLimit);
  776. pBuf->Put(&pmsg->msg.setupGame.sKillLimit);
  777. pBuf->Put(&pmsg->msg.setupGame.sCoopLevels);
  778. pBuf->Put(&pmsg->msg.setupGame.sCoopMode);
  779. }
  780. } SetupGame;
  781. // server tells clients to start game
  782. typedef struct StartGame
  783. {
  784. enum { Size = 1 + 1 + 2 + Net::MaxRealmNameSize + 2 + 2 + 2 + 2 + 2 + sizeof(Net::SEQ) + 2 + 2};
  785. unsigned char ucType; // Message type
  786. Net::ID idServer; // Server's ID
  787. short sRealmNum; // Starting realm number or -1 to use name
  788. char acRealmFile[Net::MaxRealmNameSize]; // Name of realm file to load
  789. short sDifficulty; // Difficulty level
  790. short sRejuvenate;
  791. short sTimeLimit;
  792. short sKillLimit;
  793. short sCoopLevels; // Non-zero for cooperative levels, zero for deathmatch levels.
  794. short sCoopMode; // Non-zero for cooperative mode, zero for deathmatch mode.
  795. short sFrameTime; // Milliseconds per frame
  796. Net::SEQ seqMaxAhead; // Max ahead for input versus frame seq
  797. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  798. {
  799. pBuf->Get(&pmsg->msg.startGame.ucType);
  800. pBuf->Get(&pmsg->msg.startGame.idServer);
  801. pBuf->Get(&pmsg->msg.startGame.sRealmNum);
  802. pBuf->Get(pmsg->msg.startGame.acRealmFile, sizeof(pmsg->msg.setupGame.acRealmFile));
  803. pBuf->Get(&pmsg->msg.startGame.sDifficulty);
  804. pBuf->Get(&pmsg->msg.startGame.sRejuvenate);
  805. pBuf->Get(&pmsg->msg.startGame.sTimeLimit);
  806. pBuf->Get(&pmsg->msg.startGame.sKillLimit);
  807. pBuf->Get(&pmsg->msg.startGame.sCoopLevels);
  808. pBuf->Get(&pmsg->msg.startGame.sCoopMode);
  809. pBuf->Get(&pmsg->msg.startGame.sFrameTime);
  810. pBuf->Get(&pmsg->msg.startGame.seqMaxAhead);
  811. }
  812. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  813. {
  814. pBuf->Put(&pmsg->msg.startGame.ucType);
  815. pBuf->Put(&pmsg->msg.startGame.idServer);
  816. pBuf->Put(&pmsg->msg.startGame.sRealmNum);
  817. pBuf->Put(pmsg->msg.startGame.acRealmFile, sizeof(pmsg->msg.setupGame.acRealmFile));
  818. pBuf->Put(&pmsg->msg.startGame.sDifficulty);
  819. pBuf->Put(&pmsg->msg.startGame.sRejuvenate);
  820. pBuf->Put(&pmsg->msg.startGame.sTimeLimit);
  821. pBuf->Put(&pmsg->msg.startGame.sKillLimit);
  822. pBuf->Put(&pmsg->msg.startGame.sCoopLevels);
  823. pBuf->Put(&pmsg->msg.startGame.sCoopMode);
  824. pBuf->Put(&pmsg->msg.startGame.sFrameTime);
  825. pBuf->Put(&pmsg->msg.startGame.seqMaxAhead);
  826. }
  827. } StartGame;
  828. // server tells clients to abort game
  829. typedef struct AbortGame
  830. {
  831. enum { Size = 1 + 1 };
  832. unsigned char ucType; // Message type
  833. unsigned char ucReason; // Reason for abort
  834. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  835. {
  836. pBuf->Get(&pmsg->msg.abortGame.ucType);
  837. pBuf->Get(&pmsg->msg.abortGame.ucReason);
  838. }
  839. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  840. {
  841. pBuf->Put(&pmsg->msg.abortGame.ucType);
  842. pBuf->Put(&pmsg->msg.abortGame.ucReason);
  843. }
  844. } AbortGame;
  845. // client tells server the loaded realm is ready
  846. typedef struct ReadyRealm
  847. {
  848. enum { Size = 1 };
  849. unsigned char ucType; // Message type
  850. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  851. {
  852. pBuf->Get(&pmsg->msg.readyRealm.ucType);
  853. }
  854. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  855. {
  856. pBuf->Put(&pmsg->msg.readyRealm.ucType);
  857. }
  858. } ReadyRealm;
  859. // client tells server the load failed
  860. typedef struct BadRealm
  861. {
  862. enum { Size = 1 };
  863. unsigned char ucType; // Message type
  864. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  865. {
  866. pBuf->Get(&pmsg->msg.badRealm.ucType);
  867. }
  868. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  869. {
  870. pBuf->Put(&pmsg->msg.badRealm.ucType);
  871. }
  872. } BadRealm;
  873. // server tells clients to start realm
  874. typedef struct StartRealm
  875. {
  876. enum { Size = 1 };
  877. unsigned char ucType; // Message type
  878. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  879. {
  880. pBuf->Get(&pmsg->msg.startRealm.ucType);
  881. }
  882. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  883. {
  884. pBuf->Put(&pmsg->msg.startRealm.ucType);
  885. }
  886. } StartRealm;
  887. // server tells clients to start realm
  888. typedef struct HaltRealm
  889. {
  890. enum { Size = 1 + sizeof(Net::SEQ) };
  891. unsigned char ucType; // Message type
  892. Net::SEQ seqHalt; // Which seq to halt on
  893. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  894. {
  895. pBuf->Get(&pmsg->msg.haltRealm.ucType);
  896. pBuf->Get(&pmsg->msg.haltRealm.seqHalt);
  897. }
  898. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  899. {
  900. pBuf->Put(&pmsg->msg.haltRealm.ucType);
  901. pBuf->Put(&pmsg->msg.haltRealm.seqHalt);
  902. }
  903. } HaltRealm;
  904. // server tells clients to go to next realm when the specified frame seq is reached
  905. typedef struct NextRealm
  906. {
  907. enum { Size = 1 + sizeof(Net::SEQ) };
  908. unsigned char ucType; // Message type
  909. Net::SEQ seqHalt; // Which seq to halt on
  910. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  911. {
  912. pBuf->Get(&pmsg->msg.nextRealm.ucType);
  913. pBuf->Get(&pmsg->msg.haltRealm.seqHalt);
  914. }
  915. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  916. {
  917. pBuf->Put(&pmsg->msg.nextRealm.ucType);
  918. pBuf->Put(&pmsg->msg.haltRealm.seqHalt);
  919. }
  920. } NextRealm;
  921. // server tells clients to procceed, which is used when clients are at a dialog
  922. // or something like that which requires a centralized "user input"
  923. typedef struct ProgressRealm
  924. {
  925. enum { Size = 1 + 2 + 2 };
  926. unsigned char ucType; // Message type
  927. short sNumReady; // Number of players that are ready
  928. short sNumNotReady; // Number of players that are NOT ready
  929. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  930. {
  931. pBuf->Get(&pmsg->msg.progressRealm.ucType);
  932. pBuf->Get(&pmsg->msg.progressRealm.sNumReady);
  933. pBuf->Get(&pmsg->msg.progressRealm.sNumNotReady);
  934. }
  935. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  936. {
  937. pBuf->Put(&pmsg->msg.progressRealm.ucType);
  938. pBuf->Put(&pmsg->msg.progressRealm.sNumReady);
  939. pBuf->Put(&pmsg->msg.progressRealm.sNumNotReady);
  940. }
  941. } ProgressRealm;
  942. // server tells clients to procceed, which is used when clients are at a dialog
  943. // or something like that which requires a centralized "user input"
  944. typedef struct Proceed
  945. {
  946. enum { Size = 1 };
  947. unsigned char ucType; // Message type
  948. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  949. {
  950. pBuf->Get(&pmsg->msg.proceed.ucType);
  951. }
  952. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  953. {
  954. pBuf->Put(&pmsg->msg.proceed.ucType);
  955. }
  956. } Proceed;
  957. // ping (back and forth between client and server)
  958. typedef struct Ping
  959. {
  960. enum { Size = 1 + 4 + 4 };
  961. unsigned char ucType; // Message type
  962. long lTimeStamp; // Timestap for this ping
  963. long lLatestPingResult; // Latest ping result (round trip time)
  964. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  965. {
  966. pBuf->Get(&pmsg->msg.ping.ucType);
  967. pBuf->Get(&pmsg->msg.ping.lTimeStamp);
  968. pBuf->Get(&pmsg->msg.ping.lLatestPingResult);
  969. }
  970. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  971. {
  972. pBuf->Put(&pmsg->msg.ping.ucType);
  973. pBuf->Put(&pmsg->msg.ping.lTimeStamp);
  974. pBuf->Put(&pmsg->msg.ping.lLatestPingResult);
  975. }
  976. } Ping;
  977. // debug message used to compare random number sequences
  978. typedef struct Rand
  979. {
  980. enum { Size = 1 + 4 + 4 };
  981. unsigned char ucType; // Message type
  982. long lFrame; // Current frame
  983. long lRand; // Current rand()
  984. static void Read(NetMsg* pmsg, CBufQ* pBuf)
  985. {
  986. pBuf->Get(&pmsg->msg.rand.ucType);
  987. pBuf->Get(&pmsg->msg.rand.lFrame);
  988. pBuf->Get(&pmsg->msg.rand.lRand);
  989. }
  990. static void Write(NetMsg* pmsg, CBufQ* pBuf)
  991. {
  992. pBuf->Put(&pmsg->msg.rand.ucType);
  993. pBuf->Put(&pmsg->msg.rand.lFrame);
  994. pBuf->Put(&pmsg->msg.rand.lRand);
  995. }
  996. } Rand;
  997. //------------------------------------------------------------------------------
  998. // The actual data of the NetMsg is a union of all the different message types,
  999. // each with (mostly) different members, plus some additional data tacked on
  1000. // the end WHICH IS NOT ACTUALLY TRANSMITTED AS PART OF THE MESSAGES!
  1001. //------------------------------------------------------------------------------
  1002. public:
  1003. union
  1004. {
  1005. Nothing nothing;
  1006. Stat stat;
  1007. Err err;
  1008. Login login;
  1009. LoginAccept loginAccept;
  1010. LoginDeny loginDeny;
  1011. Logout logout;
  1012. JoinReq joinReq;
  1013. JoinAccept joinAccept;
  1014. JoinDeny joinDeny;
  1015. Joined joined;
  1016. ChangeReq changeReq;
  1017. Changed changed;
  1018. DropReq dropReq;
  1019. Dropped dropped;
  1020. DropAck dropAck;
  1021. InputReq inputReq;
  1022. InputData inputData;
  1023. InputMark inputMark;
  1024. ChatReq chatReq;
  1025. Chat chat;
  1026. SetupGame setupGame;
  1027. StartGame startGame;
  1028. AbortGame abortGame;
  1029. ReadyRealm readyRealm;
  1030. BadRealm badRealm;
  1031. StartRealm startRealm;
  1032. HaltRealm haltRealm;
  1033. NextRealm nextRealm;
  1034. ProgressRealm progressRealm;
  1035. Proceed proceed;
  1036. Ping ping;
  1037. Rand rand;
  1038. } msg;
  1039. // This is not sent as part of the message. It is filled in at the receiving
  1040. // end to indicate the sender of the message. Note that this is NOT handled
  1041. // by either NetMsg or CNetMsgr, which don't know who sent what.
  1042. unsigned char ucSenderID;
  1043. protected:
  1044. // This is not sent as part of the message. It is used by variable-length
  1045. // messages that require a separate memory block for their data. Note that
  1046. // the size refers to the size of this data, not the whole msg.
  1047. U8* m_pVarData;
  1048. long m_lVarSize;
  1049. //------------------------------------------------------------------------------
  1050. // Functions
  1051. //------------------------------------------------------------------------------
  1052. public:
  1053. NetMsg()
  1054. {
  1055. m_pVarData = 0;
  1056. m_lVarSize = 0;
  1057. msg.nothing.ucType = NOTHING;
  1058. }
  1059. ~NetMsg()
  1060. {
  1061. FreeVar(this);
  1062. }
  1063. void Reset(void)
  1064. {
  1065. FreeVar(this);
  1066. msg.nothing.ucType = NOTHING;
  1067. }
  1068. U8* AllocVar(
  1069. long lSize)
  1070. {
  1071. FreeVar();
  1072. m_lVarSize = lSize;
  1073. m_pVarData = new U8[lSize];
  1074. return m_pVarData;
  1075. }
  1076. void FreeVar(void)
  1077. {
  1078. delete []m_pVarData;
  1079. m_pVarData = 0;
  1080. m_lVarSize = 0;
  1081. }
  1082. static U8* AllocVar(
  1083. NetMsg* pmsg,
  1084. long lSize)
  1085. {
  1086. return pmsg->AllocVar(lSize);
  1087. }
  1088. static void FreeVar(
  1089. NetMsg* pmsg)
  1090. {
  1091. pmsg->FreeVar();
  1092. }
  1093. const NetMsg& operator=(const NetMsg& rhs)
  1094. {
  1095. // Copy message
  1096. msg = rhs.msg;
  1097. // Copy sender ID
  1098. ucSenderID = rhs.ucSenderID;
  1099. // Copy var memory, if any
  1100. FreeVar(this);
  1101. if (rhs.m_pVarData)
  1102. {
  1103. AllocVar(this, rhs.m_lVarSize);
  1104. memcpy(m_pVarData, rhs.m_pVarData, rhs.m_lVarSize);
  1105. }
  1106. return *this;
  1107. }
  1108. };
  1109. ////////////////////////////////////////////////////////////////////////////////
  1110. //
  1111. // CNetMsgr impliments a message-based protocol over a "reliable" network
  1112. // socket connection.
  1113. //
  1114. ////////////////////////////////////////////////////////////////////////////////
  1115. class CNetMsgr
  1116. {
  1117. //------------------------------------------------------------------------------
  1118. // Types, enums, etc.
  1119. //------------------------------------------------------------------------------
  1120. public:
  1121. // Miscellaneous values
  1122. typedef enum
  1123. {
  1124. MagicNum = 0x5655595a, // Magic number
  1125. MacVersionBit = 0x1000, // Bit that indicates a Mac platform.
  1126. MinVersionNum = 0x0003, // Minimum version number we can support
  1127. CurVersionNum = 0x0003 // Current version number
  1128. };
  1129. // States
  1130. typedef enum
  1131. {
  1132. Disconnected, // Becomes "Connecting" when Connect() is called
  1133. Connecting, // Becomes "Disconnected" if connection attempt fails
  1134. // Becomes "Connected" if connection attempt succeeds
  1135. Connected, // Becomes "Disconnected" if immediate disconnect is performed
  1136. // Becomes "Disconnecting" if clean disconnect is performed
  1137. Disconnecting // Becomes "Disconnected" when disconnection process finishes
  1138. } State;
  1139. // Info about a message
  1140. typedef void (*FUNC_READ)(NetMsg* pmsg, CBufQ* pBuf); // Pointer to read function
  1141. typedef void (*FUNC_WRITE)(NetMsg* pmsg, CBufQ* pBuf); // Pointer to write function
  1142. typedef struct
  1143. {
  1144. UCHAR ucType; // Type (used only for debug ASSERTs)
  1145. size_t size; // Size of data to read/write (bytes)
  1146. FUNC_READ funcRead; // Pointer to read function
  1147. FUNC_WRITE funcWrite; // Pointer to write function
  1148. } InfoMsg;
  1149. //------------------------------------------------------------------------------
  1150. // Variables
  1151. //------------------------------------------------------------------------------
  1152. protected:
  1153. State m_state; // Current state
  1154. RSocket m_socket; // Socket
  1155. RSocket::Address m_address; // Address we're connected to
  1156. CBufQ m_bufIn; // Input buffer
  1157. CBufQ m_bufOut; // Output buffer
  1158. long m_lMsgRecvTime; // When most-recent message was recieved
  1159. long m_lMsgSentTime; // When most-recent message was sent
  1160. NetMsg::Error m_error; // Error value.
  1161. // Made public by JMB for TAPI access
  1162. public:
  1163. static InfoMsg ms_aInfoMsg[NetMsg::NumMessages]; // Information about the message structs
  1164. //------------------------------------------------------------------------------
  1165. // Functions
  1166. //------------------------------------------------------------------------------
  1167. public:
  1168. ////////////////////////////////////////////////////////////////////////////////
  1169. // Constructor
  1170. ////////////////////////////////////////////////////////////////////////////////
  1171. CNetMsgr()
  1172. {
  1173. Reset();
  1174. }
  1175. ////////////////////////////////////////////////////////////////////////////////
  1176. // Destructor
  1177. ////////////////////////////////////////////////////////////////////////////////
  1178. ~CNetMsgr()
  1179. {
  1180. Reset();
  1181. }
  1182. ////////////////////////////////////////////////////////////////////////////////
  1183. // Destructor
  1184. ////////////////////////////////////////////////////////////////////////////////
  1185. void Reset(
  1186. bool bCleanly = false)
  1187. {
  1188. m_state = Disconnected;
  1189. if (bCleanly)
  1190. m_socket.Close(false); // This tries to close the socket cleanly
  1191. else
  1192. m_socket.Reset(); // This does a more forcefull closing of the socket
  1193. m_address.Reset();
  1194. m_bufIn.Reset();
  1195. m_bufOut.Reset();
  1196. m_lMsgRecvTime = 0;
  1197. m_lMsgSentTime = 0;
  1198. m_error = NetMsg::NoError;
  1199. }
  1200. ////////////////////////////////////////////////////////////////////////////////
  1201. // Connect
  1202. //
  1203. // Connecting is an ASYNCHRONOUS operation. Call this function once to start
  1204. // the connection process, and thereafter call Update() to give it time to
  1205. // process, and call State() to determine the current state.
  1206. //
  1207. // If this function returns failure, it means the attempt to connect failed,
  1208. // and m_state will be "Disconnected".
  1209. //
  1210. // If this function succeeds, it means an attempt to connect has been initiated
  1211. // and m_state will be "Connecting". The state can be polled to determine the
  1212. // outcome of the connection attempt. If the state becomes "Connected", it
  1213. // means the connection completed. If it becomes "Disconnected", it means the
  1214. // connection failed.
  1215. ////////////////////////////////////////////////////////////////////////////////
  1216. short Connect( // Returns 0 if successfull, non-zero otherwise
  1217. const RSocket::Address* paddress, // In: Address being connected to
  1218. RSocket::BLOCK_CALLBACK callback) // In: Socket callback
  1219. {
  1220. ASSERT(m_state == Disconnected);
  1221. short sResult = 0;
  1222. // Reset to make sure we're starting with a clean slate
  1223. Reset();
  1224. // Save the address
  1225. m_address = *paddress;
  1226. // Open socket in non-blocking mode
  1227. sResult = m_socket.Open(
  1228. 0,
  1229. RSocket::typStream,
  1230. RSocket::optDontWaitOnClose | RSocket::optDontCoalesce | RSocket::optDontBlock,
  1231. callback);
  1232. if (sResult == 0)
  1233. {
  1234. m_state = Connecting;
  1235. }
  1236. else
  1237. {
  1238. TRACE("CNetMsgr::Connect(): Couldn't open socket!\n");
  1239. }
  1240. return sResult;
  1241. }
  1242. ////////////////////////////////////////////////////////////////////////////////
  1243. // Accept connection
  1244. ////////////////////////////////////////////////////////////////////////////////
  1245. short Accept(
  1246. const RSocket* psocketListen, // In: Listen socket to accept connection on
  1247. RSocket::BLOCK_CALLBACK callback) // In: Socket callback
  1248. {
  1249. ASSERT(m_state == Disconnected);
  1250. short sResult = psocketListen->Accept(&m_socket, &m_address);
  1251. if (sResult == 0)
  1252. {
  1253. m_socket.SetCallback(callback);
  1254. m_state = Connected;
  1255. m_lMsgRecvTime = rspGetMilliseconds();
  1256. m_lMsgSentTime = rspGetMilliseconds();
  1257. }
  1258. return sResult;
  1259. }
  1260. ////////////////////////////////////////////////////////////////////////////////
  1261. // Disconnect cleanly or immediately.
  1262. //
  1263. // Cleanly means the actual disconnect will occur only after all existing
  1264. // messages have been sent or a send error occurs, whichever comes first.
  1265. // This may be the case right now, or it may take a while. All future
  1266. // SendMsg() calls are ignored once this function is called.
  1267. //
  1268. // NOTE: Update() must be called periodically!!!
  1269. ////////////////////////////////////////////////////////////////////////////////
  1270. void Disconnect(
  1271. bool bCleanly = false) // In: true means cleanly, false means immediately
  1272. {
  1273. // Disconnect is special case -- don't TRACE/ASSERT if not connected
  1274. if (m_state != Disconnected)
  1275. {
  1276. // If doing immediate disconnect or if there's nothing more to send, then
  1277. // disconnect right now. Otherwise, just change the state.
  1278. if (!bCleanly || !IsMoreToSend())
  1279. {
  1280. // Disconnect by doing a "nice" reset. This ensures that everything
  1281. // is properly reset.
  1282. Reset(true);
  1283. }
  1284. else
  1285. m_state = Disconnecting;
  1286. }
  1287. }
  1288. ////////////////////////////////////////////////////////////////////////////////
  1289. // Update (must be called regularly)
  1290. ////////////////////////////////////////////////////////////////////////////////
  1291. void Update(void);
  1292. ////////////////////////////////////////////////////////////////////////////////
  1293. // Get message
  1294. ////////////////////////////////////////////////////////////////////////////////
  1295. bool GetMsg(
  1296. NetMsg* pmsg); // Out: Message is returned here
  1297. ////////////////////////////////////////////////////////////////////////////////
  1298. // Send message
  1299. ////////////////////////////////////////////////////////////////////////////////
  1300. void SendMsg(
  1301. NetMsg* pmsg, // In: Message to send
  1302. bool bSendNow = true); // In: Whether to send now or wait until Update()
  1303. ////////////////////////////////////////////////////////////////////////////////
  1304. // Determine if there is more data waiting to be sent. If there is data to
  1305. // to be sent AND there is a send error, then that data can't be sent, so we
  1306. // return false to indicate "no more data".
  1307. ////////////////////////////////////////////////////////////////////////////////
  1308. bool IsMoreToSend() // Returns true if more to send, false otherwise
  1309. {
  1310. return !m_bufOut.IsEmpty() && !IsSendError(m_error);
  1311. }
  1312. ////////////////////////////////////////////////////////////////////////////////
  1313. // Determine the current state
  1314. ////////////////////////////////////////////////////////////////////////////////
  1315. State GetState() // Returns current state
  1316. {
  1317. return m_state;
  1318. }
  1319. ////////////////////////////////////////////////////////////////////////////////
  1320. // Get address we're connected to (address is valid only if we are connected!)
  1321. ////////////////////////////////////////////////////////////////////////////////
  1322. const RSocket::Address& GetAddress(void)
  1323. {
  1324. return m_address;
  1325. }
  1326. ////////////////////////////////////////////////////////////////////////////////
  1327. // Determine the time of the most-recently received or sent message
  1328. ////////////////////////////////////////////////////////////////////////////////
  1329. long GetMostRecentMsgReceiveTime(void)
  1330. {
  1331. return m_lMsgRecvTime;
  1332. }
  1333. long GetMostRecentMsgSentTime(void)
  1334. {
  1335. return m_lMsgSentTime;
  1336. }
  1337. private:
  1338. ////////////////////////////////////////////////////////////////////////////////
  1339. // Receive data (copy data from socket into input buffer)
  1340. ////////////////////////////////////////////////////////////////////////////////
  1341. void ReceiveData(void);
  1342. ////////////////////////////////////////////////////////////////////////////////
  1343. // Send data (copy data output buffer to socket)
  1344. ////////////////////////////////////////////////////////////////////////////////
  1345. void SendData(void);
  1346. ////////////////////////////////////////////////////////////////////////////////
  1347. // Determine if this is a send-related error
  1348. ////////////////////////////////////////////////////////////////////////////////
  1349. bool IsSendError(NetMsg::Error error)
  1350. {
  1351. bool bResult = false;
  1352. switch (error)
  1353. {
  1354. case NetMsg::OutQFullError:
  1355. case NetMsg::SendError:
  1356. case NetMsg::InQReadError:
  1357. case NetMsg::OutQWriteError:
  1358. bResult = true;
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. return bResult;
  1364. }
  1365. };
  1366. #endif //NETMSGR_H
  1367. ////////////////////////////////////////////////////////////////////////////////
  1368. // EOF
  1369. ////////////////////////////////////////////////////////////////////////////////