2 Copied from PLIB into SimGear
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 For further information visit http://plib.sourceforge.net
23 $Id: netBuffer.h 1901 2004-03-21 18:19:11Z sjbaker $
28 * netBuffer - network buffer class
31 * Clients and servers built on top of netBufferChannel
32 * automatically support pipelining.
34 * Pipelining refers to a protocol capability. Normally,
35 * a conversation with a server has a back-and-forth
36 * quality to it. The client sends a command, and
37 * waits for the response. If a client needs to send
38 * many commands over a high-latency connection,
39 * waiting for each response can take a long time.
41 * For example, when sending a mail message to many recipients
42 * with SMTP, the client will send a series of RCPT commands, one
43 * for each recipient. For each of these commands, the server will
44 * send back a reply indicating whether the mailbox specified is
45 * valid. If you want to send a message to several hundred recipients,
46 * this can be rather tedious if the round-trip time for each command
47 * is long. You'd like to be able to send a bunch of RCPT commands
48 * in one batch, and then count off the responses to them as they come.
50 * I have a favorite visual when explaining the advantages of
51 * pipelining. Imagine each request to the server is a boxcar on a train.
52 * The client is in Los Angeles, and the server is in New York.
53 * Pipelining lets you hook all your cars in one long chain; send
54 * them to New York, where they are filled and sent back to you.
55 * Without pipelining you have to send one car at a time.
57 * Not all protocols allow pipelining. Not all servers support it;
58 * Sendmail, for example, does not support pipelining because it tends
59 * to fork unpredictably, leaving buffered data in a questionable state.
60 * A recent extension to the SMTP protocol allows a server to specify
61 * whether it supports pipelining. HTTP/1.1 explicitly requires that
62 * a server support pipelining.
65 * When a user passes in a buffer object, it belongs to
66 * the user. When the library gives a buffer to the user,
67 * the user should copy it.
70 * Sam Rushing <rushing@nightmare.com> - original version for Medusa
71 * Dave McClurg <dpm@efn.org> - modified for use in PLIB
78 #ifndef SG_NET_BUFFER_H
79 #define SG_NET_BUFFER_H
81 #include <simgear/io/sg_netChannel.hxx>
86 // ===========================================================================
88 // ===========================================================================
98 NetBuffer( int _max_length );
100 int getLength() const { return length ; }
101 int getMaxLength() const { return max_length ; }
104 ** getData() returns a pointer to the data
105 ** Note: a zero (0) byte is appended for convenience
106 ** but the data may have internal zero (0) bytes already
108 char* getData() { data [length] = 0 ; return data ; }
109 const char* getData() const { ((char*)data) [length] = 0 ; return data ; }
112 void remove (int pos, int n);
113 bool append (const char* s, int n);
117 // ===========================================================================
119 // ===========================================================================
121 class NetBufferChannel : public NetChannel
124 NetBuffer out_buffer;
127 virtual bool readable (void)
129 return (NetChannel::readable() &&
130 (in_buffer.getLength() < in_buffer.getMaxLength()));
133 virtual void handleRead (void) ;
135 virtual bool writable (void)
137 return (out_buffer.getLength() || should_close);
140 virtual void handleWrite (void) ;
144 NetBufferChannel (int in_buffer_size = 4096, int out_buffer_size = 16384);
145 virtual void handleClose ( void );
147 void closeWhenDone (void) { should_close = 1 ; }
149 virtual bool bufferSend (const char* msg, int msg_len);
150 virtual void handleBufferRead (NetBuffer& buffer);
153 } // namespace simgear
155 #endif // SG_NET_BUFFER_H