1 // multiplaytxmgr.cxx -- routines for transmitting 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 tx manager provides is used
34 * to send data to another player or a server for an
35 * interactive multiplayer FlightGear simulation.
37 ******************************************************************/
39 #include <sys/types.h>
40 #if !(defined(_MSC_VER) || defined(__MINGW32__))
41 # include <sys/socket.h>
42 # include <netinet/in.h>
43 # include <arpa/inet.h>
45 #include <plib/netSocket.h>
48 #include <simgear/debug/logstream.hxx>
49 #include <Main/fg_props.hxx>
51 #include "multiplaytxmgr.hxx"
52 #include "mpmessages.hxx"
53 #include "mpplayer.hxx"
55 // These constants are provided so that the ident command can list file versions.
56 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
57 const char sMULTIPLAYTXMGR_HID[] = MULTIPLAYTXMGR_HID;
61 /******************************************************************
62 * Name: FGMultiplayTxMgr
63 * Description: Constructor.
64 ******************************************************************/
65 FGMultiplayTxMgr::FGMultiplayTxMgr() {
67 int iPlayerCnt; // Count of players in player array
69 // Initialise private members
70 m_bInitialised = false;
76 /******************************************************************
77 * Name: ~FGMultiplayTxMgr
78 * Description: Destructor. Closes and deletes objects owned by
80 ******************************************************************/
81 FGMultiplayTxMgr::~FGMultiplayTxMgr() {
88 /******************************************************************
90 * Description: Initialises multiplayer transmit
91 ******************************************************************/
92 bool FGMultiplayTxMgr::init(void) {
95 string sTxAddress; // Destination address
97 bool bSuccess = true; // Result of initialisation
99 // Initialise object if not already done
100 if (!m_bInitialised) {
102 // Set members from property values
103 string sTxAddress = fgGetString("/sim/multiplay/txhost");
104 iTxPort = fgGetInt("/sim/multiplay/txport");
106 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txaddress= "
108 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txport= "
114 // Create and open tx socket
115 mDataTxSocket = new netSocket();
116 if (!mDataTxSocket->open(false)) {
117 // Failed to open tx socket
118 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Failed to create data transmit socket" );
121 mDataTxSocket->setBroadcast(true);
122 if (mDataTxSocket->connect(sTxAddress.c_str(), iTxPort) != 0) {
123 // Failed to connect tx socket
124 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Failed to connect data transmit socket" );
129 // Create a player object for the local player
131 mLocalPlayer = new MPPlayer();
132 if (!mLocalPlayer->Open(fgGetString("/sim/multiplay/rxhost"), fgGetInt("/sim/multiplay/rxport"),
133 fgGetString("/sim/multiplay/callsign"), fgGetString("/sim/model/path"), true)) {
134 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Failed to create player object for local player" );
139 // If Tx port == zero then don't initialise
142 SG_LOG( SG_NETWORK, SG_WARN, "FGMultiplayTxMgr::init - Tx Port is zero. Multiplay out disabled." );
147 // Save manager state
148 m_bInitialised = bSuccess;
151 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Attempt to init object that is already opened" );
156 /* Return true if init succeeds */
162 /******************************************************************
164 * Description: Closes and deletes the local player object. Closes
165 * and deletes the tx socket. Resets the object state to unitialised.
166 ******************************************************************/
167 void FGMultiplayTxMgr::Close(void) {
170 // Delete local player
178 mDataTxSocket->close();
179 delete mDataTxSocket;
180 mDataTxSocket = NULL;
183 m_bInitialised = false;
188 /******************************************************************
189 * Name: SendMyPosition
190 * Description: Sends the position data for the local position.
191 ******************************************************************/
192 void FGMultiplayTxMgr::SendMyPosition(const sgMat4 PlayerPosMat4) {
195 T_PositionMsg PosMsg;
196 char sMsg[sizeof(T_MsgHdr) + sizeof(T_PositionMsg)];
198 if (m_bInitialised) {
199 mLocalPlayer->SetPosition(PlayerPosMat4);
200 mLocalPlayer->FillPosMsg(&MsgHdr, &PosMsg);
201 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
202 memcpy(sMsg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
203 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_PositionMsg), 0);
211 /******************************************************************
212 * Name: SendTextMessage
213 * Description: Sends a message to the player. The message must
214 * contain a valid and correctly filled out header and optional
216 ******************************************************************/
217 void FGMultiplayTxMgr::SendTextMessage(const string &sMsgText) const {
219 bool bResult = false;
222 int iNextBlockPosition = 0;
223 char sMsg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
225 if (m_bInitialised) {
227 mLocalPlayer->FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
229 // Divide the text string into blocks that fit in the message
230 // and send the blocks.
231 while (iNextBlockPosition < sMsgText.length()) {
232 strncpy(ChatMsg.sText, sMsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(), MAX_CHAT_MSG_LEN);
233 ChatMsg.sText[MAX_CHAT_MSG_LEN - 1] = '\0';
234 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
235 memcpy(sMsg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
236 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0);
237 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
244 #endif // FG_MPLAYER_AS