#include <config.h>
#endif
+#include <iostream>
+#include <algorithm>
#include <plib/netSocket.h>
#include <simgear/misc/stdint.hxx>
#include "multiplaymgr.hxx"
#include "mpmessages.hxx"
+using std::cerr;
+
#define MAX_PACKET_SIZE 1200
#define MAX_TEXT_SIZE 128
// This should be extendable dynamically for every specific aircraft ...
// For now only that static list
FGMultiplayMgr::IdPropertyList
-FGMultiplayMgr::sIdPropertyList[] = {
+const FGMultiplayMgr::sIdPropertyList[] = {
{100, "surface-positions/left-aileron-pos-norm", SGPropertyNode::FLOAT},
{101, "surface-positions/right-aileron-pos-norm", SGPropertyNode::FLOAT},
{102, "surface-positions/elevator-pos-norm", SGPropertyNode::FLOAT},
{104, "surface-positions/flap-pos-norm", SGPropertyNode::FLOAT},
{105, "surface-positions/speedbrake-pos-norm", SGPropertyNode::FLOAT},
{106, "gear/tailhook/position-norm", SGPropertyNode::FLOAT},
+ {107, "gear/launchbar/position-norm", SGPropertyNode::FLOAT},
+ {108, "gear/launchbar/state", SGPropertyNode::STRING},
+ {109, "gear/launchbar/holdback-position-norm", SGPropertyNode::FLOAT},
+ {110, "canopy/position-norm", SGPropertyNode::FLOAT},
+ {111, "surface-positions/wing-pos-norm", SGPropertyNode::FLOAT},
+ {112, "surface-positions/wing-fold-pos-norm", SGPropertyNode::FLOAT},
{200, "gear/gear[0]/compression-norm", SGPropertyNode::FLOAT},
{201, "gear/gear[0]/position-norm", SGPropertyNode::FLOAT},
{830, "rotors/tail/blade[0]/position-deg", SGPropertyNode::FLOAT},
{831, "rotors/tail/blade[1]/position-deg", SGPropertyNode::FLOAT},
+ {900, "sim/hitches/aerotow/tow/length", SGPropertyNode::FLOAT},
+ {901, "sim/hitches/aerotow/tow/elastic-constant", SGPropertyNode::FLOAT},
+ {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m", SGPropertyNode::FLOAT},
+ {903, "sim/hitches/aerotow/tow/dist", SGPropertyNode::FLOAT},
+ {904, "sim/hitches/aerotow/tow/connected-to-property-node", SGPropertyNode::BOOL},
+ {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign", SGPropertyNode::STRING},
+ {906, "sim/hitches/aerotow/tow/brake-force", SGPropertyNode::FLOAT},
+ {907, "sim/hitches/aerotow/tow/end-force-x", SGPropertyNode::FLOAT},
+ {908, "sim/hitches/aerotow/tow/end-force-y", SGPropertyNode::FLOAT},
+ {909, "sim/hitches/aerotow/tow/end-force-z", SGPropertyNode::FLOAT},
+ {930, "sim/hitches/aerotow/is-slave", SGPropertyNode::BOOL},
+ {931, "sim/hitches/aerotow/speed-in-tow-direction", SGPropertyNode::FLOAT},
+ {932, "sim/hitches/aerotow/open", SGPropertyNode::BOOL},
+ {933, "sim/hitches/aerotow/local-pos-x", SGPropertyNode::FLOAT},
+ {934, "sim/hitches/aerotow/local-pos-y", SGPropertyNode::FLOAT},
+ {935, "sim/hitches/aerotow/local-pos-z", SGPropertyNode::FLOAT},
+
{1001, "controls/flight/slats", SGPropertyNode::FLOAT},
{1002, "controls/flight/speedbrake", SGPropertyNode::FLOAT},
{1003, "controls/flight/spoilers", SGPropertyNode::FLOAT},
{1004, "controls/gear/gear-down", SGPropertyNode::FLOAT},
{1005, "controls/lighting/nav-lights", SGPropertyNode::FLOAT},
-
+ {1006, "controls/armament/station[0]/jettison-all", SGPropertyNode::BOOL},
+
+ {1100, "sim/model/variant", SGPropertyNode::INT},
+ {1101, "sim/model/livery/file", SGPropertyNode::STRING},
+
{10001, "sim/multiplay/transmission-freq-hz", SGPropertyNode::STRING},
{10002, "sim/multiplay/chat", SGPropertyNode::STRING},
- /// termination
- {0, 0, SGPropertyNode::UNSPECIFIED}
+ {10100, "sim/multiplay/generic/string[0]", SGPropertyNode::STRING},
+ {10101, "sim/multiplay/generic/string[1]", SGPropertyNode::STRING},
+ {10102, "sim/multiplay/generic/string[2]", SGPropertyNode::STRING},
+ {10103, "sim/multiplay/generic/string[3]", SGPropertyNode::STRING},
+ {10104, "sim/multiplay/generic/string[4]", SGPropertyNode::STRING},
+ {10105, "sim/multiplay/generic/string[5]", SGPropertyNode::STRING},
+ {10106, "sim/multiplay/generic/string[6]", SGPropertyNode::STRING},
+ {10107, "sim/multiplay/generic/string[7]", SGPropertyNode::STRING},
+ {10108, "sim/multiplay/generic/string[8]", SGPropertyNode::STRING},
+ {10109, "sim/multiplay/generic/string[9]", SGPropertyNode::STRING},
+ {10110, "sim/multiplay/generic/string[10]", SGPropertyNode::STRING},
+ {10111, "sim/multiplay/generic/string[11]", SGPropertyNode::STRING},
+ {10112, "sim/multiplay/generic/string[12]", SGPropertyNode::STRING},
+ {10113, "sim/multiplay/generic/string[13]", SGPropertyNode::STRING},
+ {10114, "sim/multiplay/generic/string[14]", SGPropertyNode::STRING},
+ {10115, "sim/multiplay/generic/string[15]", SGPropertyNode::STRING},
+ {10116, "sim/multiplay/generic/string[16]", SGPropertyNode::STRING},
+ {10117, "sim/multiplay/generic/string[17]", SGPropertyNode::STRING},
+ {10118, "sim/multiplay/generic/string[18]", SGPropertyNode::STRING},
+ {10119, "sim/multiplay/generic/string[19]", SGPropertyNode::STRING},
+
+ {10200, "sim/multiplay/generic/float[0]", SGPropertyNode::FLOAT},
+ {10201, "sim/multiplay/generic/float[1]", SGPropertyNode::FLOAT},
+ {10202, "sim/multiplay/generic/float[2]", SGPropertyNode::FLOAT},
+ {10203, "sim/multiplay/generic/float[3]", SGPropertyNode::FLOAT},
+ {10204, "sim/multiplay/generic/float[4]", SGPropertyNode::FLOAT},
+ {10205, "sim/multiplay/generic/float[5]", SGPropertyNode::FLOAT},
+ {10206, "sim/multiplay/generic/float[6]", SGPropertyNode::FLOAT},
+ {10207, "sim/multiplay/generic/float[7]", SGPropertyNode::FLOAT},
+ {10208, "sim/multiplay/generic/float[8]", SGPropertyNode::FLOAT},
+ {10209, "sim/multiplay/generic/float[9]", SGPropertyNode::FLOAT},
+ {10210, "sim/multiplay/generic/float[10]", SGPropertyNode::FLOAT},
+ {10211, "sim/multiplay/generic/float[11]", SGPropertyNode::FLOAT},
+ {10212, "sim/multiplay/generic/float[12]", SGPropertyNode::FLOAT},
+ {10213, "sim/multiplay/generic/float[13]", SGPropertyNode::FLOAT},
+ {10214, "sim/multiplay/generic/float[14]", SGPropertyNode::FLOAT},
+ {10215, "sim/multiplay/generic/float[15]", SGPropertyNode::FLOAT},
+ {10216, "sim/multiplay/generic/float[16]", SGPropertyNode::FLOAT},
+ {10217, "sim/multiplay/generic/float[17]", SGPropertyNode::FLOAT},
+ {10218, "sim/multiplay/generic/float[18]", SGPropertyNode::FLOAT},
+ {10219, "sim/multiplay/generic/float[19]", SGPropertyNode::FLOAT},
+
+ {10300, "sim/multiplay/generic/int[0]", SGPropertyNode::INT},
+ {10301, "sim/multiplay/generic/int[1]", SGPropertyNode::INT},
+ {10302, "sim/multiplay/generic/int[2]", SGPropertyNode::INT},
+ {10303, "sim/multiplay/generic/int[3]", SGPropertyNode::INT},
+ {10304, "sim/multiplay/generic/int[4]", SGPropertyNode::INT},
+ {10305, "sim/multiplay/generic/int[5]", SGPropertyNode::INT},
+ {10306, "sim/multiplay/generic/int[6]", SGPropertyNode::INT},
+ {10307, "sim/multiplay/generic/int[7]", SGPropertyNode::INT},
+ {10308, "sim/multiplay/generic/int[8]", SGPropertyNode::INT},
+ {10309, "sim/multiplay/generic/int[9]", SGPropertyNode::INT},
+ {10310, "sim/multiplay/generic/int[10]", SGPropertyNode::INT},
+ {10311, "sim/multiplay/generic/int[11]", SGPropertyNode::INT},
+ {10312, "sim/multiplay/generic/int[12]", SGPropertyNode::INT},
+ {10313, "sim/multiplay/generic/int[13]", SGPropertyNode::INT},
+ {10314, "sim/multiplay/generic/int[14]", SGPropertyNode::INT},
+ {10315, "sim/multiplay/generic/int[15]", SGPropertyNode::INT},
+ {10316, "sim/multiplay/generic/int[16]", SGPropertyNode::INT},
+ {10317, "sim/multiplay/generic/int[17]", SGPropertyNode::INT},
+ {10318, "sim/multiplay/generic/int[18]", SGPropertyNode::INT},
+ {10319, "sim/multiplay/generic/int[19]", SGPropertyNode::INT},
};
+const unsigned
+FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)
+ / sizeof(FGMultiplayMgr::sIdPropertyList[0]));
+
+// Look up a property ID using binary search.
+namespace
+{
+ struct ComparePropertyId
+ {
+ bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
+ const FGMultiplayMgr::IdPropertyList& rhs)
+ {
+ return lhs.id < rhs.id;
+ }
+ bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
+ unsigned id)
+ {
+ return lhs.id < id;
+ }
+ bool operator()(unsigned id,
+ const FGMultiplayMgr::IdPropertyList& rhs)
+ {
+ return id < rhs.id;
+ }
+ };
+
+}
+const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)
+{
+ std::pair<const IdPropertyList*, const IdPropertyList*> result
+ = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,
+ ComparePropertyId());
+ if (result.first == result.second) {
+ return 0;
+ } else {
+ return result.first;
+ }
+}
+
//////////////////////////////////////////////////////////////////////
//
// MultiplayMgr constructor
//////////////////////////////////////////////////////////////////////
FGMultiplayMgr::FGMultiplayMgr()
{
+ mSocket = 0;
mInitialised = false;
mHaveServer = false;
} // FGMultiplayMgr::FGMultiplayMgr()
// Set members from property values
//////////////////////////////////////////////////
short rxPort = fgGetInt("/sim/multiplay/rxport");
- if (rxPort <= 0)
- rxPort = 5000;
- mCallsign = fgGetString("/sim/multiplay/callsign");
- if (mCallsign.empty())
- // FIXME: use getpwuid
- mCallsign = "JohnDoe";
string rxAddress = fgGetString("/sim/multiplay/rxhost");
- if (rxAddress.empty())
- rxAddress = "127.0.0.1";
short txPort = fgGetInt("/sim/multiplay/txport");
string txAddress = fgGetString("/sim/multiplay/txhost");
+ mCallsign = fgGetString("/sim/multiplay/callsign");
if (txPort > 0 && !txAddress.empty()) {
- mHaveServer = true;
mServer.set(txAddress.c_str(), txPort);
+ if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {
+ mHaveServer = false;
+ SG_LOG(SG_NETWORK, SG_ALERT,
+ "FGMultiplayMgr - could not resolve '"
+ << txAddress << "', Multiplayermode disabled");
+ } else {
+ mHaveServer = true;
+ }
+ if (rxPort <= 0)
+ rxPort = txPort;
}
+ if (rxPort <= 0) {
+ SG_LOG(SG_NETWORK, SG_ALERT,
+ "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
+ return (false);
+ }
+ if (mCallsign.empty())
+ mCallsign = "JohnDoe"; // FIXME: use getpwuid
SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
+ Close(); // Should Init be called twice, close Socket first
+ // A memory leak was reported here by valgrind
mSocket = new netSocket();
if (!mSocket->open(false)) {
SG_LOG( SG_NETWORK, SG_ALERT,
return false;
}
mSocket->setBlocking(false);
- mSocket->setBroadcast(true);
if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
perror("bind");
SG_LOG( SG_NETWORK, SG_ALERT,
void
FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
{
- if ((! mInitialised) || (! mHaveServer)) {
- if (! mInitialised)
- SG_LOG( SG_NETWORK, SG_ALERT,
- "FGMultiplayMgr::SendMyPosition - not initialised" );
- if (! mHaveServer)
- SG_LOG( SG_NETWORK, SG_ALERT,
- "FGMultiplayMgr::SendMyPosition - no server" );
+ if ((! mInitialised) || (! mHaveServer))
+ return;
+ if (! mHaveServer) {
+ SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::SendMyPosition - no server");
return;
}
it = motionInfo.properties.begin();
//cout << "OUTPUT PROPERTIES\n";
while (it != motionInfo.properties.end()
- && ptr < (Msg + MAX_PACKET_SIZE - sizeof(xdr_data_t))) {
+ && ptr + 2 * sizeof(xdr_data_t) < (Msg + MAX_PACKET_SIZE)) {
// First elements is the ID
xdr_data_t xdr = XDR_encode_uint32((*it)->id);
// Add the length
////cout << "String length: " << strlen(lcharptr) << "\n";
uint32_t len = strlen(lcharptr);
+ // XXX This should not be using 4 bytes per character!
+ if (ptr + (1 + len + (4 - len % 4)) * sizeof (xdr_data_t)
+ >= (Msg + MAX_PACKET_SIZE))
+ goto escape;
//cout << "String length unint32: " << len << "\n";
xdr = XDR_encode_uint32(len);
memcpy(ptr, &xdr, sizeof(xdr_data_t));
if (len != 0)
{
- // Now the text itself
+ // Now the text itself
+ // XXX This should not be using 4 bytes per character!
int lcount = 0;
while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE))
{
++it;
}
-
+escape:
+
T_MsgHdr MsgHdr;
FillMsgHdr(&MsgHdr, POS_DATA_ID, ptr - Msg);
memcpy(Msg, &MsgHdr, sizeof(T_MsgHdr));
if (MsgHdr->Magic != MSG_MAGIC) {
SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
<< "message has invalid magic number!" );
+ break;
}
if (MsgHdr->Version != PROTO_VER) {
SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
<< "message has invalid protocoll number!" );
+ break;
}
if (MsgHdr->MsgLen != bytes) {
SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayMgr::MP_ProcessData - "
<< "message has invalid length!" );
+ break;
}
//////////////////////////////////////////////////
// Process messages
xdr++;
// Check the ID actually exists and get the type
- unsigned i = 0;
- bool found = false;
- while (FGMultiplayMgr::sIdPropertyList[i].name)
- {
- if (sIdPropertyList[i].id == pData->id)
- {
- found = true;
- pData->type = sIdPropertyList[i].type;
- }
-
- i++;
- }
+ const IdPropertyList* plist = findProperty(pData->id);
- if (found == true)
+ if (plist)
{
+ pData->type = plist->type;
// How we decode the remainder of the property depends on the type
switch (pData->type) {
case SGPropertyNode::INT:
else
{
// We failed to find the property. We'll try the next packet immediately.
- //cout << " Unknown\n";
+ SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::ProcessPosMsg - "
+ << "found unknown property id" << pData->id);
}
}
MsgBuf[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
T_ChatMsg* ChatMsg = (T_ChatMsg *)(Msg + sizeof(T_MsgHdr));
- SG_LOG ( SG_NETWORK, SG_ALERT, "Chat [" << MsgHdr->Callsign << "]"
- << " " << MsgBuf << endl);
+ SG_LOG (SG_NETWORK, SG_ALERT, "Chat [" << MsgHdr->Callsign << "]"
+ << " " << MsgBuf);
delete [] MsgBuf;
} // FGMultiplayMgr::ProcessChatMsg ()
const std::string& modelName)
{
if (0 < mMultiPlayerMap.count(callsign))
- return mMultiPlayerMap[callsign];
+ return mMultiPlayerMap[callsign].get();
FGAIMultiplayer* mp = new FGAIMultiplayer;
mp->setPath(modelName.c_str());
aiMgr->attach(mp);
/// FIXME: that must follow the attach ATM ...
- unsigned i = 0;
- while (sIdPropertyList[i].name) {
+ for (unsigned i = 0; i < numProperties; ++i)
mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
- ++i;
- }
}
return mp;
FGMultiplayMgr::getMultiplayer(const std::string& callsign)
{
if (0 < mMultiPlayerMap.count(callsign))
- return mMultiPlayerMap[callsign];
+ return mMultiPlayerMap[callsign].get();
else
return 0;
}