FlightGear next
multiplaymgr.hxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: multiplaymgr.hxx
3 * SPDX-FileCopyrightText: Written by Duncan McCreanor, started February 2003 - duncan.mccreanor@airservicesaustralia.com
4 * SPDX-FileContributor: Copyright (C) 2003 Airservices Australia, Copyright (C) 2005 Oliver Schroeder
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#pragma once
9
10#define MULTIPLAYTXMGR_HID "$Id$"
11
14
15#include <deque>
16#include <memory>
17#include <string>
18#include <vector>
19
20#include <simgear/compiler.h>
21#include <simgear/io/raw_socket.hxx>
22#include <simgear/props/props.hxx>
23#include <simgear/structure/subsystem_mgr.hxx>
24
25
26class IRCConnection;
27class CPDLCManager;
28
29const std::string MPIRC_SERVER_HOST_DEFAULT{"mpirc.flightgear.org"};
30const std::string MPIRC_SERVER_HOST_PROPERTY{"/network/mpirc/server-host"};
31const std::string MPIRC_SERVER_PORT_PROPERTY{"/network/mpirc/server-port"};
32const std::string MPIRC_NICK_PREFIX{"MP_IRC_"};
33
36struct T_MsgHdr;
37class FGAIMultiplayer;
38
39
40class FGMultiplayMgr : public SGSubsystem
41{
42public:
45
46 // Subsystem API.
47 void init() override;
48 void reinit() override;
49 void shutdown() override;
50 void update(double dt) override;
51
52 // Subsystem identification.
53 static const char* staticSubsystemClassId() { return "mp"; }
54
55 // transmitter
56
57 void SendTextMessage(const std::string& sMsgText);
58 // receiver
59
60 FGAIMultiplayer* getMultiplayer(const std::string& callsign);
61
62 std::shared_ptr<std::vector<char>> popMessageHistory();
63 void pushMessageHistory(std::shared_ptr<std::vector<char>> message);
64
65 // Remove motion information for all multiplayer aircraft, e.g. when
66 // scrubbing during replay.
67 void ClearMotion();
68 CPDLCManager* getCPDLC() { return _cpdlc.get(); };
69
70private:
71 std::unique_ptr<IRCConnection> _mpirc;
72 std::unique_ptr<CPDLCManager> _cpdlc;
73 friend class MPPropertyListener;
74
75 void setPropertiesChanged()
76 {
77 mPropertiesChanged = true;
78 }
79 int getProtocolToUse()
80 {
81 int protocolVersion = pProtocolVersion->getIntValue();
82 if (protocolVersion >= MIN_MP_PROTOCOL_VERSION && protocolVersion <= MAX_MP_PROTOCOL_VERSION)
83 return protocolVersion;
84 else
86 }
87
88 void findProperties();
89
90 void Send(double currentMPTime);
91 void SendMyPosition(const FGExternalMotionData& motionInfo);
92 short get_scaled_short(double v, double scale);
93
94 union MsgBuf;
95 FGAIMultiplayer* addMultiplayer(const std::string& callsign,
96 const std::string& modelName,
97 const int fallback_model_index);
98 void FillMsgHdr(T_MsgHdr* MsgHdr, int iMsgId, unsigned _len = 0u);
99 void ProcessPosMsg(const MsgBuf& Msg, const simgear::IPAddress& SenderAddress,
100 long stamp);
101 void ProcessChatMsg(const MsgBuf& Msg, const simgear::IPAddress& SenderAddress);
102 bool isSane(const FGExternalMotionData& motionInfo);
103 int GetMsgNetwork(MsgBuf& msgBuf, simgear::IPAddress& SenderAddress);
104 int GetMsg(MsgBuf& msgBuf, simgear::IPAddress& SenderAddress);
105
107 typedef std::map<std::string, SGSharedPtr<FGAIMultiplayer>> MultiPlayerMap;
108 MultiPlayerMap mMultiPlayerMap;
109
110 std::unique_ptr<simgear::Socket> mSocket;
111 simgear::IPAddress mServer;
112 bool mHaveServer;
113 bool mInitialised;
114 std::string mCallsign;
115
116 // Map between the property id's from the multiplayers network packets
117 // and the property nodes
118 typedef std::map<unsigned int, SGSharedPtr<SGPropertyNode>> PropertyMap;
119 PropertyMap mPropertyMap;
120 SGPropertyNode* pProtocolVersion;
121 SGPropertyNode* pXmitLen;
122 SGPropertyNode* pMultiPlayDebugLevel;
123 SGPropertyNode* pMultiPlayRange;
124 SGPropertyNode* pMultiPlayTransmitPropertyBase;
125 SGPropertyNode* pReplayState;
126 SGPropertyNode* pLogRawSpeedMultiplayer;
127
128 typedef std::map<unsigned int, const struct IdPropertyList*> PropertyDefinitionMap;
129 PropertyDefinitionMap mPropertyDefinition;
130
131 bool mPropertiesChanged;
132
133 MPPropertyListener* mListener;
134
135 double mDt; // reciprocal of /sim/multiplay/tx-rate-hz
136 double mNextTransmitTime = 0.0;
137
138 std::deque<std::shared_ptr<std::vector<char>>> mRecordMessageQueue;
139 std::deque<std::shared_ptr<std::vector<char>>> mReplayMessageQueue;
140};
static double scale(int center, int deadband, int min, int max, int value)
friend class MPPropertyListener
void update(double dt) override
void shutdown() override
static const char * staticSubsystemClassId()
CPDLCManager * getCPDLC()
FGAIMultiplayer * getMultiplayer(const std::string &callsign)
std::shared_ptr< std::vector< char > > popMessageHistory()
void init() override
void reinit() override
void SendTextMessage(const std::string &sMsgText)
void pushMessageHistory(std::shared_ptr< std::vector< char > > message)
const std::string MPIRC_NICK_PREFIX
const std::string MPIRC_SERVER_HOST_PROPERTY
const std::string MPIRC_SERVER_PORT_PROPERTY
const std::string MPIRC_SERVER_HOST_DEFAULT
const int MIN_MP_PROTOCOL_VERSION
const int MAX_MP_PROTOCOL_VERSION
The buffer that holds a multi-player message, suitably aligned.