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