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 {10001, "sim/multiplay/transmission-freq-hz", SGPropertyNode::STRING},
151 {10002, "sim/multiplay/chat", SGPropertyNode::STRING},
154 {0, 0, SGPropertyNode::UNSPECIFIED}
157 //////////////////////////////////////////////////////////////////////
159 // MultiplayMgr constructor
161 //////////////////////////////////////////////////////////////////////
162 FGMultiplayMgr::FGMultiplayMgr()
164 mInitialised = false;
166 } // FGMultiplayMgr::FGMultiplayMgr()
167 //////////////////////////////////////////////////////////////////////
169 //////////////////////////////////////////////////////////////////////
171 // MultiplayMgr destructor
173 //////////////////////////////////////////////////////////////////////
174 FGMultiplayMgr::~FGMultiplayMgr()
177 } // FGMultiplayMgr::~FGMultiplayMgr()
178 //////////////////////////////////////////////////////////////////////
180 //////////////////////////////////////////////////////////////////////
184 //////////////////////////////////////////////////////////////////////
186 FGMultiplayMgr::init (void)
188 //////////////////////////////////////////////////
189 // Initialise object if not already done
190 //////////////////////////////////////////////////
192 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
195 //////////////////////////////////////////////////
196 // Set members from property values
197 //////////////////////////////////////////////////
198 short rxPort = fgGetInt("/sim/multiplay/rxport");
201 mCallsign = fgGetString("/sim/multiplay/callsign");
202 if (mCallsign.empty())
203 // FIXME: use getpwuid
204 mCallsign = "JohnDoe";
205 string rxAddress = fgGetString("/sim/multiplay/rxhost");
206 if (rxAddress.empty())
207 rxAddress = "127.0.0.1";
208 short txPort = fgGetInt("/sim/multiplay/txport");
209 string txAddress = fgGetString("/sim/multiplay/txhost");
210 if (txPort > 0 && !txAddress.empty()) {
212 mServer.set(txAddress.c_str(), txPort);
214 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
215 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
216 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
217 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
218 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
219 mSocket = new netSocket();
220 if (!mSocket->open(false)) {
221 SG_LOG( SG_NETWORK, SG_ALERT,
222 "FGMultiplayMgr::init - Failed to create data socket" );
225 mSocket->setBlocking(false);
226 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
228 SG_LOG( SG_NETWORK, SG_ALERT,
229 "FGMultiplayMgr::Open - Failed to bind receive socket" );
234 } // FGMultiplayMgr::init()
235 //////////////////////////////////////////////////////////////////////
237 //////////////////////////////////////////////////////////////////////
239 // Closes and deletes the local player object. Closes
240 // and deletes the tx socket. Resets the object state to unitialised.
242 //////////////////////////////////////////////////////////////////////
244 FGMultiplayMgr::Close (void)
246 mMultiPlayerMap.clear();
253 mInitialised = false;
254 } // FGMultiplayMgr::Close(void)
255 //////////////////////////////////////////////////////////////////////
257 //////////////////////////////////////////////////////////////////////
259 // Description: Sends the position data for the local position.
261 //////////////////////////////////////////////////////////////////////
263 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
265 if ((! mInitialised) || (! mHaveServer)) {
267 SG_LOG( SG_NETWORK, SG_ALERT,
268 "FGMultiplayMgr::SendMyPosition - not initialised" );
270 SG_LOG( SG_NETWORK, SG_ALERT,
271 "FGMultiplayMgr::SendMyPosition - no server" );
275 T_PositionMsg PosMsg;
276 strncpy(PosMsg.Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
277 PosMsg.Model[MAX_MODEL_NAME_LEN - 1] = '\0';
279 PosMsg.time = XDR_encode_double (motionInfo.time);
280 PosMsg.lag = XDR_encode_double (motionInfo.lag);
281 for (unsigned i = 0 ; i < 3; ++i)
282 PosMsg.position[i] = XDR_encode_double (motionInfo.position(i));
284 motionInfo.orientation.getAngleAxis(angleAxis);
285 for (unsigned i = 0 ; i < 3; ++i)
286 PosMsg.orientation[i] = XDR_encode_float (angleAxis(i));
287 for (unsigned i = 0 ; i < 3; ++i)
288 PosMsg.linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
289 for (unsigned i = 0 ; i < 3; ++i)
290 PosMsg.angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
291 for (unsigned i = 0 ; i < 3; ++i)
292 PosMsg.linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
293 for (unsigned i = 0 ; i < 3; ++i)
294 PosMsg.angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
296 char Msg[MAX_PACKET_SIZE];
297 memcpy(Msg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
299 char* ptr = Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg);
300 std::vector<FGPropertyData*>::const_iterator it;
301 it = motionInfo.properties.begin();
302 //cout << "OUTPUT PROPERTIES\n";
303 while (it != motionInfo.properties.end()
304 && ptr < (Msg + MAX_PACKET_SIZE - sizeof(xdr_data_t))) {
306 // First elements is the ID
307 xdr_data_t xdr = XDR_encode_uint32((*it)->id);
308 memcpy(ptr, &xdr, sizeof(xdr_data_t));
309 ptr += sizeof(xdr_data_t);
311 // The actual data representation depends on the type
312 switch ((*it)->type) {
313 case SGPropertyNode::INT:
314 case SGPropertyNode::BOOL:
315 case SGPropertyNode::LONG:
316 xdr = XDR_encode_uint32((*it)->int_value);
317 memcpy(ptr, &xdr, sizeof(xdr_data_t));
318 ptr += sizeof(xdr_data_t);
319 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
321 case SGPropertyNode::FLOAT:
322 case SGPropertyNode::DOUBLE:
323 xdr = XDR_encode_float((*it)->float_value);;
324 memcpy(ptr, &xdr, sizeof(xdr_data_t));
325 ptr += sizeof(xdr_data_t);
326 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
328 case SGPropertyNode::STRING:
329 case SGPropertyNode::UNSPECIFIED:
331 // String is complicated. It consists of
332 // The length of the string
334 // Padding to the nearest 4-bytes.
335 const char* lcharptr = (*it)->string_value;
340 ////cout << "String length: " << strlen(lcharptr) << "\n";
341 uint32_t len = strlen(lcharptr);
342 //cout << "String length unint32: " << len << "\n";
343 xdr = XDR_encode_uint32(len);
344 memcpy(ptr, &xdr, sizeof(xdr_data_t));
345 ptr += sizeof(xdr_data_t);
350 // Now the text itself
352 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
354 xdr = XDR_encode_int8(*lcharptr);
355 memcpy(ptr, &xdr, sizeof(xdr_data_t));
356 ptr += sizeof(xdr_data_t);
361 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
363 // Now pad if required
364 while ((lcount % 4) != 0)
366 xdr = XDR_encode_int8(0);
367 memcpy(ptr, &xdr, sizeof(xdr_data_t));
368 ptr += sizeof(xdr_data_t);
379 xdr = XDR_encode_uint32(0);
380 memcpy(ptr, &xdr, sizeof(xdr_data_t));
381 ptr += sizeof(xdr_data_t);
382 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
389 //cout << " Unknown Type: " << (*it)->type << "\n";
390 xdr = XDR_encode_float((*it)->float_value);;
391 memcpy(ptr, &xdr, sizeof(xdr_data_t));
392 ptr += sizeof(xdr_data_t);
393 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
401 FillMsgHdr(&MsgHdr, POS_DATA_ID, ptr - Msg);
402 memcpy(Msg, &MsgHdr, sizeof(T_MsgHdr));
404 mSocket->sendto(Msg, ptr - Msg, 0, &mServer);
405 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
406 } // FGMultiplayMgr::SendMyPosition()
407 //////////////////////////////////////////////////////////////////////
409 //////////////////////////////////////////////////////////////////////
411 // Name: SendTextMessage
412 // Description: Sends a message to the player. The message must
413 // contain a valid and correctly filled out header and optional
416 //////////////////////////////////////////////////////////////////////
418 FGMultiplayMgr::SendTextMessage(const string &MsgText)
420 if (!mInitialised || !mHaveServer)
424 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
425 //////////////////////////////////////////////////
426 // Divide the text string into blocks that fit
427 // in the message and send the blocks.
428 //////////////////////////////////////////////////
429 unsigned iNextBlockPosition = 0;
432 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
433 while (iNextBlockPosition < MsgText.length()) {
434 strncpy (ChatMsg.Text,
435 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
437 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
438 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
439 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
440 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
441 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
446 } // FGMultiplayMgr::SendTextMessage ()
447 //////////////////////////////////////////////////////////////////////
449 //////////////////////////////////////////////////////////////////////
452 // Description: Processes data waiting at the receive socket. The
453 // processing ends when there is no more data at the socket.
455 //////////////////////////////////////////////////////////////////////
457 FGMultiplayMgr::Update(void)
463 SGTimeStamp timestamper;
465 long stamp = timestamper.get_seconds();
467 //////////////////////////////////////////////////
468 // Read the receive socket and process any data
469 //////////////////////////////////////////////////
472 char Msg[MAX_PACKET_SIZE];
473 //////////////////////////////////////////////////
474 // Although the recv call asks for
475 // MAX_PACKET_SIZE of data, the number of bytes
476 // returned will only be that of the next
477 // packet waiting to be processed.
478 //////////////////////////////////////////////////
479 netAddress SenderAddress;
480 bytes = mSocket->recvfrom(Msg, sizeof(Msg), 0, &SenderAddress);
481 //////////////////////////////////////////////////
483 //////////////////////////////////////////////////
485 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
486 perror("FGMultiplayMgr::MP_ProcessData");
489 if (bytes <= sizeof(T_MsgHdr)) {
490 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
491 << "received message with insufficient data" );
494 //////////////////////////////////////////////////
496 //////////////////////////////////////////////////
497 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
498 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
499 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
500 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
501 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
502 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
503 if (MsgHdr->Magic != MSG_MAGIC) {
504 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
505 << "message has invalid magic number!" );
507 if (MsgHdr->Version != PROTO_VER) {
508 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
509 << "message has invalid protocoll number!" );
511 if (MsgHdr->MsgLen != bytes) {
512 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
513 << "message has invalid length!" );
515 //////////////////////////////////////////////////
517 //////////////////////////////////////////////////
518 switch (MsgHdr->MsgId) {
520 ProcessChatMsg(Msg, SenderAddress);
523 ProcessPosMsg(Msg, SenderAddress, bytes, stamp);
525 case UNUSABLE_POS_DATA_ID:
526 case OLD_OLD_POS_DATA_ID:
527 case OLD_PROP_MSG_ID:
528 case OLD_POS_DATA_ID:
531 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
532 << "Unknown message Id received: " << MsgHdr->MsgId );
538 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
539 while (it != mMultiPlayerMap.end()) {
540 if (it->second->getLastTimestamp() + 10 < stamp) {
541 std::string name = it->first;
542 it->second->setDie(true);
543 mMultiPlayerMap.erase(it);
544 it = mMultiPlayerMap.upper_bound(name);
548 } // FGMultiplayMgr::ProcessData(void)
549 //////////////////////////////////////////////////////////////////////
551 //////////////////////////////////////////////////////////////////////
553 // handle a position message
555 //////////////////////////////////////////////////////////////////////
557 FGMultiplayMgr::ProcessPosMsg(const char *Msg, netAddress & SenderAddress,
558 unsigned len, long stamp)
560 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
561 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
562 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
563 << "Position message received with insufficient data" );
566 T_PositionMsg* PosMsg = (T_PositionMsg *)(Msg + sizeof(T_MsgHdr));
567 FGExternalMotionData motionInfo;
568 motionInfo.time = XDR_decode_double(PosMsg->time);
569 motionInfo.lag = XDR_decode_double(PosMsg->lag);
570 for (unsigned i = 0; i < 3; ++i)
571 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
573 for (unsigned i = 0; i < 3; ++i)
574 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
575 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
576 for (unsigned i = 0; i < 3; ++i)
577 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
578 for (unsigned i = 0; i < 3; ++i)
579 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
580 for (unsigned i = 0; i < 3; ++i)
581 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
582 for (unsigned i = 0; i < 3; ++i)
583 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
586 //cout << "INPUT MESSAGE\n";
587 xdr_data_t* xdr = (xdr_data_t*)
588 (Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg));
589 while ((char*)xdr < Msg + len) {
590 FGPropertyData* pData = new FGPropertyData;
591 SGPropertyNode::Type type = SGPropertyNode::UNSPECIFIED;
593 // First element is always the ID
594 pData->id = XDR_decode_uint32(*xdr);
595 //cout << pData->id << " ";
598 // Check the ID actually exists and get the type
601 while (FGMultiplayMgr::sIdPropertyList[i].name)
603 if (sIdPropertyList[i].id == pData->id)
606 pData->type = sIdPropertyList[i].type;
614 // How we decode the remainder of the property depends on the type
615 switch (pData->type) {
616 case SGPropertyNode::INT:
617 case SGPropertyNode::BOOL:
618 case SGPropertyNode::LONG:
619 pData->int_value = XDR_decode_uint32(*xdr);
621 //cout << pData->int_value << "\n";
623 case SGPropertyNode::FLOAT:
624 case SGPropertyNode::DOUBLE:
625 pData->float_value = XDR_decode_float(*xdr);
627 //cout << pData->float_value << "\n";
629 case SGPropertyNode::STRING:
630 case SGPropertyNode::UNSPECIFIED:
632 // String is complicated. It consists of
633 // The length of the string
635 // Padding to the nearest 4-bytes.
636 uint32_t length = XDR_decode_uint32(*xdr);
638 //cout << length << " ";
640 if ((length > 0) && (length < MAX_TEXT_SIZE))
642 pData->string_value = new char[length + 1];
643 //cout << " String: ";
645 for (int i = 0; i < length; i++)
647 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
649 //cout << pData->string_value[i];
652 pData->string_value[length] = '\0';
654 // Now handle the padding
655 while ((length % 4) != 0)
664 pData->string_value = new char[1];
665 pData->string_value[0] = '\0';
673 pData->float_value = XDR_decode_float(*xdr);
674 cerr << "Unknown Prop type " << pData->id << " " << pData->type << "\n";
679 motionInfo.properties.push_back(pData);
683 // We failed to find the property. We'll try the next packet immediately.
684 //cout << " Unknown\n";
688 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
690 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
691 mp->addMotionInfo(motionInfo, stamp);
692 } // FGMultiplayMgr::ProcessPosMsg()
693 //////////////////////////////////////////////////////////////////////
695 //////////////////////////////////////////////////////////////////////
697 // handle a chat message
698 // FIXME: display chat message withi flightgear
700 //////////////////////////////////////////////////////////////////////
702 FGMultiplayMgr::ProcessChatMsg(const char *Msg, netAddress& SenderAddress)
704 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
705 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
706 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
707 << "Chat message received with insufficient data" );
711 char *MsgBuf = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
712 strncpy(MsgBuf, ((T_ChatMsg *)(Msg + sizeof(T_MsgHdr)))->Text,
713 MsgHdr->MsgLen - sizeof(T_MsgHdr));
714 MsgBuf[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
716 T_ChatMsg* ChatMsg = (T_ChatMsg *)(Msg + sizeof(T_MsgHdr));
717 SG_LOG ( SG_NETWORK, SG_ALERT, "Chat [" << MsgHdr->Callsign << "]"
718 << " " << MsgBuf << endl);
721 } // FGMultiplayMgr::ProcessChatMsg ()
722 //////////////////////////////////////////////////////////////////////
725 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
730 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
736 len = sizeof(T_MsgHdr);
739 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
740 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
741 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
742 MsgHdr->MsgLen = XDR_encode_uint32(len);
743 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
744 MsgHdr->ReplyPort = 0; // now
745 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
746 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
750 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
751 const std::string& modelName)
753 if (0 < mMultiPlayerMap.count(callsign))
754 return mMultiPlayerMap[callsign];
756 FGAIMultiplayer* mp = new FGAIMultiplayer;
757 mp->setPath(modelName.c_str());
758 mp->setCallSign(callsign);
759 mMultiPlayerMap[callsign] = mp;
761 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
765 /// FIXME: that must follow the attach ATM ...
767 while (sIdPropertyList[i].name) {
768 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
777 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
779 if (0 < mMultiPlayerMap.count(callsign))
780 return mMultiPlayerMap[callsign];