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