]> git.mxchange.org Git - flightgear.git/blob - src/Network/multiplay.cxx
Mathias Frö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
33 #include <Scenery/scenery.hxx>
34
35 #include "multiplay.hxx"
36
37 SG_USING_STD(string);
38
39
40 // These constants are provided so that the ident command can list file versions.
41 const char sFG_MULTIPLAY_BID[] = "$Id$";
42 const char sFG_MULTIPLAY_HID[] = FG_MULTIPLAY_HID;
43
44
45 /******************************************************************
46 * Name: FGMultiplay
47 * Description: Constructor.  Initialises the protocol and stores
48 * host and port information.
49 ******************************************************************/
50 FGMultiplay::FGMultiplay (const string &dir, const int rate, const string &host, const int port) {
51
52   set_hz(rate);
53
54   set_direction(dir);
55
56   if (get_direction() == SG_IO_IN) {
57
58     fgSetInt("/sim/multiplay/rxport", port);
59     fgSetString("/sim/multiplay/rxhost", host.c_str());
60
61   } else if (get_direction() == SG_IO_OUT) {
62
63     fgSetInt("/sim/multiplay/txport", port);
64     fgSetString("/sim/multiplay/txhost", host.c_str());
65
66   }
67
68 }
69
70
71 /******************************************************************
72 * Name: ~FGMultiplay
73 * Description: Destructor.
74 ******************************************************************/
75 FGMultiplay::~FGMultiplay () {
76 }
77
78
79 /******************************************************************
80 * Name: open
81 * Description: Enables the protocol.
82 ******************************************************************/
83 bool FGMultiplay::open() {
84
85     if ( is_enabled() ) {
86         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
87                 << "is already in use, ignoring" );
88         return false;
89     }
90
91     set_enabled(true);
92
93     return is_enabled();
94 }
95
96
97 /******************************************************************
98 * Name: process
99 * Description: Prompts the multiplayer mgr to either send
100 * or receive data over the network
101 ******************************************************************/
102 bool FGMultiplay::process() {
103
104   if (get_direction() == SG_IO_IN) {
105
106     globals->get_multiplayer_rx_mgr()->ProcessData();
107
108   } else if (get_direction() == SG_IO_OUT) {
109
110     sgMat4 posTrans;
111     sgCopyMat4(posTrans, globals->get_aircraft_model()->get3DModel()->get_POS());
112     Point3D center = globals->get_scenery()->get_center();
113     sgdVec3 PlayerPosition;
114     sgdSetVec3(PlayerPosition, posTrans[3][0] + center[0],
115                posTrans[3][1] + center[1], posTrans[3][2] + center[2]);
116     sgQuat PlayerOrientation;
117     sgMatrixToQuat(PlayerOrientation, posTrans);
118
119     globals->get_multiplayer_tx_mgr()->SendMyPosition(PlayerOrientation, PlayerPosition);
120
121   }
122
123     return true;
124 }
125
126
127 /******************************************************************
128 * Name: close
129 * Description:  Closes the multiplayer mgrs to stop any further
130 * network processing
131 ******************************************************************/
132 bool FGMultiplay::close() {
133
134   if (get_direction() == SG_IO_IN) {
135
136     globals->get_multiplayer_rx_mgr()->Close();
137
138   } else if (get_direction() == SG_IO_OUT) {
139
140     globals->get_multiplayer_tx_mgr()->Close();
141
142   }
143
144     return true;
145 }
146