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