1 //////////////////////////////////////////////////////////////////////
\r
5 // Written by Duncan McCreanor, started February 2003.
\r
6 // duncan.mccreanor@airservicesaustralia.com
\r
8 // Copyright (C) 2003 Airservices Australia
\r
9 // Copyright (C) 2005 Oliver Schroeder
\r
10 // Copyright (C) 2006 Mathias Froehlich
\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
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
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
28 //////////////////////////////////////////////////////////////////////
\r
30 #ifdef HAVE_CONFIG_H
\r
35 #include <algorithm>
\r
37 #include <osg/Math> // isNaN
\r
38 #include <plib/netSocket.h>
\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
45 #include <AIModel/AIManager.hxx>
\r
46 #include <Main/fg_props.hxx>
\r
47 #include "multiplaymgr.hxx"
\r
48 #include "mpmessages.hxx"
\r
50 using namespace std;
\r
52 #define MAX_PACKET_SIZE 1200
\r
53 #define MAX_TEXT_SIZE 128
\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
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
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
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
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
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
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
158 {1100, "sim/model/variant", simgear::props::INT},
\r
159 {1101, "sim/model/livery/file", simgear::props::STRING},
\r
161 {1200, "environment/wildfire/data", simgear::props::STRING},
\r
162 {1201, "environment/contrail", simgear::props::INT},
\r
164 {1300, "tanker", simgear::props::INT},
\r
166 {10001, "sim/multiplay/transmission-freq-hz", simgear::props::STRING},
\r
167 {10002, "sim/multiplay/chat", simgear::props::STRING},
\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
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
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
234 FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)
\r
235 / sizeof(FGMultiplayMgr::sIdPropertyList[0]));
\r
237 // Look up a property ID using binary search.
\r
240 struct ComparePropertyId
\r
242 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
\r
243 const FGMultiplayMgr::IdPropertyList& rhs)
\r
245 return lhs.id < rhs.id;
\r
247 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
\r
250 return lhs.id < id;
\r
252 bool operator()(unsigned id,
\r
253 const FGMultiplayMgr::IdPropertyList& rhs)
\r
255 return id < rhs.id;
\r
260 const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)
\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
268 return result.first;
\r
274 bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)
\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
285 // How we decode the remainder of the property depends on the type
\r
286 switch (plist->type) {
\r
293 case props::DOUBLE:
\r
295 float val = XDR_decode_float(*xdr);
\r
296 if (osg::isNaN(val))
\r
301 case props::STRING:
\r
302 case props::UNSPECIFIED:
\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
310 uint32_t length = XDR_decode_uint32(*xdr);
\r
312 // Old versions truncated the string but left the length
\r
314 if (length > MAX_TEXT_SIZE)
\r
315 length = MAX_TEXT_SIZE;
\r
317 // Now handle the padding
\r
318 while ((length % 4) != 0)
\r
327 // cerr << "Unknown Prop type " << id << " " << type << "\n";
\r
333 // give up; this is a malformed property list.
\r
340 //////////////////////////////////////////////////////////////////////
\r
342 // MultiplayMgr constructor
\r
344 //////////////////////////////////////////////////////////////////////
\r
345 FGMultiplayMgr::FGMultiplayMgr()
\r
348 mInitialised = false;
\r
349 mHaveServer = false;
\r
350 } // FGMultiplayMgr::FGMultiplayMgr()
\r
351 //////////////////////////////////////////////////////////////////////
\r
353 //////////////////////////////////////////////////////////////////////
\r
355 // MultiplayMgr destructor
\r
357 //////////////////////////////////////////////////////////////////////
\r
358 FGMultiplayMgr::~FGMultiplayMgr()
\r
361 } // FGMultiplayMgr::~FGMultiplayMgr()
\r
362 //////////////////////////////////////////////////////////////////////
\r
364 //////////////////////////////////////////////////////////////////////
\r
366 // Initialise object
\r
368 //////////////////////////////////////////////////////////////////////
\r
370 FGMultiplayMgr::init (void)
\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
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
395 mHaveServer = true;
\r
401 SG_LOG(SG_NETWORK, SG_DEBUG,
\r
402 "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
\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
420 mSocket->setBlocking(false);
\r
421 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
\r
423 SG_LOG( SG_NETWORK, SG_DEBUG,
\r
424 "FGMultiplayMgr::Open - Failed to bind receive socket" );
\r
427 mInitialised = true;
\r
429 } // FGMultiplayMgr::init()
\r
430 //////////////////////////////////////////////////////////////////////
\r
432 //////////////////////////////////////////////////////////////////////
\r
434 // Closes and deletes the local player object. Closes
\r
435 // and deletes the tx socket. Resets the object state to unitialised.
\r
437 //////////////////////////////////////////////////////////////////////
\r
439 FGMultiplayMgr::Close (void)
\r
441 mMultiPlayerMap.clear();
\r
448 mInitialised = false;
\r
449 } // FGMultiplayMgr::Close(void)
\r
450 //////////////////////////////////////////////////////////////////////
\r
452 //////////////////////////////////////////////////////////////////////
\r
454 // Description: Sends the position data for the local position.
\r
456 //////////////////////////////////////////////////////////////////////
\r
459 * The buffer that holds a multi-player message, suitably aligned.
\r
461 union FGMultiplayMgr::MsgBuf
\r
465 memset(&Msg, 0, sizeof(Msg));
\r
470 return reinterpret_cast<T_MsgHdr*>(Msg);
\r
473 const T_MsgHdr* msgHdr() const
\r
475 return reinterpret_cast<const T_MsgHdr*>(Msg);
\r
478 T_PositionMsg* posMsg()
\r
480 return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
\r
483 const T_PositionMsg* posMsg() const
\r
485 return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
\r
488 xdr_data_t* properties()
\r
490 return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
\r
491 + sizeof(T_PositionMsg));
\r
494 const xdr_data_t* properties() const
\r
496 return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
\r
497 + sizeof(T_PositionMsg));
\r
500 * The end of the properties buffer.
\r
502 xdr_data_t* propsEnd()
\r
504 return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
\r
507 const xdr_data_t* propsEnd() const
\r
509 return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
\r
512 * The end of properties actually in the buffer. This assumes that
\r
513 * the message header is valid.
\r
515 xdr_data_t* propsRecvdEnd()
\r
517 return reinterpret_cast<xdr_data_t*>(Msg + msgHdr()->MsgLen);
\r
520 const xdr_data_t* propsRecvdEnd() const
\r
522 return reinterpret_cast<const xdr_data_t*>(Msg + msgHdr()->MsgLen);
\r
525 xdr_data2_t double_val;
\r
526 char Msg[MAX_PACKET_SIZE];
\r
530 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
\r
532 if ((! mInitialised) || (! mHaveServer))
\r
534 if (! mHaveServer) {
\r
535 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");
\r
540 T_PositionMsg* PosMsg = msgBuf.posMsg();
\r
542 strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
\r
543 PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
\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
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
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
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
578 *ptr++ = XDR_encode_uint32((*it)->int_value);
\r
579 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
\r
581 case simgear::props::FLOAT:
\r
582 case simgear::props::DOUBLE:
\r
584 *ptr++ = XDR_encode_float((*it)->float_value);
\r
585 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
\r
587 case simgear::props::STRING:
\r
588 case simgear::props::UNSPECIFIED:
\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
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
606 if (ptr + 2 + ((len + 3) & ~3) > msgEnd)
\r
608 //cout << "String length unint32: " << len << "\n";
\r
610 *ptr++ = XDR_encode_uint32(len);
\r
613 // Now the text itself
\r
614 // XXX This should not be using 4 bytes per character!
\r
616 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
\r
618 *ptr++ = XDR_encode_int8(*lcharptr);
\r
623 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
\r
625 // Now pad if required
\r
626 while ((lcount % 4) != 0)
\r
628 *ptr++ = XDR_encode_int8(0);
\r
638 // Nothing to encode
\r
640 *ptr++ = XDR_encode_uint32(0);
\r
641 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
\r
647 //cout << " Unknown Type: " << (*it)->type << "\n";
\r
649 *ptr++ = XDR_encode_float((*it)->float_value);;
\r
650 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
\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
663 //////////////////////////////////////////////////////////////////////
\r
665 //////////////////////////////////////////////////////////////////////
\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
672 //////////////////////////////////////////////////////////////////////
\r
674 FGMultiplayMgr::SendTextMessage(const string &MsgText)
\r
676 if (!mInitialised || !mHaveServer)
\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
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
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
702 } // FGMultiplayMgr::SendTextMessage ()
\r
703 //////////////////////////////////////////////////////////////////////
\r
705 //////////////////////////////////////////////////////////////////////
\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
711 //////////////////////////////////////////////////////////////////////
\r
713 FGMultiplayMgr::Update(void)
\r
718 /// Just for expiry
\r
719 long stamp = SGTimeStamp::now().getSeconds();
\r
721 //////////////////////////////////////////////////
\r
722 // Read the receive socket and process any data
\r
723 //////////////////////////////////////////////////
\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
736 //////////////////////////////////////////////////
\r
737 // no Data received
\r
738 //////////////////////////////////////////////////
\r
740 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
\r
741 perror("FGMultiplayMgr::MP_ProcessData");
\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
749 //////////////////////////////////////////////////
\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
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
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
774 //////////////////////////////////////////////////
\r
775 // Process messages
\r
776 //////////////////////////////////////////////////
\r
777 switch (MsgHdr->MsgId) {
\r
779 ProcessChatMsg(msgBuf, SenderAddress);
\r
782 ProcessPosMsg(msgBuf, SenderAddress, stamp);
\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
790 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
791 << "Unknown message Id received: " << MsgHdr->MsgId );
\r
794 } while (bytes > 0);
\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
807 } // FGMultiplayMgr::ProcessData(void)
\r
808 //////////////////////////////////////////////////////////////////////
\r
810 //////////////////////////////////////////////////////////////////////
\r
812 // handle a position message
\r
814 //////////////////////////////////////////////////////////////////////
\r
816 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,
\r
817 const netAddress& SenderAddress, long stamp)
\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
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
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
845 //cout << "INPUT MESSAGE\n";
\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
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
869 while (xdr < Msg.propsRecvdEnd()) {
\r
870 FGPropertyData* pData = new FGPropertyData;
\r
871 // simgear::props::Type type = simgear::props::UNSPECIFIED;
\r
873 // First element is always the ID
\r
874 pData->id = XDR_decode_uint32(*xdr);
\r
875 //cout << pData->id << " ";
\r
878 // Check the ID actually exists and get the type
\r
879 const IdPropertyList* plist = findProperty(pData->id);
\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
891 //cout << pData->int_value << "\n";
\r
893 case simgear::props::FLOAT:
\r
894 case simgear::props::DOUBLE:
\r
895 pData->float_value = XDR_decode_float(*xdr);
\r
897 //cout << pData->float_value << "\n";
\r
899 case simgear::props::STRING:
\r
900 case simgear::props::UNSPECIFIED:
\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
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
916 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
\r
918 //cout << pData->string_value[i];
\r
921 pData->string_value[length] = '\0';
\r
923 // Now handle the padding
\r
924 while ((length % 4) != 0)
\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
941 motionInfo.properties.push_back(pData);
\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
952 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
\r
954 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
\r
955 mp->addMotionInfo(motionInfo, stamp);
\r
956 } // FGMultiplayMgr::ProcessPosMsg()
\r
957 //////////////////////////////////////////////////////////////////////
\r
959 //////////////////////////////////////////////////////////////////////
\r
961 // handle a chat message
\r
962 // FIXME: display chat message withi flightgear
\r
964 //////////////////////////////////////////////////////////////////////
\r
966 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
\r
967 const netAddress& SenderAddress)
\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
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
983 SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"
\r
984 << " " << chatStr);
\r
987 } // FGMultiplayMgr::ProcessChatMsg ()
\r
988 //////////////////////////////////////////////////////////////////////
\r
991 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
\r
996 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
\r
1002 len = sizeof(T_MsgHdr);
\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
1016 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
\r
1017 const std::string& modelName)
\r
1019 if (0 < mMultiPlayerMap.count(callsign))
\r
1020 return mMultiPlayerMap[callsign].get();
\r
1022 FGAIMultiplayer* mp = new FGAIMultiplayer;
\r
1023 mp->setPath(modelName.c_str());
\r
1024 mp->setCallSign(callsign);
\r
1025 mMultiPlayerMap[callsign] = mp;
\r
1027 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
\r
1029 aiMgr->attach(mp);
\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
1040 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
\r
1042 if (0 < mMultiPlayerMap.count(callsign))
\r
1043 return mMultiPlayerMap[callsign].get();
\r