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 //////////////////////////////////////////////////////////////////////
39 #include <simgear/misc/stdint.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/props/props.hxx>
43 #include <simgear/structure/commands.hxx>
45 #include <AIModel/AIManager.hxx>
46 #include <AIModel/AIMultiplayer.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Network/RemoteXMLRequest.hxx>
49 #include <Network/HTTPClient.hxx>
50 #include "multiplaymgr.hxx"
51 #include "mpmessages.hxx"
52 #include <FDM/flightProperties.hxx>
56 #define MAX_PACKET_SIZE 1200
57 #define MAX_TEXT_SIZE 128
59 struct IdPropertyList {
62 simgear::props::Type type;
65 static const IdPropertyList* findProperty(unsigned id);
67 // A static map of protocol property id values to property paths,
68 // This should be extendable dynamically for every specific aircraft ...
69 // For now only that static list
70 static const IdPropertyList sIdPropertyList[] = {
71 {100, "surface-positions/left-aileron-pos-norm", simgear::props::FLOAT},
72 {101, "surface-positions/right-aileron-pos-norm", simgear::props::FLOAT},
73 {102, "surface-positions/elevator-pos-norm", simgear::props::FLOAT},
74 {103, "surface-positions/rudder-pos-norm", simgear::props::FLOAT},
75 {104, "surface-positions/flap-pos-norm", simgear::props::FLOAT},
76 {105, "surface-positions/speedbrake-pos-norm", simgear::props::FLOAT},
77 {106, "gear/tailhook/position-norm", simgear::props::FLOAT},
78 {107, "gear/launchbar/position-norm", simgear::props::FLOAT},
79 {108, "gear/launchbar/state", simgear::props::STRING},
80 {109, "gear/launchbar/holdback-position-norm", simgear::props::FLOAT},
81 {110, "canopy/position-norm", simgear::props::FLOAT},
82 {111, "surface-positions/wing-pos-norm", simgear::props::FLOAT},
83 {112, "surface-positions/wing-fold-pos-norm", simgear::props::FLOAT},
85 {200, "gear/gear[0]/compression-norm", simgear::props::FLOAT},
86 {201, "gear/gear[0]/position-norm", simgear::props::FLOAT},
87 {210, "gear/gear[1]/compression-norm", simgear::props::FLOAT},
88 {211, "gear/gear[1]/position-norm", simgear::props::FLOAT},
89 {220, "gear/gear[2]/compression-norm", simgear::props::FLOAT},
90 {221, "gear/gear[2]/position-norm", simgear::props::FLOAT},
91 {230, "gear/gear[3]/compression-norm", simgear::props::FLOAT},
92 {231, "gear/gear[3]/position-norm", simgear::props::FLOAT},
93 {240, "gear/gear[4]/compression-norm", simgear::props::FLOAT},
94 {241, "gear/gear[4]/position-norm", simgear::props::FLOAT},
96 {300, "engines/engine[0]/n1", simgear::props::FLOAT},
97 {301, "engines/engine[0]/n2", simgear::props::FLOAT},
98 {302, "engines/engine[0]/rpm", simgear::props::FLOAT},
99 {310, "engines/engine[1]/n1", simgear::props::FLOAT},
100 {311, "engines/engine[1]/n2", simgear::props::FLOAT},
101 {312, "engines/engine[1]/rpm", simgear::props::FLOAT},
102 {320, "engines/engine[2]/n1", simgear::props::FLOAT},
103 {321, "engines/engine[2]/n2", simgear::props::FLOAT},
104 {322, "engines/engine[2]/rpm", simgear::props::FLOAT},
105 {330, "engines/engine[3]/n1", simgear::props::FLOAT},
106 {331, "engines/engine[3]/n2", simgear::props::FLOAT},
107 {332, "engines/engine[3]/rpm", simgear::props::FLOAT},
108 {340, "engines/engine[4]/n1", simgear::props::FLOAT},
109 {341, "engines/engine[4]/n2", simgear::props::FLOAT},
110 {342, "engines/engine[4]/rpm", simgear::props::FLOAT},
111 {350, "engines/engine[5]/n1", simgear::props::FLOAT},
112 {351, "engines/engine[5]/n2", simgear::props::FLOAT},
113 {352, "engines/engine[5]/rpm", simgear::props::FLOAT},
114 {360, "engines/engine[6]/n1", simgear::props::FLOAT},
115 {361, "engines/engine[6]/n2", simgear::props::FLOAT},
116 {362, "engines/engine[6]/rpm", simgear::props::FLOAT},
117 {370, "engines/engine[7]/n1", simgear::props::FLOAT},
118 {371, "engines/engine[7]/n2", simgear::props::FLOAT},
119 {372, "engines/engine[7]/rpm", simgear::props::FLOAT},
120 {380, "engines/engine[8]/n1", simgear::props::FLOAT},
121 {381, "engines/engine[8]/n2", simgear::props::FLOAT},
122 {382, "engines/engine[8]/rpm", simgear::props::FLOAT},
123 {390, "engines/engine[9]/n1", simgear::props::FLOAT},
124 {391, "engines/engine[9]/n2", simgear::props::FLOAT},
125 {392, "engines/engine[9]/rpm", simgear::props::FLOAT},
127 {800, "rotors/main/rpm", simgear::props::FLOAT},
128 {801, "rotors/tail/rpm", simgear::props::FLOAT},
129 {810, "rotors/main/blade[0]/position-deg", simgear::props::FLOAT},
130 {811, "rotors/main/blade[1]/position-deg", simgear::props::FLOAT},
131 {812, "rotors/main/blade[2]/position-deg", simgear::props::FLOAT},
132 {813, "rotors/main/blade[3]/position-deg", simgear::props::FLOAT},
133 {820, "rotors/main/blade[0]/flap-deg", simgear::props::FLOAT},
134 {821, "rotors/main/blade[1]/flap-deg", simgear::props::FLOAT},
135 {822, "rotors/main/blade[2]/flap-deg", simgear::props::FLOAT},
136 {823, "rotors/main/blade[3]/flap-deg", simgear::props::FLOAT},
137 {830, "rotors/tail/blade[0]/position-deg", simgear::props::FLOAT},
138 {831, "rotors/tail/blade[1]/position-deg", simgear::props::FLOAT},
140 {900, "sim/hitches/aerotow/tow/length", simgear::props::FLOAT},
141 {901, "sim/hitches/aerotow/tow/elastic-constant", simgear::props::FLOAT},
142 {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m", simgear::props::FLOAT},
143 {903, "sim/hitches/aerotow/tow/dist", simgear::props::FLOAT},
144 {904, "sim/hitches/aerotow/tow/connected-to-property-node", simgear::props::BOOL},
145 {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign", simgear::props::STRING},
146 {906, "sim/hitches/aerotow/tow/brake-force", simgear::props::FLOAT},
147 {907, "sim/hitches/aerotow/tow/end-force-x", simgear::props::FLOAT},
148 {908, "sim/hitches/aerotow/tow/end-force-y", simgear::props::FLOAT},
149 {909, "sim/hitches/aerotow/tow/end-force-z", simgear::props::FLOAT},
150 {930, "sim/hitches/aerotow/is-slave", simgear::props::BOOL},
151 {931, "sim/hitches/aerotow/speed-in-tow-direction", simgear::props::FLOAT},
152 {932, "sim/hitches/aerotow/open", simgear::props::BOOL},
153 {933, "sim/hitches/aerotow/local-pos-x", simgear::props::FLOAT},
154 {934, "sim/hitches/aerotow/local-pos-y", simgear::props::FLOAT},
155 {935, "sim/hitches/aerotow/local-pos-z", simgear::props::FLOAT},
157 {1001, "controls/flight/slats", simgear::props::FLOAT},
158 {1002, "controls/flight/speedbrake", simgear::props::FLOAT},
159 {1003, "controls/flight/spoilers", simgear::props::FLOAT},
160 {1004, "controls/gear/gear-down", simgear::props::FLOAT},
161 {1005, "controls/lighting/nav-lights", simgear::props::FLOAT},
162 {1006, "controls/armament/station[0]/jettison-all", simgear::props::BOOL},
164 {1100, "sim/model/variant", simgear::props::INT},
165 {1101, "sim/model/livery/file", simgear::props::STRING},
167 {1200, "environment/wildfire/data", simgear::props::STRING},
168 {1201, "environment/contrail", simgear::props::INT},
170 {1300, "tanker", simgear::props::INT},
172 {1400, "scenery/events", simgear::props::STRING},
174 {1500, "instrumentation/transponder/transmitted-id", simgear::props::INT},
175 {1501, "instrumentation/transponder/altitude", simgear::props::INT},
176 {1502, "instrumentation/transponder/ident", simgear::props::BOOL},
177 {1503, "instrumentation/transponder/inputs/mode", simgear::props::INT},
179 {10001, "sim/multiplay/transmission-freq-hz", simgear::props::STRING},
180 {10002, "sim/multiplay/chat", simgear::props::STRING},
182 {10100, "sim/multiplay/generic/string[0]", simgear::props::STRING},
183 {10101, "sim/multiplay/generic/string[1]", simgear::props::STRING},
184 {10102, "sim/multiplay/generic/string[2]", simgear::props::STRING},
185 {10103, "sim/multiplay/generic/string[3]", simgear::props::STRING},
186 {10104, "sim/multiplay/generic/string[4]", simgear::props::STRING},
187 {10105, "sim/multiplay/generic/string[5]", simgear::props::STRING},
188 {10106, "sim/multiplay/generic/string[6]", simgear::props::STRING},
189 {10107, "sim/multiplay/generic/string[7]", simgear::props::STRING},
190 {10108, "sim/multiplay/generic/string[8]", simgear::props::STRING},
191 {10109, "sim/multiplay/generic/string[9]", simgear::props::STRING},
192 {10110, "sim/multiplay/generic/string[10]", simgear::props::STRING},
193 {10111, "sim/multiplay/generic/string[11]", simgear::props::STRING},
194 {10112, "sim/multiplay/generic/string[12]", simgear::props::STRING},
195 {10113, "sim/multiplay/generic/string[13]", simgear::props::STRING},
196 {10114, "sim/multiplay/generic/string[14]", simgear::props::STRING},
197 {10115, "sim/multiplay/generic/string[15]", simgear::props::STRING},
198 {10116, "sim/multiplay/generic/string[16]", simgear::props::STRING},
199 {10117, "sim/multiplay/generic/string[17]", simgear::props::STRING},
200 {10118, "sim/multiplay/generic/string[18]", simgear::props::STRING},
201 {10119, "sim/multiplay/generic/string[19]", simgear::props::STRING},
203 {10200, "sim/multiplay/generic/float[0]", simgear::props::FLOAT},
204 {10201, "sim/multiplay/generic/float[1]", simgear::props::FLOAT},
205 {10202, "sim/multiplay/generic/float[2]", simgear::props::FLOAT},
206 {10203, "sim/multiplay/generic/float[3]", simgear::props::FLOAT},
207 {10204, "sim/multiplay/generic/float[4]", simgear::props::FLOAT},
208 {10205, "sim/multiplay/generic/float[5]", simgear::props::FLOAT},
209 {10206, "sim/multiplay/generic/float[6]", simgear::props::FLOAT},
210 {10207, "sim/multiplay/generic/float[7]", simgear::props::FLOAT},
211 {10208, "sim/multiplay/generic/float[8]", simgear::props::FLOAT},
212 {10209, "sim/multiplay/generic/float[9]", simgear::props::FLOAT},
213 {10210, "sim/multiplay/generic/float[10]", simgear::props::FLOAT},
214 {10211, "sim/multiplay/generic/float[11]", simgear::props::FLOAT},
215 {10212, "sim/multiplay/generic/float[12]", simgear::props::FLOAT},
216 {10213, "sim/multiplay/generic/float[13]", simgear::props::FLOAT},
217 {10214, "sim/multiplay/generic/float[14]", simgear::props::FLOAT},
218 {10215, "sim/multiplay/generic/float[15]", simgear::props::FLOAT},
219 {10216, "sim/multiplay/generic/float[16]", simgear::props::FLOAT},
220 {10217, "sim/multiplay/generic/float[17]", simgear::props::FLOAT},
221 {10218, "sim/multiplay/generic/float[18]", simgear::props::FLOAT},
222 {10219, "sim/multiplay/generic/float[19]", simgear::props::FLOAT},
224 {10300, "sim/multiplay/generic/int[0]", simgear::props::INT},
225 {10301, "sim/multiplay/generic/int[1]", simgear::props::INT},
226 {10302, "sim/multiplay/generic/int[2]", simgear::props::INT},
227 {10303, "sim/multiplay/generic/int[3]", simgear::props::INT},
228 {10304, "sim/multiplay/generic/int[4]", simgear::props::INT},
229 {10305, "sim/multiplay/generic/int[5]", simgear::props::INT},
230 {10306, "sim/multiplay/generic/int[6]", simgear::props::INT},
231 {10307, "sim/multiplay/generic/int[7]", simgear::props::INT},
232 {10308, "sim/multiplay/generic/int[8]", simgear::props::INT},
233 {10309, "sim/multiplay/generic/int[9]", simgear::props::INT},
234 {10310, "sim/multiplay/generic/int[10]", simgear::props::INT},
235 {10311, "sim/multiplay/generic/int[11]", simgear::props::INT},
236 {10312, "sim/multiplay/generic/int[12]", simgear::props::INT},
237 {10313, "sim/multiplay/generic/int[13]", simgear::props::INT},
238 {10314, "sim/multiplay/generic/int[14]", simgear::props::INT},
239 {10315, "sim/multiplay/generic/int[15]", simgear::props::INT},
240 {10316, "sim/multiplay/generic/int[16]", simgear::props::INT},
241 {10317, "sim/multiplay/generic/int[17]", simgear::props::INT},
242 {10318, "sim/multiplay/generic/int[18]", simgear::props::INT},
243 {10319, "sim/multiplay/generic/int[19]", simgear::props::INT}
246 const unsigned int numProperties = (sizeof(sIdPropertyList)
247 / sizeof(sIdPropertyList[0]));
249 // Look up a property ID using binary search.
252 struct ComparePropertyId
254 bool operator()(const IdPropertyList& lhs,
255 const IdPropertyList& rhs)
257 return lhs.id < rhs.id;
259 bool operator()(const IdPropertyList& lhs,
264 bool operator()(unsigned id,
265 const IdPropertyList& rhs)
272 const IdPropertyList* findProperty(unsigned id)
274 std::pair<const IdPropertyList*, const IdPropertyList*> result
275 = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,
276 ComparePropertyId());
277 if (result.first == result.second) {
286 bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)
288 using namespace simgear;
289 const xdr_data_t* xdr = data;
291 unsigned id = XDR_decode_uint32(*xdr);
292 const IdPropertyList* plist = findProperty(id);
296 // How we decode the remainder of the property depends on the type
297 switch (plist->type) {
306 float val = XDR_decode_float(*xdr);
307 if (SGMisc<float>::isNaN(val))
313 case props::UNSPECIFIED:
315 // String is complicated. It consists of
316 // The length of the string
318 // Padding to the nearest 4-bytes.
319 // XXX Yes, each byte is padded out to a word! Too late
321 uint32_t length = XDR_decode_uint32(*xdr);
323 // Old versions truncated the string but left the length
325 if (length > MAX_TEXT_SIZE)
326 length = MAX_TEXT_SIZE;
328 // Now handle the padding
329 while ((length % 4) != 0)
338 // cerr << "Unknown Prop type " << id << " " << type << "\n";
344 // give up; this is a malformed property list.
352 class MPPropertyListener : public SGPropertyChangeListener
355 MPPropertyListener(FGMultiplayMgr* mp) :
360 virtual void childAdded(SGPropertyNode*, SGPropertyNode*)
362 _multiplay->setPropertiesChanged();
366 FGMultiplayMgr* _multiplay;
369 //////////////////////////////////////////////////////////////////////
371 // handle command "multiplayer"
372 // command: connect|disconnect|refreshserverlist
375 // server: servername to connect (mandatory)
376 // txport: outgoing port number (default: 5000)
377 // rxport: incoming port number (default: 5000)
382 // refreshserverlist args:
385 //////////////////////////////////////////////////////////////////////
386 static bool do_multiplayer (const SGPropertyNode * arg)
388 FGMultiplayMgr * self = (FGMultiplayMgr*) globals->get_subsystem("mp");
390 const string &command = arg->getStringValue("command", "");
391 SG_LOG(SG_NETWORK, SG_WARN, "do_multiplayer: " << command );
392 if( command == "connect" ) {
393 string servername = arg->getStringValue("servername", "");
394 if( servername.empty() ) {
395 SG_LOG(SG_NETWORK, SG_WARN, "do_multiplayer.connect: no server name given, command ignored." );
398 int port = arg->getIntValue("rxport", -1 );
399 if( port > 0 && port <= 0xffff ) {
400 fgSetInt( "/sim/multiplay/rxport", port );
403 port = arg->getIntValue("txport", -1 );
404 if( port > 0 && port <= 0xffff ) {
405 fgSetInt( "/sim/multiplay/txport", port );
408 servername = servername.substr( 0, servername.find_first_of(' '));
409 fgSetString("/sim/multiplay/txhost", servername );
413 } else if( command == "disconnect" ) {
414 fgSetString("/sim/multiplay/txhost", "");
418 } else if( command == "refreshserverlist" ) {
419 string url(fgGetString("/sim/multiplay/serverlist-url",
420 "http://liveries.flightgear.org/mpstatus/mpservers.xml" ));
423 SG_LOG(SG_IO, SG_ALERT, "do_multiplayer.refreshserverlist: no URL given");
427 FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
429 SG_LOG(SG_IO, SG_ALERT, "do_multiplayer.refreshserverlist: HTTP client not running");
433 SGPropertyNode *targetnode = fgGetNode("/sim/multiplay/server-list", true );
434 SGPropertyNode *completeNode = fgGetNode("/sim/multiplay/got-servers", true);
435 SGPropertyNode *failureNode = fgGetNode("/sim/multiplay/get-servers-failure", true);
436 RemoteXMLRequest* req = new RemoteXMLRequest(url, targetnode);
437 req->setCompletionProp( completeNode );
438 req->setFailedProp( failureNode );
439 completeNode->setBoolValue(false);
440 failureNode->setBoolValue(false);
441 http->makeRequest(req);
445 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr do_multiplayer: command" << command << " unknown. ignored.");
450 //////////////////////////////////////////////////////////////////////
452 // MultiplayMgr constructor
454 //////////////////////////////////////////////////////////////////////
455 FGMultiplayMgr::FGMultiplayMgr()
457 mInitialised = false;
460 globals->get_commands()->addCommand("multiplayer", do_multiplayer);
461 } // FGMultiplayMgr::FGMultiplayMgr()
462 //////////////////////////////////////////////////////////////////////
464 //////////////////////////////////////////////////////////////////////
466 // MultiplayMgr destructor
468 //////////////////////////////////////////////////////////////////////
469 FGMultiplayMgr::~FGMultiplayMgr()
471 globals->get_commands()->removeCommand("multiplayer");
472 } // FGMultiplayMgr::~FGMultiplayMgr()
473 //////////////////////////////////////////////////////////////////////
475 //////////////////////////////////////////////////////////////////////
479 //////////////////////////////////////////////////////////////////////
481 FGMultiplayMgr::init (void)
483 //////////////////////////////////////////////////
484 // Initialise object if not already done
485 //////////////////////////////////////////////////
487 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
491 SGPropertyNode* propOnline = fgGetNode("/sim/multiplay/online", true);
492 propOnline->setBoolValue(false);
493 propOnline->setAttribute(SGPropertyNode::PRESERVE, true);
495 //////////////////////////////////////////////////
496 // Set members from property values
497 //////////////////////////////////////////////////
498 short rxPort = fgGetInt("/sim/multiplay/rxport");
499 string rxAddress = fgGetString("/sim/multiplay/rxhost");
500 short txPort = fgGetInt("/sim/multiplay/txport", 5000);
501 string txAddress = fgGetString("/sim/multiplay/txhost");
503 int hz = fgGetInt("/sim/multiplay/tx-rate-hz", 10);
509 mTimeUntilSend = 0.0;
511 mCallsign = fgGetString("/sim/multiplay/callsign");
512 fgGetNode("/sim/multiplay/callsign", true)->setAttribute(SGPropertyNode::PRESERVE, true);
514 if ((!txAddress.empty()) && (txAddress!="0")) {
515 mServer.set(txAddress.c_str(), txPort);
516 if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {
518 SG_LOG(SG_NETWORK, SG_ALERT,
519 "Cannot enable multiplayer mode: resolving MP server address '"
520 << txAddress << "' failed.");
523 SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr - have server");
529 SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr - multiplayer mode disabled (no MP server specificed).");
534 SG_LOG(SG_NETWORK, SG_ALERT,
535 "Cannot enable multiplayer mode: No receiver port specified.");
538 if (mCallsign.empty())
539 mCallsign = "JohnDoe"; // FIXME: use getpwuid
540 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
541 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
542 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
543 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
544 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
546 mSocket.reset(new simgear::Socket());
547 if (!mSocket->open(false)) {
548 SG_LOG( SG_NETWORK, SG_ALERT,
549 "Cannot enable multiplayer mode: creating data socket failed." );
552 mSocket->setBlocking(false);
553 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
554 SG_LOG( SG_NETWORK, SG_ALERT,
555 "Cannot enable multiplayer mode: binding receive socket failed. "
556 << strerror(errno) << "(errno " << errno << ")");
560 mPropertiesChanged = true;
561 mListener = new MPPropertyListener(this);
562 globals->get_props()->addChangeListener(mListener, false);
564 fgSetBool("/sim/multiplay/online", true);
567 SG_LOG(SG_NETWORK, SG_ALERT, "Multiplayer mode active!");
569 if (!fgGetBool("/sim/ai/enabled"))
571 // multiplayer depends on AI module
572 fgSetBool("/sim/ai/enabled", true);
574 } // FGMultiplayMgr::init()
575 //////////////////////////////////////////////////////////////////////
577 //////////////////////////////////////////////////////////////////////
579 // Closes and deletes the local player object. Closes
580 // and deletes the tx socket. Resets the object state to unitialised.
582 //////////////////////////////////////////////////////////////////////
584 FGMultiplayMgr::shutdown (void)
586 fgSetBool("/sim/multiplay/online", false);
593 MultiPlayerMap::iterator it = mMultiPlayerMap.begin(),
594 end = mMultiPlayerMap.end();
595 for (; it != end; ++it) {
596 it->second->setDie(true);
598 mMultiPlayerMap.clear();
601 globals->get_props()->removeChangeListener(mListener);
606 mInitialised = false;
607 } // FGMultiplayMgr::Close(void)
608 //////////////////////////////////////////////////////////////////////
611 FGMultiplayMgr::reinit()
617 //////////////////////////////////////////////////////////////////////
619 // Description: Sends the position data for the local position.
621 //////////////////////////////////////////////////////////////////////
624 * The buffer that holds a multi-player message, suitably aligned.
626 union FGMultiplayMgr::MsgBuf
630 memset(&Msg, 0, sizeof(Msg));
638 const T_MsgHdr* msgHdr() const
640 return reinterpret_cast<const T_MsgHdr*>(&Header);
643 T_PositionMsg* posMsg()
645 return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
648 const T_PositionMsg* posMsg() const
650 return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
653 xdr_data_t* properties()
655 return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
656 + sizeof(T_PositionMsg));
659 const xdr_data_t* properties() const
661 return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
662 + sizeof(T_PositionMsg));
665 * The end of the properties buffer.
667 xdr_data_t* propsEnd()
669 return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
672 const xdr_data_t* propsEnd() const
674 return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
677 * The end of properties actually in the buffer. This assumes that
678 * the message header is valid.
680 xdr_data_t* propsRecvdEnd()
682 return reinterpret_cast<xdr_data_t*>(Msg + Header.MsgLen);
685 const xdr_data_t* propsRecvdEnd() const
687 return reinterpret_cast<const xdr_data_t*>(Msg + Header.MsgLen);
690 xdr_data2_t double_val;
691 char Msg[MAX_PACKET_SIZE];
696 FGMultiplayMgr::isSane(const FGExternalMotionData& motionInfo)
698 // check for corrupted data (NaNs)
699 bool isCorrupted = false;
700 isCorrupted |= ((SGMisc<double>::isNaN(motionInfo.time )) ||
701 (SGMisc<double>::isNaN(motionInfo.lag )) ||
702 (osg::isNaN(motionInfo.orientation(3) )));
703 for (unsigned i = 0; (i < 3)&&(!isCorrupted); ++i)
705 isCorrupted |= ((osg::isNaN(motionInfo.position(i) ))||
706 (osg::isNaN(motionInfo.orientation(i) ))||
707 (osg::isNaN(motionInfo.linearVel(i)) )||
708 (osg::isNaN(motionInfo.angularVel(i)) )||
709 (osg::isNaN(motionInfo.linearAccel(i)) )||
710 (osg::isNaN(motionInfo.angularAccel(i)) ));
716 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
718 if ((! mInitialised) || (! mHaveServer))
722 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");
726 if (!isSane(motionInfo))
728 // Current local data is invalid (NaN), so stop MP transmission.
729 // => Be nice to older FG versions (no NaN checks) and don't waste bandwidth.
730 SG_LOG(SG_NETWORK, SG_ALERT, "FGMultiplayMgr::SendMyPosition - "
731 << "Local data is invalid (NaN). Data not transmitted.");
735 static MsgBuf msgBuf;
736 static unsigned msgLen = 0;
737 T_PositionMsg* PosMsg = msgBuf.posMsg();
739 strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
740 PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
741 if (fgGetBool("/sim/freeze/replay-state", true)&&
742 fgGetBool("/sim/multiplay/freeze-on-replay",true))
744 // do not send position updates during replay
745 for (unsigned i = 0 ; i < 3; ++i)
747 // no movement during replay
748 PosMsg->linearVel[i] = XDR_encode_float (0.0);
749 PosMsg->angularVel[i] = XDR_encode_float (0.0);
750 PosMsg->linearAccel[i] = XDR_encode_float (0.0);
751 PosMsg->angularAccel[i] = XDR_encode_float (0.0);
753 // all other data remains unchanged (resend last state)
757 PosMsg->time = XDR_encode_double (motionInfo.time);
758 PosMsg->lag = XDR_encode_double (motionInfo.lag);
759 for (unsigned i = 0 ; i < 3; ++i)
760 PosMsg->position[i] = XDR_encode_double (motionInfo.position(i));
762 motionInfo.orientation.getAngleAxis(angleAxis);
763 for (unsigned i = 0 ; i < 3; ++i)
764 PosMsg->orientation[i] = XDR_encode_float (angleAxis(i));
765 for (unsigned i = 0 ; i < 3; ++i)
766 PosMsg->linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
767 for (unsigned i = 0 ; i < 3; ++i)
768 PosMsg->angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
769 for (unsigned i = 0 ; i < 3; ++i)
770 PosMsg->linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
771 for (unsigned i = 0 ; i < 3; ++i)
772 PosMsg->angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
774 xdr_data_t* ptr = msgBuf.properties();
775 std::vector<FGPropertyData*>::const_iterator it;
776 it = motionInfo.properties.begin();
777 //cout << "OUTPUT PROPERTIES\n";
778 xdr_data_t* msgEnd = msgBuf.propsEnd();
779 while (it != motionInfo.properties.end() && ptr + 2 < msgEnd) {
781 // First element is the ID. Write it out when we know we have room for
782 // the whole property.
783 xdr_data_t id = XDR_encode_uint32((*it)->id);
784 // The actual data representation depends on the type
785 switch ((*it)->type) {
786 case simgear::props::INT:
787 case simgear::props::BOOL:
788 case simgear::props::LONG:
790 *ptr++ = XDR_encode_uint32((*it)->int_value);
791 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
793 case simgear::props::FLOAT:
794 case simgear::props::DOUBLE:
796 *ptr++ = XDR_encode_float((*it)->float_value);
797 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
799 case simgear::props::STRING:
800 case simgear::props::UNSPECIFIED:
802 // String is complicated. It consists of
803 // The length of the string
805 // Padding to the nearest 4-bytes.
806 const char* lcharptr = (*it)->string_value;
811 ////cout << "String length: " << strlen(lcharptr) << "\n";
812 uint32_t len = strlen(lcharptr);
813 if (len > MAX_TEXT_SIZE)
815 // XXX This should not be using 4 bytes per character!
816 // If there's not enough room for this property, drop it
818 if (ptr + 2 + ((len + 3) & ~3) > msgEnd)
820 //cout << "String length unint32: " << len << "\n";
822 *ptr++ = XDR_encode_uint32(len);
825 // Now the text itself
826 // XXX This should not be using 4 bytes per character!
828 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
830 *ptr++ = XDR_encode_int8(*lcharptr);
835 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
837 // Now pad if required
838 while ((lcount % 4) != 0)
840 *ptr++ = XDR_encode_int8(0);
852 *ptr++ = XDR_encode_uint32(0);
853 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
859 //cout << " Unknown Type: " << (*it)->type << "\n";
861 *ptr++ = XDR_encode_float((*it)->float_value);;
862 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
869 msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;
870 FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);
873 mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);
874 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
875 } // FGMultiplayMgr::SendMyPosition()
877 //////////////////////////////////////////////////////////////////////
879 //////////////////////////////////////////////////////////////////////
881 // Name: SendTextMessage
882 // Description: Sends a message to the player. The message must
883 // contain a valid and correctly filled out header and optional
886 //////////////////////////////////////////////////////////////////////
888 FGMultiplayMgr::SendTextMessage(const string &MsgText)
890 if (!mInitialised || !mHaveServer)
894 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
895 //////////////////////////////////////////////////
896 // Divide the text string into blocks that fit
897 // in the message and send the blocks.
898 //////////////////////////////////////////////////
899 unsigned iNextBlockPosition = 0;
902 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
903 while (iNextBlockPosition < MsgText.length()) {
904 strncpy (ChatMsg.Text,
905 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
907 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
908 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
909 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
910 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
911 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
916 } // FGMultiplayMgr::SendTextMessage ()
917 //////////////////////////////////////////////////////////////////////
919 //////////////////////////////////////////////////////////////////////
922 // Description: Processes data waiting at the receive socket. The
923 // processing ends when there is no more data at the socket.
925 //////////////////////////////////////////////////////////////////////
927 FGMultiplayMgr::update(double dt)
933 long stamp = SGTimeStamp::now().getSeconds();
935 //////////////////////////////////////////////////
937 //////////////////////////////////////////////////
938 mTimeUntilSend -= dt;
939 if (mTimeUntilSend <= 0.0) {
943 //////////////////////////////////////////////////
944 // Read the receive socket and process any data
945 //////////////////////////////////////////////////
949 //////////////////////////////////////////////////
950 // Although the recv call asks for
951 // MAX_PACKET_SIZE of data, the number of bytes
952 // returned will only be that of the next
953 // packet waiting to be processed.
954 //////////////////////////////////////////////////
955 simgear::IPAddress SenderAddress;
956 int RecvStatus = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,
958 //////////////////////////////////////////////////
960 //////////////////////////////////////////////////
964 // socket error reported?
965 // errno isn't thread-safe - so only check its value when
966 // socket return status < 0 really indicates a failure.
967 if ((RecvStatus < 0)&&
968 ((errno == EAGAIN) || (errno == 0))) // MSVC output "NoError" otherwise
970 // ignore "normal" errors
976 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - Unable to receive data. "
977 << strerror(errno) << "(errno " << errno << ")");
981 // status is positive: bytes received
982 bytes = (ssize_t) RecvStatus;
983 if (bytes <= static_cast<ssize_t>(sizeof(T_MsgHdr))) {
984 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
985 << "received message with insufficient data" );
988 //////////////////////////////////////////////////
990 //////////////////////////////////////////////////
991 T_MsgHdr* MsgHdr = msgBuf.msgHdr();
992 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
993 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
994 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
995 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
996 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
997 MsgHdr->Callsign[MAX_CALLSIGN_LEN -1] = '\0';
998 if (MsgHdr->Magic != MSG_MAGIC) {
999 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1000 << "message has invalid magic number!" );
1003 if (MsgHdr->Version != PROTO_VER) {
1004 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1005 << "message has invalid protocol number!" );
1008 if (static_cast<ssize_t>(MsgHdr->MsgLen) != bytes) {
1009 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1010 << "message from " << MsgHdr->Callsign << " has invalid length!");
1013 //////////////////////////////////////////////////
1015 //////////////////////////////////////////////////
1016 switch (MsgHdr->MsgId) {
1018 ProcessChatMsg(msgBuf, SenderAddress);
1021 ProcessPosMsg(msgBuf, SenderAddress, stamp);
1023 case UNUSABLE_POS_DATA_ID:
1024 case OLD_OLD_POS_DATA_ID:
1025 case OLD_PROP_MSG_ID:
1026 case OLD_POS_DATA_ID:
1029 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1030 << "Unknown message Id received: " << MsgHdr->MsgId );
1033 } while (bytes > 0);
1036 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
1037 while (it != mMultiPlayerMap.end()) {
1038 if (it->second->getLastTimestamp() + 10 < stamp) {
1039 std::string name = it->first;
1040 it->second->setDie(true);
1041 mMultiPlayerMap.erase(it);
1042 it = mMultiPlayerMap.upper_bound(name);
1046 } // FGMultiplayMgr::ProcessData(void)
1047 //////////////////////////////////////////////////////////////////////
1050 FGMultiplayMgr::Send()
1052 using namespace simgear;
1056 // smooth the send rate, by adjusting based on the 'remainder' time, which
1057 // is how -ve mTimeUntilSend is. Watch for large values and ignore them,
1059 if ((mTimeUntilSend < 0.0) && (fabs(mTimeUntilSend) < mDt)) {
1060 mTimeUntilSend = mDt + mTimeUntilSend;
1062 mTimeUntilSend = mDt;
1065 double sim_time = globals->get_sim_time_sec();
1066 // static double lastTime = 0.0;
1068 // SG_LOG(SG_GENERAL, SG_INFO, "actual dt=" << sim_time - lastTime);
1069 // lastTime = sim_time;
1071 FlightProperties ifce;
1073 // put together a motion info struct, you will get that later
1074 // from FGInterface directly ...
1075 FGExternalMotionData motionInfo;
1077 // The current simulation time we need to update for,
1078 // note that the simulation time is updated before calling all the
1079 // update methods. Thus it contains the time intervals *end* time.
1080 // The FDM is already run, so the states belong to that time.
1081 motionInfo.time = sim_time;
1082 motionInfo.lag = mDt;
1084 // These are for now converted from lat/lon/alt and euler angles.
1085 // But this should change in FGInterface ...
1086 double lon = ifce.get_Longitude();
1087 double lat = ifce.get_Latitude();
1088 // first the aprioriate structure for the geodetic one
1089 SGGeod geod = SGGeod::fromRadFt(lon, lat, ifce.get_Altitude());
1090 // Convert to cartesion coordinate
1091 motionInfo.position = SGVec3d::fromGeod(geod);
1093 // The quaternion rotating from the earth centered frame to the
1094 // horizontal local frame
1095 SGQuatf qEc2Hl = SGQuatf::fromLonLatRad((float)lon, (float)lat);
1096 // The orientation wrt the horizontal local frame
1097 float heading = ifce.get_Psi();
1098 float pitch = ifce.get_Theta();
1099 float roll = ifce.get_Phi();
1100 SGQuatf hlOr = SGQuatf::fromYawPitchRoll(heading, pitch, roll);
1101 // The orientation of the vehicle wrt the earth centered frame
1102 motionInfo.orientation = qEc2Hl*hlOr;
1104 if (!globals->get_subsystem("flight")->is_suspended()) {
1106 motionInfo.linearVel = SG_FEET_TO_METER*SGVec3f(ifce.get_uBody(),
1109 motionInfo.angularVel = SGVec3f(ifce.get_P_body(),
1113 // accels, set that to zero for now.
1114 // Angular accelerations are missing from the interface anyway,
1115 // linear accelerations are screwed up at least for JSBSim.
1116 // motionInfo.linearAccel = SG_FEET_TO_METER*SGVec3f(ifce.get_U_dot_body(),
1117 // ifce.get_V_dot_body(),
1118 // ifce.get_W_dot_body());
1119 motionInfo.linearAccel = SGVec3f::zeros();
1120 motionInfo.angularAccel = SGVec3f::zeros();
1122 // if the interface is suspendend, prevent the client from
1123 // wild extrapolations
1124 motionInfo.linearVel = SGVec3f::zeros();
1125 motionInfo.angularVel = SGVec3f::zeros();
1126 motionInfo.linearAccel = SGVec3f::zeros();
1127 motionInfo.angularAccel = SGVec3f::zeros();
1130 // now send the properties
1131 PropertyMap::iterator it;
1132 for (it = mPropertyMap.begin(); it != mPropertyMap.end(); ++it) {
1133 FGPropertyData* pData = new FGPropertyData;
1134 pData->id = it->first;
1135 pData->type = it->second->getType();
1137 switch (pData->type) {
1141 pData->int_value = it->second->getIntValue();
1145 pData->float_value = it->second->getFloatValue();
1148 case props::UNSPECIFIED:
1150 // FIXME: We assume unspecified are strings for the moment.
1152 const char* cstr = it->second->getStringValue();
1153 int len = strlen(cstr);
1157 pData->string_value = new char[len + 1];
1158 strcpy(pData->string_value, cstr);
1163 pData->string_value = 0;
1166 //cout << " Sending property " << pData->id << " " << pData->type << " " << pData->string_value << "\n";
1170 // FIXME Currently default to a float.
1171 //cout << "Unknown type when iterating through props: " << pData->type << "\n";
1172 pData->float_value = it->second->getFloatValue();
1176 motionInfo.properties.push_back(pData);
1179 SendMyPosition(motionInfo);
1183 //////////////////////////////////////////////////////////////////////
1185 // handle a position message
1187 //////////////////////////////////////////////////////////////////////
1189 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,
1190 const simgear::IPAddress& SenderAddress, long stamp)
1192 const T_MsgHdr* MsgHdr = Msg.msgHdr();
1193 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
1194 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1195 << "Position message received with insufficient data" );
1198 const T_PositionMsg* PosMsg = Msg.posMsg();
1199 FGExternalMotionData motionInfo;
1200 motionInfo.time = XDR_decode_double(PosMsg->time);
1201 motionInfo.lag = XDR_decode_double(PosMsg->lag);
1202 for (unsigned i = 0; i < 3; ++i)
1203 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
1205 for (unsigned i = 0; i < 3; ++i)
1206 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
1207 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
1208 for (unsigned i = 0; i < 3; ++i)
1209 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
1210 for (unsigned i = 0; i < 3; ++i)
1211 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
1212 for (unsigned i = 0; i < 3; ++i)
1213 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
1214 for (unsigned i = 0; i < 3; ++i)
1215 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
1217 // sanity check: do not allow injection of corrupted data (NaNs)
1218 if (!isSane(motionInfo))
1220 // drop this message, keep old position until receiving valid data
1221 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::ProcessPosMsg - "
1222 << "Position message with invalid data (NaN) received from "
1223 << MsgHdr->Callsign);
1227 //cout << "INPUT MESSAGE\n";
1229 // There was a bug in 1.9.0 and before: T_PositionMsg was 196 bytes
1230 // on 32 bit architectures and 200 bytes on 64 bit, and this
1231 // structure is put directly on the wire. By looking at the padding,
1232 // we can sort through the mess, mostly:
1233 // If padding is 0 (which is not a valid property type), then the
1234 // message was produced by a new client or an old 64 bit client that
1235 // happened to have 0 on the stack;
1236 // Else if the property list starting with the padding word is
1237 // well-formed, then the client is probably an old 32 bit client and
1238 // we'll go with that;
1239 // Else it is an old 64-bit client and properties start after the
1241 // There is a chance that we could be fooled by garbage in the
1242 // padding looking like a valid property, so verifyProperties() is
1243 // strict about the validity of the property values.
1244 const xdr_data_t* xdr = Msg.properties();
1245 if (PosMsg->pad != 0) {
1246 if (verifyProperties(&PosMsg->pad, Msg.propsRecvdEnd()))
1248 else if (!verifyProperties(xdr, Msg.propsRecvdEnd()))
1251 while (xdr < Msg.propsRecvdEnd()) {
1252 // simgear::props::Type type = simgear::props::UNSPECIFIED;
1254 // First element is always the ID
1255 unsigned id = XDR_decode_uint32(*xdr);
1256 //cout << pData->id << " ";
1259 // Check the ID actually exists and get the type
1260 const IdPropertyList* plist = findProperty(id);
1264 FGPropertyData* pData = new FGPropertyData;
1266 pData->type = plist->type;
1267 // How we decode the remainder of the property depends on the type
1268 switch (pData->type) {
1269 case simgear::props::INT:
1270 case simgear::props::BOOL:
1271 case simgear::props::LONG:
1272 pData->int_value = XDR_decode_uint32(*xdr);
1274 //cout << pData->int_value << "\n";
1276 case simgear::props::FLOAT:
1277 case simgear::props::DOUBLE:
1278 pData->float_value = XDR_decode_float(*xdr);
1280 //cout << pData->float_value << "\n";
1282 case simgear::props::STRING:
1283 case simgear::props::UNSPECIFIED:
1285 // String is complicated. It consists of
1286 // The length of the string
1287 // The string itself
1288 // Padding to the nearest 4-bytes.
1289 uint32_t length = XDR_decode_uint32(*xdr);
1291 //cout << length << " ";
1292 // Old versions truncated the string but left the length unadjusted.
1293 if (length > MAX_TEXT_SIZE)
1294 length = MAX_TEXT_SIZE;
1295 pData->string_value = new char[length + 1];
1296 //cout << " String: ";
1297 for (unsigned i = 0; i < length; i++)
1299 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
1301 //cout << pData->string_value[i];
1304 pData->string_value[length] = '\0';
1306 // Now handle the padding
1307 while ((length % 4) != 0)
1318 pData->float_value = XDR_decode_float(*xdr);
1319 SG_LOG(SG_NETWORK, SG_DEBUG, "Unknown Prop type " << pData->id << " " << pData->type);
1324 motionInfo.properties.push_back(pData);
1328 // We failed to find the property. We'll try the next packet immediately.
1329 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::ProcessPosMsg - "
1330 "message from " << MsgHdr->Callsign << " has unknown property id "
1335 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
1337 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
1338 mp->addMotionInfo(motionInfo, stamp);
1339 } // FGMultiplayMgr::ProcessPosMsg()
1340 //////////////////////////////////////////////////////////////////////
1342 //////////////////////////////////////////////////////////////////////
1344 // handle a chat message
1345 // FIXME: display chat message within flightgear
1347 //////////////////////////////////////////////////////////////////////
1349 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
1350 const simgear::IPAddress& SenderAddress)
1352 const T_MsgHdr* MsgHdr = Msg.msgHdr();
1353 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
1354 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1355 << "Chat message received with insufficient data" );
1359 char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
1360 const T_ChatMsg* ChatMsg
1361 = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));
1362 strncpy(chatStr, ChatMsg->Text,
1363 MsgHdr->MsgLen - sizeof(T_MsgHdr));
1364 chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
1366 SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"
1370 } // FGMultiplayMgr::ProcessChatMsg ()
1371 //////////////////////////////////////////////////////////////////////
1374 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
1379 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
1385 len = sizeof(T_MsgHdr);
1388 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
1389 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
1390 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
1391 MsgHdr->MsgLen = XDR_encode_uint32(len);
1392 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
1393 MsgHdr->ReplyPort = 0; // now
1394 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
1395 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
1399 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
1400 const std::string& modelName)
1402 if (0 < mMultiPlayerMap.count(callsign))
1403 return mMultiPlayerMap[callsign].get();
1405 FGAIMultiplayer* mp = new FGAIMultiplayer;
1406 mp->setPath(modelName.c_str());
1407 mp->setCallSign(callsign);
1408 mMultiPlayerMap[callsign] = mp;
1410 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai-model");
1414 /// FIXME: that must follow the attach ATM ...
1415 for (unsigned i = 0; i < numProperties; ++i)
1416 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
1423 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
1425 if (0 < mMultiPlayerMap.count(callsign))
1426 return mMultiPlayerMap[callsign].get();
1432 FGMultiplayMgr::findProperties()
1434 if (!mPropertiesChanged) {
1438 mPropertiesChanged = false;
1440 for (unsigned i = 0; i < numProperties; ++i) {
1441 const char* name = sIdPropertyList[i].name;
1442 SGPropertyNode* pNode = globals->get_props()->getNode(name);
1447 int id = sIdPropertyList[i].id;
1448 if (mPropertyMap.find(id) != mPropertyMap.end()) {
1449 continue; // already activated
1452 mPropertyMap[id] = pNode;
1453 SG_LOG(SG_NETWORK, SG_DEBUG, "activating MP property:" << pNode->getPath());