1 // multiplayrxmgr.cxx -- routines for receiving multiplayer data
4 // Written by Duncan McCreanor, started February 2003.
5 // duncan.mccreanor@airservicesaustralia.com
7 // Copyright (C) 2003 Airservices Australia
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 /******************************************************************
33 * Description: The multiplayer rx manager provides control over
34 * multiplayer data reception and processing for an interactive
35 * multiplayer FlightGear simulation.
37 * The objects that hold player information are accessed via
38 * a fixed size array. A fixed array is used since it provides
39 * speed benefits over working with linked lists and is easier
40 * to code. Also, there is no point allowing for an unlimited
41 * number of players as too many players will slow the game
42 * down to the point where it is unplayable.
44 * When player position data is received, the callsign of
45 * the player is checked against existing players. If the
46 * player does not exist, a new player is created in the
47 * next free slot of the player array. If the player does
48 * exist, the player's positional matrix is updated.
50 * The Update method is used to move the players on the
51 * scene. The return value from calling MPPlayer::Draw
52 * indicates the state of the player. If data for a player
53 * has not been received data for some time, the player object
54 * is deleted and the array element freed.
56 ******************************************************************/
58 #include <sys/types.h>
59 #if !(defined(_MSC_VER) || defined(__MINGW32__))
60 # include <sys/socket.h>
61 # include <netinet/in.h>
62 # include <arpa/inet.h>
64 #include <plib/netSocket.h>
67 #include <simgear/debug/logstream.hxx>
68 #include <Main/fg_props.hxx>
70 #include "multiplayrxmgr.hxx"
71 #include "mpmessages.hxx"
72 #include "mpplayer.hxx"
74 #define MAX_PACKET_SIZE 1024
76 // These constants are provided so that the ident command can list file versions.
77 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
78 const char sMULTIPLAYRXMGR_HID[] = MULTIPLAYRXMGR_HID;
82 /******************************************************************
83 * Name: FGMultiplayRxMgr
84 * Description: Constructor.
85 ******************************************************************/
86 FGMultiplayRxMgr::FGMultiplayRxMgr() {
88 int iPlayerCnt; // Count of players in player array
90 // Initialise private members
93 m_bInitialised = false;
95 // Clear the player array
96 for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
97 m_Player[iPlayerCnt] = NULL;
103 /******************************************************************
104 * Name: ~FGMultiplayRxMgr
105 * Description: Destructor. Closes and deletes objects owned by
107 ******************************************************************/
108 FGMultiplayRxMgr::~FGMultiplayRxMgr() {
115 /******************************************************************
117 * Description: Initialises multiplayer receive.
118 ******************************************************************/
119 bool FGMultiplayRxMgr::init(void) {
121 bool bSuccess = true; // Result of initialisation
123 // Initialise object if not already done
124 if (!m_bInitialised) {
126 // Set members from property values
127 m_sCallsign = fgGetString("/sim/multiplay/callsign");
128 m_sRxAddress = fgGetString("/sim/multiplay/rxhost");
129 m_iRxPort = fgGetInt("/sim/multiplay/rxport");
131 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayRxMgr::init - rxaddress= "
133 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayRxMgr::init - rxport= "
135 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayRxMgr::init - callsign= "
139 // Create and open rx socket
140 mDataRxSocket = new netSocket();
141 if (!mDataRxSocket->open(false)) {
142 // Failed to open rx socket
143 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::Open - Failed to create data receive socket" );
147 // Configure the socket
148 mDataRxSocket->setBlocking(false);
149 mDataRxSocket->setBroadcast(true);
150 if (mDataRxSocket->bind(m_sRxAddress.c_str(), m_iRxPort) != 0) {
153 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::Open - Failed to bind receive socket" );
159 // Save manager state
160 m_bInitialised = bSuccess;
163 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::OpenRx - Receiver open requested when receiver is already open" );
167 /* Return true if open succeeds */
173 /******************************************************************
175 * Description: Closes and deletes and player connections. Closes
176 * and deletes the rx socket. Resets the object state
178 ******************************************************************/
179 void FGMultiplayRxMgr::Close(void) {
181 int iPlayerCnt; // Count of players in player array
183 // Delete any existing players
184 for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
185 if (m_Player[iPlayerCnt] != NULL) {
186 delete m_Player[iPlayerCnt];
187 m_Player[iPlayerCnt] = NULL;
193 mDataRxSocket->close();
194 delete mDataRxSocket;
195 mDataRxSocket = NULL;
198 m_bInitialised = false;
203 /******************************************************************
205 * Description: Processes data waiting at the receive socket. The
206 * processing ends when there is no more data at the socket.
207 ******************************************************************/
208 void FGMultiplayRxMgr::ProcessData(void) {
210 char sMsg[MAX_PACKET_SIZE]; // Buffer for received message
211 int iBytes; // Bytes received
212 T_MsgHdr *MsgHdr; // Pointer to header in received data
213 T_ChatMsg *ChatMsg; // Pointer to chat message in received data
214 T_PositionMsg *PosMsg; // Pointer to position message in received data
215 char *sIpAddress; // Address information from header
216 char *sModelName; // Model that the remote player is using
217 char *sCallsign; // Callsign of the remote player
218 struct in_addr PlayerAddress; // Used for converting the player's address into dot notation
219 int iPlayerCnt; // Count of players in player array
220 bool bActivePlayer = false; // The state of the player that sent the data
221 int iPort; // Port that the remote player receives on
224 if (m_bInitialised) {
226 // Read the receive socket and process any data
229 // Although the recv call asks for MAX_PACKET_SIZE of data,
230 // the number of bytes returned will only be that of the next
231 // packet waiting to be processed.
232 iBytes = mDataRxSocket->recv(sMsg, MAX_PACKET_SIZE, 0);
236 if (iBytes >= (int)sizeof(MsgHdr)) {
239 MsgHdr = (T_MsgHdr *)sMsg;
240 PlayerAddress.s_addr = MsgHdr->lReplyAddress;
241 sIpAddress = inet_ntoa(PlayerAddress);
242 iPort = MsgHdr->iReplyPort;
243 sCallsign = MsgHdr->sCallsign;
245 // Process the player data unless we generated it
246 if (m_sCallsign != MsgHdr->sCallsign) {
250 switch(MsgHdr->MsgId) {
252 if (MsgHdr->iMsgLen == sizeof(T_MsgHdr) + sizeof(T_ChatMsg)) {
253 ChatMsg = (T_ChatMsg *)(sMsg + sizeof(T_MsgHdr));
254 SG_LOG( SG_NETWORK, SG_BULK, "Chat [" << MsgHdr->sCallsign << "]" << " " << ChatMsg->sText );
256 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::MP_ProcessData - Chat message received with insufficient data" );
261 if (MsgHdr->iMsgLen == sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
262 PosMsg = (T_PositionMsg *)(sMsg + sizeof(T_MsgHdr));
263 sModelName = PosMsg->sModel;
265 // Check if the player is already in the game by using the Callsign.
266 bActivePlayer = false;
267 for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
268 if (m_Player[iPlayerCnt] != NULL) {
269 if (m_Player[iPlayerCnt]->CompareCallsign(MsgHdr->sCallsign)) {
271 // Player found. Update the data for the player.
272 m_Player[iPlayerCnt]->SetPosition(PosMsg->PlayerPos);
273 bActivePlayer = true;
279 // Player not active, so add as new player
280 if (!bActivePlayer) {
283 if (m_Player[iPlayerCnt] == NULL) {
284 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayRxMgr::ProcessRxData - Add new player. IP: " << sIpAddress << ", Call: " << sCallsign << ", model: " << sModelName );
285 m_Player[iPlayerCnt] = new MPPlayer;
286 m_Player[iPlayerCnt]->Open(sIpAddress, iPort, sCallsign, sModelName, false);
287 m_Player[iPlayerCnt]->SetPosition(PosMsg->PlayerPos);
288 bActivePlayer = true;
291 } while (iPlayerCnt < MAX_PLAYERS && !bActivePlayer);
293 // Check if the player was added
294 if (!bActivePlayer) {
295 if (iPlayerCnt == MAX_PLAYERS) {
296 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::MP_ProcessData - Unable to add new player (" << sCallsign << "). Too many players." );
302 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::MP_ProcessData - Position message received with insufficient data" );
307 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayRxMgr::MP_ProcessData - Unknown message Id received: " << MsgHdr->MsgId );
317 } else if (iBytes == -1) {
318 if (errno != EAGAIN) {
319 perror("FGMultiplayRxMgr::MP_ProcessData");
323 } while (iBytes > 0);
331 /******************************************************************
333 * Description: For each active player, tell the player object
334 * to update its position on the scene. If a player object
335 * returns status information indicating that it has not
336 * had an update for some time then the player is deleted.
337 ******************************************************************/
338 void FGMultiplayRxMgr::Update(void) {
340 MPPlayer::TPlayerDataState ePlayerDataState;
343 for (iPlayerId = 0; iPlayerId < MAX_PLAYERS; iPlayerId++) {
344 if (m_Player[iPlayerId] != NULL) {
345 ePlayerDataState = m_Player[iPlayerId]->Draw();
347 // If the player has not received an update for some
348 // time then assume that the player has quit.
349 if (ePlayerDataState == MPPlayer::PLAYER_DATA_EXPIRED) {
350 SG_LOG( SG_NETWORK, SG_BULK, "FGMultiplayRxMgr::Update - Deleting player from game. Callsign: "
351 << m_Player[iPlayerId]->Callsign() );
352 delete m_Player[iPlayerId];
353 m_Player[iPlayerId] = NULL;
361 #endif // FG_MPLAYER_AS