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