| 1 | /* |
| 2 | * SPDX-FileName: multiplaymgr.hxx |
| 3 | * SPDX-FileCopyrightText: Written by Duncan McCreanor, started February 2003 - [email protected] |
| 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 | |
| 12 | const int MIN_MP_PROTOCOL_VERSION = 1; |
| 13 | const int MAX_MP_PROTOCOL_VERSION = 2; |
| 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 | |
| 26 | class IRCConnection; |
| 27 | class CPDLCManager; |
| 28 | |
| 29 | const std::string MPIRC_SERVER_HOST_DEFAULT{"mpirc.flightgear.org"}; |
| 30 | const std::string MPIRC_SERVER_HOST_PROPERTY{"/network/mpirc/server-host"}; |
| 31 | const std::string MPIRC_SERVER_PORT_PROPERTY{"/network/mpirc/server-port"}; |
| 32 | const std::string MPIRC_NICK_PREFIX{"MP_IRC_"}; |
| 33 | |
| 34 | struct FGExternalMotionData; |
| 35 | class MPPropertyListener; |
| 36 | struct T_MsgHdr; |
| 37 | class FGAIMultiplayer; |
| 38 | |
| 39 | |
| 40 | class FGMultiplayMgr : public SGSubsystem |
| 41 | { |
| 42 | public: |
| 43 | FGMultiplayMgr(); |
| 44 | ~FGMultiplayMgr(); |
| 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 | |
| 70 | private: |
| 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 |
| 85 | return MIN_MP_PROTOCOL_VERSION; |
| 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 | |
| 106 | /// maps from the callsign string to the FGAIMultiplayer |
| 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 | }; |