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},
67 {200, "gear/gear[0]/compression-norm", SGPropertyNode::FLOAT},
68 {201, "gear/gear[0]/position-norm", SGPropertyNode::FLOAT},
69 {210, "gear/gear[1]/compression-norm", SGPropertyNode::FLOAT},
70 {211, "gear/gear[1]/position-norm", SGPropertyNode::FLOAT},
71 {220, "gear/gear[2]/compression-norm", SGPropertyNode::FLOAT},
72 {221, "gear/gear[2]/position-norm", SGPropertyNode::FLOAT},
73 {230, "gear/gear[3]/compression-norm", SGPropertyNode::FLOAT},
74 {231, "gear/gear[3]/position-norm", SGPropertyNode::FLOAT},
75 {240, "gear/gear[4]/compression-norm", SGPropertyNode::FLOAT},
76 {241, "gear/gear[4]/position-norm", SGPropertyNode::FLOAT},
78 {300, "engines/engine[0]/n1", SGPropertyNode::FLOAT},
79 {301, "engines/engine[0]/n2", SGPropertyNode::FLOAT},
80 {302, "engines/engine[0]/rpm", SGPropertyNode::FLOAT},
81 {310, "engines/engine[1]/n1", SGPropertyNode::FLOAT},
82 {311, "engines/engine[1]/n2", SGPropertyNode::FLOAT},
83 {312, "engines/engine[1]/rpm", SGPropertyNode::FLOAT},
84 {320, "engines/engine[2]/n1", SGPropertyNode::FLOAT},
85 {321, "engines/engine[2]/n2", SGPropertyNode::FLOAT},
86 {322, "engines/engine[2]/rpm", SGPropertyNode::FLOAT},
87 {330, "engines/engine[3]/n1", SGPropertyNode::FLOAT},
88 {331, "engines/engine[3]/n2", SGPropertyNode::FLOAT},
89 {332, "engines/engine[3]/rpm", SGPropertyNode::FLOAT},
90 {340, "engines/engine[4]/n1", SGPropertyNode::FLOAT},
91 {341, "engines/engine[4]/n2", SGPropertyNode::FLOAT},
92 {342, "engines/engine[4]/rpm", SGPropertyNode::FLOAT},
93 {350, "engines/engine[5]/n1", SGPropertyNode::FLOAT},
94 {351, "engines/engine[5]/n2", SGPropertyNode::FLOAT},
95 {352, "engines/engine[5]/rpm", SGPropertyNode::FLOAT},
96 {360, "engines/engine[6]/n1", SGPropertyNode::FLOAT},
97 {361, "engines/engine[6]/n2", SGPropertyNode::FLOAT},
98 {362, "engines/engine[6]/rpm", SGPropertyNode::FLOAT},
99 {370, "engines/engine[7]/n1", SGPropertyNode::FLOAT},
100 {371, "engines/engine[7]/n2", SGPropertyNode::FLOAT},
101 {372, "engines/engine[7]/rpm", SGPropertyNode::FLOAT},
102 {380, "engines/engine[8]/n1", SGPropertyNode::FLOAT},
103 {381, "engines/engine[8]/n2", SGPropertyNode::FLOAT},
104 {382, "engines/engine[8]/rpm", SGPropertyNode::FLOAT},
105 {390, "engines/engine[9]/n1", SGPropertyNode::FLOAT},
106 {391, "engines/engine[9]/n2", SGPropertyNode::FLOAT},
107 {392, "engines/engine[9]/rpm", SGPropertyNode::FLOAT},
109 {800, "rotors/main/rpm", SGPropertyNode::FLOAT},
110 {801, "rotors/tail/rpm", SGPropertyNode::FLOAT},
111 {810, "rotors/main/blade[0]/position-deg", SGPropertyNode::FLOAT},
112 {811, "rotors/main/blade[1]/position-deg", SGPropertyNode::FLOAT},
113 {812, "rotors/main/blade[2]/position-deg", SGPropertyNode::FLOAT},
114 {813, "rotors/main/blade[3]/position-deg", SGPropertyNode::FLOAT},
115 {820, "rotors/main/blade[0]/flap-deg", SGPropertyNode::FLOAT},
116 {821, "rotors/main/blade[1]/flap-deg", SGPropertyNode::FLOAT},
117 {822, "rotors/main/blade[2]/flap-deg", SGPropertyNode::FLOAT},
118 {823, "rotors/main/blade[3]/flap-deg", SGPropertyNode::FLOAT},
119 {830, "rotors/tail/blade[0]/position-deg", SGPropertyNode::FLOAT},
120 {831, "rotors/tail/blade[1]/position-deg", SGPropertyNode::FLOAT},
122 {1001, "controls/flight/slats", SGPropertyNode::FLOAT},
123 {1002, "controls/flight/speedbrake", SGPropertyNode::FLOAT},
124 {1003, "controls/flight/spoilers", SGPropertyNode::FLOAT},
125 {1004, "controls/gear/gear-down", SGPropertyNode::FLOAT},
126 {1005, "controls/lighting/nav-lights", SGPropertyNode::FLOAT},
128 {10001, "sim/multiplay/transmission-freq-hz", SGPropertyNode::STRING},
129 {10002, "sim/multiplay/chat", SGPropertyNode::STRING},
132 {0, 0, SGPropertyNode::UNSPECIFIED}
135 //////////////////////////////////////////////////////////////////////
137 // MultiplayMgr constructor
139 //////////////////////////////////////////////////////////////////////
140 FGMultiplayMgr::FGMultiplayMgr()
142 mInitialised = false;
144 } // FGMultiplayMgr::FGMultiplayMgr()
145 //////////////////////////////////////////////////////////////////////
147 //////////////////////////////////////////////////////////////////////
149 // MultiplayMgr destructor
151 //////////////////////////////////////////////////////////////////////
152 FGMultiplayMgr::~FGMultiplayMgr()
155 } // FGMultiplayMgr::~FGMultiplayMgr()
156 //////////////////////////////////////////////////////////////////////
158 //////////////////////////////////////////////////////////////////////
162 //////////////////////////////////////////////////////////////////////
164 FGMultiplayMgr::init (void)
166 //////////////////////////////////////////////////
167 // Initialise object if not already done
168 //////////////////////////////////////////////////
170 SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
173 //////////////////////////////////////////////////
174 // Set members from property values
175 //////////////////////////////////////////////////
176 short rxPort = fgGetInt("/sim/multiplay/rxport");
179 mCallsign = fgGetString("/sim/multiplay/callsign");
180 if (mCallsign.empty())
181 // FIXME: use getpwuid
182 mCallsign = "JohnDoe";
183 string rxAddress = fgGetString("/sim/multiplay/rxhost");
184 if (rxAddress.empty())
185 rxAddress = "127.0.0.1";
186 short txPort = fgGetInt("/sim/multiplay/txport");
187 string txAddress = fgGetString("/sim/multiplay/txhost");
188 if (txPort > 0 && !txAddress.empty()) {
190 mServer.set(txAddress.c_str(), txPort);
192 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
193 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
194 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
195 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
196 SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
197 mSocket = new netSocket();
198 if (!mSocket->open(false)) {
199 SG_LOG( SG_NETWORK, SG_ALERT,
200 "FGMultiplayMgr::init - Failed to create data socket" );
203 mSocket->setBlocking(false);
204 mSocket->setBroadcast(true);
205 if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
207 SG_LOG( SG_NETWORK, SG_ALERT,
208 "FGMultiplayMgr::Open - Failed to bind receive socket" );
213 } // FGMultiplayMgr::init()
214 //////////////////////////////////////////////////////////////////////
216 //////////////////////////////////////////////////////////////////////
218 // Closes and deletes the local player object. Closes
219 // and deletes the tx socket. Resets the object state to unitialised.
221 //////////////////////////////////////////////////////////////////////
223 FGMultiplayMgr::Close (void)
225 mMultiPlayerMap.clear();
232 mInitialised = false;
233 } // FGMultiplayMgr::Close(void)
234 //////////////////////////////////////////////////////////////////////
236 //////////////////////////////////////////////////////////////////////
238 // Description: Sends the position data for the local position.
240 //////////////////////////////////////////////////////////////////////
242 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
244 if ((! mInitialised) || (! mHaveServer)) {
246 SG_LOG( SG_NETWORK, SG_ALERT,
247 "FGMultiplayMgr::SendMyPosition - not initialised" );
249 SG_LOG( SG_NETWORK, SG_ALERT,
250 "FGMultiplayMgr::SendMyPosition - no server" );
254 T_PositionMsg PosMsg;
255 strncpy(PosMsg.Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
256 PosMsg.Model[MAX_MODEL_NAME_LEN - 1] = '\0';
258 PosMsg.time = XDR_encode_double (motionInfo.time);
259 PosMsg.lag = XDR_encode_double (motionInfo.lag);
260 for (unsigned i = 0 ; i < 3; ++i)
261 PosMsg.position[i] = XDR_encode_double (motionInfo.position(i));
263 motionInfo.orientation.getAngleAxis(angleAxis);
264 for (unsigned i = 0 ; i < 3; ++i)
265 PosMsg.orientation[i] = XDR_encode_float (angleAxis(i));
266 for (unsigned i = 0 ; i < 3; ++i)
267 PosMsg.linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
268 for (unsigned i = 0 ; i < 3; ++i)
269 PosMsg.angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
270 for (unsigned i = 0 ; i < 3; ++i)
271 PosMsg.linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
272 for (unsigned i = 0 ; i < 3; ++i)
273 PosMsg.angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
275 char Msg[MAX_PACKET_SIZE];
276 memcpy(Msg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
278 char* ptr = Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg);
279 std::vector<FGPropertyData*>::const_iterator it;
280 it = motionInfo.properties.begin();
281 //cout << "OUTPUT PROPERTIES\n";
282 while (it != motionInfo.properties.end()
283 && ptr < (Msg + MAX_PACKET_SIZE - sizeof(xdr_data_t))) {
285 // First elements is the ID
286 xdr_data_t xdr = XDR_encode_uint32((*it)->id);
287 memcpy(ptr, &xdr, sizeof(xdr_data_t));
288 ptr += sizeof(xdr_data_t);
290 // The actual data representation depends on the type
291 switch ((*it)->type) {
292 case SGPropertyNode::INT:
293 case SGPropertyNode::BOOL:
294 case SGPropertyNode::LONG:
295 xdr = XDR_encode_uint32((*it)->int_value);
296 memcpy(ptr, &xdr, sizeof(xdr_data_t));
297 ptr += sizeof(xdr_data_t);
298 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
300 case SGPropertyNode::FLOAT:
301 case SGPropertyNode::DOUBLE:
302 xdr = XDR_encode_float((*it)->float_value);;
303 memcpy(ptr, &xdr, sizeof(xdr_data_t));
304 ptr += sizeof(xdr_data_t);
305 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
307 case SGPropertyNode::STRING:
308 case SGPropertyNode::UNSPECIFIED:
310 // String is complicated. It consists of
311 // The length of the string
313 // Padding to the nearest 4-bytes.
314 const char* lcharptr = (*it)->string_value;
319 ////cout << "String length: " << strlen(lcharptr) << "\n";
320 uint32_t len = strlen(lcharptr);
321 //cout << "String length unint32: " << len << "\n";
322 xdr = XDR_encode_uint32(len);
323 memcpy(ptr, &xdr, sizeof(xdr_data_t));
324 ptr += sizeof(xdr_data_t);
329 // Now the text itself
331 while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
333 xdr = XDR_encode_int8(*lcharptr);
334 memcpy(ptr, &xdr, sizeof(xdr_data_t));
335 ptr += sizeof(xdr_data_t);
340 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
342 // Now pad if required
343 while ((lcount % 4) != 0)
345 xdr = XDR_encode_int8(0);
346 memcpy(ptr, &xdr, sizeof(xdr_data_t));
347 ptr += sizeof(xdr_data_t);
358 xdr = XDR_encode_uint32(0);
359 memcpy(ptr, &xdr, sizeof(xdr_data_t));
360 ptr += sizeof(xdr_data_t);
361 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
368 //cout << " Unknown Type: " << (*it)->type << "\n";
369 xdr = XDR_encode_float((*it)->float_value);;
370 memcpy(ptr, &xdr, sizeof(xdr_data_t));
371 ptr += sizeof(xdr_data_t);
372 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
380 FillMsgHdr(&MsgHdr, POS_DATA_ID, ptr - Msg);
381 memcpy(Msg, &MsgHdr, sizeof(T_MsgHdr));
383 mSocket->sendto(Msg, ptr - Msg, 0, &mServer);
384 SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
385 } // FGMultiplayMgr::SendMyPosition()
386 //////////////////////////////////////////////////////////////////////
388 //////////////////////////////////////////////////////////////////////
390 // Name: SendTextMessage
391 // Description: Sends a message to the player. The message must
392 // contain a valid and correctly filled out header and optional
395 //////////////////////////////////////////////////////////////////////
397 FGMultiplayMgr::SendTextMessage(const string &MsgText)
399 if (!mInitialised || !mHaveServer)
403 FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
404 //////////////////////////////////////////////////
405 // Divide the text string into blocks that fit
406 // in the message and send the blocks.
407 //////////////////////////////////////////////////
408 unsigned iNextBlockPosition = 0;
411 char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
412 while (iNextBlockPosition < MsgText.length()) {
413 strncpy (ChatMsg.Text,
414 MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
416 ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
417 memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
418 memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
419 mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
420 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
425 } // FGMultiplayMgr::SendTextMessage ()
426 //////////////////////////////////////////////////////////////////////
428 //////////////////////////////////////////////////////////////////////
431 // Description: Processes data waiting at the receive socket. The
432 // processing ends when there is no more data at the socket.
434 //////////////////////////////////////////////////////////////////////
436 FGMultiplayMgr::Update(void)
442 SGTimeStamp timestamper;
444 long stamp = timestamper.get_seconds();
446 //////////////////////////////////////////////////
447 // Read the receive socket and process any data
448 //////////////////////////////////////////////////
451 char Msg[MAX_PACKET_SIZE];
452 //////////////////////////////////////////////////
453 // Although the recv call asks for
454 // MAX_PACKET_SIZE of data, the number of bytes
455 // returned will only be that of the next
456 // packet waiting to be processed.
457 //////////////////////////////////////////////////
458 netAddress SenderAddress;
459 bytes = mSocket->recvfrom(Msg, sizeof(Msg), 0, &SenderAddress);
460 //////////////////////////////////////////////////
462 //////////////////////////////////////////////////
464 if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
465 perror("FGMultiplayMgr::MP_ProcessData");
468 if (bytes <= sizeof(T_MsgHdr)) {
469 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
470 << "received message with insufficient data" );
473 //////////////////////////////////////////////////
475 //////////////////////////////////////////////////
476 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
477 MsgHdr->Magic = XDR_decode_uint32 (MsgHdr->Magic);
478 MsgHdr->Version = XDR_decode_uint32 (MsgHdr->Version);
479 MsgHdr->MsgId = XDR_decode_uint32 (MsgHdr->MsgId);
480 MsgHdr->MsgLen = XDR_decode_uint32 (MsgHdr->MsgLen);
481 MsgHdr->ReplyPort = XDR_decode_uint32 (MsgHdr->ReplyPort);
482 if (MsgHdr->Magic != MSG_MAGIC) {
483 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
484 << "message has invalid magic number!" );
486 if (MsgHdr->Version != PROTO_VER) {
487 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
488 << "message has invalid protocoll number!" );
490 if (MsgHdr->MsgLen != bytes) {
491 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
492 << "message has invalid length!" );
494 //////////////////////////////////////////////////
496 //////////////////////////////////////////////////
497 switch (MsgHdr->MsgId) {
499 ProcessChatMsg(Msg, SenderAddress);
502 ProcessPosMsg(Msg, SenderAddress, bytes, stamp);
504 case UNUSABLE_POS_DATA_ID:
505 case OLD_OLD_POS_DATA_ID:
506 case OLD_PROP_MSG_ID:
507 case OLD_POS_DATA_ID:
510 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
511 << "Unknown message Id received: " << MsgHdr->MsgId );
517 MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
518 while (it != mMultiPlayerMap.end()) {
519 if (it->second->getLastTimestamp() + 10 < stamp) {
520 std::string name = it->first;
521 it->second->setDie(true);
522 mMultiPlayerMap.erase(it);
523 it = mMultiPlayerMap.upper_bound(name);
527 } // FGMultiplayMgr::ProcessData(void)
528 //////////////////////////////////////////////////////////////////////
530 //////////////////////////////////////////////////////////////////////
532 // handle a position message
534 //////////////////////////////////////////////////////////////////////
536 FGMultiplayMgr::ProcessPosMsg(const char *Msg, netAddress & SenderAddress,
537 unsigned len, long stamp)
539 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
540 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
541 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
542 << "Position message received with insufficient data" );
545 T_PositionMsg* PosMsg = (T_PositionMsg *)(Msg + sizeof(T_MsgHdr));
546 FGExternalMotionData motionInfo;
547 motionInfo.time = XDR_decode_double(PosMsg->time);
548 motionInfo.lag = XDR_decode_double(PosMsg->lag);
549 for (unsigned i = 0; i < 3; ++i)
550 motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
552 for (unsigned i = 0; i < 3; ++i)
553 angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
554 motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
555 for (unsigned i = 0; i < 3; ++i)
556 motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
557 for (unsigned i = 0; i < 3; ++i)
558 motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
559 for (unsigned i = 0; i < 3; ++i)
560 motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
561 for (unsigned i = 0; i < 3; ++i)
562 motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
565 //cout << "INPUT MESSAGE\n";
566 xdr_data_t* xdr = (xdr_data_t*)
567 (Msg + sizeof(T_MsgHdr) + sizeof(T_PositionMsg));
568 while ((char*)xdr < Msg + len) {
569 FGPropertyData* pData = new FGPropertyData;
570 SGPropertyNode::Type type = SGPropertyNode::UNSPECIFIED;
572 // First element is always the ID
573 pData->id = XDR_decode_uint32(*xdr);
574 //cout << pData->id << " ";
577 // Check the ID actually exists and get the type
580 while (FGMultiplayMgr::sIdPropertyList[i].name)
582 if (sIdPropertyList[i].id == pData->id)
585 pData->type = sIdPropertyList[i].type;
593 // How we decode the remainder of the property depends on the type
594 switch (pData->type) {
595 case SGPropertyNode::INT:
596 case SGPropertyNode::BOOL:
597 case SGPropertyNode::LONG:
598 pData->int_value = XDR_decode_uint32(*xdr);
600 //cout << pData->int_value << "\n";
602 case SGPropertyNode::FLOAT:
603 case SGPropertyNode::DOUBLE:
604 pData->float_value = XDR_decode_float(*xdr);
606 //cout << pData->float_value << "\n";
608 case SGPropertyNode::STRING:
609 case SGPropertyNode::UNSPECIFIED:
611 // String is complicated. It consists of
612 // The length of the string
614 // Padding to the nearest 4-bytes.
615 uint32_t length = XDR_decode_uint32(*xdr);
617 //cout << length << " ";
619 if ((length > 0) && (length < MAX_TEXT_SIZE))
621 pData->string_value = new char[length + 1];
622 //cout << " String: ";
624 for (int i = 0; i < length; i++)
626 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
628 //cout << pData->string_value[i];
631 pData->string_value[length] = '\0';
633 // Now handle the padding
634 while ((length % 4) != 0)
643 pData->string_value = new char[1];
644 pData->string_value[0] = '\0';
652 pData->float_value = XDR_decode_float(*xdr);
653 cerr << "Unknown Prop type " << pData->id << " " << pData->type << "\n";
658 motionInfo.properties.push_back(pData);
662 // We failed to find the property. We'll try the next packet immediately.
663 //cout << " Unknown\n";
667 FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
669 mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
670 mp->addMotionInfo(motionInfo, stamp);
671 } // FGMultiplayMgr::ProcessPosMsg()
672 //////////////////////////////////////////////////////////////////////
674 //////////////////////////////////////////////////////////////////////
676 // handle a chat message
677 // FIXME: display chat message withi flightgear
679 //////////////////////////////////////////////////////////////////////
681 FGMultiplayMgr::ProcessChatMsg(const char *Msg, netAddress& SenderAddress)
683 T_MsgHdr* MsgHdr = (T_MsgHdr *)Msg;
684 if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
685 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
686 << "Chat message received with insufficient data" );
690 char *MsgBuf = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
691 strncpy(MsgBuf, ((T_ChatMsg *)(Msg + sizeof(T_MsgHdr)))->Text,
692 MsgHdr->MsgLen - sizeof(T_MsgHdr));
693 MsgBuf[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
695 T_ChatMsg* ChatMsg = (T_ChatMsg *)(Msg + sizeof(T_MsgHdr));
696 SG_LOG ( SG_NETWORK, SG_ALERT, "Chat [" << MsgHdr->Callsign << "]"
697 << " " << MsgBuf << endl);
700 } // FGMultiplayMgr::ProcessChatMsg ()
701 //////////////////////////////////////////////////////////////////////
704 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
709 len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
715 len = sizeof(T_MsgHdr);
718 MsgHdr->Magic = XDR_encode_uint32(MSG_MAGIC);
719 MsgHdr->Version = XDR_encode_uint32(PROTO_VER);
720 MsgHdr->MsgId = XDR_encode_uint32(MsgId);
721 MsgHdr->MsgLen = XDR_encode_uint32(len);
722 MsgHdr->ReplyAddress = 0; // Are obsolete, keep them for the server for
723 MsgHdr->ReplyPort = 0; // now
724 strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
725 MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
729 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
730 const std::string& modelName)
732 if (0 < mMultiPlayerMap.count(callsign))
733 return mMultiPlayerMap[callsign];
735 FGAIMultiplayer* mp = new FGAIMultiplayer;
736 mp->setPath(modelName.c_str());
737 mp->setCallSign(callsign);
738 mMultiPlayerMap[callsign] = mp;
740 FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
744 /// FIXME: that must follow the attach ATM ...
746 while (sIdPropertyList[i].name) {
747 mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
756 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
758 if (0 < mMultiPlayerMap.count(callsign))
759 return mMultiPlayerMap[callsign];