-// message.hxx -- Multiplayer Client/Server message base class\r
-//\r
-// Written by John Barrett, started November 2003.\r
-//\r
-// Copyright (C) 2003 John R. Barrett - jbarrett@accesshosting.com\r
-//\r
-// This program is free software; you can redistribute it and/or\r
-// modify it under the terms of the GNU General Public License as\r
-// published by the Free Software Foundation; either version 2 of the\r
-// License, or (at your option) any later version.\r
-//\r
-// This program is distributed in the hope that it will be useful, but\r
-// WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
-// General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU General Public License\r
-// along with this program; if not, write to the Free Software\r
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
-//\r
-\r
-\r
-#include "message.hxx"\r
-\r
-FGMPSInstanceFuncMap FGMPSMessage::funcmap;\r
-\r
-FGMPSMessage::FGMPSMessage() \r
-{\r
- msgid = 0x0000;\r
- elements[0].type = fgmps_null;\r
- elements[0].data = NULL; \r
- \r
-}\r
-\r
-string FGMPSMessage::encodemsg()\r
-{\r
- FGMPSMsgElementEntry* ptr = getelements();\r
-\r
- buf.clr();\r
- if (msgid > 255) {\r
- buf.put(fgmps_som16, true);\r
- buf.write2(&msgid);\r
- } else {\r
- buf.put(fgmps_som8, true);\r
- buf.write1(&msgid);\r
- }\r
- while (ptr->type != fgmps_null) {\r
- buf.put(ptr->type, true);\r
- //printf ("adding %02x\n", ptr->type);\r
- switch (ptr->type) {\r
- case fgmps_uchar:\r
- case fgmps_char:\r
- buf.write1(ptr->data);\r
- break;\r
- case fgmps_uint:\r
- case fgmps_int:\r
- buf.write2(ptr->data);\r
- break;\r
- case fgmps_ulong:\r
- case fgmps_long:\r
- buf.write4(ptr->data);\r
- break;\r
- case fgmps_float:\r
- buf.writef(*(float*)ptr->data);\r
- break;\r
- case fgmps_double:\r
- buf.writed(*(double*)ptr->data);\r
- break;\r
- case fgmps_string:\r
- buf.writes(*(string*)ptr->data);\r
- break;\r
- }\r
- ptr++;\r
- }\r
-\r
- buf.put(fgmps_eom, true);\r
-\r
- return buf.str();\r
-}\r
-\r
-FGMPSMessage* FGMPSMessage::decodemsg(string msg)\r
-{\r
- FGMPSMessageBuf buf;\r
- unsigned char ch;\r
- unsigned int mid;\r
- FGMPSInstanceFuncMap::iterator fmitr;\r
-\r
- buf.set(msg);\r
- buf.ofs(0);\r
- ch = buf.get(true);\r
- if (ch != fgmps_som8 && ch != fgmps_som16) {\r
- throw FGMPSDataException("Invalid start of message");\r
- }\r
- if (ch == fgmps_som8) {\r
- ch = buf.get();\r
- mid = ch;\r
- } else {\r
- mid = *(unsigned int *)buf.read2();\r
- }\r
- \r
- fmitr = funcmap.find(mid);\r
- if (fmitr == funcmap.end()) {\r
- throw FGMPSDataException("MessageID has no registered Message Class");\r
- }\r
- FGMPSMessage* msgclass = (fmitr->second)();\r
- FGMPSMsgElementEntry* elements = msgclass->getelements();\r
- while ((ch = buf.get()) != fgmps_eom) {\r
- //printf("dump: ");\r
- //for (int i=-1; i<16; i++) printf("%02x ", buf.peek(i));\r
- //printf("\n");\r
-\r
- if (ch != elements->type) {\r
- delete msgclass;\r
- throw FGMPSDataException("Decode: Message Structure Error");\r
- }\r
- switch (ch) {\r
- case fgmps_uchar:\r
- case fgmps_char:\r
- memcpy(elements->data, buf.read1(), 1);\r
- break;\r
- case fgmps_uint:\r
- case fgmps_int:\r
- memcpy(elements->data, buf.read2(), 2);\r
- break;\r
- case fgmps_ulong:\r
- case fgmps_long:\r
- memcpy(elements->data, buf.read4(), 4);\r
- break;\r
- case fgmps_float:\r
- *(float*)elements->data = buf.readf();\r
- break;\r
- case fgmps_double:\r
- *(double*)elements->data = buf.readd();\r
- break;\r
- case fgmps_string:\r
- ch = buf.get();\r
- *(string*)elements->data = buf.reads(ch);\r
- break;\r
- default:\r
- delete msgclass;\r
- throw FGMPSDataException("Decode: Unknown data type");\r
- break;\r
- }\r
- elements++;\r
- }\r
- return msgclass;\r
-}\r
-\r
-\r
+// message.hxx -- Multiplayer Client/Server message base class
+//
+// Written by John Barrett, started November 2003.
+//
+// Copyright (C) 2003 John R. Barrett - jbarrett@accesshosting.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+
+#include "message.hxx"
+
+FGMPSInstanceFuncMap FGMPSMessage::funcmap;
+
+FGMPSMessage::FGMPSMessage()
+{
+ msgid = 0x0000;
+ elements[0].type = fgmps_null;
+ elements[0].data = NULL;
+
+}
+
+string FGMPSMessage::encodemsg()
+{
+ FGMPSMsgElementEntry* ptr = getelements();
+
+ buf.clr();
+ if (msgid > 255) {
+ buf.put(fgmps_som16, true);
+ buf.write2(&msgid);
+ } else {
+ buf.put(fgmps_som8, true);
+ buf.write1(&msgid);
+ }
+ while (ptr->type != fgmps_null) {
+ buf.put(ptr->type, true);
+ //printf ("adding %02x\n", ptr->type);
+ switch (ptr->type) {
+ case fgmps_uchar:
+ case fgmps_char:
+ buf.write1(ptr->data);
+ break;
+ case fgmps_uint:
+ case fgmps_int:
+ buf.write2(ptr->data);
+ break;
+ case fgmps_ulong:
+ case fgmps_long:
+ buf.write4(ptr->data);
+ break;
+ case fgmps_float:
+ buf.writef(*(float*)ptr->data);
+ break;
+ case fgmps_double:
+ buf.writed(*(double*)ptr->data);
+ break;
+ case fgmps_string:
+ buf.writes(*(string*)ptr->data);
+ break;
+ }
+ ptr++;
+ }
+
+ buf.put(fgmps_eom, true);
+
+ return buf.str();
+}
+
+FGMPSMessage* FGMPSMessage::decodemsg(string msg)
+{
+ FGMPSMessageBuf buf;
+ unsigned char ch;
+ unsigned int mid;
+ FGMPSInstanceFuncMap::iterator fmitr;
+
+ buf.set(msg);
+ buf.ofs(0);
+ ch = buf.get(true);
+ if (ch != fgmps_som8 && ch != fgmps_som16) {
+ throw FGMPSDataException("Invalid start of message");
+ }
+ if (ch == fgmps_som8) {
+ ch = buf.get();
+ mid = ch;
+ } else {
+ mid = *(unsigned int *)buf.read2();
+ }
+
+ fmitr = funcmap.find(mid);
+ if (fmitr == funcmap.end()) {
+ throw FGMPSDataException("MessageID has no registered Message Class");
+ }
+ FGMPSMessage* msgclass = (fmitr->second)();
+ FGMPSMsgElementEntry* elements = msgclass->getelements();
+ while ((ch = buf.get()) != fgmps_eom) {
+ //printf("dump: ");
+ //for (int i=-1; i<16; i++) printf("%02x ", buf.peek(i));
+ //printf("\n");
+
+ if (ch != elements->type) {
+ delete msgclass;
+ throw FGMPSDataException("Decode: Message Structure Error");
+ }
+ switch (ch) {
+ case fgmps_uchar:
+ case fgmps_char:
+ memcpy(elements->data, buf.read1(), 1);
+ break;
+ case fgmps_uint:
+ case fgmps_int:
+ memcpy(elements->data, buf.read2(), 2);
+ break;
+ case fgmps_ulong:
+ case fgmps_long:
+ memcpy(elements->data, buf.read4(), 4);
+ break;
+ case fgmps_float:
+ *(float*)elements->data = buf.readf();
+ break;
+ case fgmps_double:
+ *(double*)elements->data = buf.readd();
+ break;
+ case fgmps_string:
+ ch = buf.get();
+ *(string*)elements->data = buf.reads(ch);
+ break;
+ default:
+ delete msgclass;
+ throw FGMPSDataException("Decode: Unknown data type");
+ break;
+ }
+ elements++;
+ }
+ return msgclass;
+}
+
+
-// message.hxx -- Multiplayer Client/Server message base class\r
-//\r
-// Written by John Barrett, started November 2003.\r
-//\r
-// Copyright (C) 2003 John R. Barrett - jbarrett@accesshosting.com\r
-//\r
-// This program is free software; you can redistribute it and/or\r
-// modify it under the terms of the GNU General Public License as\r
-// published by the Free Software Foundation; either version 2 of the\r
-// License, or (at your option) any later version.\r
-//\r
-// This program is distributed in the hope that it will be useful, but\r
-// WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
-// General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU General Public License\r
-// along with this program; if not, write to the Free Software\r
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
-//\r
-\r
-\r
-#ifndef _FG_MPS_MESSAGE_HXX\r
-#define _FG_MPS_MESSAGE_HXX\r
-\r
-#include <simgear/compiler.h>\r
-\r
-#include STL_STRING\r
-#include <stdexcept>\r
-#include <map>\r
-\r
-SG_USING_STD(string);\r
-SG_USING_STD(invalid_argument);\r
-SG_USING_STD(map);\r
-\r
-#include "messagebuf.hxx"\r
-\r
-typedef enum \r
-{\r
- fgmps_null = 0x00,\r
- fgmps_uchar = 0xf0,\r
- fgmps_uint = 0xf1,\r
- fgmps_ulong = 0xf2,\r
- fgmps_ulonglong = 0xf3,\r
- fgmps_char = 0xf4,\r
- fgmps_int = 0xf5,\r
- fgmps_long = 0xf6,\r
- fgmps_longlong = 0xf7,\r
- fgmps_float = 0xf8,\r
- fgmps_double = 0xf9,\r
- fgmps_string = 0xfa,\r
- fgmps_reserved = 0xfb,\r
- fgmps_eom = 0xfc,\r
- fgmps_som8 = 0xfd,\r
- fgmps_som16 = 0xfe,\r
- fgmps_esc = 0xfe\r
-} FGMPSMsgElementType;\r
-\r
-typedef struct \r
-{\r
- FGMPSMsgElementType type;\r
- void * data;\r
-} FGMPSMsgElementEntry;\r
-\r
-#define FGMPSMsgElementArrayEnd {fgmps_null, 0}\r
-\r
-class FGMPSMessage;\r
-\r
-typedef FGMPSMessage* FGMPSMessagePtr;\r
-typedef FGMPSMessagePtr (*FGMPSMsgInstanceFunc)(void);\r
-\r
-typedef map<unsigned int, FGMPSMsgInstanceFunc> FGMPSInstanceFuncMap;\r
-\r
-class FGMPSMessage\r
-{\r
-private:\r
- static FGMPSInstanceFuncMap funcmap;\r
- FGMPSMsgElementEntry elements[1];\r
-protected:\r
- FGMPSMessageBuf buf;\r
- unsigned int msgid;\r
-public:\r
- static int registermsg(int msgid, FGMPSMsgInstanceFunc func)\r
- {\r
- funcmap[msgid] = func;\r
- }\r
-\r
- FGMPSMessage();\r
- ~FGMPSMessage() {}\r
-\r
- string encodemsg();\r
- static FGMPSMessage* decodemsg(string msg);\r
- unsigned int getmessageid() { return msgid; }\r
-\r
- virtual FGMPSMsgElementEntry* getelements() { return elements; }\r
-};\r
-\r
-#endif\r
+// message.hxx -- Multiplayer Client/Server message base class
+//
+// Written by John Barrett, started November 2003.
+//
+// Copyright (C) 2003 John R. Barrett - jbarrett@accesshosting.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+
+#ifndef _FG_MPS_MESSAGE_HXX
+#define _FG_MPS_MESSAGE_HXX
+
+#include <simgear/compiler.h>
+
+#include STL_STRING
+#include <stdexcept>
+#include <map>
+
+SG_USING_STD(string);
+SG_USING_STD(invalid_argument);
+SG_USING_STD(map);
+
+#include "messagebuf.hxx"
+
+typedef enum
+{
+ fgmps_null = 0x00,
+ fgmps_uchar = 0xf0,
+ fgmps_uint = 0xf1,
+ fgmps_ulong = 0xf2,
+ fgmps_ulonglong = 0xf3,
+ fgmps_char = 0xf4,
+ fgmps_int = 0xf5,
+ fgmps_long = 0xf6,
+ fgmps_longlong = 0xf7,
+ fgmps_float = 0xf8,
+ fgmps_double = 0xf9,
+ fgmps_string = 0xfa,
+ fgmps_reserved = 0xfb,
+ fgmps_eom = 0xfc,
+ fgmps_som8 = 0xfd,
+ fgmps_som16 = 0xfe,
+ fgmps_esc = 0xfe
+} FGMPSMsgElementType;
+
+typedef struct
+{
+ FGMPSMsgElementType type;
+ void * data;
+} FGMPSMsgElementEntry;
+
+#define FGMPSMsgElementArrayEnd {fgmps_null, 0}
+
+class FGMPSMessage;
+
+typedef FGMPSMessage* FGMPSMessagePtr;
+typedef FGMPSMessagePtr (*FGMPSMsgInstanceFunc)(void);
+
+typedef map<unsigned int, FGMPSMsgInstanceFunc> FGMPSInstanceFuncMap;
+
+class FGMPSMessage
+{
+private:
+ static FGMPSInstanceFuncMap funcmap;
+ FGMPSMsgElementEntry elements[1];
+protected:
+ FGMPSMessageBuf buf;
+ unsigned int msgid;
+public:
+ static int registermsg(int msgid, FGMPSMsgInstanceFunc func)
+ {
+ funcmap[msgid] = func;
+ }
+
+ FGMPSMessage();
+ ~FGMPSMessage() {}
+
+ string encodemsg();
+ static FGMPSMessage* decodemsg(string msg);
+ unsigned int getmessageid() { return msgid; }
+
+ virtual FGMPSMsgElementEntry* getelements() { return elements; }
+};
+
+#endif