2 NetChannel - copied from PLIB
4 PLIB - A Suite of Portable Game Libraries
5 Copyright (C) 1998,2002 Steve Baker
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 For further information visit http://plib.sourceforge.net
23 $Id: netChannel.h 1899 2004-03-21 17:41:07Z sjbaker $
28 * netChannel - network channel class
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.
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.
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.
45 * Sam Rushing <rushing@nightmare.com> - original version for Medusa
46 * Dave McClurg <dpm@efn.org> - modified for use in PLIB
53 #ifndef SG_NET_CHANNEL_H
54 #define SG_NET_CHANNEL_H
56 #include <simgear/io/raw_socket.hxx>
57 #include <simgear/structure/SGSharedPtr.hxx>
65 class NetChannel : public Socket
67 bool closed, connected, accepting, write_blocked, should_delete ;
68 NetChannel* next_channel ;
69 SGSharedPtr<HostLookup> host_lookup ;
72 friend bool netPoll (unsigned int timeout);
78 virtual ~NetChannel () ;
80 void setHandle (int s, bool is_connected = true);
81 bool isConnected () const { return connected; }
82 bool isClosed () const { return closed; }
83 void shouldDelete () { should_delete = true ; }
85 // --------------------------------------------------
87 // --------------------------------------------------
91 int listen ( int backlog ) ;
92 int connect ( const char* host, int port ) ;
93 int send ( const void * buf, int size, int flags = 0 ) ;
94 int recv ( void * buf, int size, int flags = 0 ) ;
96 // poll() eligibility predicates
97 virtual bool readable (void) { return (connected || accepting); }
98 virtual bool writable (void) { return (!connected || write_blocked); }
100 // --------------------------------------------------
102 // --------------------------------------------------
104 void handleReadEvent (void);
105 void handleWriteEvent (void);
107 // These are meant to be overridden.
108 virtual void handleClose (void) {
111 virtual void handleRead (void);
112 virtual void handleWrite (void);
113 virtual void handleAccept (void);
114 virtual void handleError (int error);
116 static bool poll (unsigned int timeout = 0 ) ;
117 static void loop (unsigned int timeout = 0 ) ;
120 } // of namespace simgear
122 #endif // SG_NET_CHANNEL_H