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>
35 # include <sys/socket.h>
36 # include <netinet/in.h>
37 # include <arpa/inet.h>
39 #include <plib/netSocket.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <Main/fg_props.hxx>
45 #include "multiplaytxmgr.hxx"
46 #include "mpmessages.hxx"
47 #include "mpplayer.hxx"
49 // These constants are provided so that the ident command can list file versions.
50 const char sMULTIPLAYTXMGR_BID[] = "$Id$";
51 const char sMULTIPLAYTXMGR_HID[] = MULTIPLAYTXMGR_HID;
55 /******************************************************************
56 * Name: FGMultiplayTxMgr
57 * Description: Constructor.
58 ******************************************************************/
59 FGMultiplayTxMgr::FGMultiplayTxMgr() {
61 int iPlayerCnt; // Count of players in player array
63 // Initialise private members
64 m_bInitialised = false;
70 /******************************************************************
71 * Name: ~FGMultiplayTxMgr
72 * Description: Destructor. Closes and deletes objects owned by
74 ******************************************************************/
75 FGMultiplayTxMgr::~FGMultiplayTxMgr() {
82 /******************************************************************
84 * Description: Initialises multiplayer transmit
85 ******************************************************************/
86 bool FGMultiplayTxMgr::init(void) {
89 string sTxAddress; // Destination address
91 bool bSuccess = true; // Result of initialisation
93 // Initialise object if not already done
94 if (!m_bInitialised) {
96 // Set members from property values
97 string sTxAddress = fgGetString("/sim/multiplay/txhost");
98 iTxPort = fgGetInt("/sim/multiplay/txport");
100 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txaddress= "
102 SG_LOG( SG_NETWORK, SG_INFO, "FGMultiplayTxMgr::init - txport= "
108 // Create and open tx socket
109 mDataTxSocket = new netSocket();
110 if (!mDataTxSocket->open(false)) {
111 // Failed to open tx socket
112 cerr << "FGMultiplayTxMgr::init - Failed to create data transmit socket" << endl;
115 mDataTxSocket->setBroadcast(true);
116 if (mDataTxSocket->connect(sTxAddress.c_str(), iTxPort) != 0) {
117 // Failed to connect tx socket
118 cerr << "FGMultiplayTxMgr::init - Failed to connect data transmit socket" << endl;
123 // Create a player object for the local player
125 mLocalPlayer = new MPPlayer();
126 if (!mLocalPlayer->Open(fgGetString("/sim/multiplay/rxaddress"), fgGetInt("/sim/multiplay/rxport"),
127 fgGetString("/sim/multiplay/callsign"), fgGetString("/sim/model/path"), true)) {
128 cerr << "FGMultiplayTxMgr::init - Failed to create player object for local player" << endl;
133 // If Tx port == zero then don't initialise
136 SG_LOG( SG_NETWORK, SG_WARN, "FGMultiplayTxMgr::init - Tx Port is zero. Multiplay out disabled." );
141 // Save manager state
142 m_bInitialised = bSuccess;
145 SG_LOG( SG_NETWORK, SG_ALERT, "FGMultiplayTxMgr::init - Attempt to init object that is already opened" );
150 /* Return true if init succeeds */
156 /******************************************************************
158 * Description: Closes and deletes the local player object. Closes
159 * and deletes the tx socket. Resets the object state to unitialised.
160 ******************************************************************/
161 void FGMultiplayTxMgr::Close(void) {
164 // Delete local player
172 mDataTxSocket->close();
173 delete mDataTxSocket;
174 mDataTxSocket = NULL;
177 m_bInitialised = false;
182 /******************************************************************
183 * Name: SendMyPosition
184 * Description: Sends the position data for the local position.
185 ******************************************************************/
186 void FGMultiplayTxMgr::SendMyPosition(const sgMat4 PlayerPosMat4) {
189 T_PositionMsg PosMsg;
190 char sMsg[sizeof(T_MsgHdr) + sizeof(T_PositionMsg)];
192 if (m_bInitialised) {
193 mLocalPlayer->SetPosition(PlayerPosMat4);
194 mLocalPlayer->FillPosMsg(&MsgHdr, &PosMsg);
195 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
196 memcpy(sMsg + sizeof(T_MsgHdr), &PosMsg, sizeof(T_PositionMsg));
197 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_PositionMsg), 0);
205 /******************************************************************
206 * Name: SendTextMessage
207 * Description: Sends a message to the player. The message must
208 * contain a valid and correctly filled out header and optional
210 ******************************************************************/
211 void FGMultiplayTxMgr::SendTextMessage(const string &sMsgText) const {
213 bool bResult = false;
216 int iNextBlockPosition = 0;
217 char sMsg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
219 if (m_bInitialised) {
221 mLocalPlayer->FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
223 // Divide the text string into blocks that fit in the message
224 // and send the blocks.
225 while (iNextBlockPosition < sMsgText.length()) {
226 strncpy(ChatMsg.sText, sMsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(), MAX_CHAT_MSG_LEN);
227 ChatMsg.sText[MAX_CHAT_MSG_LEN - 1] = '\0';
228 memcpy(sMsg, &MsgHdr, sizeof(T_MsgHdr));
229 memcpy(sMsg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
230 mDataTxSocket->send(sMsg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0);
231 iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;