1 //////////////////////////////////////////////////////////////////////
5 // Written by Duncan McCreanor, started February 2003.
6 // duncan.mccreanor@airservicesaustralia.com
8 // Copyright (C) 2003 Airservices Australia
9 // Copyright (C) 2005 Oliver Schroeder
10 // Copyright (C) 2006 Mathias Froehlich
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 //////////////////////////////////////////////////////////////////////
37 #include <osg/Math> // isNaN
39 #include <simgear/misc/stdint.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/props/props.hxx>
44 #include <AIModel/AIManager.hxx>
45 #include <Main/fg_props.hxx>
46 #include "multiplaymgr.hxx"
47 #include "mpmessages.hxx"
51 #define MAX_PACKET_SIZE 1200
52 #define MAX_TEXT_SIZE 128
54 // These constants are provided so that the ident
55 // command can list file versions
56 const char sMULTIPLAYMGR_BID[] = "$Id$";
57 const char sMULTIPLAYMGR_HID[] = MULTIPLAYTXMGR_HID;
59 // A static map of protocol property id values to property paths,
60 // This should be extendable dynamically for every specific aircraft ...
61 // For now only that static list
62 const FGMultiplayMgr::IdPropertyList
63 FGMultiplayMgr::sIdPropertyList[] = {
64 {100, "surface-positions/left-aileron-pos-norm", simgear::props::FLOAT},
65 {101, "surface-positions/right-aileron-pos-norm", simgear::props::FLOAT},
66 {102, "surface-positions/elevator-pos-norm", simgear::props::FLOAT},
67 {103, "surface-positions/rudder-pos-norm", simgear::props::FLOAT},
68 {104, "surface-positions/flap-pos-norm", simgear::props::FLOAT},
69 {105, "surface-positions/speedbrake-pos-norm", simgear::props::FLOAT},
70 {106, "gear/tailhook/position-norm", simgear::props::FLOAT},
71 {107, "gear/launchbar/position-norm", simgear::props::FLOAT},
72 {108, "gear/launchbar/state", simgear::props::STRING},
73 {109, "gear/launchbar/holdback-position-norm", simgear::props::FLOAT},
74 {110, "canopy/position-norm", simgear::props::FLOAT},
75 {111, "surface-positions/wing-pos-norm", simgear::props::FLOAT},
76 {112, "surface-positions/wing-fold-pos-norm", simgear::props::FLOAT},
78 {200, "gear/gear[0]/compression-norm", simgear::props::FLOAT},
79 {201, "gear/gear[0]/position-norm", simgear::props::FLOAT},
80 {210, "gear/gear[1]/compression-norm", simgear::props::FLOAT},
81 {211, "gear/gear[1]/position-norm", simgear::props::FLOAT},
82 {220, "gear/gear[2]/compression-norm", simgear::props::FLOAT},
83 {221, "gear/gear[2]/position-norm", simgear::props::FLOAT},
84 {230, "gear/gear[3]/compression-norm", simgear::props::FLOAT},
85 {231, "gear/gear[3]/position-norm", simgear::props::FLOAT},
86 {240, "gear/gear[4]/compression-norm", simgear::props::FLOAT},
87 {241, "gear/gear[4]/position-norm", simgear::props::FLOAT},
89 {300, "engines/engine[0]/n1", simgear::props::FLOAT},
90 {301, "engines/engine[0]/n2", simgear::props::FLOAT},
91 {302, "engines/engine[0]/rpm", simgear::props::FLOAT},
92 {310, "engines/engine[1]/n1", simgear::props::FLOAT},
93 {311, "engines/engine[1]/n2", simgear::props::FLOAT},
94 {312, "engines/engine[1]/rpm", simgear::props::FLOAT},
95 {320, "engines/engine[2]/n1", simgear::props::FLOAT},
96 {321, "engines/engine[2]/n2", simgear::props::FLOAT},
97 {322, "engines/engine[2]/rpm", simgear::props::FLOAT},
98 {330, "engines/engine[3]/n1", simgear::props::FLOAT},
99 {331, "engines/engine[3]/n2", simgear::props::FLOAT},
100 {332, "engines/engine[3]/rpm", simgear::props::FLOAT},
101 {340, "engines/engine[4]/n1", simgear::props::FLOAT},
102 {341, "engines/engine[4]/n2", simgear::props::FLOAT},
103 {342, "engines/engine[4]/rpm", simgear::props::FLOAT},
104 {350, "engines/engine[5]/n1", simgear::props::FLOAT},
105 {351, "engines/engine[5]/n2", simgear::props::FLOAT},
106 {352, "engines/engine[5]/rpm", simgear::props::FLOAT},
107 {360, "engines/engine[6]/n1", simgear::props::FLOAT},
108 {361, "engines/engine[6]/n2", simgear::props::FLOAT},
109 {362, "engines/engine[6]/rpm", simgear::props::FLOAT},
110 {370, "engines/engine[7]/n1", simgear::props::FLOAT},
111 {371, "engines/engine[7]/n2", simgear::props::FLOAT},
112 {372, "engines/engine[7]/rpm", simgear::props::FLOAT},
113 {380, "engines/engine[8]/n1", simgear::props::FLOAT},
114 {381, "engines/engine[8]/n2", simgear::props::FLOAT},
115 {382, "engines/engine[8]/rpm", simgear::props::FLOAT},
116 {390, "engines/engine[9]/n1", simgear::props::FLOAT},
117 {391, "engines/engine[9]/n2", simgear::props::FLOAT},
118 {392, "engines/engine[9]/rpm", simgear::props::FLOAT},
120 {800, "rotors/main/rpm", simgear::props::FLOAT},
121 {801, "rotors/tail/rpm", simgear::props::FLOAT},
122 {810, "rotors/main/blade[0]/position-deg", simgear::props::FLOAT},
123 {811, "rotors/main/blade[1]/position-deg", simgear::props::FLOAT},
124 {812, "rotors/main/blade[2]/position-deg", simgear::props::FLOAT},
125 {813, "rotors/main/blade[3]/position-deg", simgear::props::FLOAT},
126 {820, "rotors/main/blade[0]/flap-deg", simgear::props::FLOAT},
127 {821, "rotors/main/blade[1]/flap-deg", simgear::props::FLOAT},
128 {822, "rotors/main/blade[2]/flap-deg", simgear::props::FLOAT},
129 {823, "rotors/main/blade[3]/flap-deg", simgear::props::FLOAT},
130 {830, "rotors/tail/blade[0]/position-deg", simgear::props::FLOAT},
131 {831, "rotors/tail/blade[1]/position-deg", simgear::props::FLOAT},
133 {900, "sim/hitches/aerotow/tow/length", simgear::props::FLOAT},
134 {901, "sim/hitches/aerotow/tow/elastic-constant", simgear::props::FLOAT},
135 {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m", simgear::props::FLOAT},
136 {903, "sim/hitches/aerotow/tow/dist", simgear::props::FLOAT},
137 {904, "sim/hitches/aerotow/tow/connected-to-property-node", simgear::props::BOOL},
138 {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign", simgear::props::STRING},
139 {906, "sim/hitches/aerotow/tow/brake-force", simgear::props::FLOAT},
140 {907, "sim/hitches/aerotow/tow/end-force-x", simgear::props::FLOAT},
141 {908, "sim/hitches/aerotow/tow/end-force-y", simgear::props::FLOAT},
142 {909, "sim/hitches/aerotow/tow/end-force-z", simgear::props::FLOAT},
143 {930, "sim/hitches/aerotow/is-slave", simgear::props::BOOL},
144 {931, "sim/hitches/aerotow/speed-in-tow-direction", simgear::props::FLOAT},
145 {932, "sim/hitches/aerotow/open", simgear::props::BOOL},
146 {933, "sim/hitches/aerotow/local-pos-x", simgear::props::FLOAT},
147 {934, "sim/hitches/aerotow/local-pos-y", simgear::props::FLOAT},
148 {935, "sim/hitches/aerotow/local-pos-z", simgear::props::FLOAT},
150 {1001, "controls/flight/slats", simgear::props::FLOAT},
151 {1002, "controls/flight/speedbrake", simgear::props::FLOAT},
152 {1003, "controls/flight/spoilers", simgear::props::FLOAT},
153 {1004, "controls/gear/gear-down", simgear::props::FLOAT},
154 {1005, "controls/lighting/nav-lights", simgear::props::FLOAT},
155 {1006, "controls/armament/station[0]/jettison-all", simgear::props::BOOL},
157 {1100, "sim/model/variant", simgear::props::INT},
158 {1101, "sim/model/livery/file", simgear::props::STRING},
160 {1200, "environment/wildfire/data", simgear::props::STRING},
161 {1201, "environment/contrail", simgear::props::INT},
163 {1300, "tanker", simgear::props::INT},
165 {10001, "sim/multiplay/transmission-freq-hz", simgear::props::STRING},
166 {10002, "sim/multiplay/chat", simgear::props::STRING},
168 {10100, "sim/multiplay/generic/string[0]", simgear::props::STRING},
169 {10101, "sim/multiplay/generic/string[1]", simgear::props::STRING},
170 {10102, "sim/multiplay/generic/string[2]", simgear::props::STRING},
171 {10103, "sim/multiplay/generic/string[3]", simgear::props::STRING},
172 {10104, "sim/multiplay/generic/string[4]", simgear::props::STRING},
173 {10105, "sim/multiplay/generic/string[5]", simgear::props::STRING},
174 {10106, "sim/multiplay/generic/string[6]", simgear::props::STRING},
175 {10107, "sim/multiplay/generic/string[7]", simgear::props::STRING},
176 {10108, "sim/multiplay/generic/string[8]", simgear::props::STRING},
177 {10109, "sim/multiplay/generic/string[9]", simgear::props::STRING},
178 {10110, "sim/multiplay/generic/string[10]", simgear::props::STRING},
179 {10111, "sim/multiplay/generic/string[11]", simgear::props::STRING},
180 {10112, "sim/multiplay/generic/string[12]", simgear::props::STRING},
181 {10113, "sim/multiplay/generic/string[13]", simgear::props::STRING},
182 {10114, "sim/multiplay/generic/string[14]", simgear::props::STRING},
183 {10115, "sim/multiplay/generic/string[15]", simgear::props::STRING},
184 {10116, "sim/multiplay/generic/string[16]", simgear::props::STRING},
185 {10117, "sim/multiplay/generic/string[17]", simgear::props::STRING},
186 {10118, "sim/multiplay/generic/string[18]", simgear::props::STRING},
187 {10119, "sim/multiplay/generic/string[19]", simgear::props::STRING},
189 {10200, "sim/multiplay/generic/float[0]", simgear::props::FLOAT},
190 {10201, "sim/multiplay/generic/float[1]", simgear::props::FLOAT},
191 {10202, "sim/multiplay/generic/float[2]", simgear::props::FLOAT},
192 {10203, "sim/multiplay/generic/float[3]", simgear::props::FLOAT},
193 {10204, "sim/multiplay/generic/float[4]", simgear::props::FLOAT},
194 {10205, "sim/multiplay/generic/float[5]", simgear::props::FLOAT},
195 {10206, "sim/multiplay/generic/float[6]", simgear::props::FLOAT},
196 {10207, "sim/multiplay/generic/float[7]", simgear::props::FLOAT},
197 {10208, "sim/multiplay/generic/float[8]", simgear::props::FLOAT},
198 {10209, "sim/multiplay/generic/float[9]", simgear::props::FLOAT},
199 {10210, "sim/multiplay/generic/float[10]", simgear::props::FLOAT},
200 {10211, "sim/multiplay/generic/float[11]", simgear::props::FLOAT},
201 {10212, "sim/multiplay/generic/float[12]", simgear::props::FLOAT},
202 {10213, "sim/multiplay/generic/float[13]", simgear::props::FLOAT},
203 {10214, "sim/multiplay/generic/float[14]", simgear::props::FLOAT},
204 {10215, "sim/multiplay/generic/float[15]", simgear::props::FLOAT},
205 {10216, "sim/multiplay/generic/float[16]", simgear::props::FLOAT},
206 {10217, "sim/multiplay/generic/float[17]", simgear::props::FLOAT},
207 {10218, "sim/multiplay/generic/float[18]", simgear::props::FLOAT},
208 {10219, "sim/multiplay/generic/float[19]", simgear::props::FLOAT},
210 {10300, "sim/multiplay/generic/int[0]", simgear::props::INT},
211 {10301, "sim/multiplay/generic/int[1]", simgear::props::INT},
212 {10302, "sim/multiplay/generic/int[2]", simgear::props::INT},
213 {10303, "sim/multiplay/generic/int[3]", simgear::props::INT},
214 {10304, "sim/multiplay/generic/int[4]", simgear::props::INT},
215 {10305, "sim/multiplay/generic/int[5]", simgear::props::INT},
216 {10306, "sim/multiplay/generic/int[6]", simgear::props::INT},
217 {10307, "sim/multiplay/generic/int[7]", simgear::props::INT},
218 {10308, "sim/multiplay/generic/int[8]", simgear::props::INT},
219 {10309, "sim/multiplay/generic/int[9]", simgear::props::INT},
220 {10310, "sim/multiplay/generic/int[10]", simgear::props::INT},
221 {10311, "sim/multiplay/generic/int[11]", simgear::props::INT},
222 {10312, "sim/multiplay/generic/int[12]", simgear::props::INT},
223 {10313, "sim/multiplay/generic/int[13]", simgear::props::INT},
224 {10314, "sim/multiplay/generic/int[14]", simgear::props::INT},
225 {10315, "sim/multiplay/generic/int[15]", simgear::props::INT},
226 {10316, "sim/multiplay/generic/int[16]", simgear::props::INT},
227 {10317, "sim/multiplay/generic/int[17]", simgear::props::INT},
228 {10318, "sim/multiplay/generic/int[18]", simgear::props::INT},
229 {10319, "sim/multiplay/generic/int[19]", simgear::props::INT}
233 FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)
234 / sizeof(FGMultiplayMgr::sIdPropertyList[0]));
236 // Look up a property ID using binary search.
239 struct ComparePropertyId
241 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
242 const FGMultiplayMgr::IdPropertyList& rhs)
244 return lhs.id < rhs.id;
246 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
251 bool operator()(unsigned id,
252 const FGMultiplayMgr::IdPropertyList& rhs)
259 const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)
261 std::pair<const IdPropertyList*, const IdPropertyList*> result
262 = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,
263 ComparePropertyId());
264 if (result.first == result.second) {
273 bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)
275 using namespace simgear;
276 const xdr_data_t* xdr = data;
278 unsigned id = XDR_decode_uint32(*xdr);
279 const FGMultiplayMgr::IdPropertyList* plist
280 = FGMultiplayMgr::findProperty(id);
284 // How we decode the remainder of the property depends on the type
285 switch (plist->type) {
294 float val = XDR_decode_float(*xdr);
301 case props::UNSPECIFIED:
303 // String is complicated. It consists of
304 // The length of the string
306 // Padding to the nearest 4-bytes.
307 // XXX Yes, each byte is padded out to a word! Too late
309 uint32_t length = XDR_decode_uint32(*xdr);
311 // Old versions truncated the string but left the length
313 if (length > MAX_TEXT_SIZE)
314 length = MAX_TEXT_SIZE;
316 // Now handle the padding
317 while ((length % 4) != 0)
326 // cerr << "Unknown Prop type " << id << " " << type << "\n";
332 // give up; this is a malformed property list.
339 //////////////////////////////////////////////////////////////////////
341 // MultiplayMgr constructor
343 //////////////////////////////////////////////////////////////////////
344 FGMultiplayMgr::FGMultiplayMgr()
347 mInitialised = false;
349 } // FGMultiplayMgr::FGMultiplayMgr()
350 //////////////////////////////////////////////////////////////////////
352 //////////////////////////////////////////////////////////////////////
354 // MultiplayMgr destructor
356 //////////////////////////////////////////////////////////////////////
357 FGMultiplayMgr::~FGMultiplayMgr()
360 } // FGMultiplayMgr::~FGMultiplayMgr()
361 //////////////////////////////////////////////////////////////////////
363 //////////////////////////////////////////////////////////////////////
367 //////////////////////////////////////////////////////////////////////
369 FGMultiplayMgr::init (void)
371 //////////////////////////////////////////////////
372 // Initialise object if not already done
373 //////////////////////////////////////////////////
375 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
378 //////////////////////////////////////////////////
379 // Set members from property values
380 //////////////////////////////////////////////////
381 short rxPort = fgGetInt("/sim/multiplay/rxport");
382 string rxAddress = fgGetString("/sim/multiplay/rxhost");
383 short txPort = fgGetInt("/sim/multiplay/txport");
384 string txAddress = fgGetString("/sim/multiplay/txhost");
385 mCallsign = fgGetString("/sim/multiplay/callsign");
386 if (txPort > 0 && !txAddress.empty()) {
387 mServer.set(txAddress.c_str(), txPort);
388 if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {
390 SG_LOG(SG_NETWORK, SG_DEBUG,
391 "FGMultiplayMgr - could not resolve '"
392 << txAddress << "', Multiplayermode disabled");
400 SG_LOG(SG_NETWORK, SG_DEBUG,
401 "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
404 if (mCallsign.empty())
405 mCallsign = "JohnDoe"; // FIXME: use getpwuid
406 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
407 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
408 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
409 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
410 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
411 Close(); // Should Init be called twice, close Socket first
412 // A memory leak was reported here by valgrind
413 mSocket = new simgear::Socket();
414 if (!mSocket->open(false)) {
415 SG_LOG( SG_NETWORK, SG_DEBUG,
416 "FGMultiplayMgr::init - Failed to create data socket" );
419 mSocket->setBlocking(false);
420 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
422 SG_LOG( SG_NETWORK, SG_DEBUG,
423 "FGMultiplayMgr::Open - Failed to bind receive socket" );
428 } // FGMultiplayMgr::init()
429 //////////////////////////////////////////////////////////////////////
431 //////////////////////////////////////////////////////////////////////
433 // Closes and deletes the local player object. Closes
434 // and deletes the tx socket. Resets the object state to unitialised.
436 //////////////////////////////////////////////////////////////////////
438 FGMultiplayMgr::Close (void)
440 mMultiPlayerMap.clear();
447 mInitialised = false;
448 } // FGMultiplayMgr::Close(void)
449 //////////////////////////////////////////////////////////////////////
451 //////////////////////////////////////////////////////////////////////
453 // Description: Sends the position data for the local position.
455 //////////////////////////////////////////////////////////////////////
458 * The buffer that holds a multi-player message, suitably aligned.
460 union FGMultiplayMgr::MsgBuf
464 memset(&Msg, 0, sizeof(Msg));
472 const T_MsgHdr* msgHdr() const
474 return reinterpret_cast<const T_MsgHdr*>(&Header);
477 T_PositionMsg* posMsg()
479 return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
482 const T_PositionMsg* posMsg() const
484 return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
487 xdr_data_t* properties()
489 return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
490 + sizeof(T_PositionMsg));
493 const xdr_data_t* properties() const
495 return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
496 + sizeof(T_PositionMsg));
499 * The end of the properties buffer.
501 xdr_data_t* propsEnd()
503 return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
506 const xdr_data_t* propsEnd() const
508 return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
511 * The end of properties actually in the buffer. This assumes that
512 * the message header is valid.
514 xdr_data_t* propsRecvdEnd()
516 return reinterpret_cast<xdr_data_t*>(Msg + Header.MsgLen);
519 const xdr_data_t* propsRecvdEnd() const
521 return reinterpret_cast<const xdr_data_t*>(Msg + Header.MsgLen);
524 xdr_data2_t double_val;
525 char Msg[MAX_PACKET_SIZE];
530 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
532 if ((! mInitialised) || (! mHaveServer))
535 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");
539 static MsgBuf msgBuf;
540 static unsigned msgLen = 0;
541 T_PositionMsg* PosMsg = msgBuf.posMsg();
543 strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
544 PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
545 if (fgGetBool("/sim/freeze/replay-state", true))
547 // do not send position updates during replay
548 for (unsigned i = 0 ; i < 3; ++i)
550 // no movement during replay
551 PosMsg->linearVel[i] = XDR_encode_float (0.0);
552 PosMsg->angularVel[i] = XDR_encode_float (0.0);
553 PosMsg->linearAccel[i] = XDR_encode_float (0.0);
554 PosMsg->angularAccel[i] = XDR_encode_float (0.0);
556 // all other data remains unchanged (resend last state)
560 PosMsg->time = XDR_encode_double (motionInfo.time);
561 PosMsg->lag = XDR_encode_double (motionInfo.lag);
562 for (unsigned i = 0 ; i < 3; ++i)
563 PosMsg->position[i] = XDR_encode_double (motionInfo.position(i));
565 motionInfo.orientation.getAngleAxis(angleAxis);
566 for (unsigned i = 0 ; i < 3; ++i)
567 PosMsg->orientation[i] = XDR_encode_float (angleAxis(i));
568 for (unsigned i = 0 ; i < 3; ++i)
569 PosMsg->linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
570 for (unsigned i = 0 ; i < 3; ++i)
571 PosMsg->angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
572 for (unsigned i = 0 ; i < 3; ++i)
573 PosMsg->linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
574 for (unsigned i = 0 ; i < 3; ++i)
575 PosMsg->angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
577 xdr_data_t* ptr = msgBuf.properties();
578 std::vector<FGPropertyData*>::const_iterator it;
579 it = motionInfo.properties.begin();
580 //cout << "OUTPUT PROPERTIES\n";
581 xdr_data_t* msgEnd = msgBuf.propsEnd();
582 while (it != motionInfo.properties.end() && ptr + 2 < msgEnd) {
584 // First element is the ID. Write it out when we know we have room for
585 // the whole property.
586 xdr_data_t id = XDR_encode_uint32((*it)->id);
587 // The actual data representation depends on the type
588 switch ((*it)->type) {
589 case simgear::props::INT:
590 case simgear::props::BOOL:
591 case simgear::props::LONG:
593 *ptr++ = XDR_encode_uint32((*it)->int_value);
594 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
596 case simgear::props::FLOAT:
597 case simgear::props::DOUBLE:
599 *ptr++ = XDR_encode_float((*it)->float_value);
600 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
602 case simgear::props::STRING:
603 case simgear::props::UNSPECIFIED:
605 // String is complicated. It consists of
606 // The length of the string
608 // Padding to the nearest 4-bytes.
609 const char* lcharptr = (*it)->string_value;
614 ////cout << "String length: " << strlen(lcharptr) << "\n";
615 uint32_t len = strlen(lcharptr);
616 if (len > MAX_TEXT_SIZE)
618 // XXX This should not be using 4 bytes per character!
619 // If there's not enough room for this property, drop it
621 if (ptr + 2 + ((len + 3) & ~3) > msgEnd)
623 //cout << "String length unint32: " << len << "\n";
625 *ptr++ = XDR_encode_uint32(len);
628 // Now the text itself
629 // XXX This should not be using 4 bytes per character!
631 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
633 *ptr++ = XDR_encode_int8(*lcharptr);
638 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
640 // Now pad if required
641 while ((lcount % 4) != 0)
643 *ptr++ = XDR_encode_int8(0);
655 *ptr++ = XDR_encode_uint32(0);
656 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
662 //cout << " Unknown Type: " << (*it)->type << "\n";
664 *ptr++ = XDR_encode_float((*it)->float_value);;
665 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
672 msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;
673 FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);
676 mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);
677 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
678 } // FGMultiplayMgr::SendMyPosition()
680 //////////////////////////////////////////////////////////////////////
682 //////////////////////////////////////////////////////////////////////
684 // Name: SendTextMessage
685 // Description: Sends a message to the player. The message must
686 // contain a valid and correctly filled out header and optional
689 //////////////////////////////////////////////////////////////////////
691 FGMultiplayMgr::SendTextMessage(const string &MsgText)
693 if (!mInitialised || !mHaveServer)
697 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
698 //////////////////////////////////////////////////
699 // Divide the text string into blocks that fit
700 // in the message and send the blocks.
701 //////////////////////////////////////////////////
702 unsigned iNextBlockPosition = 0;
705 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
706 while (iNextBlockPosition < MsgText.length()) {
707 strncpy (ChatMsg.Text,
708 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
710 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
711 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
712 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
713 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
714 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
719 } // FGMultiplayMgr::SendTextMessage ()
720 //////////////////////////////////////////////////////////////////////
722 //////////////////////////////////////////////////////////////////////
725 // Description: Processes data waiting at the receive socket. The
726 // processing ends when there is no more data at the socket.
728 //////////////////////////////////////////////////////////////////////
730 FGMultiplayMgr::update(double)
736 long stamp = SGTimeStamp::now().getSeconds();
738 //////////////////////////////////////////////////
739 // Read the receive socket and process any data
740 //////////////////////////////////////////////////
744 //////////////////////////////////////////////////
745 // Although the recv call asks for
746 // MAX_PACKET_SIZE of data, the number of bytes
747 // returned will only be that of the next
748 // packet waiting to be processed.
749 //////////////////////////////////////////////////
750 simgear::IPAddress SenderAddress;
751 bytes = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,
753 //////////////////////////////////////////////////
755 //////////////////////////////////////////////////
757 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
758 perror("FGMultiplayMgr::MP_ProcessData");
761 if (bytes <= static_cast<ssize_t>(sizeof(T_MsgHdr))) {
762 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
763 << "received message with insufficient data" );
766 //////////////////////////////////////////////////
768 //////////////////////////////////////////////////
769 T_MsgHdr* MsgHdr = msgBuf.msgHdr();
770 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
771 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
772 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
773 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
774 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
775 MsgHdr->Callsign[MAX_CALLSIGN_LEN -1] = '\0';
776 if (MsgHdr->Magic != MSG_MAGIC) {
777 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
778 << "message has invalid magic number!" );
781 if (MsgHdr->Version != PROTO_VER) {
782 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
783 << "message has invalid protocoll number!" );
786 if (MsgHdr->MsgLen != bytes) {
787 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
788 << "message from " << MsgHdr->Callsign << " has invalid length!");
791 //////////////////////////////////////////////////
793 //////////////////////////////////////////////////
794 switch (MsgHdr->MsgId) {
796 ProcessChatMsg(msgBuf, SenderAddress);
799 ProcessPosMsg(msgBuf, SenderAddress, stamp);
801 case UNUSABLE_POS_DATA_ID:
802 case OLD_OLD_POS_DATA_ID:
803 case OLD_PROP_MSG_ID:
804 case OLD_POS_DATA_ID:
807 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
808 << "Unknown message Id received: " << MsgHdr->MsgId );
814 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
815 while (it != mMultiPlayerMap.end()) {
816 if (it->second->getLastTimestamp() + 10 < stamp) {
817 std::string name = it->first;
818 it->second->setDie(true);
819 mMultiPlayerMap.erase(it);
820 it = mMultiPlayerMap.upper_bound(name);
824 } // FGMultiplayMgr::ProcessData(void)
825 //////////////////////////////////////////////////////////////////////
827 //////////////////////////////////////////////////////////////////////
829 // handle a position message
831 //////////////////////////////////////////////////////////////////////
833 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,
834 const simgear::IPAddress& SenderAddress, long stamp)
836 const T_MsgHdr* MsgHdr = Msg.msgHdr();
837 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
838 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
839 << "Position message received with insufficient data" );
842 const T_PositionMsg* PosMsg = Msg.posMsg();
843 FGExternalMotionData motionInfo;
844 motionInfo.time = XDR_decode_double(PosMsg->time);
845 motionInfo.lag = XDR_decode_double(PosMsg->lag);
846 for (unsigned i = 0; i < 3; ++i)
847 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
849 for (unsigned i = 0; i < 3; ++i)
850 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
851 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
852 for (unsigned i = 0; i < 3; ++i)
853 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
854 for (unsigned i = 0; i < 3; ++i)
855 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
856 for (unsigned i = 0; i < 3; ++i)
857 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
858 for (unsigned i = 0; i < 3; ++i)
859 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
862 //cout << "INPUT MESSAGE\n";
864 // There was a bug in 1.9.0 and before: T_PositionMsg was 196 bytes
865 // on 32 bit architectures and 200 bytes on 64 bit, and this
866 // structure is put directly on the wire. By looking at the padding,
867 // we can sort through the mess, mostly:
868 // If padding is 0 (which is not a valid property type), then the
869 // message was produced by a new client or an old 64 bit client that
870 // happened to have 0 on the stack;
871 // Else if the property list starting with the padding word is
872 // well-formed, then the client is probably an old 32 bit client and
873 // we'll go with that;
874 // Else it is an old 64-bit client and properties start after the
876 // There is a chance that we could be fooled by garbage in the
877 // padding looking like a valid property, so verifyProperties() is
878 // strict about the validity of the property values.
879 const xdr_data_t* xdr = Msg.properties();
880 if (PosMsg->pad != 0) {
881 if (verifyProperties(&PosMsg->pad, Msg.propsRecvdEnd()))
883 else if (!verifyProperties(xdr, Msg.propsRecvdEnd()))
886 while (xdr < Msg.propsRecvdEnd()) {
887 FGPropertyData* pData = new FGPropertyData;
888 // simgear::props::Type type = simgear::props::UNSPECIFIED;
890 // First element is always the ID
891 pData->id = XDR_decode_uint32(*xdr);
892 //cout << pData->id << " ";
895 // Check the ID actually exists and get the type
896 const IdPropertyList* plist = findProperty(pData->id);
900 pData->type = plist->type;
901 // How we decode the remainder of the property depends on the type
902 switch (pData->type) {
903 case simgear::props::INT:
904 case simgear::props::BOOL:
905 case simgear::props::LONG:
906 pData->int_value = XDR_decode_uint32(*xdr);
908 //cout << pData->int_value << "\n";
910 case simgear::props::FLOAT:
911 case simgear::props::DOUBLE:
912 pData->float_value = XDR_decode_float(*xdr);
914 //cout << pData->float_value << "\n";
916 case simgear::props::STRING:
917 case simgear::props::UNSPECIFIED:
919 // String is complicated. It consists of
920 // The length of the string
922 // Padding to the nearest 4-bytes.
923 uint32_t length = XDR_decode_uint32(*xdr);
925 //cout << length << " ";
926 // Old versions truncated the string but left the length unadjusted.
927 if (length > MAX_TEXT_SIZE)
928 length = MAX_TEXT_SIZE;
929 pData->string_value = new char[length + 1];
930 //cout << " String: ";
931 for (unsigned i = 0; i < length; i++)
933 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
935 //cout << pData->string_value[i];
938 pData->string_value[length] = '\0';
940 // Now handle the padding
941 while ((length % 4) != 0)
952 pData->float_value = XDR_decode_float(*xdr);
953 SG_LOG(SG_NETWORK, SG_DEBUG, "Unknown Prop type " << pData->id << " " << pData->type);
958 motionInfo.properties.push_back(pData);
962 // We failed to find the property. We'll try the next packet immediately.
963 SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr::ProcessPosMsg - "
964 "message from " << MsgHdr->Callsign << " has unknown property id "
969 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
971 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
972 mp->addMotionInfo(motionInfo, stamp);
973 } // FGMultiplayMgr::ProcessPosMsg()
974 //////////////////////////////////////////////////////////////////////
976 //////////////////////////////////////////////////////////////////////
978 // handle a chat message
979 // FIXME: display chat message withi flightgear
981 //////////////////////////////////////////////////////////////////////
983 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
984 const simgear::IPAddress& SenderAddress)
986 const T_MsgHdr* MsgHdr = Msg.msgHdr();
987 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
988 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
989 << "Chat message received with insufficient data" );
993 char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
994 const T_ChatMsg* ChatMsg
995 = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));
996 strncpy(chatStr, ChatMsg->Text,
997 MsgHdr->MsgLen - sizeof(T_MsgHdr));
998 chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
1000 SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"
1004 } // FGMultiplayMgr::ProcessChatMsg ()
1005 //////////////////////////////////////////////////////////////////////
1008 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
1013 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
1019 len = sizeof(T_MsgHdr);
1022 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
1023 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
1024 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
1025 MsgHdr->MsgLen = XDR_encode_uint32(len);
1026 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
1027 MsgHdr->ReplyPort = 0; // now
1028 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
1029 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
1033 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
1034 const std::string& modelName)
1036 if (0 < mMultiPlayerMap.count(callsign))
1037 return mMultiPlayerMap[callsign].get();
1039 FGAIMultiplayer* mp = new FGAIMultiplayer;
1040 mp->setPath(modelName.c_str());
1041 mp->setCallSign(callsign);
1042 mMultiPlayerMap[callsign] = mp;
1044 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
1048 /// FIXME: that must follow the attach ATM ...
1049 for (unsigned i = 0; i < numProperties; ++i)
1050 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
1057 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
1059 if (0 < mMultiPlayerMap.count(callsign))
1060 return mMultiPlayerMap[callsign].get();