]> git.mxchange.org Git - simgear.git/blob - simgear/io/iochannel.hxx
3b693214fec2a21c1102cb211c48bdfd76e0fb21
[simgear.git] / simgear / io / iochannel.hxx
1 /**
2  * \file iochannel.hxx
3  * High level IO channel base class.
4  */
5
6 // Written by Curtis Olson, started November 1999.
7 //
8 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _IOCHANNEL_HXX
28 #define _IOCHANNEL_HXX
29
30
31 #include <simgear/compiler.h>
32
33 // #include "protocol.hxx"
34
35 #include STL_STRING
36 #include <vector>
37
38 SG_USING_STD(vector);
39 SG_USING_STD(string);
40
41
42 #define SG_IO_MAX_MSG_SIZE 16384
43
44 /**
45  * Specify if this is a read (IN), write (OUT), or r/w (BI) directional
46  * channel
47  */
48 enum SGProtocolDir {
49     SG_IO_NONE = 0,
50     SG_IO_IN = 1,
51     SG_IO_OUT = 2,
52     SG_IO_BI = 3
53 };
54
55 /**
56  * Specify the channel type
57  */
58 enum SGChannelType {
59     sgFileType = 0,
60     sgSerialType = 1,
61     sgSocketType = 2
62 };
63
64
65 /**
66  * The SGIOChannel base class provides a consistent method for
67  * applications to communication through various mediums. By providing
68  * a base class with multiple derived classes, and application such as
69  * FlightGear can implement a way to speak any protocol via any kind
70  * of I/O channel.
71  *
72  * All of the SGIOChannel derived classes have exactly the same usage
73  * interface once an instance has been created.
74  *
75  */
76 class SGIOChannel {
77
78     SGChannelType type;
79     SGProtocolDir dir;
80     bool valid;
81
82 public:
83
84     /** Constructor */
85     SGIOChannel();
86
87     /** Destructor */
88     virtual ~SGIOChannel();
89
90     /** Open a channel.
91      * @param d channel communication "direction"
92      * Direction can be one of: 
93      *  - SG_IO_IN - data will be flowing into this object to the application. 
94      *  - SG_IO_OUT - data will be flowing out of this object from the
95      *                application. 
96      *  - SG_IO_BI - data will be flowing in both directions. 
97      *  - SG_IO_NONE - data will not be flowing in either direction.
98      *                 This is here for the sake of completeness. 
99      */
100     virtual bool open( const SGProtocolDir d );
101
102     /**
103      * The read() method is modeled after the read() Unix system
104      * call. You must provide a pointer to a character buffer that has
105      * enough allocated space for your potential read. You can also
106      * specify the maximum number of bytes allowed for this particular
107      * read. The actual number of bytes read is returned.  You are
108      * responsible to ensure that the size of buf is large enough to
109      * accomodate your input message
110      * @param buf a char pointer to your input buffer 
111      * @param length max number of bytes to read
112      */
113     virtual int read( char *buf, int length );
114
115     /**
116      * The readline() method is similar to read() except that it will
117      * stop at the first end of line encountered in the input buffer.
118      * @param buf a char pointer to your input buffer 
119      * @param length max number of bytes to read
120      */
121     virtual int readline( char *buf, int length );
122
123
124     /**
125      * The write() method is modeled after the write() Unix system
126      * call and is analogous to the read() method. You provide a
127      * pointer to a buffer of data, and then length of that data to be
128      * written out. The number of bytes written is returned.
129      * @param buf a char pointer to your output buffer 
130      * @param length number of bytes to write
131      */
132     virtual int write( const char *buf, const int length );
133
134     /**
135      * The writestring() method is a simple wrapper that will
136      * calculate the length of a null terminated character array and
137      * write it to the output channel.
138      * @param buf a char pointer to your output buffer 
139      */
140     virtual int writestring( const char *str );
141
142     /**
143      * The close() method is modeled after the close() Unix system
144      * call and will close an open device. You should call this method
145      * when you are done using your IO class, before it is destructed.
146      */
147     virtual bool close();
148
149     inline void set_type( SGChannelType t ) { type = t; }
150     inline SGChannelType get_type() const { return type; }
151
152     inline void set_dir( const SGProtocolDir d ) { dir = d; }
153     inline SGProtocolDir get_dir() const { return dir; }
154     inline bool isvalid() const { return valid; }
155 };
156
157
158 #endif // _IOCHANNEL_HXX
159
160