]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/multiplaymgr.cxx
Merge branch 'next' of D:\Git_New\flightgear into next
[flightgear.git] / src / MultiPlayer / multiplaymgr.cxx
1 //////////////////////////////////////////////////////////////////////\r
2 //\r
3 // multiplaymgr.hpp\r
4 //\r
5 // Written by Duncan McCreanor, started February 2003.\r
6 // duncan.mccreanor@airservicesaustralia.com\r
7 //\r
8 // Copyright (C) 2003  Airservices Australia\r
9 // Copyright (C) 2005  Oliver Schroeder\r
10 // Copyright (C) 2006  Mathias Froehlich\r
11 //\r
12 // This program is free software; you can redistribute it and/or\r
13 // modify it under the terms of the GNU General Public License as\r
14 // published by the Free Software Foundation; either version 2 of the\r
15 // License, or (at your option) any later version.\r
16 //\r
17 // This program is distributed in the hope that it will be useful, but\r
18 // WITHOUT ANY WARRANTY; without even the implied warranty of\r
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
20 // General Public License for more details.\r
21 //\r
22 // You should have received a copy of the GNU General Public License\r
23 // along with this program; if not, write to the Free Software\r
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
25 //\r
26 // $Id$\r
27 //  \r
28 //////////////////////////////////////////////////////////////////////\r
29 \r
30 #ifdef HAVE_CONFIG_H\r
31 #include <config.h>\r
32 #endif\r
33 \r
34 #include <iostream>\r
35 #include <algorithm>\r
36 #include <cstring>\r
37 #include <osg/Math>             // isNaN\r
38 #include <plib/netSocket.h>\r
39 \r
40 #include <simgear/misc/stdint.hxx>\r
41 #include <simgear/timing/timestamp.hxx>\r
42 #include <simgear/debug/logstream.hxx>\r
43 #include <simgear/props/props.hxx>\r
44 \r
45 #include <AIModel/AIManager.hxx>\r
46 #include <Main/fg_props.hxx>\r
47 #include "multiplaymgr.hxx"\r
48 #include "mpmessages.hxx"\r
49 \r
50 using namespace std;\r
51 \r
52 #define MAX_PACKET_SIZE 1200\r
53 #define MAX_TEXT_SIZE 128\r
54 \r
55 // These constants are provided so that the ident \r
56 // command can list file versions\r
57 const char sMULTIPLAYMGR_BID[] = "$Id$";\r
58 const char sMULTIPLAYMGR_HID[] = MULTIPLAYTXMGR_HID;\r
59 \r
60 // A static map of protocol property id values to property paths,\r
61 // This should be extendable dynamically for every specific aircraft ...\r
62 // For now only that static list\r
63 const FGMultiplayMgr::IdPropertyList\r
64 FGMultiplayMgr::sIdPropertyList[] = {\r
65   {100, "surface-positions/left-aileron-pos-norm",  simgear::props::FLOAT},\r
66   {101, "surface-positions/right-aileron-pos-norm", simgear::props::FLOAT},\r
67   {102, "surface-positions/elevator-pos-norm",      simgear::props::FLOAT},\r
68   {103, "surface-positions/rudder-pos-norm",        simgear::props::FLOAT},\r
69   {104, "surface-positions/flap-pos-norm",          simgear::props::FLOAT},\r
70   {105, "surface-positions/speedbrake-pos-norm",    simgear::props::FLOAT},\r
71   {106, "gear/tailhook/position-norm",              simgear::props::FLOAT},\r
72   {107, "gear/launchbar/position-norm",             simgear::props::FLOAT},\r
73   {108, "gear/launchbar/state",                     simgear::props::STRING},\r
74   {109, "gear/launchbar/holdback-position-norm",    simgear::props::FLOAT},\r
75   {110, "canopy/position-norm",                     simgear::props::FLOAT},\r
76   {111, "surface-positions/wing-pos-norm",          simgear::props::FLOAT},\r
77   {112, "surface-positions/wing-fold-pos-norm",     simgear::props::FLOAT},\r
78 \r
79   {200, "gear/gear[0]/compression-norm",           simgear::props::FLOAT},\r
80   {201, "gear/gear[0]/position-norm",              simgear::props::FLOAT},\r
81   {210, "gear/gear[1]/compression-norm",           simgear::props::FLOAT},\r
82   {211, "gear/gear[1]/position-norm",              simgear::props::FLOAT},\r
83   {220, "gear/gear[2]/compression-norm",           simgear::props::FLOAT},\r
84   {221, "gear/gear[2]/position-norm",              simgear::props::FLOAT},\r
85   {230, "gear/gear[3]/compression-norm",           simgear::props::FLOAT},\r
86   {231, "gear/gear[3]/position-norm",              simgear::props::FLOAT},\r
87   {240, "gear/gear[4]/compression-norm",           simgear::props::FLOAT},\r
88   {241, "gear/gear[4]/position-norm",              simgear::props::FLOAT},\r
89 \r
90   {300, "engines/engine[0]/n1",  simgear::props::FLOAT},\r
91   {301, "engines/engine[0]/n2",  simgear::props::FLOAT},\r
92   {302, "engines/engine[0]/rpm", simgear::props::FLOAT},\r
93   {310, "engines/engine[1]/n1",  simgear::props::FLOAT},\r
94   {311, "engines/engine[1]/n2",  simgear::props::FLOAT},\r
95   {312, "engines/engine[1]/rpm", simgear::props::FLOAT},\r
96   {320, "engines/engine[2]/n1",  simgear::props::FLOAT},\r
97   {321, "engines/engine[2]/n2",  simgear::props::FLOAT},\r
98   {322, "engines/engine[2]/rpm", simgear::props::FLOAT},\r
99   {330, "engines/engine[3]/n1",  simgear::props::FLOAT},\r
100   {331, "engines/engine[3]/n2",  simgear::props::FLOAT},\r
101   {332, "engines/engine[3]/rpm", simgear::props::FLOAT},\r
102   {340, "engines/engine[4]/n1",  simgear::props::FLOAT},\r
103   {341, "engines/engine[4]/n2",  simgear::props::FLOAT},\r
104   {342, "engines/engine[4]/rpm", simgear::props::FLOAT},\r
105   {350, "engines/engine[5]/n1",  simgear::props::FLOAT},\r
106   {351, "engines/engine[5]/n2",  simgear::props::FLOAT},\r
107   {352, "engines/engine[5]/rpm", simgear::props::FLOAT},\r
108   {360, "engines/engine[6]/n1",  simgear::props::FLOAT},\r
109   {361, "engines/engine[6]/n2",  simgear::props::FLOAT},\r
110   {362, "engines/engine[6]/rpm", simgear::props::FLOAT},\r
111   {370, "engines/engine[7]/n1",  simgear::props::FLOAT},\r
112   {371, "engines/engine[7]/n2",  simgear::props::FLOAT},\r
113   {372, "engines/engine[7]/rpm", simgear::props::FLOAT},\r
114   {380, "engines/engine[8]/n1",  simgear::props::FLOAT},\r
115   {381, "engines/engine[8]/n2",  simgear::props::FLOAT},\r
116   {382, "engines/engine[8]/rpm", simgear::props::FLOAT},\r
117   {390, "engines/engine[9]/n1",  simgear::props::FLOAT},\r
118   {391, "engines/engine[9]/n2",  simgear::props::FLOAT},\r
119   {392, "engines/engine[9]/rpm", simgear::props::FLOAT},\r
120 \r
121   {800, "rotors/main/rpm", simgear::props::FLOAT},\r
122   {801, "rotors/tail/rpm", simgear::props::FLOAT},\r
123   {810, "rotors/main/blade[0]/position-deg",  simgear::props::FLOAT},\r
124   {811, "rotors/main/blade[1]/position-deg",  simgear::props::FLOAT},\r
125   {812, "rotors/main/blade[2]/position-deg",  simgear::props::FLOAT},\r
126   {813, "rotors/main/blade[3]/position-deg",  simgear::props::FLOAT},\r
127   {820, "rotors/main/blade[0]/flap-deg",  simgear::props::FLOAT},\r
128   {821, "rotors/main/blade[1]/flap-deg",  simgear::props::FLOAT},\r
129   {822, "rotors/main/blade[2]/flap-deg",  simgear::props::FLOAT},\r
130   {823, "rotors/main/blade[3]/flap-deg",  simgear::props::FLOAT},\r
131   {830, "rotors/tail/blade[0]/position-deg",  simgear::props::FLOAT},\r
132   {831, "rotors/tail/blade[1]/position-deg",  simgear::props::FLOAT},\r
133 \r
134   {900, "sim/hitches/aerotow/tow/length",                       simgear::props::FLOAT},\r
135   {901, "sim/hitches/aerotow/tow/elastic-constant",             simgear::props::FLOAT},\r
136   {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m",            simgear::props::FLOAT},\r
137   {903, "sim/hitches/aerotow/tow/dist",                         simgear::props::FLOAT},\r
138   {904, "sim/hitches/aerotow/tow/connected-to-property-node",   simgear::props::BOOL},\r
139   {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign",   simgear::props::STRING},\r
140   {906, "sim/hitches/aerotow/tow/brake-force",                  simgear::props::FLOAT},\r
141   {907, "sim/hitches/aerotow/tow/end-force-x",                  simgear::props::FLOAT},\r
142   {908, "sim/hitches/aerotow/tow/end-force-y",                  simgear::props::FLOAT},\r
143   {909, "sim/hitches/aerotow/tow/end-force-z",                  simgear::props::FLOAT},\r
144   {930, "sim/hitches/aerotow/is-slave",                         simgear::props::BOOL},\r
145   {931, "sim/hitches/aerotow/speed-in-tow-direction",           simgear::props::FLOAT},\r
146   {932, "sim/hitches/aerotow/open",                             simgear::props::BOOL},\r
147   {933, "sim/hitches/aerotow/local-pos-x",                      simgear::props::FLOAT},\r
148   {934, "sim/hitches/aerotow/local-pos-y",                      simgear::props::FLOAT},\r
149   {935, "sim/hitches/aerotow/local-pos-z",                      simgear::props::FLOAT},\r
150 \r
151   {1001, "controls/flight/slats",  simgear::props::FLOAT},\r
152   {1002, "controls/flight/speedbrake",  simgear::props::FLOAT},\r
153   {1003, "controls/flight/spoilers",  simgear::props::FLOAT},\r
154   {1004, "controls/gear/gear-down",  simgear::props::FLOAT},\r
155   {1005, "controls/lighting/nav-lights",  simgear::props::FLOAT},\r
156   {1006, "controls/armament/station[0]/jettison-all",  simgear::props::BOOL},\r
157 \r
158   {1100, "sim/model/variant", simgear::props::INT},\r
159   {1101, "sim/model/livery/file", simgear::props::STRING},\r
160 \r
161   {1200, "environment/wildfire/data", simgear::props::STRING},\r
162   {1201, "environment/contrail", simgear::props::INT},\r
163 \r
164   {1300, "tanker", simgear::props::INT},\r
165 \r
166   {10001, "sim/multiplay/transmission-freq-hz",  simgear::props::STRING},\r
167   {10002, "sim/multiplay/chat",  simgear::props::STRING},\r
168 \r
169   {10100, "sim/multiplay/generic/string[0]", simgear::props::STRING},\r
170   {10101, "sim/multiplay/generic/string[1]", simgear::props::STRING},\r
171   {10102, "sim/multiplay/generic/string[2]", simgear::props::STRING},\r
172   {10103, "sim/multiplay/generic/string[3]", simgear::props::STRING},\r
173   {10104, "sim/multiplay/generic/string[4]", simgear::props::STRING},\r
174   {10105, "sim/multiplay/generic/string[5]", simgear::props::STRING},\r
175   {10106, "sim/multiplay/generic/string[6]", simgear::props::STRING},\r
176   {10107, "sim/multiplay/generic/string[7]", simgear::props::STRING},\r
177   {10108, "sim/multiplay/generic/string[8]", simgear::props::STRING},\r
178   {10109, "sim/multiplay/generic/string[9]", simgear::props::STRING},\r
179   {10110, "sim/multiplay/generic/string[10]", simgear::props::STRING},\r
180   {10111, "sim/multiplay/generic/string[11]", simgear::props::STRING},\r
181   {10112, "sim/multiplay/generic/string[12]", simgear::props::STRING},\r
182   {10113, "sim/multiplay/generic/string[13]", simgear::props::STRING},\r
183   {10114, "sim/multiplay/generic/string[14]", simgear::props::STRING},\r
184   {10115, "sim/multiplay/generic/string[15]", simgear::props::STRING},\r
185   {10116, "sim/multiplay/generic/string[16]", simgear::props::STRING},\r
186   {10117, "sim/multiplay/generic/string[17]", simgear::props::STRING},\r
187   {10118, "sim/multiplay/generic/string[18]", simgear::props::STRING},\r
188   {10119, "sim/multiplay/generic/string[19]", simgear::props::STRING},\r
189 \r
190   {10200, "sim/multiplay/generic/float[0]", simgear::props::FLOAT},\r
191   {10201, "sim/multiplay/generic/float[1]", simgear::props::FLOAT},\r
192   {10202, "sim/multiplay/generic/float[2]", simgear::props::FLOAT},\r
193   {10203, "sim/multiplay/generic/float[3]", simgear::props::FLOAT},\r
194   {10204, "sim/multiplay/generic/float[4]", simgear::props::FLOAT},\r
195   {10205, "sim/multiplay/generic/float[5]", simgear::props::FLOAT},\r
196   {10206, "sim/multiplay/generic/float[6]", simgear::props::FLOAT},\r
197   {10207, "sim/multiplay/generic/float[7]", simgear::props::FLOAT},\r
198   {10208, "sim/multiplay/generic/float[8]", simgear::props::FLOAT},\r
199   {10209, "sim/multiplay/generic/float[9]", simgear::props::FLOAT},\r
200   {10210, "sim/multiplay/generic/float[10]", simgear::props::FLOAT},\r
201   {10211, "sim/multiplay/generic/float[11]", simgear::props::FLOAT},\r
202   {10212, "sim/multiplay/generic/float[12]", simgear::props::FLOAT},\r
203   {10213, "sim/multiplay/generic/float[13]", simgear::props::FLOAT},\r
204   {10214, "sim/multiplay/generic/float[14]", simgear::props::FLOAT},\r
205   {10215, "sim/multiplay/generic/float[15]", simgear::props::FLOAT},\r
206   {10216, "sim/multiplay/generic/float[16]", simgear::props::FLOAT},\r
207   {10217, "sim/multiplay/generic/float[17]", simgear::props::FLOAT},\r
208   {10218, "sim/multiplay/generic/float[18]", simgear::props::FLOAT},\r
209   {10219, "sim/multiplay/generic/float[19]", simgear::props::FLOAT},\r
210 \r
211   {10300, "sim/multiplay/generic/int[0]", simgear::props::INT},\r
212   {10301, "sim/multiplay/generic/int[1]", simgear::props::INT},\r
213   {10302, "sim/multiplay/generic/int[2]", simgear::props::INT},\r
214   {10303, "sim/multiplay/generic/int[3]", simgear::props::INT},\r
215   {10304, "sim/multiplay/generic/int[4]", simgear::props::INT},\r
216   {10305, "sim/multiplay/generic/int[5]", simgear::props::INT},\r
217   {10306, "sim/multiplay/generic/int[6]", simgear::props::INT},\r
218   {10307, "sim/multiplay/generic/int[7]", simgear::props::INT},\r
219   {10308, "sim/multiplay/generic/int[8]", simgear::props::INT},\r
220   {10309, "sim/multiplay/generic/int[9]", simgear::props::INT},\r
221   {10310, "sim/multiplay/generic/int[10]", simgear::props::INT},\r
222   {10311, "sim/multiplay/generic/int[11]", simgear::props::INT},\r
223   {10312, "sim/multiplay/generic/int[12]", simgear::props::INT},\r
224   {10313, "sim/multiplay/generic/int[13]", simgear::props::INT},\r
225   {10314, "sim/multiplay/generic/int[14]", simgear::props::INT},\r
226   {10315, "sim/multiplay/generic/int[15]", simgear::props::INT},\r
227   {10316, "sim/multiplay/generic/int[16]", simgear::props::INT},\r
228   {10317, "sim/multiplay/generic/int[17]", simgear::props::INT},\r
229   {10318, "sim/multiplay/generic/int[18]", simgear::props::INT},\r
230   {10319, "sim/multiplay/generic/int[19]", simgear::props::INT}\r
231 };\r
232 \r
233 const unsigned\r
234 FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)\r
235                                  / sizeof(FGMultiplayMgr::sIdPropertyList[0]));\r
236 \r
237 // Look up a property ID using binary search.\r
238 namespace\r
239 {\r
240   struct ComparePropertyId\r
241   {\r
242     bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,\r
243                     const FGMultiplayMgr::IdPropertyList& rhs)\r
244     {\r
245       return lhs.id < rhs.id;\r
246     }\r
247     bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,\r
248                     unsigned id)\r
249     {\r
250       return lhs.id < id;\r
251     }\r
252     bool operator()(unsigned id,\r
253                     const FGMultiplayMgr::IdPropertyList& rhs)\r
254     {\r
255       return id < rhs.id;\r
256     }\r
257   };\r
258     \r
259 }\r
260 const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)\r
261 {\r
262   std::pair<const IdPropertyList*, const IdPropertyList*> result\r
263     = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,\r
264                        ComparePropertyId());\r
265   if (result.first == result.second) {\r
266     return 0;\r
267   } else {\r
268     return result.first;\r
269   }\r
270 }\r
271 \r
272 namespace\r
273 {\r
274   bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)\r
275   {\r
276     using namespace simgear;\r
277     const xdr_data_t* xdr = data;\r
278     while (xdr < end) {\r
279       unsigned id = XDR_decode_uint32(*xdr);\r
280       const FGMultiplayMgr::IdPropertyList* plist\r
281         = FGMultiplayMgr::findProperty(id);\r
282     \r
283       if (plist) {\r
284         xdr++;\r
285         // How we decode the remainder of the property depends on the type\r
286         switch (plist->type) {\r
287         case props::INT:\r
288         case props::BOOL:\r
289         case props::LONG:\r
290           xdr++;\r
291           break;\r
292         case props::FLOAT:\r
293         case props::DOUBLE:\r
294           {\r
295             float val = XDR_decode_float(*xdr);\r
296             if (osg::isNaN(val))\r
297               return false;\r
298             xdr++;\r
299             break;\r
300           }\r
301         case props::STRING:\r
302         case props::UNSPECIFIED:\r
303           {\r
304             // String is complicated. It consists of\r
305             // The length of the string\r
306             // The string itself\r
307             // Padding to the nearest 4-bytes.\r
308             // XXX Yes, each byte is padded out to a word! Too late\r
309             // to change...\r
310             uint32_t length = XDR_decode_uint32(*xdr);\r
311             xdr++;\r
312             // Old versions truncated the string but left the length\r
313             // unadjusted.\r
314             if (length > MAX_TEXT_SIZE)\r
315               length = MAX_TEXT_SIZE;\r
316             xdr += length;\r
317             // Now handle the padding\r
318             while ((length % 4) != 0)\r
319               {\r
320                 xdr++;\r
321                 length++;\r
322                 //cout << "0";\r
323               }\r
324           }\r
325           break;\r
326         default:\r
327           // cerr << "Unknown Prop type " << id << " " << type << "\n";\r
328           xdr++;\r
329           break;\r
330         }            \r
331       }\r
332       else {\r
333         // give up; this is a malformed property list.\r
334         return false;\r
335       }\r
336     }\r
337     return true;\r
338   }\r
339 }\r
340 //////////////////////////////////////////////////////////////////////\r
341 //\r
342 //  MultiplayMgr constructor\r
343 //\r
344 //////////////////////////////////////////////////////////////////////\r
345 FGMultiplayMgr::FGMultiplayMgr() \r
346 {\r
347   mSocket        = 0;\r
348   mInitialised   = false;\r
349   mHaveServer    = false;\r
350 } // FGMultiplayMgr::FGMultiplayMgr()\r
351 //////////////////////////////////////////////////////////////////////\r
352 \r
353 //////////////////////////////////////////////////////////////////////\r
354 //\r
355 //  MultiplayMgr destructor\r
356 //\r
357 //////////////////////////////////////////////////////////////////////\r
358 FGMultiplayMgr::~FGMultiplayMgr() \r
359 {\r
360   Close();\r
361 } // FGMultiplayMgr::~FGMultiplayMgr()\r
362 //////////////////////////////////////////////////////////////////////\r
363 \r
364 //////////////////////////////////////////////////////////////////////\r
365 //\r
366 //  Initialise object\r
367 //\r
368 //////////////////////////////////////////////////////////////////////\r
369 bool\r
370 FGMultiplayMgr::init (void) \r
371 {\r
372   //////////////////////////////////////////////////\r
373   //  Initialise object if not already done\r
374   //////////////////////////////////////////////////\r
375   if (mInitialised) {\r
376     SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");\r
377     return false;\r
378   }\r
379   //////////////////////////////////////////////////\r
380   //  Set members from property values\r
381   //////////////////////////////////////////////////\r
382   short rxPort = fgGetInt("/sim/multiplay/rxport");\r
383   string rxAddress = fgGetString("/sim/multiplay/rxhost");\r
384   short txPort = fgGetInt("/sim/multiplay/txport");\r
385   string txAddress = fgGetString("/sim/multiplay/txhost");\r
386   mCallsign = fgGetString("/sim/multiplay/callsign");\r
387   if (txPort > 0 && !txAddress.empty()) {\r
388     mServer.set(txAddress.c_str(), txPort);\r
389     if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {\r
390       mHaveServer = false;\r
391       SG_LOG(SG_NETWORK, SG_DEBUG,\r
392         "FGMultiplayMgr - could not resolve '"\r
393         << txAddress << "', Multiplayermode disabled");\r
394     } else {\r
395       mHaveServer = true;\r
396     }\r
397     if (rxPort <= 0)\r
398       rxPort = txPort;\r
399   }\r
400   if (rxPort <= 0) {\r
401     SG_LOG(SG_NETWORK, SG_DEBUG,\r
402       "FGMultiplayMgr - No receiver port, Multiplayermode disabled");\r
403     return (false);\r
404   }\r
405   if (mCallsign.empty())\r
406     mCallsign = "JohnDoe"; // FIXME: use getpwuid\r
407   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);\r
408   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );\r
409   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );\r
410   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);\r
411   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);\r
412   Close(); // Should Init be called twice, close Socket first\r
413            // A memory leak was reported here by valgrind\r
414   mSocket = new netSocket();\r
415   if (!mSocket->open(false)) {\r
416     SG_LOG( SG_NETWORK, SG_DEBUG,\r
417             "FGMultiplayMgr::init - Failed to create data socket" );\r
418     return false;\r
419   }\r
420   mSocket->setBlocking(false);\r
421   if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {\r
422     perror("bind");\r
423     SG_LOG( SG_NETWORK, SG_DEBUG,\r
424             "FGMultiplayMgr::Open - Failed to bind receive socket" );\r
425     return false;\r
426   }\r
427   mInitialised = true;\r
428   return true;\r
429 } // FGMultiplayMgr::init()\r
430 //////////////////////////////////////////////////////////////////////\r
431 \r
432 //////////////////////////////////////////////////////////////////////\r
433 //\r
434 //  Closes and deletes the local player object. Closes\r
435 //  and deletes the tx socket. Resets the object state to unitialised.\r
436 //\r
437 //////////////////////////////////////////////////////////////////////\r
438 void\r
439 FGMultiplayMgr::Close (void) \r
440 {\r
441   mMultiPlayerMap.clear();\r
442 \r
443   if (mSocket) {\r
444     mSocket->close();\r
445     delete mSocket;\r
446     mSocket = 0;\r
447   }\r
448   mInitialised = false;\r
449 } // FGMultiplayMgr::Close(void)\r
450 //////////////////////////////////////////////////////////////////////\r
451 \r
452 //////////////////////////////////////////////////////////////////////\r
453 //\r
454 //  Description: Sends the position data for the local position.\r
455 //\r
456 //////////////////////////////////////////////////////////////////////\r
457 \r
458 /**\r
459  * The buffer that holds a multi-player message, suitably aligned.\r
460  */\r
461 union FGMultiplayMgr::MsgBuf\r
462 {\r
463     MsgBuf()\r
464     {\r
465         memset(&Msg, 0, sizeof(Msg));\r
466     }\r
467 \r
468     T_MsgHdr* msgHdr()\r
469     {\r
470         return reinterpret_cast<T_MsgHdr*>(Msg);\r
471     }\r
472 \r
473     const T_MsgHdr* msgHdr() const\r
474     {\r
475         return reinterpret_cast<const T_MsgHdr*>(Msg);\r
476     }\r
477 \r
478     T_PositionMsg* posMsg()\r
479     {\r
480         return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));\r
481     }\r
482 \r
483     const T_PositionMsg* posMsg() const\r
484     {\r
485         return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));\r
486     }\r
487 \r
488     xdr_data_t* properties()\r
489     {\r
490         return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)\r
491                                              + sizeof(T_PositionMsg));\r
492     }\r
493 \r
494     const xdr_data_t* properties() const\r
495     {\r
496         return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)\r
497                                                    + sizeof(T_PositionMsg));\r
498     }\r
499     /**\r
500      * The end of the properties buffer.\r
501      */\r
502     xdr_data_t* propsEnd()\r
503     {\r
504         return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);\r
505     };\r
506 \r
507     const xdr_data_t* propsEnd() const\r
508     {\r
509         return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);\r
510     };\r
511     /**\r
512      * The end of properties actually in the buffer. This assumes that\r
513      * the message header is valid.\r
514      */\r
515     xdr_data_t* propsRecvdEnd()\r
516     {\r
517         return reinterpret_cast<xdr_data_t*>(Msg + msgHdr()->MsgLen);\r
518     }\r
519 \r
520     const xdr_data_t* propsRecvdEnd() const\r
521     {\r
522         return reinterpret_cast<const xdr_data_t*>(Msg + msgHdr()->MsgLen);\r
523     }\r
524     \r
525     xdr_data2_t double_val;\r
526     char Msg[MAX_PACKET_SIZE];\r
527 };\r
528 \r
529 void\r
530 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)\r
531 {\r
532   if ((! mInitialised) || (! mHaveServer))\r
533         return;\r
534   if (! mHaveServer) {\r
535     SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");\r
536     return;\r
537   }\r
538 \r
539   MsgBuf msgBuf;\r
540   T_PositionMsg* PosMsg = msgBuf.posMsg();\r
541 \r
542   strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);\r
543   PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';\r
544   \r
545   PosMsg->time = XDR_encode_double (motionInfo.time);\r
546   PosMsg->lag = XDR_encode_double (motionInfo.lag);\r
547   for (unsigned i = 0 ; i < 3; ++i)\r
548     PosMsg->position[i] = XDR_encode_double (motionInfo.position(i));\r
549   SGVec3f angleAxis;\r
550   motionInfo.orientation.getAngleAxis(angleAxis);\r
551   for (unsigned i = 0 ; i < 3; ++i)\r
552     PosMsg->orientation[i] = XDR_encode_float (angleAxis(i));\r
553   for (unsigned i = 0 ; i < 3; ++i)\r
554     PosMsg->linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));\r
555   for (unsigned i = 0 ; i < 3; ++i)\r
556     PosMsg->angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));\r
557   for (unsigned i = 0 ; i < 3; ++i)\r
558     PosMsg->linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));\r
559   for (unsigned i = 0 ; i < 3; ++i)\r
560     PosMsg->angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));\r
561   \r
562   xdr_data_t* ptr = msgBuf.properties();\r
563   std::vector<FGPropertyData*>::const_iterator it;\r
564   it = motionInfo.properties.begin();\r
565   //cout << "OUTPUT PROPERTIES\n";\r
566   xdr_data_t* msgEnd = msgBuf.propsEnd();\r
567   while (it != motionInfo.properties.end() && ptr + 2 < msgEnd) {\r
568     \r
569     // First element is the ID. Write it out when we know we have room for\r
570     // the whole property.\r
571     xdr_data_t id =  XDR_encode_uint32((*it)->id);\r
572     // The actual data representation depends on the type\r
573     switch ((*it)->type) {\r
574       case simgear::props::INT:\r
575       case simgear::props::BOOL:\r
576       case simgear::props::LONG:\r
577         *ptr++ = id;\r
578         *ptr++ = XDR_encode_uint32((*it)->int_value);\r
579         //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";\r
580         break;\r
581       case simgear::props::FLOAT:\r
582       case simgear::props::DOUBLE:\r
583         *ptr++ = id;\r
584         *ptr++ = XDR_encode_float((*it)->float_value);\r
585         //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";\r
586         break;\r
587       case simgear::props::STRING:\r
588       case simgear::props::UNSPECIFIED:\r
589         {\r
590           // String is complicated. It consists of\r
591           // The length of the string\r
592           // The string itself\r
593           // Padding to the nearest 4-bytes.        \r
594           const char* lcharptr = (*it)->string_value;\r
595           \r
596           if (lcharptr != 0)\r
597           {\r
598             // Add the length         \r
599             ////cout << "String length: " << strlen(lcharptr) << "\n";\r
600             uint32_t len = strlen(lcharptr);\r
601             if (len > MAX_TEXT_SIZE)\r
602               len = MAX_TEXT_SIZE;\r
603             // XXX This should not be using 4 bytes per character!\r
604             // If there's not enough room for this property, drop it\r
605             // on the floor.\r
606             if (ptr + 2 + ((len + 3) & ~3) > msgEnd)\r
607                 goto escape;\r
608             //cout << "String length unint32: " << len << "\n";\r
609             *ptr++ = id;\r
610             *ptr++ = XDR_encode_uint32(len);\r
611             if (len != 0)\r
612             {\r
613               // Now the text itself\r
614               // XXX This should not be using 4 bytes per character!\r
615               int lcount = 0;\r
616               while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE)) \r
617               {\r
618                 *ptr++ = XDR_encode_int8(*lcharptr);\r
619                 lcharptr++;\r
620                 lcount++;          \r
621               }\r
622 \r
623               //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;\r
624 \r
625               // Now pad if required\r
626               while ((lcount % 4) != 0)\r
627               {\r
628                 *ptr++ = XDR_encode_int8(0);\r
629                 lcount++;          \r
630                 //cout << "0";\r
631               }\r
632               \r
633               //cout << "\n";\r
634             }\r
635           }\r
636           else\r
637           {\r
638             // Nothing to encode\r
639             *ptr++ = id;\r
640             *ptr++ = XDR_encode_uint32(0);\r
641             //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";\r
642           }\r
643         }\r
644         break;\r
645         \r
646       default:\r
647         //cout << " Unknown Type: " << (*it)->type << "\n";\r
648         *ptr++ = id;\r
649         *ptr++ = XDR_encode_float((*it)->float_value);;\r
650         //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";\r
651         break;\r
652     }\r
653         \r
654     ++it;\r
655   }\r
656 escape:\r
657   unsigned msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;\r
658   FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);\r
659   mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);\r
660   SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");\r
661 } // FGMultiplayMgr::SendMyPosition()\r
662 \r
663 //////////////////////////////////////////////////////////////////////\r
664 \r
665 //////////////////////////////////////////////////////////////////////\r
666 //\r
667 //  Name: SendTextMessage\r
668 //  Description: Sends a message to the player. The message must\r
669 //  contain a valid and correctly filled out header and optional\r
670 //  message body.\r
671 //\r
672 //////////////////////////////////////////////////////////////////////\r
673 void\r
674 FGMultiplayMgr::SendTextMessage(const string &MsgText)\r
675 {\r
676   if (!mInitialised || !mHaveServer)\r
677     return;\r
678 \r
679   T_MsgHdr MsgHdr;\r
680   FillMsgHdr(&MsgHdr, CHAT_MSG_ID);\r
681   //////////////////////////////////////////////////\r
682   // Divide the text string into blocks that fit\r
683   // in the message and send the blocks.\r
684   //////////////////////////////////////////////////\r
685   unsigned iNextBlockPosition = 0;\r
686   T_ChatMsg ChatMsg;\r
687   \r
688   char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];\r
689   while (iNextBlockPosition < MsgText.length()) {\r
690     strncpy (ChatMsg.Text, \r
691              MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),\r
692              MAX_CHAT_MSG_LEN);\r
693     ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';\r
694     memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));\r
695     memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));\r
696     mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);\r
697     iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;\r
698 \r
699   }\r
700   \r
701   \r
702 } // FGMultiplayMgr::SendTextMessage ()\r
703 //////////////////////////////////////////////////////////////////////\r
704 \r
705 //////////////////////////////////////////////////////////////////////\r
706 //\r
707 //  Name: ProcessData\r
708 //  Description: Processes data waiting at the receive socket. The\r
709 //  processing ends when there is no more data at the socket.\r
710 //  \r
711 //////////////////////////////////////////////////////////////////////\r
712 void\r
713 FGMultiplayMgr::Update(void) \r
714 {\r
715   if (!mInitialised)\r
716     return;\r
717 \r
718   /// Just for expiry\r
719   long stamp = SGTimeStamp::now().getSeconds();\r
720 \r
721   //////////////////////////////////////////////////\r
722   //  Read the receive socket and process any data\r
723   //////////////////////////////////////////////////\r
724   ssize_t bytes;\r
725   do {\r
726     MsgBuf msgBuf;\r
727     //////////////////////////////////////////////////\r
728     //  Although the recv call asks for \r
729     //  MAX_PACKET_SIZE of data, the number of bytes\r
730     //  returned will only be that of the next\r
731     //  packet waiting to be processed.\r
732     //////////////////////////////////////////////////\r
733     netAddress SenderAddress;\r
734     bytes = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,\r
735                               &SenderAddress);\r
736     //////////////////////////////////////////////////\r
737     //  no Data received\r
738     //////////////////////////////////////////////////\r
739     if (bytes <= 0) {\r
740       if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise\r
741         perror("FGMultiplayMgr::MP_ProcessData");\r
742       break;\r
743     }\r
744     if (bytes <= static_cast<ssize_t>(sizeof(T_MsgHdr))) {\r
745       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
746               << "received message with insufficient data" );\r
747       break;\r
748     }\r
749     //////////////////////////////////////////////////\r
750     //  Read header\r
751     //////////////////////////////////////////////////\r
752     T_MsgHdr* MsgHdr = msgBuf.msgHdr();\r
753     MsgHdr->Magic       = XDR_decode_uint32 (MsgHdr->Magic);\r
754     MsgHdr->Version     = XDR_decode_uint32 (MsgHdr->Version);\r
755     MsgHdr->MsgId       = XDR_decode_uint32 (MsgHdr->MsgId);\r
756     MsgHdr->MsgLen      = XDR_decode_uint32 (MsgHdr->MsgLen);\r
757     MsgHdr->ReplyPort   = XDR_decode_uint32 (MsgHdr->ReplyPort);\r
758     MsgHdr->Callsign[MAX_CALLSIGN_LEN -1] = '\0';\r
759     if (MsgHdr->Magic != MSG_MAGIC) {\r
760       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
761               << "message has invalid magic number!" );\r
762       break;\r
763     }\r
764     if (MsgHdr->Version != PROTO_VER) {\r
765       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
766               << "message has invalid protocoll number!" );\r
767       break;\r
768     }\r
769     if (MsgHdr->MsgLen != bytes) {\r
770       SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
771              << "message from " << MsgHdr->Callsign << " has invalid length!");\r
772       break;\r
773     }\r
774     //////////////////////////////////////////////////\r
775     //  Process messages\r
776     //////////////////////////////////////////////////\r
777     switch (MsgHdr->MsgId) {\r
778     case CHAT_MSG_ID:\r
779       ProcessChatMsg(msgBuf, SenderAddress);\r
780       break;\r
781     case POS_DATA_ID:\r
782       ProcessPosMsg(msgBuf, SenderAddress, stamp);\r
783       break;\r
784     case UNUSABLE_POS_DATA_ID:\r
785     case OLD_OLD_POS_DATA_ID:\r
786     case OLD_PROP_MSG_ID:\r
787     case OLD_POS_DATA_ID:\r
788       break;\r
789     default:\r
790       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
791               << "Unknown message Id received: " << MsgHdr->MsgId );\r
792       break;\r
793     }\r
794   } while (bytes > 0);\r
795 \r
796   // check for expiry\r
797   MultiPlayerMap::iterator it = mMultiPlayerMap.begin();\r
798   while (it != mMultiPlayerMap.end()) {\r
799     if (it->second->getLastTimestamp() + 10 < stamp) {\r
800       std::string name = it->first;\r
801       it->second->setDie(true);\r
802       mMultiPlayerMap.erase(it);\r
803       it = mMultiPlayerMap.upper_bound(name);\r
804     } else\r
805       ++it;\r
806   }\r
807 } // FGMultiplayMgr::ProcessData(void)\r
808 //////////////////////////////////////////////////////////////////////\r
809 \r
810 //////////////////////////////////////////////////////////////////////\r
811 //\r
812 //  handle a position message\r
813 //\r
814 //////////////////////////////////////////////////////////////////////\r
815 void\r
816 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,\r
817                               const netAddress& SenderAddress, long stamp)\r
818 {\r
819   const T_MsgHdr* MsgHdr = Msg.msgHdr();\r
820   if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {\r
821     SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
822             << "Position message received with insufficient data" );\r
823     return;\r
824   }\r
825   const T_PositionMsg* PosMsg = Msg.posMsg();\r
826   FGExternalMotionData motionInfo;\r
827   motionInfo.time = XDR_decode_double(PosMsg->time);\r
828   motionInfo.lag = XDR_decode_double(PosMsg->lag);\r
829   for (unsigned i = 0; i < 3; ++i)\r
830     motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);\r
831   SGVec3f angleAxis;\r
832   for (unsigned i = 0; i < 3; ++i)\r
833     angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);\r
834   motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);\r
835   for (unsigned i = 0; i < 3; ++i)\r
836     motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);\r
837   for (unsigned i = 0; i < 3; ++i)\r
838     motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);\r
839   for (unsigned i = 0; i < 3; ++i)\r
840     motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);\r
841   for (unsigned i = 0; i < 3; ++i)\r
842     motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);\r
843 \r
844 \r
845   //cout << "INPUT MESSAGE\n";\r
846 \r
847   // There was a bug in 1.9.0 and before: T_PositionMsg was 196 bytes\r
848   // on 32 bit architectures and 200 bytes on 64 bit, and this\r
849   // structure is put directly on the wire. By looking at the padding,\r
850   // we can sort through the mess, mostly:\r
851   // If padding is 0 (which is not a valid property type), then the\r
852   // message was produced by a new client or an old 64 bit client that\r
853   // happened to have 0 on the stack;\r
854   // Else if the property list starting with the padding word is\r
855   // well-formed, then the client is probably an old 32 bit client and\r
856   // we'll go with that;\r
857   // Else it is an old 64-bit client and properties start after the\r
858   // padding.\r
859   // There is a chance that we could be fooled by garbage in the\r
860   // padding looking like a valid property, so verifyProperties() is\r
861   // strict about the validity of the property values.\r
862   const xdr_data_t* xdr = Msg.properties();\r
863   if (PosMsg->pad != 0) {\r
864     if (verifyProperties(&PosMsg->pad, Msg.propsRecvdEnd()))\r
865       xdr = &PosMsg->pad;\r
866     else if (!verifyProperties(xdr, Msg.propsRecvdEnd()))\r
867       goto noprops;\r
868   }\r
869   while (xdr < Msg.propsRecvdEnd()) {\r
870     FGPropertyData* pData = new FGPropertyData;\r
871     // simgear::props::Type type = simgear::props::UNSPECIFIED;\r
872     \r
873     // First element is always the ID\r
874     pData->id = XDR_decode_uint32(*xdr);\r
875     //cout << pData->id << " ";\r
876     xdr++;\r
877     \r
878     // Check the ID actually exists and get the type\r
879     const IdPropertyList* plist = findProperty(pData->id);\r
880     \r
881     if (plist)\r
882     {\r
883       pData->type = plist->type;\r
884       // How we decode the remainder of the property depends on the type\r
885       switch (pData->type) {\r
886         case simgear::props::INT:\r
887         case simgear::props::BOOL:\r
888         case simgear::props::LONG:\r
889           pData->int_value = XDR_decode_uint32(*xdr);\r
890           xdr++;\r
891           //cout << pData->int_value << "\n";\r
892           break;\r
893         case simgear::props::FLOAT:\r
894         case simgear::props::DOUBLE:\r
895           pData->float_value = XDR_decode_float(*xdr);\r
896           xdr++;\r
897           //cout << pData->float_value << "\n";\r
898           break;\r
899         case simgear::props::STRING:\r
900         case simgear::props::UNSPECIFIED:\r
901           {\r
902             // String is complicated. It consists of\r
903             // The length of the string\r
904             // The string itself\r
905             // Padding to the nearest 4-bytes.    \r
906             uint32_t length = XDR_decode_uint32(*xdr);\r
907             xdr++;\r
908             //cout << length << " ";\r
909             // Old versions truncated the string but left the length unadjusted.\r
910             if (length > MAX_TEXT_SIZE)\r
911               length = MAX_TEXT_SIZE;\r
912             pData->string_value = new char[length + 1];\r
913             //cout << " String: ";\r
914             for (unsigned i = 0; i < length; i++)\r
915               {\r
916                 pData->string_value[i] = (char) XDR_decode_int8(*xdr);\r
917                 xdr++;\r
918                 //cout << pData->string_value[i];\r
919               }\r
920 \r
921             pData->string_value[length] = '\0';\r
922 \r
923             // Now handle the padding\r
924             while ((length % 4) != 0)\r
925               {\r
926                 xdr++;\r
927                 length++;\r
928                 //cout << "0";\r
929               }\r
930             //cout << "\n";\r
931           }\r
932           break;\r
933 \r
934         default:\r
935           pData->float_value = XDR_decode_float(*xdr);\r
936           SG_LOG(SG_NETWORK, SG_DEBUG, "Unknown Prop type " << pData->id << " " << pData->type);\r
937           xdr++;\r
938           break;\r
939       }            \r
940 \r
941       motionInfo.properties.push_back(pData);\r
942     }\r
943     else\r
944     {\r
945       // We failed to find the property. We'll try the next packet immediately.\r
946       SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr::ProcessPosMsg - "\r
947              "message from " << MsgHdr->Callsign << " has unknown property id "\r
948              << pData->id); \r
949     }\r
950   }\r
951  noprops:\r
952   FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);\r
953   if (!mp)\r
954     mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);\r
955   mp->addMotionInfo(motionInfo, stamp);\r
956 } // FGMultiplayMgr::ProcessPosMsg()\r
957 //////////////////////////////////////////////////////////////////////\r
958 \r
959 //////////////////////////////////////////////////////////////////////\r
960 //\r
961 //  handle a chat message\r
962 //  FIXME: display chat message withi flightgear\r
963 //\r
964 //////////////////////////////////////////////////////////////////////\r
965 void\r
966 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,\r
967                                const netAddress& SenderAddress)\r
968 {\r
969   const T_MsgHdr* MsgHdr = Msg.msgHdr();\r
970   if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {\r
971     SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "\r
972             << "Chat message received with insufficient data" );\r
973     return;\r
974   }\r
975   \r
976   char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];\r
977   const T_ChatMsg* ChatMsg\r
978       = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));\r
979   strncpy(chatStr, ChatMsg->Text,\r
980           MsgHdr->MsgLen - sizeof(T_MsgHdr));\r
981   chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';\r
982   \r
983   SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"\r
984            << " " << chatStr);\r
985 \r
986   delete [] chatStr;\r
987 } // FGMultiplayMgr::ProcessChatMsg ()\r
988 //////////////////////////////////////////////////////////////////////\r
989 \r
990 void\r
991 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)\r
992 {\r
993   uint32_t len;\r
994   switch (MsgId) {\r
995   case CHAT_MSG_ID:\r
996     len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);\r
997     break;\r
998   case POS_DATA_ID:\r
999     len = _len;\r
1000     break;\r
1001   default:\r
1002     len = sizeof(T_MsgHdr);\r
1003     break;\r
1004   }\r
1005   MsgHdr->Magic           = XDR_encode_uint32(MSG_MAGIC);\r
1006   MsgHdr->Version         = XDR_encode_uint32(PROTO_VER);\r
1007   MsgHdr->MsgId           = XDR_encode_uint32(MsgId);\r
1008   MsgHdr->MsgLen          = XDR_encode_uint32(len);\r
1009   MsgHdr->ReplyAddress    = 0; // Are obsolete, keep them for the server for\r
1010   MsgHdr->ReplyPort       = 0; // now\r
1011   strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);\r
1012   MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';\r
1013 }\r
1014 \r
1015 FGAIMultiplayer*\r
1016 FGMultiplayMgr::addMultiplayer(const std::string& callsign,\r
1017                                const std::string& modelName)\r
1018 {\r
1019   if (0 < mMultiPlayerMap.count(callsign))\r
1020     return mMultiPlayerMap[callsign].get();\r
1021 \r
1022   FGAIMultiplayer* mp = new FGAIMultiplayer;\r
1023   mp->setPath(modelName.c_str());\r
1024   mp->setCallSign(callsign);\r
1025   mMultiPlayerMap[callsign] = mp;\r
1026 \r
1027   FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");\r
1028   if (aiMgr) {\r
1029     aiMgr->attach(mp);\r
1030 \r
1031     /// FIXME: that must follow the attach ATM ...\r
1032     for (unsigned i = 0; i < numProperties; ++i)\r
1033       mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);\r
1034   }\r
1035 \r
1036   return mp;\r
1037 }\r
1038 \r
1039 FGAIMultiplayer*\r
1040 FGMultiplayMgr::getMultiplayer(const std::string& callsign)\r
1041 {\r
1042   if (0 < mMultiPlayerMap.count(callsign))\r
1043     return mMultiPlayerMap[callsign].get();\r
1044   else\r
1045     return 0;\r
1046 }\r