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.
24 /******************************************************************
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.
31 ******************************************************************/
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <plib/netSocket.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <Main/fg_props.hxx>
43 #include "multiplaytxmgr.hxx"
44 #include "mpmessages.hxx"
45 #include "mpplayer.hxx"
47 // These constants are provided so that the ident command can list file versions.
48 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
49 const char sMULTIPLAYTXMGR_HID[] = MULTIPLAYTXMGR_HID;
53 /******************************************************************
54 * Name: FGMultiplayTxMgr
55 * Description: Constructor.
56 ******************************************************************/
57 FGMultiplayTxMgr::FGMultiplayTxMgr() {
59 int iPlayerCnt; // Count of players in player array
61 // Initialise private members
62 m_bInitialised = false;
68 /******************************************************************
69 * Name: ~FGMultiplayTxMgr
70 * Description: Destructor. Closes and deletes objects owned by
72 ******************************************************************/
73 FGMultiplayTxMgr::~FGMultiplayTxMgr() {
80 /******************************************************************
82 * Description: Initialises multiplayer transmit
83 ******************************************************************/
84 bool FGMultiplayTxMgr::init(void) {
87 string sTxAddress; // Destination address
89 bool bSuccess = true; // Result of initialisation
91 // Initialise object if not already done
92 if (!m_bInitialised) {
94 // Set members from property values
95 string sTxAddress = fgGetString("/sim/multiplay/txhost");
96 iTxPort = fgGetInt("/sim/multiplay/txport");
98 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txaddress= "
100 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txport= "
106 // Create and open tx socket
107 mDataTxSocket = new netSocket();
108 if (!mDataTxSocket->open(false)) {
109 // Failed to open tx socket
110 cerr << "FGMultiplayTxMgr::init - Failed to create data transmit socket" << endl;
113 mDataTxSocket->setBroadcast(true);
114 if (mDataTxSocket->connect(sTxAddress.c_str(), iTxPort) != 0) {
115 // Failed to connect tx socket
116 cerr << "FGMultiplayTxMgr::init - Failed to connect data transmit socket" << endl;
121 // Create a player object for the local player
123 mLocalPlayer = new MPPlayer();
124 if (!mLocalPlayer->Open(fgGetString("/sim/multiplay/rxaddress"), fgGetInt("/sim/multiplay/rxport"),
125 fgGetString("/sim/multiplay/callsign"), fgGetString("/sim/model/path"), true)) {
126 cerr << "FGMultiplayTxMgr::init - Failed to create player object for local player" << endl;
131 // If Tx port == zero then don't initialise
134 SG_LOG( SG_NETWORK, SG_WARN, "FGMultiplayTxMgr::init - Tx Port is zero. Multiplay out disabled." );
139 // Save manager state
140 m_bInitialised = bSuccess;
143 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Attempt to init object that is already opened" );
148 /* Return true if init succeeds */
154 /******************************************************************
156 * Description: Closes and deletes the local player object. Closes
157 * and deletes the tx socket. Resets the object state to unitialised.
158 ******************************************************************/
159 void FGMultiplayTxMgr::Close(void) {
162 // Delete local player
170 mDataTxSocket->close();
171 delete mDataTxSocket;
172 mDataTxSocket = NULL;
175 m_bInitialised = false;
180 /******************************************************************
181 * Name: SendMyPosition
182 * Description: Sends the position data for the local position.
183 ******************************************************************/
184 void FGMultiplayTxMgr::SendMyPosition(const sgMat4 PlayerPosMat4) {
187 T_PositionMsg PosMsg;
188 char sMsg[sizeof(T_MsgHdr) + sizeof(T_PositionMsg)];
190 if (m_bInitialised) {
191 mLocalPlayer->SetPosition(PlayerPosMat4);
192 mLocalPlayer->FillPosMsg(&MsgHdr, &PosMsg);
193 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
194 memcpy(sMsg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
195 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_PositionMsg), 0);
203 /******************************************************************
204 * Name: SendTextMessage
205 * Description: Sends a message to the player. The message must
206 * contain a valid and correctly filled out header and optional
208 ******************************************************************/
209 void FGMultiplayTxMgr::SendTextMessage(const string &sMsgText) const {
211 bool bResult = false;
214 int iNextBlockPosition = 0;
215 char sMsg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
217 if (m_bInitialised) {
219 mLocalPlayer->FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
221 // Divide the text string into blocks that fit in the message
222 // and send the blocks.
223 while (iNextBlockPosition < sMsgText.length()) {
224 strncpy(ChatMsg.sText, sMsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(), MAX_CHAT_MSG_LEN);
225 ChatMsg.sText[MAX_CHAT_MSG_LEN - 1] = '\0';
226 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
227 memcpy(sMsg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
228 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0);
229 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;