]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/multiplayrxmgr.cxx
Add multiplayer support from Duncan McCreanor and Diarmuid Tyson
[flightgear.git] / src / MultiPlayer / multiplayrxmgr.cxx
1 // multiplayrxmgr.cxx -- routines for receiving multiplayer data
2 //                       for Flightgear
3 //
4 // Written by Duncan McCreanor, started February 2003.
5 // duncan.mccreanor@airservicesaustralia.com
6 //
7 // Copyright (C) 2003  Airservices Australia
8 //
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.
13 //
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.
18 //
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.
22 //
23
24
25 /******************************************************************
26 * $Id$
27 *
28 * Description: The multiplayer rx manager provides control over
29 * multiplayer data reception and processing for an interactive
30 * multiplayer FlightGear simulation.
31 *
32 * The objects that hold player information are accessed via
33 * a fixed size array. A fixed array is used since it provides
34 * speed benefits over working with linked lists and is easier
35 * to code. Also, there is no point allowing for an unlimited
36 * number of players as too many players will slow the game
37 * down to the point where it is unplayable.
38 *
39 * When player position data is received, the callsign of
40 * the player is checked against existing players. If the
41 * player does not exist, a new player is created in the
42 * next free slot of the player array. If the player does
43 * exist, the player's positional matrix is updated.
44 *
45 * The Update method is used to move the players on the
46 * scene. The return value from calling MPPlayer::Draw
47 * indicates the state of the player. If data for a player
48 * has not been received data for some time, the player object
49 * is deleted and the array element freed.
50 *
51 ******************************************************************/
52
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <plib/netSocket.h>
57 #include <stdlib.h>
58 #include <Main/fg_props.hxx>
59
60 #include "multiplayrxmgr.hxx"
61 #include "mpmessages.hxx"
62 #include "mpplayer.hxx"
63
64 #define MAX_PACKET_SIZE 1024
65
66 // These constants are provided so that the ident command can list file versions.
67 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
68 const char sMULTIPLAYRXMGR_HID[] = MULTIPLAYRXMGR_HID;
69
70
71
72 /******************************************************************
73 * Name: FGMultiplayRxMgr
74 * Description: Constructor.
75 ******************************************************************/
76 FGMultiplayRxMgr::FGMultiplayRxMgr() {
77
78     int iPlayerCnt;         // Count of players in player array
79
80     // Initialise private members
81     m_sRxAddress = "0";
82     m_iRxPort = 0;
83     m_bInitialised = false;
84
85     // Clear the player array
86     for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
87         m_Player[iPlayerCnt] = NULL;
88     }
89
90 }
91
92
93 /******************************************************************
94 * Name: ~FGMultiplayRxMgr
95 * Description: Destructor. Closes and deletes objects owned by
96 * this object.
97 ******************************************************************/
98 FGMultiplayRxMgr::~FGMultiplayRxMgr() {
99
100     Close();
101
102 }
103
104
105 /******************************************************************
106 * Name: init
107 * Description: Initialises multiplayer receive.
108 ******************************************************************/
109 bool FGMultiplayRxMgr::init(void) {
110
111     bool bSuccess = true;                       // Result of initialisation
112
113     // Initialise object if not already done
114     if (!m_bInitialised) {
115
116         // Set members from property values
117         m_sCallsign = fgGetString("/sim/multiplay/callsign");
118         m_sRxAddress = fgGetString("/sim/multiplay/rxhost");
119         m_iRxPort = fgGetInt("/sim/multiplay/rxport");
120
121         cout << "FGMultiplayRxMgr::init - rxaddress= " << m_sRxAddress << endl;
122         cout << "FGMultiplayRxMgr::init - rxport= " << m_iRxPort << endl;
123         cout << "FGMultiplayRxMgr::init - callsign= " << m_sCallsign << endl;
124
125         // Create and open rx socket
126         mDataRxSocket = new netSocket();
127         if (!mDataRxSocket->open(false)) {
128             // Failed to open rx socket
129             cerr << "FGMultiplayRxMgr::Open - Failed to create data receive socket" << endl;
130             bSuccess = false;
131         } else {
132
133             // Configure the socket
134             mDataRxSocket->setBlocking(false);
135             mDataRxSocket->setBroadcast(true);
136             if (mDataRxSocket->bind(m_sRxAddress.c_str(), m_iRxPort) != 0) {
137                 perror("bind");
138                 // Failed to bind
139                 cerr << "FGMultiplayRxMgr::Open - Failed to bind receive socket" << endl;
140                 bSuccess = false;
141             }
142
143         }
144
145         // Save manager state
146         m_bInitialised = bSuccess;
147
148     } else {
149         cerr << "FGMultiplayRxMgr::OpenRx - Receiver open requested when receiver is already open" << endl;
150         bSuccess = false;
151     }
152
153     /* Return true if open succeeds */
154     return bSuccess;
155
156 }
157
158
159 /******************************************************************
160 * Name: Close
161 * Description: Closes and deletes and player connections. Closes
162 * and deletes the rx socket. Resets the object state
163 * to unitialised.
164 ******************************************************************/
165 void FGMultiplayRxMgr::Close(void) {
166
167     int iPlayerCnt;         // Count of players in player array
168
169     // Delete any existing players
170     for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
171         if (m_Player[iPlayerCnt] != NULL) {
172             delete m_Player[iPlayerCnt];
173             m_Player[iPlayerCnt] = NULL;
174         }
175     }
176
177     // Delete socket
178     if (mDataRxSocket) {
179         mDataRxSocket->close();
180         delete mDataRxSocket;
181         mDataRxSocket = NULL;
182     }
183
184     m_bInitialised = false;
185
186 }
187
188
189 /******************************************************************
190 * Name: ProcessData
191 * Description: Processes data waiting at the receive socket. The
192 * processing ends when there is no more data at the socket.
193 ******************************************************************/
194 void FGMultiplayRxMgr::ProcessData(void) {
195
196     char sMsg[MAX_PACKET_SIZE];         // Buffer for received message
197     int iBytes;                         // Bytes received
198     T_MsgHdr *MsgHdr;                   // Pointer to header in received data
199     T_ChatMsg *ChatMsg;                 // Pointer to chat message in received data
200     T_PositionMsg *PosMsg;              // Pointer to position message in received data
201     char *sIpAddress;                   // Address information from header
202     char *sModelName;                   // Model that the remote player is using
203     char *sCallsign;                    // Callsign of the remote player
204     struct in_addr PlayerAddress;       // Used for converting the player's address into dot notation
205     int iPlayerCnt;                     // Count of players in player array
206     bool bActivePlayer = false;         // The state of the player that sent the data
207     int iPort;                          // Port that the remote player receives on
208
209
210     if (m_bInitialised) {
211
212         // Read the receive socket and process any data
213         do {
214
215             // Although the recv call asks for MAX_PACKET_SIZE of data,
216             // the number of bytes returned will only be that of the next
217             // packet waiting to be processed.
218             iBytes = mDataRxSocket->recv(sMsg, MAX_PACKET_SIZE, 0);
219
220             // Data received
221             if (iBytes > 0) {
222                 if (iBytes >= sizeof(MsgHdr)) {
223
224                     // Read header
225                     MsgHdr = (T_MsgHdr *)sMsg;
226                     PlayerAddress.s_addr = MsgHdr->lReplyAddress;
227                     sIpAddress = inet_ntoa(PlayerAddress);
228                     iPort = MsgHdr->iReplyPort;
229                     sCallsign = MsgHdr->sCallsign;
230
231                     // Process the player data unless we generated it
232                     if (m_sCallsign != string(MsgHdr->sCallsign)) {
233
234
235                         // Process messages
236                         switch(MsgHdr->MsgId) {
237                             case CHAT_MSG_ID:
238                                 if (MsgHdr->iMsgLen == sizeof(T_MsgHdr) + sizeof(T_ChatMsg)) {
239                                     ChatMsg = (T_ChatMsg *)(sMsg + sizeof(T_MsgHdr));
240                                     cout << "Chat [" << MsgHdr->sCallsign << "]" << " " << ChatMsg->sText << endl;
241                                 } else {
242                                     cerr << "FGMultiplayRxMgr::MP_ProcessData - Chat message received with insufficient data" << endl;
243                                 }
244                                 break;
245
246                             case POS_DATA_ID:
247                                 if (MsgHdr->iMsgLen == sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
248                                     PosMsg = (T_PositionMsg *)(sMsg + sizeof(T_MsgHdr));
249                                     sModelName = PosMsg->sModel;
250
251                                     // Check if the player is already in the game by using the Callsign.
252                                     bActivePlayer = false;
253                                     for (iPlayerCnt = 0; iPlayerCnt < MAX_PLAYERS; iPlayerCnt++) {
254                                         if (m_Player[iPlayerCnt] != NULL) {
255                                             if (m_Player[iPlayerCnt]->CompareCallsign(MsgHdr->sCallsign)) {
256
257                                                 // Player found. Update the data for the player.
258                                                 m_Player[iPlayerCnt]->SetPosition(PosMsg->PlayerPos);
259                                                 bActivePlayer = true;
260
261                                             }
262                                         }
263                                     }
264
265                                     // Player not active, so add as new player
266                                     if (!bActivePlayer) {
267                                         iPlayerCnt = 0;
268                                         do {
269                                             if (m_Player[iPlayerCnt] == NULL) {
270                                                 cout << "FGMultiplayRxMgr::ProcessRxData - Add new player. IP: " << sIpAddress
271                                                     << ", Call: " <<  sCallsign << ", model: " << sModelName << endl;
272                                                 m_Player[iPlayerCnt] = new MPPlayer;
273                                                 m_Player[iPlayerCnt]->Open(string(sIpAddress), iPort, string(sCallsign), string(sModelName), false);
274                                                 m_Player[iPlayerCnt]->SetPosition(PosMsg->PlayerPos);
275                                                 bActivePlayer = true;
276                                             }
277                                             iPlayerCnt++;
278                                         } while (iPlayerCnt < MAX_PLAYERS && !bActivePlayer);
279
280                                         // Check if the player was added
281                                         if (!bActivePlayer) {
282                                             if (iPlayerCnt == MAX_PLAYERS) {
283                                                 cerr << "FGMultiplayRxMgr::MP_ProcessData - Unable to add new player (" << sCallsign << "). Too many players." << endl;
284                                             }
285                                         }
286                                     }
287
288                                 } else {
289                                     cerr << "FGMultiplayRxMgr::MP_ProcessData - Position message received with insufficient data" << endl;
290                                 }
291                                 break;
292
293                             default:
294                                 cerr << "FGMultiplayRxMgr::MP_ProcessData - Unknown message Id received: " << MsgHdr->MsgId << endl;
295                                 break;
296
297
298                         }
299                     }
300                 }
301
302
303             // Error or no data
304             } else if (iBytes == -1) {
305                 if (errno != EAGAIN) {
306                     perror("FGMultiplayRxMgr::MP_ProcessData");
307                 }
308             }
309
310         } while (iBytes > 0);
311
312     }
313
314
315 }
316
317
318 /******************************************************************
319 * Name: Update
320 * Description: For each active player, tell the player object
321 * to update its position on the scene. If a player object
322 * returns status information indicating that it has not
323 * had an update for some time then the player is deleted.
324 ******************************************************************/
325 void FGMultiplayRxMgr::Update(void) {
326
327     int iPlayerDataState;
328     int iPlayerId;
329
330     for (iPlayerId = 0; iPlayerId < MAX_PLAYERS; iPlayerId++) {
331         if (m_Player[iPlayerId] != NULL) {
332             iPlayerDataState = m_Player[iPlayerId]->Draw();
333
334             // If the player has not received an update for some
335             // time then assume that the player has quit.
336             if (iPlayerDataState == PLAYER_DATA_EXPIRED) {
337                 delete m_Player[iPlayerId];
338                 m_Player[iPlayerId] = NULL;
339             }
340
341         }
342     }
343
344 }
345
346