1 // message.hxx -- Multiplayer Client/Server message base class
\r
3 // Written by John Barrett, started November 2003.
\r
5 // Copyright (C) 2003 John R. Barrett - jbarrett@accesshosting.com
\r
7 // This program is free software; you can redistribute it and/or
\r
8 // modify it under the terms of the GNU General Public License as
\r
9 // published by the Free Software Foundation; either version 2 of the
\r
10 // License, or (at your option) any later version.
\r
12 // This program is distributed in the hope that it will be useful, but
\r
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
15 // General Public License for more details.
\r
17 // You should have received a copy of the GNU General Public License
\r
18 // along with this program; if not, write to the Free Software
\r
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
23 #include "message.hxx"
\r
25 FGMPSInstanceFuncMap FGMPSMessage::funcmap;
\r
27 FGMPSMessage::FGMPSMessage()
\r
30 elements[0].type = fgmps_null;
\r
31 elements[0].data = NULL;
\r
35 string FGMPSMessage::encodemsg()
\r
37 FGMPSMsgElementEntry* ptr = getelements();
\r
41 buf.put(fgmps_som16, true);
\r
44 buf.put(fgmps_som8, true);
\r
47 while (ptr->type != fgmps_null) {
\r
48 buf.put(ptr->type, true);
\r
49 //printf ("adding %02x\n", ptr->type);
\r
50 switch (ptr->type) {
\r
53 buf.write1(ptr->data);
\r
57 buf.write2(ptr->data);
\r
61 buf.write4(ptr->data);
\r
64 buf.writef(*(float*)ptr->data);
\r
67 buf.writed(*(double*)ptr->data);
\r
70 buf.writes(*(string*)ptr->data);
\r
76 buf.put(fgmps_eom, true);
\r
81 FGMPSMessage* FGMPSMessage::decodemsg(string msg)
\r
83 FGMPSMessageBuf buf;
\r
86 FGMPSInstanceFuncMap::iterator fmitr;
\r
91 if (ch != fgmps_som8 && ch != fgmps_som16) {
\r
92 throw FGMPSDataException("Invalid start of message");
\r
94 if (ch == fgmps_som8) {
\r
98 mid = *(unsigned int *)buf.read2();
\r
101 fmitr = funcmap.find(mid);
\r
102 if (fmitr == funcmap.end()) {
\r
103 throw FGMPSDataException("MessageID has no registered Message Class");
\r
105 FGMPSMessage* msgclass = (fmitr->second)();
\r
106 FGMPSMsgElementEntry* elements = msgclass->getelements();
\r
107 while ((ch = buf.get()) != fgmps_eom) {
\r
108 //printf("dump: ");
\r
109 //for (int i=-1; i<16; i++) printf("%02x ", buf.peek(i));
\r
112 if (ch != elements->type) {
\r
114 throw FGMPSDataException("Decode: Message Structure Error");
\r
119 memcpy(elements->data, buf.read1(), 1);
\r
123 memcpy(elements->data, buf.read2(), 2);
\r
127 memcpy(elements->data, buf.read4(), 4);
\r
130 *(float*)elements->data = buf.readf();
\r
133 *(double*)elements->data = buf.readd();
\r
137 *(string*)elements->data = buf.reads(ch);
\r
141 throw FGMPSDataException("Decode: Unknown data type");
\r