]> git.mxchange.org Git - flightgear.git/blob - src/Network/multiplay.cxx
Mathias Fröhölöiööhlich:
[flightgear.git] / src / Network / multiplay.cxx
1 // multiplay.cxx -- protocol object for multiplay in Flightgear
2 //
3 // Written by Diarmuid Tyson, started February 2003.
4 // diarmuid.tyson@airservicesaustralia.com
5 //
6 // Copyright (C) 2003  Airservices Australia
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22
23
24 #include <simgear/compiler.h>
25
26 #include STL_STRING
27
28 #include <iostream>
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/scene/model/placement.hxx>
32 #include <simgear/scene/model/placementtrans.hxx>
33
34 #include <Scenery/scenery.hxx>
35
36 #include "multiplay.hxx"
37
38 SG_USING_STD(string);
39
40
41 // These constants are provided so that the ident command can list file versions.
42 const char sFG_MULTIPLAY_BID[] = "$Id$";
43 const char sFG_MULTIPLAY_HID[] = FG_MULTIPLAY_HID;
44
45
46 /******************************************************************
47 * Name: FGMultiplay
48 * Description: Constructor.  Initialises the protocol and stores
49 * host and port information.
50 ******************************************************************/
51 FGMultiplay::FGMultiplay (const string &dir, const int rate, const string &host, const int port) {
52
53   set_hz(rate);
54
55   set_direction(dir);
56
57   if (get_direction() == SG_IO_IN) {
58
59     fgSetInt("/sim/multiplay/rxport", port);
60     fgSetString("/sim/multiplay/rxhost", host.c_str());
61
62   } else if (get_direction() == SG_IO_OUT) {
63
64     fgSetInt("/sim/multiplay/txport", port);
65     fgSetString("/sim/multiplay/txhost", host.c_str());
66
67   }
68
69 }
70
71
72 /******************************************************************
73 * Name: ~FGMultiplay
74 * Description: Destructor.
75 ******************************************************************/
76 FGMultiplay::~FGMultiplay () {
77 }
78
79
80 /******************************************************************
81 * Name: open
82 * Description: Enables the protocol.
83 ******************************************************************/
84 bool FGMultiplay::open() {
85
86     if ( is_enabled() ) {
87         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
88                 << "is already in use, ignoring" );
89         return false;
90     }
91
92     set_enabled(true);
93
94     return is_enabled();
95 }
96
97
98 /******************************************************************
99 * Name: process
100 * Description: Prompts the multiplayer mgr to either send
101 * or receive data over the network
102 ******************************************************************/
103 bool FGMultiplay::process() {
104
105   if (get_direction() == SG_IO_IN) {
106
107     globals->get_multiplayer_rx_mgr()->ProcessData();
108
109   } else if (get_direction() == SG_IO_OUT) {
110
111     sgMat4 posTrans;
112     globals->get_aircraft_model()->get3DModel()->getTransform()->getTransform(posTrans);
113     Point3D center = globals->get_scenery()->get_center();
114     sgdVec3 PlayerPosition;
115     sgdSetVec3(PlayerPosition, posTrans[3][0] + center[0],
116                posTrans[3][1] + center[1], posTrans[3][2] + center[2]);
117     sgQuat PlayerOrientation;
118     sgMatrixToQuat(PlayerOrientation, posTrans);
119
120     globals->get_multiplayer_tx_mgr()->SendMyPosition(PlayerOrientation, PlayerPosition);
121
122   }
123
124     return true;
125 }
126
127
128 /******************************************************************
129 * Name: close
130 * Description:  Closes the multiplayer mgrs to stop any further
131 * network processing
132 ******************************************************************/
133 bool FGMultiplay::close() {
134
135   if (get_direction() == SG_IO_IN) {
136
137     globals->get_multiplayer_rx_mgr()->Close();
138
139   } else if (get_direction() == SG_IO_OUT) {
140
141     globals->get_multiplayer_tx_mgr()->Close();
142
143   }
144
145     return true;
146 }
147