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
163 {1300, "tanker", simgear::props::INT},
\r
165 {10001, "sim/multiplay/transmission-freq-hz", simgear::props::STRING},
\r
166 {10002, "sim/multiplay/chat", simgear::props::STRING},
\r
168 {10100, "sim/multiplay/generic/string[0]", simgear::props::STRING},
\r
169 {10101, "sim/multiplay/generic/string[1]", simgear::props::STRING},
\r
170 {10102, "sim/multiplay/generic/string[2]", simgear::props::STRING},
\r
171 {10103, "sim/multiplay/generic/string[3]", simgear::props::STRING},
\r
172 {10104, "sim/multiplay/generic/string[4]", simgear::props::STRING},
\r
173 {10105, "sim/multiplay/generic/string[5]", simgear::props::STRING},
\r
174 {10106, "sim/multiplay/generic/string[6]", simgear::props::STRING},
\r
175 {10107, "sim/multiplay/generic/string[7]", simgear::props::STRING},
\r
176 {10108, "sim/multiplay/generic/string[8]", simgear::props::STRING},
\r
177 {10109, "sim/multiplay/generic/string[9]", simgear::props::STRING},
\r
178 {10110, "sim/multiplay/generic/string[10]", simgear::props::STRING},
\r
179 {10111, "sim/multiplay/generic/string[11]", simgear::props::STRING},
\r
180 {10112, "sim/multiplay/generic/string[12]", simgear::props::STRING},
\r
181 {10113, "sim/multiplay/generic/string[13]", simgear::props::STRING},
\r
182 {10114, "sim/multiplay/generic/string[14]", simgear::props::STRING},
\r
183 {10115, "sim/multiplay/generic/string[15]", simgear::props::STRING},
\r
184 {10116, "sim/multiplay/generic/string[16]", simgear::props::STRING},
\r
185 {10117, "sim/multiplay/generic/string[17]", simgear::props::STRING},
\r
186 {10118, "sim/multiplay/generic/string[18]", simgear::props::STRING},
\r
187 {10119, "sim/multiplay/generic/string[19]", simgear::props::STRING},
\r
189 {10200, "sim/multiplay/generic/float[0]", simgear::props::FLOAT},
\r
190 {10201, "sim/multiplay/generic/float[1]", simgear::props::FLOAT},
\r
191 {10202, "sim/multiplay/generic/float[2]", simgear::props::FLOAT},
\r
192 {10203, "sim/multiplay/generic/float[3]", simgear::props::FLOAT},
\r
193 {10204, "sim/multiplay/generic/float[4]", simgear::props::FLOAT},
\r
194 {10205, "sim/multiplay/generic/float[5]", simgear::props::FLOAT},
\r
195 {10206, "sim/multiplay/generic/float[6]", simgear::props::FLOAT},
\r
196 {10207, "sim/multiplay/generic/float[7]", simgear::props::FLOAT},
\r
197 {10208, "sim/multiplay/generic/float[8]", simgear::props::FLOAT},
\r
198 {10209, "sim/multiplay/generic/float[9]", simgear::props::FLOAT},
\r
199 {10210, "sim/multiplay/generic/float[10]", simgear::props::FLOAT},
\r
200 {10211, "sim/multiplay/generic/float[11]", simgear::props::FLOAT},
\r
201 {10212, "sim/multiplay/generic/float[12]", simgear::props::FLOAT},
\r
202 {10213, "sim/multiplay/generic/float[13]", simgear::props::FLOAT},
\r
203 {10214, "sim/multiplay/generic/float[14]", simgear::props::FLOAT},
\r
204 {10215, "sim/multiplay/generic/float[15]", simgear::props::FLOAT},
\r
205 {10216, "sim/multiplay/generic/float[16]", simgear::props::FLOAT},
\r
206 {10217, "sim/multiplay/generic/float[17]", simgear::props::FLOAT},
\r
207 {10218, "sim/multiplay/generic/float[18]", simgear::props::FLOAT},
\r
208 {10219, "sim/multiplay/generic/float[19]", simgear::props::FLOAT},
\r
210 {10300, "sim/multiplay/generic/int[0]", simgear::props::INT},
\r
211 {10301, "sim/multiplay/generic/int[1]", simgear::props::INT},
\r
212 {10302, "sim/multiplay/generic/int[2]", simgear::props::INT},
\r
213 {10303, "sim/multiplay/generic/int[3]", simgear::props::INT},
\r
214 {10304, "sim/multiplay/generic/int[4]", simgear::props::INT},
\r
215 {10305, "sim/multiplay/generic/int[5]", simgear::props::INT},
\r
216 {10306, "sim/multiplay/generic/int[6]", simgear::props::INT},
\r
217 {10307, "sim/multiplay/generic/int[7]", simgear::props::INT},
\r
218 {10308, "sim/multiplay/generic/int[8]", simgear::props::INT},
\r
219 {10309, "sim/multiplay/generic/int[9]", simgear::props::INT},
\r
220 {10310, "sim/multiplay/generic/int[10]", simgear::props::INT},
\r
221 {10311, "sim/multiplay/generic/int[11]", simgear::props::INT},
\r
222 {10312, "sim/multiplay/generic/int[12]", simgear::props::INT},
\r
223 {10313, "sim/multiplay/generic/int[13]", simgear::props::INT},
\r
224 {10314, "sim/multiplay/generic/int[14]", simgear::props::INT},
\r
225 {10315, "sim/multiplay/generic/int[15]", simgear::props::INT},
\r
226 {10316, "sim/multiplay/generic/int[16]", simgear::props::INT},
\r
227 {10317, "sim/multiplay/generic/int[17]", simgear::props::INT},
\r
228 {10318, "sim/multiplay/generic/int[18]", simgear::props::INT},
\r
229 {10319, "sim/multiplay/generic/int[19]", simgear::props::INT}
\r
233 FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)
\r
234 / sizeof(FGMultiplayMgr::sIdPropertyList[0]));
\r
236 // Look up a property ID using binary search.
\r
239 struct ComparePropertyId
\r
241 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
\r
242 const FGMultiplayMgr::IdPropertyList& rhs)
\r
244 return lhs.id < rhs.id;
\r
246 bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
\r
249 return lhs.id < id;
\r
251 bool operator()(unsigned id,
\r
252 const FGMultiplayMgr::IdPropertyList& rhs)
\r
254 return id < rhs.id;
\r
259 const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)
\r
261 std::pair<const IdPropertyList*, const IdPropertyList*> result
\r
262 = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,
\r
263 ComparePropertyId());
\r
264 if (result.first == result.second) {
\r
267 return result.first;
\r
273 bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)
\r
275 using namespace simgear;
\r
276 const xdr_data_t* xdr = data;
\r
277 while (xdr < end) {
\r
278 unsigned id = XDR_decode_uint32(*xdr);
\r
279 const FGMultiplayMgr::IdPropertyList* plist
\r
280 = FGMultiplayMgr::findProperty(id);
\r
284 // How we decode the remainder of the property depends on the type
\r
285 switch (plist->type) {
\r
292 case props::DOUBLE:
\r
294 float val = XDR_decode_float(*xdr);
\r
295 if (osg::isNaN(val))
\r
300 case props::STRING:
\r
301 case props::UNSPECIFIED:
\r
303 // String is complicated. It consists of
\r
304 // The length of the string
\r
305 // The string itself
\r
306 // Padding to the nearest 4-bytes.
\r
307 // XXX Yes, each byte is padded out to a word! Too late
\r
309 uint32_t length = XDR_decode_uint32(*xdr);
\r
311 // Old versions truncated the string but left the length
\r
313 if (length > MAX_TEXT_SIZE)
\r
314 length = MAX_TEXT_SIZE;
\r
316 // Now handle the padding
\r
317 while ((length % 4) != 0)
\r
326 // cerr << "Unknown Prop type " << id << " " << type << "\n";
\r
332 // give up; this is a malformed property list.
\r
339 //////////////////////////////////////////////////////////////////////
\r
341 // MultiplayMgr constructor
\r
343 //////////////////////////////////////////////////////////////////////
\r
344 FGMultiplayMgr::FGMultiplayMgr()
\r
347 mInitialised = false;
\r
348 mHaveServer = false;
\r
349 } // FGMultiplayMgr::FGMultiplayMgr()
\r
350 //////////////////////////////////////////////////////////////////////
\r
352 //////////////////////////////////////////////////////////////////////
\r
354 // MultiplayMgr destructor
\r
356 //////////////////////////////////////////////////////////////////////
\r
357 FGMultiplayMgr::~FGMultiplayMgr()
\r
360 } // FGMultiplayMgr::~FGMultiplayMgr()
\r
361 //////////////////////////////////////////////////////////////////////
\r
363 //////////////////////////////////////////////////////////////////////
\r
365 // Initialise object
\r
367 //////////////////////////////////////////////////////////////////////
\r
369 FGMultiplayMgr::init (void)
\r
371 //////////////////////////////////////////////////
\r
372 // Initialise object if not already done
\r
373 //////////////////////////////////////////////////
\r
374 if (mInitialised) {
\r
375 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
\r
378 //////////////////////////////////////////////////
\r
379 // Set members from property values
\r
380 //////////////////////////////////////////////////
\r
381 short rxPort = fgGetInt("/sim/multiplay/rxport");
\r
382 string rxAddress = fgGetString("/sim/multiplay/rxhost");
\r
383 short txPort = fgGetInt("/sim/multiplay/txport");
\r
384 string txAddress = fgGetString("/sim/multiplay/txhost");
\r
385 mCallsign = fgGetString("/sim/multiplay/callsign");
\r
386 if (txPort > 0 && !txAddress.empty()) {
\r
387 mServer.set(txAddress.c_str(), txPort);
\r
388 if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {
\r
389 mHaveServer = false;
\r
390 SG_LOG(SG_NETWORK, SG_DEBUG,
\r
391 "FGMultiplayMgr - could not resolve '"
\r
392 << txAddress << "', Multiplayermode disabled");
\r
394 mHaveServer = true;
\r
400 SG_LOG(SG_NETWORK, SG_DEBUG,
\r
401 "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
\r
404 if (mCallsign.empty())
\r
405 mCallsign = "JohnDoe"; // FIXME: use getpwuid
\r
406 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
\r
407 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
\r
408 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
\r
409 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
\r
410 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
\r
411 Close(); // Should Init be called twice, close Socket first
\r
412 // A memory leak was reported here by valgrind
\r
413 mSocket = new netSocket();
\r
414 if (!mSocket->open(false)) {
\r
415 SG_LOG( SG_NETWORK, SG_DEBUG,
\r
416 "FGMultiplayMgr::init - Failed to create data socket" );
\r
419 mSocket->setBlocking(false);
\r
420 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
\r
422 SG_LOG( SG_NETWORK, SG_DEBUG,
\r
423 "FGMultiplayMgr::Open - Failed to bind receive socket" );
\r
426 mInitialised = true;
\r
428 } // FGMultiplayMgr::init()
\r
429 //////////////////////////////////////////////////////////////////////
\r
431 //////////////////////////////////////////////////////////////////////
\r
433 // Closes and deletes the local player object. Closes
\r
434 // and deletes the tx socket. Resets the object state to unitialised.
\r
436 //////////////////////////////////////////////////////////////////////
\r
438 FGMultiplayMgr::Close (void)
\r
440 mMultiPlayerMap.clear();
\r
447 mInitialised = false;
\r
448 } // FGMultiplayMgr::Close(void)
\r
449 //////////////////////////////////////////////////////////////////////
\r
451 //////////////////////////////////////////////////////////////////////
\r
453 // Description: Sends the position data for the local position.
\r
455 //////////////////////////////////////////////////////////////////////
\r
458 * The buffer that holds a multi-player message, suitably aligned.
\r
460 union FGMultiplayMgr::MsgBuf
\r
464 memset(&Msg, 0, sizeof(Msg));
\r
469 return reinterpret_cast<T_MsgHdr*>(Msg);
\r
472 const T_MsgHdr* msgHdr() const
\r
474 return reinterpret_cast<const T_MsgHdr*>(Msg);
\r
477 T_PositionMsg* posMsg()
\r
479 return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
\r
482 const T_PositionMsg* posMsg() const
\r
484 return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
\r
487 xdr_data_t* properties()
\r
489 return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
\r
490 + sizeof(T_PositionMsg));
\r
493 const xdr_data_t* properties() const
\r
495 return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
\r
496 + sizeof(T_PositionMsg));
\r
499 * The end of the properties buffer.
\r
501 xdr_data_t* propsEnd()
\r
503 return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
\r
506 const xdr_data_t* propsEnd() const
\r
508 return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
\r
511 * The end of properties actually in the buffer. This assumes that
\r
512 * the message header is valid.
\r
514 xdr_data_t* propsRecvdEnd()
\r
516 return reinterpret_cast<xdr_data_t*>(Msg + msgHdr()->MsgLen);
\r
519 const xdr_data_t* propsRecvdEnd() const
\r
521 return reinterpret_cast<const xdr_data_t*>(Msg + msgHdr()->MsgLen);
\r
524 xdr_data2_t double_val;
\r
525 char Msg[MAX_PACKET_SIZE];
\r
529 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
\r
531 if ((! mInitialised) || (! mHaveServer))
\r
533 if (! mHaveServer) {
\r
534 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");
\r
539 T_PositionMsg* PosMsg = msgBuf.posMsg();
\r
541 strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
\r
542 PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
\r
544 PosMsg->time = XDR_encode_double (motionInfo.time);
\r
545 PosMsg->lag = XDR_encode_double (motionInfo.lag);
\r
546 for (unsigned i = 0 ; i < 3; ++i)
\r
547 PosMsg->position[i] = XDR_encode_double (motionInfo.position(i));
\r
549 motionInfo.orientation.getAngleAxis(angleAxis);
\r
550 for (unsigned i = 0 ; i < 3; ++i)
\r
551 PosMsg->orientation[i] = XDR_encode_float (angleAxis(i));
\r
552 for (unsigned i = 0 ; i < 3; ++i)
\r
553 PosMsg->linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
\r
554 for (unsigned i = 0 ; i < 3; ++i)
\r
555 PosMsg->angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
\r
556 for (unsigned i = 0 ; i < 3; ++i)
\r
557 PosMsg->linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
\r
558 for (unsigned i = 0 ; i < 3; ++i)
\r
559 PosMsg->angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
\r
561 xdr_data_t* ptr = msgBuf.properties();
\r
562 std::vector<FGPropertyData*>::const_iterator it;
\r
563 it = motionInfo.properties.begin();
\r
564 //cout << "OUTPUT PROPERTIES\n";
\r
565 xdr_data_t* msgEnd = msgBuf.propsEnd();
\r
566 while (it != motionInfo.properties.end() && ptr + 2 < msgEnd) {
\r
568 // First element is the ID. Write it out when we know we have room for
\r
569 // the whole property.
\r
570 xdr_data_t id = XDR_encode_uint32((*it)->id);
\r
571 // The actual data representation depends on the type
\r
572 switch ((*it)->type) {
\r
573 case simgear::props::INT:
\r
574 case simgear::props::BOOL:
\r
575 case simgear::props::LONG:
\r
577 *ptr++ = XDR_encode_uint32((*it)->int_value);
\r
578 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
\r
580 case simgear::props::FLOAT:
\r
581 case simgear::props::DOUBLE:
\r
583 *ptr++ = XDR_encode_float((*it)->float_value);
\r
584 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
\r
586 case simgear::props::STRING:
\r
587 case simgear::props::UNSPECIFIED:
\r
589 // String is complicated. It consists of
\r
590 // The length of the string
\r
591 // The string itself
\r
592 // Padding to the nearest 4-bytes.
\r
593 const char* lcharptr = (*it)->string_value;
\r
598 ////cout << "String length: " << strlen(lcharptr) << "\n";
\r
599 uint32_t len = strlen(lcharptr);
\r
600 if (len > MAX_TEXT_SIZE)
\r
601 len = MAX_TEXT_SIZE;
\r
602 // XXX This should not be using 4 bytes per character!
\r
603 // If there's not enough room for this property, drop it
\r
605 if (ptr + 2 + ((len + 3) & ~3) > msgEnd)
\r
607 //cout << "String length unint32: " << len << "\n";
\r
609 *ptr++ = XDR_encode_uint32(len);
\r
612 // Now the text itself
\r
613 // XXX This should not be using 4 bytes per character!
\r
615 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
\r
617 *ptr++ = XDR_encode_int8(*lcharptr);
\r
622 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
\r
624 // Now pad if required
\r
625 while ((lcount % 4) != 0)
\r
627 *ptr++ = XDR_encode_int8(0);
\r
637 // Nothing to encode
\r
639 *ptr++ = XDR_encode_uint32(0);
\r
640 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
\r
646 //cout << " Unknown Type: " << (*it)->type << "\n";
\r
648 *ptr++ = XDR_encode_float((*it)->float_value);;
\r
649 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
\r
656 unsigned msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;
\r
657 FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);
\r
658 mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);
\r
659 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
\r
660 } // FGMultiplayMgr::SendMyPosition()
\r
662 //////////////////////////////////////////////////////////////////////
\r
664 //////////////////////////////////////////////////////////////////////
\r
666 // Name: SendTextMessage
\r
667 // Description: Sends a message to the player. The message must
\r
668 // contain a valid and correctly filled out header and optional
\r
671 //////////////////////////////////////////////////////////////////////
\r
673 FGMultiplayMgr::SendTextMessage(const string &MsgText)
\r
675 if (!mInitialised || !mHaveServer)
\r
679 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
\r
680 //////////////////////////////////////////////////
\r
681 // Divide the text string into blocks that fit
\r
682 // in the message and send the blocks.
\r
683 //////////////////////////////////////////////////
\r
684 unsigned iNextBlockPosition = 0;
\r
687 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
\r
688 while (iNextBlockPosition < MsgText.length()) {
\r
689 strncpy (ChatMsg.Text,
\r
690 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
\r
692 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
\r
693 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
\r
694 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
\r
695 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
\r
696 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
\r
701 } // FGMultiplayMgr::SendTextMessage ()
\r
702 //////////////////////////////////////////////////////////////////////
\r
704 //////////////////////////////////////////////////////////////////////
\r
706 // Name: ProcessData
\r
707 // Description: Processes data waiting at the receive socket. The
\r
708 // processing ends when there is no more data at the socket.
\r
710 //////////////////////////////////////////////////////////////////////
\r
712 FGMultiplayMgr::Update(void)
\r
717 /// Just for expiry
\r
718 long stamp = SGTimeStamp::now().getSeconds();
\r
720 //////////////////////////////////////////////////
\r
721 // Read the receive socket and process any data
\r
722 //////////////////////////////////////////////////
\r
726 //////////////////////////////////////////////////
\r
727 // Although the recv call asks for
\r
728 // MAX_PACKET_SIZE of data, the number of bytes
\r
729 // returned will only be that of the next
\r
730 // packet waiting to be processed.
\r
731 //////////////////////////////////////////////////
\r
732 netAddress SenderAddress;
\r
733 bytes = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,
\r
735 //////////////////////////////////////////////////
\r
736 // no Data received
\r
737 //////////////////////////////////////////////////
\r
739 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
\r
740 perror("FGMultiplayMgr::MP_ProcessData");
\r
743 if (bytes <= static_cast<ssize_t>(sizeof(T_MsgHdr))) {
\r
744 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
745 << "received message with insufficient data" );
\r
748 //////////////////////////////////////////////////
\r
750 //////////////////////////////////////////////////
\r
751 T_MsgHdr* MsgHdr = msgBuf.msgHdr();
\r
752 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
\r
753 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
\r
754 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
\r
755 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
\r
756 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
\r
757 MsgHdr->Callsign[MAX_CALLSIGN_LEN -1] = '\0';
\r
758 if (MsgHdr->Magic != MSG_MAGIC) {
\r
759 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
760 << "message has invalid magic number!" );
\r
763 if (MsgHdr->Version != PROTO_VER) {
\r
764 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
765 << "message has invalid protocoll number!" );
\r
768 if (MsgHdr->MsgLen != bytes) {
\r
769 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
770 << "message from " << MsgHdr->Callsign << " has invalid length!");
\r
773 //////////////////////////////////////////////////
\r
774 // Process messages
\r
775 //////////////////////////////////////////////////
\r
776 switch (MsgHdr->MsgId) {
\r
778 ProcessChatMsg(msgBuf, SenderAddress);
\r
781 ProcessPosMsg(msgBuf, SenderAddress, stamp);
\r
783 case UNUSABLE_POS_DATA_ID:
\r
784 case OLD_OLD_POS_DATA_ID:
\r
785 case OLD_PROP_MSG_ID:
\r
786 case OLD_POS_DATA_ID:
\r
789 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
790 << "Unknown message Id received: " << MsgHdr->MsgId );
\r
793 } while (bytes > 0);
\r
795 // check for expiry
\r
796 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
\r
797 while (it != mMultiPlayerMap.end()) {
\r
798 if (it->second->getLastTimestamp() + 10 < stamp) {
\r
799 std::string name = it->first;
\r
800 it->second->setDie(true);
\r
801 mMultiPlayerMap.erase(it);
\r
802 it = mMultiPlayerMap.upper_bound(name);
\r
806 } // FGMultiplayMgr::ProcessData(void)
\r
807 //////////////////////////////////////////////////////////////////////
\r
809 //////////////////////////////////////////////////////////////////////
\r
811 // handle a position message
\r
813 //////////////////////////////////////////////////////////////////////
\r
815 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,
\r
816 const netAddress& SenderAddress, long stamp)
\r
818 const T_MsgHdr* MsgHdr = Msg.msgHdr();
\r
819 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
\r
820 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
821 << "Position message received with insufficient data" );
\r
824 const T_PositionMsg* PosMsg = Msg.posMsg();
\r
825 FGExternalMotionData motionInfo;
\r
826 motionInfo.time = XDR_decode_double(PosMsg->time);
\r
827 motionInfo.lag = XDR_decode_double(PosMsg->lag);
\r
828 for (unsigned i = 0; i < 3; ++i)
\r
829 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
\r
831 for (unsigned i = 0; i < 3; ++i)
\r
832 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
\r
833 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
\r
834 for (unsigned i = 0; i < 3; ++i)
\r
835 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
\r
836 for (unsigned i = 0; i < 3; ++i)
\r
837 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
\r
838 for (unsigned i = 0; i < 3; ++i)
\r
839 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
\r
840 for (unsigned i = 0; i < 3; ++i)
\r
841 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
\r
844 //cout << "INPUT MESSAGE\n";
\r
846 // There was a bug in 1.9.0 and before: T_PositionMsg was 196 bytes
\r
847 // on 32 bit architectures and 200 bytes on 64 bit, and this
\r
848 // structure is put directly on the wire. By looking at the padding,
\r
849 // we can sort through the mess, mostly:
\r
850 // If padding is 0 (which is not a valid property type), then the
\r
851 // message was produced by a new client or an old 64 bit client that
\r
852 // happened to have 0 on the stack;
\r
853 // Else if the property list starting with the padding word is
\r
854 // well-formed, then the client is probably an old 32 bit client and
\r
855 // we'll go with that;
\r
856 // Else it is an old 64-bit client and properties start after the
\r
858 // There is a chance that we could be fooled by garbage in the
\r
859 // padding looking like a valid property, so verifyProperties() is
\r
860 // strict about the validity of the property values.
\r
861 const xdr_data_t* xdr = Msg.properties();
\r
862 if (PosMsg->pad != 0) {
\r
863 if (verifyProperties(&PosMsg->pad, Msg.propsRecvdEnd()))
\r
864 xdr = &PosMsg->pad;
\r
865 else if (!verifyProperties(xdr, Msg.propsRecvdEnd()))
\r
868 while (xdr < Msg.propsRecvdEnd()) {
\r
869 FGPropertyData* pData = new FGPropertyData;
\r
870 // simgear::props::Type type = simgear::props::UNSPECIFIED;
\r
872 // First element is always the ID
\r
873 pData->id = XDR_decode_uint32(*xdr);
\r
874 //cout << pData->id << " ";
\r
877 // Check the ID actually exists and get the type
\r
878 const IdPropertyList* plist = findProperty(pData->id);
\r
882 pData->type = plist->type;
\r
883 // How we decode the remainder of the property depends on the type
\r
884 switch (pData->type) {
\r
885 case simgear::props::INT:
\r
886 case simgear::props::BOOL:
\r
887 case simgear::props::LONG:
\r
888 pData->int_value = XDR_decode_uint32(*xdr);
\r
890 //cout << pData->int_value << "\n";
\r
892 case simgear::props::FLOAT:
\r
893 case simgear::props::DOUBLE:
\r
894 pData->float_value = XDR_decode_float(*xdr);
\r
896 //cout << pData->float_value << "\n";
\r
898 case simgear::props::STRING:
\r
899 case simgear::props::UNSPECIFIED:
\r
901 // String is complicated. It consists of
\r
902 // The length of the string
\r
903 // The string itself
\r
904 // Padding to the nearest 4-bytes.
\r
905 uint32_t length = XDR_decode_uint32(*xdr);
\r
907 //cout << length << " ";
\r
908 // Old versions truncated the string but left the length unadjusted.
\r
909 if (length > MAX_TEXT_SIZE)
\r
910 length = MAX_TEXT_SIZE;
\r
911 pData->string_value = new char[length + 1];
\r
912 //cout << " String: ";
\r
913 for (unsigned i = 0; i < length; i++)
\r
915 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
\r
917 //cout << pData->string_value[i];
\r
920 pData->string_value[length] = '\0';
\r
922 // Now handle the padding
\r
923 while ((length % 4) != 0)
\r
934 pData->float_value = XDR_decode_float(*xdr);
\r
935 SG_LOG(SG_NETWORK, SG_DEBUG, "Unknown Prop type " << pData->id << " " << pData->type);
\r
940 motionInfo.properties.push_back(pData);
\r
944 // We failed to find the property. We'll try the next packet immediately.
\r
945 SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr::ProcessPosMsg - "
\r
946 "message from " << MsgHdr->Callsign << " has unknown property id "
\r
951 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
\r
953 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
\r
954 mp->addMotionInfo(motionInfo, stamp);
\r
955 } // FGMultiplayMgr::ProcessPosMsg()
\r
956 //////////////////////////////////////////////////////////////////////
\r
958 //////////////////////////////////////////////////////////////////////
\r
960 // handle a chat message
\r
961 // FIXME: display chat message withi flightgear
\r
963 //////////////////////////////////////////////////////////////////////
\r
965 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
\r
966 const netAddress& SenderAddress)
\r
968 const T_MsgHdr* MsgHdr = Msg.msgHdr();
\r
969 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
\r
970 SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
\r
971 << "Chat message received with insufficient data" );
\r
975 char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
\r
976 const T_ChatMsg* ChatMsg
\r
977 = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));
\r
978 strncpy(chatStr, ChatMsg->Text,
\r
979 MsgHdr->MsgLen - sizeof(T_MsgHdr));
\r
980 chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
\r
982 SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"
\r
983 << " " << chatStr);
\r
986 } // FGMultiplayMgr::ProcessChatMsg ()
\r
987 //////////////////////////////////////////////////////////////////////
\r
990 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
\r
995 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
\r
1001 len = sizeof(T_MsgHdr);
\r
1004 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
\r
1005 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
\r
1006 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
\r
1007 MsgHdr->MsgLen = XDR_encode_uint32(len);
\r
1008 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
\r
1009 MsgHdr->ReplyPort = 0; // now
\r
1010 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
\r
1011 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
\r
1015 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
\r
1016 const std::string& modelName)
\r
1018 if (0 < mMultiPlayerMap.count(callsign))
\r
1019 return mMultiPlayerMap[callsign].get();
\r
1021 FGAIMultiplayer* mp = new FGAIMultiplayer;
\r
1022 mp->setPath(modelName.c_str());
\r
1023 mp->setCallSign(callsign);
\r
1024 mMultiPlayerMap[callsign] = mp;
\r
1026 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
\r
1028 aiMgr->attach(mp);
\r
1030 /// FIXME: that must follow the attach ATM ...
\r
1031 for (unsigned i = 0; i < numProperties; ++i)
\r
1032 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
\r
1039 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
\r
1041 if (0 < mMultiPlayerMap.count(callsign))
\r
1042 return mMultiPlayerMap[callsign].get();
\r