]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_netChannel.hxx
Don't use object returned from vector::end()
[simgear.git] / simgear / io / sg_netChannel.hxx
1 /*
2     NetChannel -  copied from PLIB
3
4      PLIB - A Suite of Portable Game Libraries
5      Copyright (C) 1998,2002  Steve Baker
6  
7      This library is free software; you can redistribute it and/or
8      modify it under the terms of the GNU Library General Public
9      License as published by the Free Software Foundation; either
10      version 2 of the License, or (at your option) any later version.
11  
12      This library is distributed in the hope that it will be useful,
13      but WITHOUT ANY WARRANTY; without even the implied warranty of
14      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15      Library General Public License for more details.
16  
17      You should have received a copy of the GNU Library General Public
18      License along with this library; if not, write to the Free Software
19      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
20  
21      For further information visit http://plib.sourceforge.net
22
23      $Id: netChannel.h 1899 2004-03-21 17:41:07Z sjbaker $
24 */
25
26 /****
27 * NAME
28 *   netChannel - network channel class
29 *
30 * DESCRIPTION
31 *   netChannel is adds event-handling to the low-level
32 *   netSocket class.  Otherwise, it can be treated as
33 *   a normal non-blocking socket object.
34 *
35 *   The direct interface between the netPoll() loop and
36 *   the channel object are the handleReadEvent and
37 *   handleWriteEvent methods. These are called
38 *   whenever a channel object 'fires' that event.
39 *
40 *   The firing of these low-level events can tell us whether
41 *   certain higher-level events have taken place, depending on
42 *   the timing and state of the connection.
43 *
44 * AUTHORS
45 *   Sam Rushing <rushing@nightmare.com> - original version for Medusa
46 *   Dave McClurg <dpm@efn.org> - modified for use in PLIB
47 *
48 * CREATION DATE
49 *   Dec-2000
50 *
51 ****/
52
53 #ifndef SG_NET_CHANNEL_H
54 #define SG_NET_CHANNEL_H
55
56 #include <simgear/io/raw_socket.hxx>
57
58 #include <string>
59 #include <vector>
60
61 namespace simgear
62 {
63
64 class NetChannelPoller;
65     
66 class NetChannel : public Socket
67 {
68   bool closed, connected, accepting, write_blocked, should_delete, resolving_host ;
69   std::string host;
70   int port;
71   
72     friend class NetChannelPoller;
73     NetChannelPoller* poller;
74 public:
75
76   NetChannel () ;
77   virtual ~NetChannel () ;
78
79   void setHandle (int s, bool is_connected = true);
80   bool isConnected () const { return connected; }
81   bool isClosed () const { return closed; }
82   void shouldDelete () { should_delete = true ; }
83
84   // --------------------------------------------------
85   // socket methods
86   // --------------------------------------------------
87   
88   bool  open    ( void ) ;
89   void  close   ( void ) ;
90   int   listen  ( int backlog ) ;
91   int   connect ( const char* host, int port ) ;
92   int   send    ( const void * buf, int size, int flags = 0 ) ;
93   int   recv    ( void * buf, int size, int flags = 0 ) ;
94
95   // poll() eligibility predicates
96   virtual bool readable (void) { return (connected || accepting); }
97   virtual bool writable (void) { return (!connected || write_blocked); }
98   
99   // --------------------------------------------------
100   // event handlers
101   // --------------------------------------------------
102   
103   void handleReadEvent (void);
104   void handleWriteEvent (void);
105   int handleResolve (void);
106   
107 // These are meant to be overridden.
108   virtual void handleClose (void) {
109   }
110   
111   virtual void handleRead (void);
112   virtual void handleWrite (void);
113   virtual void handleAccept (void);
114   virtual void handleError (int error);
115
116 };
117
118 class NetChannelPoller
119 {
120     typedef std::vector<NetChannel*> ChannelList;
121     ChannelList channels;
122 public:
123     void addChannel(NetChannel* channel);
124     void removeChannel(NetChannel* channel);
125     
126     bool hasChannels() const { return !channels.empty(); }
127     
128     bool poll(unsigned int timeout = 0);
129     void loop(unsigned int timeout = 0);
130 };
131
132 } // of namespace simgear
133
134 #endif // SG_NET_CHANNEL_H