]> git.mxchange.org Git - flightgear.git/blob - src/Network/native.cxx
Merge branch 'work4' into next
[flightgear.git] / src / Network / native.cxx
1 // native.cxx -- FGFS "Native" protocal class
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
29
30 #include "native.hxx"
31
32 #include <FDM/flight.hxx>
33
34 FGNative::FGNative() {
35 }
36
37 FGNative::~FGNative() {
38 }
39
40
41 // open hailing frequencies
42 bool FGNative::open() {
43     if ( is_enabled() ) {
44         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
45                 << "is already in use, ignoring" );
46         return false;
47     }
48
49     SGIOChannel *io = get_io_channel();
50
51     if ( ! io->open( get_direction() ) ) {
52         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
53         return false;
54     }
55
56     set_enabled( true );
57
58     return true;
59 }
60
61 /**
62  * The design of FGNative requires direct, memcpy access to FGInterface,
63  * unfortunately. Since this is the only remaining place that does, the
64  * extern lives here, rather than a header file.
65  *
66  */
67 extern FGInterface* evil_global_fdm_state;
68
69 // process work for this port
70 bool FGNative::process() {
71     SGIOChannel *io = get_io_channel();
72     int length = sizeof(FGInterface);
73
74     if ( get_direction() == SG_IO_OUT ) {
75         buf = *evil_global_fdm_state;
76         if ( ! io->write( (char *)(& buf), length ) ) {
77             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
78             return false;
79         }
80     } else if ( get_direction() == SG_IO_IN ) {
81         if ( io->get_type() == sgFileType ) {
82             if ( io->read( (char *)(& buf), length ) == length ) {
83                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
84                 *evil_global_fdm_state = buf;
85             }
86         } else {
87             while ( io->read( (char *)(& buf), length ) == length ) {
88                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
89                 *evil_global_fdm_state = buf;
90             }
91         }
92     }
93
94     return true;
95 }
96
97
98 // close the channel
99 bool FGNative::close() {
100     SGIOChannel *io = get_io_channel();
101
102     set_enabled( false );
103
104     if ( ! io->close() ) {
105         return false;
106     }
107
108     return true;
109 }