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 //////////////////////////////////////////////////////////////////////
34 #include <plib/netSocket.h>
36 #include <simgear/misc/stdint.hxx>
37 #include <simgear/timing/timestamp.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/props/props.hxx>
41 #include <AIModel/AIManager.hxx>
42 #include <Main/fg_props.hxx>
43 #include "multiplaymgr.hxx"
44 #include "mpmessages.hxx"
46 #define MAX_PACKET_SIZE 1200
47 #define MAX_TEXT_SIZE 128
49 // These constants are provided so that the ident
50 // command can list file versions
51 const char sMULTIPLAYMGR_BID[] = "$Id$";
52 const char sMULTIPLAYMGR_HID[] = MULTIPLAYTXMGR_HID;
54 // A static map of protocol property id values to property paths,
55 // This should be extendable dynamically for every specific aircraft ...
56 // For now only that static list
57 FGMultiplayMgr::IdPropertyList
58 FGMultiplayMgr::sIdPropertyList[] = {
59 {100, "surface-positions/left-aileron-pos-norm", SGPropertyNode::FLOAT},
60 {101, "surface-positions/right-aileron-pos-norm", SGPropertyNode::FLOAT},
61 {102, "surface-positions/elevator-pos-norm", SGPropertyNode::FLOAT},
62 {103, "surface-positions/rudder-pos-norm", SGPropertyNode::FLOAT},
63 {104, "surface-positions/flap-pos-norm", SGPropertyNode::FLOAT},
64 {105, "surface-positions/speedbrake-pos-norm", SGPropertyNode::FLOAT},
65 {106, "gear/tailhook/position-norm", SGPropertyNode::FLOAT},
66 {107, "gear/launchbar/position-norm", SGPropertyNode::FLOAT},
67 {108, "gear/launchbar/state", SGPropertyNode::STRING},
68 {109, "gear/launchbar/holdback-position-norm", SGPropertyNode::FLOAT},
69 {110, "canopy/position-norm", SGPropertyNode::FLOAT},
71 {200, "gear/gear[0]/compression-norm", SGPropertyNode::FLOAT},
72 {201, "gear/gear[0]/position-norm", SGPropertyNode::FLOAT},
73 {210, "gear/gear[1]/compression-norm", SGPropertyNode::FLOAT},
74 {211, "gear/gear[1]/position-norm", SGPropertyNode::FLOAT},
75 {220, "gear/gear[2]/compression-norm", SGPropertyNode::FLOAT},
76 {221, "gear/gear[2]/position-norm", SGPropertyNode::FLOAT},
77 {230, "gear/gear[3]/compression-norm", SGPropertyNode::FLOAT},
78 {231, "gear/gear[3]/position-norm", SGPropertyNode::FLOAT},
79 {240, "gear/gear[4]/compression-norm", SGPropertyNode::FLOAT},
80 {241, "gear/gear[4]/position-norm", SGPropertyNode::FLOAT},
82 {300, "engines/engine[0]/n1", SGPropertyNode::FLOAT},
83 {301, "engines/engine[0]/n2", SGPropertyNode::FLOAT},
84 {302, "engines/engine[0]/rpm", SGPropertyNode::FLOAT},
85 {310, "engines/engine[1]/n1", SGPropertyNode::FLOAT},
86 {311, "engines/engine[1]/n2", SGPropertyNode::FLOAT},
87 {312, "engines/engine[1]/rpm", SGPropertyNode::FLOAT},
88 {320, "engines/engine[2]/n1", SGPropertyNode::FLOAT},
89 {321, "engines/engine[2]/n2", SGPropertyNode::FLOAT},
90 {322, "engines/engine[2]/rpm", SGPropertyNode::FLOAT},
91 {330, "engines/engine[3]/n1", SGPropertyNode::FLOAT},
92 {331, "engines/engine[3]/n2", SGPropertyNode::FLOAT},
93 {332, "engines/engine[3]/rpm", SGPropertyNode::FLOAT},
94 {340, "engines/engine[4]/n1", SGPropertyNode::FLOAT},
95 {341, "engines/engine[4]/n2", SGPropertyNode::FLOAT},
96 {342, "engines/engine[4]/rpm", SGPropertyNode::FLOAT},
97 {350, "engines/engine[5]/n1", SGPropertyNode::FLOAT},
98 {351, "engines/engine[5]/n2", SGPropertyNode::FLOAT},
99 {352, "engines/engine[5]/rpm", SGPropertyNode::FLOAT},
100 {360, "engines/engine[6]/n1", SGPropertyNode::FLOAT},
101 {361, "engines/engine[6]/n2", SGPropertyNode::FLOAT},
102 {362, "engines/engine[6]/rpm", SGPropertyNode::FLOAT},
103 {370, "engines/engine[7]/n1", SGPropertyNode::FLOAT},
104 {371, "engines/engine[7]/n2", SGPropertyNode::FLOAT},
105 {372, "engines/engine[7]/rpm", SGPropertyNode::FLOAT},
106 {380, "engines/engine[8]/n1", SGPropertyNode::FLOAT},
107 {381, "engines/engine[8]/n2", SGPropertyNode::FLOAT},
108 {382, "engines/engine[8]/rpm", SGPropertyNode::FLOAT},
109 {390, "engines/engine[9]/n1", SGPropertyNode::FLOAT},
110 {391, "engines/engine[9]/n2", SGPropertyNode::FLOAT},
111 {392, "engines/engine[9]/rpm", SGPropertyNode::FLOAT},
113 {800, "rotors/main/rpm", SGPropertyNode::FLOAT},
114 {801, "rotors/tail/rpm", SGPropertyNode::FLOAT},
115 {810, "rotors/main/blade[0]/position-deg", SGPropertyNode::FLOAT},
116 {811, "rotors/main/blade[1]/position-deg", SGPropertyNode::FLOAT},
117 {812, "rotors/main/blade[2]/position-deg", SGPropertyNode::FLOAT},
118 {813, "rotors/main/blade[3]/position-deg", SGPropertyNode::FLOAT},
119 {820, "rotors/main/blade[0]/flap-deg", SGPropertyNode::FLOAT},
120 {821, "rotors/main/blade[1]/flap-deg", SGPropertyNode::FLOAT},
121 {822, "rotors/main/blade[2]/flap-deg", SGPropertyNode::FLOAT},
122 {823, "rotors/main/blade[3]/flap-deg", SGPropertyNode::FLOAT},
123 {830, "rotors/tail/blade[0]/position-deg", SGPropertyNode::FLOAT},
124 {831, "rotors/tail/blade[1]/position-deg", SGPropertyNode::FLOAT},
126 {900, "sim/hitches/aerotow/tow/length", SGPropertyNode::FLOAT},
127 {901, "sim/hitches/aerotow/tow/elastic-constant", SGPropertyNode::FLOAT},
128 {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m", SGPropertyNode::FLOAT},
129 {903, "sim/hitches/aerotow/tow/dist", SGPropertyNode::FLOAT},
130 {904, "sim/hitches/aerotow/tow/connected-to-property-node", SGPropertyNode::BOOL},
131 {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign", SGPropertyNode::STRING},
132 {906, "sim/hitches/aerotow/tow/brake-force", SGPropertyNode::FLOAT},
133 {907, "sim/hitches/aerotow/tow/end-force-x", SGPropertyNode::FLOAT},
134 {908, "sim/hitches/aerotow/tow/end-force-y", SGPropertyNode::FLOAT},
135 {909, "sim/hitches/aerotow/tow/end-force-z", SGPropertyNode::FLOAT},
136 {930, "sim/hitches/aerotow/is-slave", SGPropertyNode::BOOL},
137 {931, "sim/hitches/aerotow/speed-in-tow-direction", SGPropertyNode::FLOAT},
138 {932, "sim/hitches/aerotow/open", SGPropertyNode::BOOL},
139 {933, "sim/hitches/aerotow/local-pos-x", SGPropertyNode::FLOAT},
140 {934, "sim/hitches/aerotow/local-pos-y", SGPropertyNode::FLOAT},
141 {935, "sim/hitches/aerotow/local-pos-z", SGPropertyNode::FLOAT},
143 {1001, "controls/flight/slats", SGPropertyNode::FLOAT},
144 {1002, "controls/flight/speedbrake", SGPropertyNode::FLOAT},
145 {1003, "controls/flight/spoilers", SGPropertyNode::FLOAT},
146 {1004, "controls/gear/gear-down", SGPropertyNode::FLOAT},
147 {1005, "controls/lighting/nav-lights", SGPropertyNode::FLOAT},
148 {1006, "controls/armament/station[0]/jettison-all", SGPropertyNode::BOOL},
150 {1100, "sim/model/variant", SGPropertyNode::INT},
151 {1101, "sim/model/livery/file", SGPropertyNode::STRING},
153 {10001, "sim/multiplay/transmission-freq-hz", SGPropertyNode::STRING},
154 {10002, "sim/multiplay/chat", SGPropertyNode::STRING},
156 {10100, "sim/multiplay/generic/string[0]", SGPropertyNode::STRING},
157 {10101, "sim/multiplay/generic/string[1]", SGPropertyNode::STRING},
158 {10102, "sim/multiplay/generic/string[2]", SGPropertyNode::STRING},
159 {10103, "sim/multiplay/generic/string[3]", SGPropertyNode::STRING},
160 {10104, "sim/multiplay/generic/string[4]", SGPropertyNode::STRING},
161 {10105, "sim/multiplay/generic/string[5]", SGPropertyNode::STRING},
162 {10106, "sim/multiplay/generic/string[6]", SGPropertyNode::STRING},
163 {10107, "sim/multiplay/generic/string[7]", SGPropertyNode::STRING},
164 {10108, "sim/multiplay/generic/string[8]", SGPropertyNode::STRING},
165 {10109, "sim/multiplay/generic/string[9]", SGPropertyNode::STRING},
167 {10200, "sim/multiplay/generic/float[0]", SGPropertyNode::FLOAT},
168 {10201, "sim/multiplay/generic/float[1]", SGPropertyNode::FLOAT},
169 {10202, "sim/multiplay/generic/float[2]", SGPropertyNode::FLOAT},
170 {10203, "sim/multiplay/generic/float[3]", SGPropertyNode::FLOAT},
171 {10204, "sim/multiplay/generic/float[4]", SGPropertyNode::FLOAT},
172 {10205, "sim/multiplay/generic/float[5]", SGPropertyNode::FLOAT},
173 {10206, "sim/multiplay/generic/float[6]", SGPropertyNode::FLOAT},
174 {10207, "sim/multiplay/generic/float[7]", SGPropertyNode::FLOAT},
175 {10208, "sim/multiplay/generic/float[8]", SGPropertyNode::FLOAT},
176 {10209, "sim/multiplay/generic/float[9]", SGPropertyNode::FLOAT},
178 {10300, "sim/multiplay/generic/int[0]", SGPropertyNode::INT},
179 {10301, "sim/multiplay/generic/int[1]", SGPropertyNode::INT},
180 {10302, "sim/multiplay/generic/int[2]", SGPropertyNode::INT},
181 {10303, "sim/multiplay/generic/int[3]", SGPropertyNode::INT},
182 {10304, "sim/multiplay/generic/int[4]", SGPropertyNode::INT},
183 {10305, "sim/multiplay/generic/int[5]", SGPropertyNode::INT},
184 {10306, "sim/multiplay/generic/int[6]", SGPropertyNode::INT},
185 {10307, "sim/multiplay/generic/int[7]", SGPropertyNode::INT},
186 {10308, "sim/multiplay/generic/int[8]", SGPropertyNode::INT},
187 {10309, "sim/multiplay/generic/int[9]", SGPropertyNode::INT},
190 {0, 0, SGPropertyNode::UNSPECIFIED}
193 //////////////////////////////////////////////////////////////////////
195 // MultiplayMgr constructor
197 //////////////////////////////////////////////////////////////////////
198 FGMultiplayMgr::FGMultiplayMgr()
201 mInitialised = false;
203 } // FGMultiplayMgr::FGMultiplayMgr()
204 //////////////////////////////////////////////////////////////////////
206 //////////////////////////////////////////////////////////////////////
208 // MultiplayMgr destructor
210 //////////////////////////////////////////////////////////////////////
211 FGMultiplayMgr::~FGMultiplayMgr()
214 } // FGMultiplayMgr::~FGMultiplayMgr()
215 //////////////////////////////////////////////////////////////////////
217 //////////////////////////////////////////////////////////////////////
221 //////////////////////////////////////////////////////////////////////
223 FGMultiplayMgr::init (void)
225 //////////////////////////////////////////////////
226 // Initialise object if not already done
227 //////////////////////////////////////////////////
229 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
232 //////////////////////////////////////////////////
233 // Set members from property values
234 //////////////////////////////////////////////////
235 short rxPort = fgGetInt("/sim/multiplay/rxport");
238 mCallsign = fgGetString("/sim/multiplay/callsign");
239 if (mCallsign.empty())
240 // FIXME: use getpwuid
241 mCallsign = "JohnDoe";
242 string rxAddress = fgGetString("/sim/multiplay/rxhost");
243 if (rxAddress.empty())
244 rxAddress = "127.0.0.1";
245 short txPort = fgGetInt("/sim/multiplay/txport");
246 string txAddress = fgGetString("/sim/multiplay/txhost");
247 if (txPort > 0 && !txAddress.empty()) {
249 mServer.set(txAddress.c_str(), txPort);
251 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
252 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
253 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
254 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
255 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
256 Close(); // Should Init be called twice, close Socket first
257 // A memory leak was reported here by valgrind
258 mSocket = new netSocket();
259 if (!mSocket->open(false)) {
260 SG_LOG( SG_NETWORK, SG_ALERT,
261 "FGMultiplayMgr::init - Failed to create data socket" );
264 mSocket->setBlocking(false);
265 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
267 SG_LOG( SG_NETWORK, SG_ALERT,
268 "FGMultiplayMgr::Open - Failed to bind receive socket" );
273 } // FGMultiplayMgr::init()
274 //////////////////////////////////////////////////////////////////////
276 //////////////////////////////////////////////////////////////////////
278 // Closes and deletes the local player object. Closes
279 // and deletes the tx socket. Resets the object state to unitialised.
281 //////////////////////////////////////////////////////////////////////
283 FGMultiplayMgr::Close (void)
285 mMultiPlayerMap.clear();
292 mInitialised = false;
293 } // FGMultiplayMgr::Close(void)
294 //////////////////////////////////////////////////////////////////////
296 //////////////////////////////////////////////////////////////////////
298 // Description: Sends the position data for the local position.
300 //////////////////////////////////////////////////////////////////////
302 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
304 if ((! mInitialised) || (! mHaveServer)) {
306 SG_LOG( SG_NETWORK, SG_ALERT,
307 "FGMultiplayMgr::SendMyPosition - not initialised" );
309 SG_LOG( SG_NETWORK, SG_ALERT,
310 "FGMultiplayMgr::SendMyPosition - no server" );
314 T_PositionMsg PosMsg;
315 strncpy(PosMsg.Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
316 PosMsg.Model[MAX_MODEL_NAME_LEN - 1] = '\0';
318 PosMsg.time = XDR_encode_double (motionInfo.time);
319 PosMsg.lag = XDR_encode_double (motionInfo.lag);
320 for (unsigned i = 0 ; i < 3; ++i)
321 PosMsg.position[i] = XDR_encode_double (motionInfo.position(i));
323 motionInfo.orientation.getAngleAxis(angleAxis);
324 for (unsigned i = 0 ; i < 3; ++i)
325 PosMsg.orientation[i] = XDR_encode_float (angleAxis(i));
326 for (unsigned i = 0 ; i < 3; ++i)
327 PosMsg.linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
328 for (unsigned i = 0 ; i < 3; ++i)
329 PosMsg.angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
330 for (unsigned i = 0 ; i < 3; ++i)
331 PosMsg.linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
332 for (unsigned i = 0 ; i < 3; ++i)
333 PosMsg.angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
335 char Msg[MAX_PACKET_SIZE];
336 memcpy(Msg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
338 char* ptr = Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg);
339 std::vector<FGPropertyData*>::const_iterator it;
340 it = motionInfo.properties.begin();
341 //cout << "OUTPUT PROPERTIES\n";
342 while (it != motionInfo.properties.end()
343 && ptr + 2 * sizeof(xdr_data_t) < (Msg + MAX_PACKET_SIZE)) {
345 // First elements is the ID
346 xdr_data_t xdr = XDR_encode_uint32((*it)->id);
347 memcpy(ptr, &xdr, sizeof(xdr_data_t));
348 ptr += sizeof(xdr_data_t);
350 // The actual data representation depends on the type
351 switch ((*it)->type) {
352 case SGPropertyNode::INT:
353 case SGPropertyNode::BOOL:
354 case SGPropertyNode::LONG:
355 xdr = XDR_encode_uint32((*it)->int_value);
356 memcpy(ptr, &xdr, sizeof(xdr_data_t));
357 ptr += sizeof(xdr_data_t);
358 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
360 case SGPropertyNode::FLOAT:
361 case SGPropertyNode::DOUBLE:
362 xdr = XDR_encode_float((*it)->float_value);;
363 memcpy(ptr, &xdr, sizeof(xdr_data_t));
364 ptr += sizeof(xdr_data_t);
365 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
367 case SGPropertyNode::STRING:
368 case SGPropertyNode::UNSPECIFIED:
370 // String is complicated. It consists of
371 // The length of the string
373 // Padding to the nearest 4-bytes.
374 const char* lcharptr = (*it)->string_value;
379 ////cout << "String length: " << strlen(lcharptr) << "\n";
380 uint32_t len = strlen(lcharptr);
381 // XXX This should not be using 4 bytes per character!
382 if (ptr + (1 + len + (4 - len % 4)) * sizeof (xdr_data_t)
383 >= (Msg + MAX_PACKET_SIZE))
385 //cout << "String length unint32: " << len << "\n";
386 xdr = XDR_encode_uint32(len);
387 memcpy(ptr, &xdr, sizeof(xdr_data_t));
388 ptr += sizeof(xdr_data_t);
393 // Now the text itself
394 // XXX This should not be using 4 bytes per character!
396 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
398 xdr = XDR_encode_int8(*lcharptr);
399 memcpy(ptr, &xdr, sizeof(xdr_data_t));
400 ptr += sizeof(xdr_data_t);
405 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
407 // Now pad if required
408 while ((lcount % 4) != 0)
410 xdr = XDR_encode_int8(0);
411 memcpy(ptr, &xdr, sizeof(xdr_data_t));
412 ptr += sizeof(xdr_data_t);
423 xdr = XDR_encode_uint32(0);
424 memcpy(ptr, &xdr, sizeof(xdr_data_t));
425 ptr += sizeof(xdr_data_t);
426 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
433 //cout << " Unknown Type: " << (*it)->type << "\n";
434 xdr = XDR_encode_float((*it)->float_value);;
435 memcpy(ptr, &xdr, sizeof(xdr_data_t));
436 ptr += sizeof(xdr_data_t);
437 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
446 FillMsgHdr(&MsgHdr, POS_DATA_ID, ptr - Msg);
447 memcpy(Msg, &MsgHdr, sizeof(T_MsgHdr));
449 mSocket->sendto(Msg, ptr - Msg, 0, &mServer);
450 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
451 } // FGMultiplayMgr::SendMyPosition()
452 //////////////////////////////////////////////////////////////////////
454 //////////////////////////////////////////////////////////////////////
456 // Name: SendTextMessage
457 // Description: Sends a message to the player. The message must
458 // contain a valid and correctly filled out header and optional
461 //////////////////////////////////////////////////////////////////////
463 FGMultiplayMgr::SendTextMessage(const string &MsgText)
465 if (!mInitialised || !mHaveServer)
469 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
470 //////////////////////////////////////////////////
471 // Divide the text string into blocks that fit
472 // in the message and send the blocks.
473 //////////////////////////////////////////////////
474 unsigned iNextBlockPosition = 0;
477 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
478 while (iNextBlockPosition < MsgText.length()) {
479 strncpy (ChatMsg.Text,
480 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
482 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
483 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
484 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
485 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
486 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
491 } // FGMultiplayMgr::SendTextMessage ()
492 //////////////////////////////////////////////////////////////////////
494 //////////////////////////////////////////////////////////////////////
497 // Description: Processes data waiting at the receive socket. The
498 // processing ends when there is no more data at the socket.
500 //////////////////////////////////////////////////////////////////////
502 FGMultiplayMgr::Update(void)
508 SGTimeStamp timestamper;
510 long stamp = timestamper.get_seconds();
512 //////////////////////////////////////////////////
513 // Read the receive socket and process any data
514 //////////////////////////////////////////////////
517 char Msg[MAX_PACKET_SIZE];
518 //////////////////////////////////////////////////
519 // Although the recv call asks for
520 // MAX_PACKET_SIZE of data, the number of bytes
521 // returned will only be that of the next
522 // packet waiting to be processed.
523 //////////////////////////////////////////////////
524 netAddress SenderAddress;
525 bytes = mSocket->recvfrom(Msg, sizeof(Msg), 0, &SenderAddress);
526 //////////////////////////////////////////////////
528 //////////////////////////////////////////////////
530 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
531 perror("FGMultiplayMgr::MP_ProcessData");
534 if (bytes <= sizeof(T_MsgHdr)) {
535 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
536 << "received message with insufficient data" );
539 //////////////////////////////////////////////////
541 //////////////////////////////////////////////////
542 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
543 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
544 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
545 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
546 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
547 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
548 if (MsgHdr->Magic != MSG_MAGIC) {
549 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
550 << "message has invalid magic number!" );
553 if (MsgHdr->Version != PROTO_VER) {
554 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
555 << "message has invalid protocoll number!" );
558 if (MsgHdr->MsgLen != bytes) {
559 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
560 << "message has invalid length!" );
563 //////////////////////////////////////////////////
565 //////////////////////////////////////////////////
566 switch (MsgHdr->MsgId) {
568 ProcessChatMsg(Msg, SenderAddress);
571 ProcessPosMsg(Msg, SenderAddress, bytes, stamp);
573 case UNUSABLE_POS_DATA_ID:
574 case OLD_OLD_POS_DATA_ID:
575 case OLD_PROP_MSG_ID:
576 case OLD_POS_DATA_ID:
579 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
580 << "Unknown message Id received: " << MsgHdr->MsgId );
586 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
587 while (it != mMultiPlayerMap.end()) {
588 if (it->second->getLastTimestamp() + 10 < stamp) {
589 std::string name = it->first;
590 it->second->setDie(true);
591 mMultiPlayerMap.erase(it);
592 it = mMultiPlayerMap.upper_bound(name);
596 } // FGMultiplayMgr::ProcessData(void)
597 //////////////////////////////////////////////////////////////////////
599 //////////////////////////////////////////////////////////////////////
601 // handle a position message
603 //////////////////////////////////////////////////////////////////////
605 FGMultiplayMgr::ProcessPosMsg(const char *Msg, netAddress & SenderAddress,
606 unsigned len, long stamp)
608 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
609 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
610 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
611 << "Position message received with insufficient data" );
614 T_PositionMsg* PosMsg = (T_PositionMsg *)(Msg + sizeof(T_MsgHdr));
615 FGExternalMotionData motionInfo;
616 motionInfo.time = XDR_decode_double(PosMsg->time);
617 motionInfo.lag = XDR_decode_double(PosMsg->lag);
618 for (unsigned i = 0; i < 3; ++i)
619 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
621 for (unsigned i = 0; i < 3; ++i)
622 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
623 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
624 for (unsigned i = 0; i < 3; ++i)
625 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
626 for (unsigned i = 0; i < 3; ++i)
627 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
628 for (unsigned i = 0; i < 3; ++i)
629 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
630 for (unsigned i = 0; i < 3; ++i)
631 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
634 //cout << "INPUT MESSAGE\n";
635 xdr_data_t* xdr = (xdr_data_t*)
636 (Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg));
637 while ((char*)xdr < Msg + len) {
638 FGPropertyData* pData = new FGPropertyData;
639 SGPropertyNode::Type type = SGPropertyNode::UNSPECIFIED;
641 // First element is always the ID
642 pData->id = XDR_decode_uint32(*xdr);
643 //cout << pData->id << " ";
646 // Check the ID actually exists and get the type
649 while (FGMultiplayMgr::sIdPropertyList[i].name)
651 if (sIdPropertyList[i].id == pData->id)
654 pData->type = sIdPropertyList[i].type;
662 // How we decode the remainder of the property depends on the type
663 switch (pData->type) {
664 case SGPropertyNode::INT:
665 case SGPropertyNode::BOOL:
666 case SGPropertyNode::LONG:
667 pData->int_value = XDR_decode_uint32(*xdr);
669 //cout << pData->int_value << "\n";
671 case SGPropertyNode::FLOAT:
672 case SGPropertyNode::DOUBLE:
673 pData->float_value = XDR_decode_float(*xdr);
675 //cout << pData->float_value << "\n";
677 case SGPropertyNode::STRING:
678 case SGPropertyNode::UNSPECIFIED:
680 // String is complicated. It consists of
681 // The length of the string
683 // Padding to the nearest 4-bytes.
684 uint32_t length = XDR_decode_uint32(*xdr);
686 //cout << length << " ";
688 if ((length > 0) && (length < MAX_TEXT_SIZE))
690 pData->string_value = new char[length + 1];
691 //cout << " String: ";
693 for (int i = 0; i < length; i++)
695 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
697 //cout << pData->string_value[i];
700 pData->string_value[length] = '\0';
702 // Now handle the padding
703 while ((length % 4) != 0)
712 pData->string_value = new char[1];
713 pData->string_value[0] = '\0';
721 pData->float_value = XDR_decode_float(*xdr);
722 cerr << "Unknown Prop type " << pData->id << " " << pData->type << "\n";
727 motionInfo.properties.push_back(pData);
731 // We failed to find the property. We'll try the next packet immediately.
732 //cout << " Unknown\n";
736 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
738 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
739 mp->addMotionInfo(motionInfo, stamp);
740 } // FGMultiplayMgr::ProcessPosMsg()
741 //////////////////////////////////////////////////////////////////////
743 //////////////////////////////////////////////////////////////////////
745 // handle a chat message
746 // FIXME: display chat message withi flightgear
748 //////////////////////////////////////////////////////////////////////
750 FGMultiplayMgr::ProcessChatMsg(const char *Msg, netAddress& SenderAddress)
752 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
753 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
754 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
755 << "Chat message received with insufficient data" );
759 char *MsgBuf = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
760 strncpy(MsgBuf, ((T_ChatMsg *)(Msg + sizeof(T_MsgHdr)))->Text,
761 MsgHdr->MsgLen - sizeof(T_MsgHdr));
762 MsgBuf[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
764 T_ChatMsg* ChatMsg = (T_ChatMsg *)(Msg + sizeof(T_MsgHdr));
765 SG_LOG ( SG_NETWORK, SG_ALERT, "Chat [" << MsgHdr->Callsign << "]"
766 << " " << MsgBuf << endl);
769 } // FGMultiplayMgr::ProcessChatMsg ()
770 //////////////////////////////////////////////////////////////////////
773 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
778 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
784 len = sizeof(T_MsgHdr);
787 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
788 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
789 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
790 MsgHdr->MsgLen = XDR_encode_uint32(len);
791 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
792 MsgHdr->ReplyPort = 0; // now
793 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
794 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
798 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
799 const std::string& modelName)
801 if (0 < mMultiPlayerMap.count(callsign))
802 return mMultiPlayerMap[callsign].get();
804 FGAIMultiplayer* mp = new FGAIMultiplayer;
805 mp->setPath(modelName.c_str());
806 mp->setCallSign(callsign);
807 mMultiPlayerMap[callsign] = mp;
809 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
813 /// FIXME: that must follow the attach ATM ...
815 while (sIdPropertyList[i].name) {
816 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
825 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
827 if (0 < mMultiPlayerMap.count(callsign))
828 return mMultiPlayerMap[callsign].get();