]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/multiplaytxmgr.cxx
Add multiplayer support from Duncan McCreanor and Diarmuid Tyson
[flightgear.git] / src / MultiPlayer / multiplaytxmgr.cxx
1 // multiplaytxmgr.cxx -- routines for transmitting 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 * $Id$
26 *
27 * Description: The multiplayer tx manager provides is used
28 * to send data to another player or a server for an
29 * interactive multiplayer FlightGear simulation.
30 *
31 ******************************************************************/
32
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <plib/netSocket.h>
37 #include <stdlib.h>
38 #include <Main/fg_props.hxx>
39 #include "multiplaytxmgr.hxx"
40 #include "mpmessages.hxx"
41 #include "mpplayer.hxx"
42
43 // These constants are provided so that the ident command can list file versions.
44 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
45 const char sMULTIPLAYTXMGR_HID[] = MULTIPLAYTXMGR_HID;
46
47
48
49 /******************************************************************
50 * Name: FGMultiplayTxMgr
51 * Description: Constructor.
52 ******************************************************************/
53 FGMultiplayTxMgr::FGMultiplayTxMgr() {
54
55     int iPlayerCnt;         // Count of players in player array
56
57     // Initialise private members
58     m_bInitialised = false;
59     mLocalPlayer = NULL;
60
61 }
62
63
64 /******************************************************************
65 * Name: ~FGMultiplayTxMgr
66 * Description: Destructor. Closes and deletes objects owned by
67 * this object.
68 ******************************************************************/
69 FGMultiplayTxMgr::~FGMultiplayTxMgr() {
70
71     Close();
72
73 }
74
75
76 /******************************************************************
77 * Name: init
78 * Description: Initialises multiplayer transmit
79 ******************************************************************/
80 bool FGMultiplayTxMgr::init(void) {
81
82
83     string sTxAddress;                          // Destination address
84     int iTxPort;
85     bool bSuccess = true;                       // Result of initialisation
86
87     // Initialise object if not already done
88     if (!m_bInitialised) {
89
90         // Set members from property values
91         string sTxAddress = fgGetString("/sim/multiplay/txhost");
92         iTxPort = fgGetInt("/sim/multiplay/txport");
93
94         cout << "FGMultiplayTxMgr::init - txaddress= " << sTxAddress << endl;
95         cout << "FGMultiplayTxMgr::init - txport= " << iTxPort << endl;
96         
97         if (iTxPort > 0) {
98
99
100             // Create and open tx socket
101             mDataTxSocket = new netSocket();
102             if (!mDataTxSocket->open(false)) {
103                 // Failed to open tx socket
104                 cerr << "FGMultiplayTxMgr::init - Failed to create data transmit socket" << endl;
105                 bSuccess = false;
106             } else {
107                 mDataTxSocket->setBroadcast(true);
108                 if (mDataTxSocket->connect(sTxAddress.c_str(), iTxPort) != 0) {
109                     // Failed to connect tx socket
110                     cerr << "FGMultiplayTxMgr::init - Failed to connect data transmit socket" << endl;
111                     bSuccess = false;
112                 }
113             }
114
115             // Create a player object for the local player
116             if (bSuccess) {
117                 mLocalPlayer = new MPPlayer();
118                 if (!mLocalPlayer->Open(fgGetString("/sim/multiplay/rxaddress"), fgGetInt("/sim/multiplay/rxport"),
119                                         fgGetString("/sim/multiplay/callsign"), fgGetString("/sim/model/path"), true)) {
120                     cerr << "FGMultiplayTxMgr::init - Failed to create player object for local player" << endl;
121                     bSuccess = false;
122                 }
123             }
124
125         // If Tx port == zero then don't initialise
126         } else {
127
128             cout << "FGMultiplayTxMgr::init - Tx Port is zero. Multiplay out disabled." << endl;
129             bSuccess = false;
130
131         }
132
133         // Save manager state
134         m_bInitialised = bSuccess;
135
136     } else {
137         cerr << "FGMultiplayTxMgr::init - Attempt to init object that is already opened" << endl;
138         bSuccess = false;
139     }
140
141
142     /* Return true if init succeeds */
143     return bSuccess;
144
145 }
146
147
148 /******************************************************************
149 * Name: Close
150 * Description: Closes and deletes the local player object. Closes
151 * and deletes the tx socket. Resets the object state to unitialised.
152 ******************************************************************/
153 void FGMultiplayTxMgr::Close(void) {
154
155
156     // Delete local player
157     if (mLocalPlayer) {
158         delete mLocalPlayer;
159         mLocalPlayer = NULL;
160     }
161
162     // Delete socket
163     if (mDataTxSocket) {
164         mDataTxSocket->close();
165         delete mDataTxSocket;
166         mDataTxSocket = NULL;
167     }
168
169     m_bInitialised = false;
170
171 }
172
173
174 /******************************************************************
175 * Name: SendMyPosition
176 * Description: Sends the position data for the local position.
177 ******************************************************************/
178 void FGMultiplayTxMgr::SendMyPosition(const sgMat4 PlayerPosMat4) {
179
180     T_MsgHdr MsgHdr;
181     T_PositionMsg PosMsg;
182     char sMsg[sizeof(T_MsgHdr) + sizeof(T_PositionMsg)];
183
184     if (m_bInitialised) {
185         mLocalPlayer->SetPosition(PlayerPosMat4);
186         mLocalPlayer->FillPosMsg(&MsgHdr, &PosMsg);
187         memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
188         memcpy(sMsg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
189         mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_PositionMsg), 0);
190     }
191
192
193 }
194
195
196
197 /******************************************************************
198 * Name: SendTextMessage
199 * Description: Sends a message to the player. The message must
200 * contain a valid and correctly filled out header and optional
201 * message body.
202 ******************************************************************/
203 void FGMultiplayTxMgr::SendTextMessage(const string &sMsgText) const {
204
205     bool bResult = false;
206     T_MsgHdr MsgHdr;
207     T_ChatMsg ChatMsg;
208     int iNextBlockPosition = 0;
209     char sMsg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
210
211     if (m_bInitialised) {
212
213         mLocalPlayer->FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
214
215         // Divide the text string into blocks that fit in the message
216         // and send the blocks.
217         while (iNextBlockPosition < sMsgText.length()) {
218             strncpy(ChatMsg.sText, sMsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(), MAX_CHAT_MSG_LEN);
219             ChatMsg.sText[MAX_CHAT_MSG_LEN - 1] = '\0';
220             memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
221             memcpy(sMsg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
222             mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0);
223             iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
224         }
225
226     }
227
228 }
229