]> git.mxchange.org Git - flightgear.git/blob - src/Network/native.cxx
Merge branch 'jmt/track-bug' 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
33 FGNative::FGNative() {
34 }
35
36 FGNative::~FGNative() {
37 }
38
39
40 // open hailing frequencies
41 bool FGNative::open() {
42     if ( is_enabled() ) {
43         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
44                 << "is already in use, ignoring" );
45         return false;
46     }
47
48     SGIOChannel *io = get_io_channel();
49
50     if ( ! io->open( get_direction() ) ) {
51         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
52         return false;
53     }
54
55     set_enabled( true );
56
57     return true;
58 }
59
60
61 // process work for this port
62 bool FGNative::process() {
63     SGIOChannel *io = get_io_channel();
64     int length = sizeof(*cur_fdm_state);
65
66     if ( get_direction() == SG_IO_OUT ) {
67         // cout << "size of cur_fdm_state = " << length << endl;
68         buf = *cur_fdm_state;
69         if ( ! io->write( (char *)(& buf), length ) ) {
70             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
71             return false;
72         }
73     } else if ( get_direction() == SG_IO_IN ) {
74         if ( io->get_type() == sgFileType ) {
75             if ( io->read( (char *)(& buf), length ) == length ) {
76                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
77                 *cur_fdm_state = buf;
78             }
79         } else {
80             while ( io->read( (char *)(& buf), length ) == length ) {
81                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
82                 *cur_fdm_state = buf;
83             }
84         }
85     }
86
87     return true;
88 }
89
90
91 // close the channel
92 bool FGNative::close() {
93     SGIOChannel *io = get_io_channel();
94
95     set_enabled( false );
96
97     if ( ! io->close() ) {
98         return false;
99     }
100
101     return true;
102 }