aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bzip2.cpp7
-rw-r--r--src/bzip2.h2
-rw-r--r--src/client.cpp69
-rw-r--r--src/client.h20
-rw-r--r--src/file.cpp17
-rw-r--r--src/file.h6
-rw-r--r--src/filter.cpp19
-rw-r--r--src/filter.h6
-rw-r--r--src/protocol.cpp12
-rw-r--r--src/protocol.h26
-rw-r--r--src/server.cpp11
-rw-r--r--src/server.h8
-rw-r--r--src/socket.cpp48
-rw-r--r--src/socket.h6
-rw-r--r--src/stream.h35
15 files changed, 266 insertions, 26 deletions
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
index d3f237a..66786e4 100644
--- a/src/bzip2.cpp
+++ b/src/bzip2.cpp
@@ -128,7 +128,7 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes )
128 { 128 {
129 if( bzState.avail_in > 0 ) 129 if( bzState.avail_in > 0 )
130 { 130 {
131 if( rNext.canSeek() ) 131 if( rNext.isSeekable() )
132 { 132 {
133 rNext.seek( -bzState.avail_in ); 133 rNext.seek( -bzState.avail_in );
134 } 134 }
@@ -185,3 +185,8 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes )
185 return sTotalOut; 185 return sTotalOut;
186} 186}
187 187
188bool Bu::BZip2::isOpen()
189{
190 return (bzState.state != NULL);
191}
192
diff --git a/src/bzip2.h b/src/bzip2.h
index a23f07a..25f10c5 100644
--- a/src/bzip2.h
+++ b/src/bzip2.h
@@ -22,6 +22,8 @@ namespace Bu
22 virtual size_t read( void *pBuf, size_t nBytes ); 22 virtual size_t read( void *pBuf, size_t nBytes );
23 virtual size_t write( const void *pBuf, size_t nBytes ); 23 virtual size_t write( const void *pBuf, size_t nBytes );
24 24
25 virtual bool isOpen();
26
25 private: 27 private:
26 void bzError( int code ); 28 void bzError( int code );
27 bz_stream bzState; 29 bz_stream bzState;
diff --git a/src/client.cpp b/src/client.cpp
index a33cdc3..cf96424 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -1,6 +1,16 @@
1#include "client.h" 1#include "bu/client.h"
2#include "bu/socket.h"
3#include <stdlib.h>
4#include <errno.h>
5#include "bu/exceptions.h"
6#include "bu/protocol.h"
2 7
3Bu::Client::Client() 8/** Read buffer size. */
9#define RBS (1024*2)
10
11Bu::Client::Client( Bu::Socket *pSocket ) :
12 pSocket( pSocket ),
13 pProto( NULL )
4{ 14{
5} 15}
6 16
@@ -8,3 +18,58 @@ Bu::Client::~Client()
8{ 18{
9} 19}
10 20
21void Bu::Client::processInput()
22{
23 char buf[RBS];
24 size_t nRead, nTotal=0;
25
26 for(;;)
27 {
28 nRead = pSocket->read( buf, nRead );
29 if( nRead < 0 )
30 {
31 throw Bu::ConnectionException(
32 excodeReadError,
33 "Read error: %s",
34 strerror( errno )
35 );
36 }
37 else if( nRead == 0 )
38 {
39 break;
40 }
41 else
42 {
43 nTotal += nRead;
44 sReadBuf.append( buf, nRead );
45 if( !pSocket->canRead() )
46 break;
47 }
48 }
49
50 if( pProto && nTotal )
51 {
52 pProto->onNewData( this );
53 }
54}
55
56void Bu::Client::setProtocol( Protocol *pProto )
57{
58 this->pProto = pProto;
59}
60
61Bu::Protocol *Bu::Client::getProtocol()
62{
63 return pProto;
64}
65
66void Bu::Client::clearProtocol()
67{
68 pProto = NULL;
69}
70
71Bu::FString &Bu::Client::getInput()
72{
73 return sReadBuf;
74}
75
diff --git a/src/client.h b/src/client.h
index 27fbad4..1a189e2 100644
--- a/src/client.h
+++ b/src/client.h
@@ -2,21 +2,35 @@
2#define CLIENT_H 2#define CLIENT_H
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include "bu/socket.h" 5
6#include "bu/fstring.h"
6 7
7namespace Bu 8namespace Bu
8{ 9{
10 class Protocol;
11 class Socket;
12
9 /** 13 /**
10 * 14 *
11 */ 15 */
12 class Client 16 class Client
13 { 17 {
14 public: 18 public:
15 Client(); 19 Client( Bu::Socket *pSocket );
16 virtual ~Client(); 20 virtual ~Client();
17 21
18 private: 22 void processInput();
19 23
24 Bu::FString &getInput();
25
26 void setProtocol( Protocol *pProto );
27 Bu::Protocol *getProtocol();
28 void clearProtocol();
29
30 private:
31 Bu::Socket *pSocket;
32 Bu::Protocol *pProto;
33 Bu::FString sReadBuf;
20 }; 34 };
21} 35}
22 36
diff --git a/src/file.cpp b/src/file.cpp
index 2965afa..368b788 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -109,7 +109,17 @@ bool Bu::File::canWrite()
109 return true; 109 return true;
110} 110}
111 111
112bool Bu::File::canSeek() 112bool Bu::File::isReadable()
113{
114 return true;
115}
116
117bool Bu::File::isWritable()
118{
119 return true;
120}
121
122bool Bu::File::isSeekable()
113{ 123{
114 return true; 124 return true;
115} 125}
@@ -139,3 +149,8 @@ void Bu::File::chmod( mode_t t )
139 fchmod( fileno( fh ), t ); 149 fchmod( fileno( fh ), t );
140} 150}
141 151
152bool Bu::File::isOpen()
153{
154 return (fh != NULL);
155}
156
diff --git a/src/file.h b/src/file.h
index 8107a1b..fe8dbda 100644
--- a/src/file.h
+++ b/src/file.h
@@ -25,12 +25,16 @@ namespace Bu
25 virtual void setPos( long pos ); 25 virtual void setPos( long pos );
26 virtual void setPosEnd( long pos ); 26 virtual void setPosEnd( long pos );
27 virtual bool isEOS(); 27 virtual bool isEOS();
28 virtual bool isOpen();
28 29
29 virtual void flush(); 30 virtual void flush();
30 31
31 virtual bool canRead(); 32 virtual bool canRead();
32 virtual bool canWrite(); 33 virtual bool canWrite();
33 virtual bool canSeek(); 34
35 virtual bool isReadable();
36 virtual bool isWritable();
37 virtual bool isSeekable();
34 38
35 virtual bool isBlocking(); 39 virtual bool isBlocking();
36 virtual void setBlocking( bool bBlocking=true ); 40 virtual void setBlocking( bool bBlocking=true );
diff --git a/src/filter.cpp b/src/filter.cpp
index 693fb9f..96a8694 100644
--- a/src/filter.cpp
+++ b/src/filter.cpp
@@ -50,6 +50,11 @@ bool Bu::Filter::isEOS()
50 return rNext.isEOS(); 50 return rNext.isEOS();
51} 51}
52 52
53bool Bu::Filter::isOpen()
54{
55 return rNext.isOpen();
56}
57
53bool Bu::Filter::canRead() 58bool Bu::Filter::canRead()
54{ 59{
55 return rNext.canRead(); 60 return rNext.canRead();
@@ -60,9 +65,19 @@ bool Bu::Filter::canWrite()
60 return rNext.canWrite(); 65 return rNext.canWrite();
61} 66}
62 67
63bool Bu::Filter::canSeek() 68bool Bu::Filter::isReadable()
69{
70 return rNext.isReadable();
71}
72
73bool Bu::Filter::isWritable()
74{
75 return rNext.isWritable();
76}
77
78bool Bu::Filter::isSeekable()
64{ 79{
65 return rNext.canSeek(); 80 return rNext.isSeekable();
66} 81}
67 82
68bool Bu::Filter::isBlocking() 83bool Bu::Filter::isBlocking()
diff --git a/src/filter.h b/src/filter.h
index 088d46e..7bb04bc 100644
--- a/src/filter.h
+++ b/src/filter.h
@@ -40,12 +40,16 @@ namespace Bu
40 virtual void setPos( long pos ); 40 virtual void setPos( long pos );
41 virtual void setPosEnd( long pos ); 41 virtual void setPosEnd( long pos );
42 virtual bool isEOS(); 42 virtual bool isEOS();
43 virtual bool isOpen();
43 44
44 virtual void flush(); 45 virtual void flush();
45 46
46 virtual bool canRead(); 47 virtual bool canRead();
47 virtual bool canWrite(); 48 virtual bool canWrite();
48 virtual bool canSeek(); 49
50 virtual bool isReadable();
51 virtual bool isWritable();
52 virtual bool isSeekable();
49 53
50 virtual bool isBlocking(); 54 virtual bool isBlocking();
51 virtual void setBlocking( bool bBlocking=true ); 55 virtual void setBlocking( bool bBlocking=true );
diff --git a/src/protocol.cpp b/src/protocol.cpp
new file mode 100644
index 0000000..0976b3b
--- /dev/null
+++ b/src/protocol.cpp
@@ -0,0 +1,12 @@
1#include "bu/protocol.h"
2
3using namespace Bu;
4
5Bu::Protocol::Protocol()
6{
7}
8
9Bu::Protocol::~Protocol()
10{
11}
12
diff --git a/src/protocol.h b/src/protocol.h
new file mode 100644
index 0000000..3accd99
--- /dev/null
+++ b/src/protocol.h
@@ -0,0 +1,26 @@
1#ifndef PROTOCOL_H
2#define PROTOCOL_H
3
4#include <stdint.h>
5
6namespace Bu
7{
8 class Client;
9
10 /**
11 *
12 */
13 class Protocol
14 {
15 public:
16 Protocol();
17 virtual ~Protocol();
18
19 virtual void onNewData( Bu::Client *pClient )=0;
20
21 private:
22
23 };
24}
25
26#endif
diff --git a/src/server.cpp b/src/server.cpp
index abf4c5b..bceeb81 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -1,5 +1,8 @@
1#include "server.h" 1#include "bu/server.h"
2#include <errno.h> 2#include <errno.h>
3#include "bu/serversocket.h"
4#include "bu/client.h"
5#include "bu/socket.h"
3 6
4Bu::Server::Server() : 7Bu::Server::Server() :
5 nTimeoutSec( 0 ), 8 nTimeoutSec( 0 ),
@@ -58,7 +61,7 @@ void Bu::Server::scan()
58 } 61 }
59 else 62 else
60 { 63 {
61 64 hClients.get( j )->processInput();
62 } 65 }
63 } 66 }
64 } 67 }
@@ -68,7 +71,9 @@ void Bu::Server::addClient( int nSocket, int nPort )
68{ 71{
69 FD_SET( nSocket, &fdActive ); 72 FD_SET( nSocket, &fdActive );
70 73
71 Client *c = new Client(); 74 Client *c = new Client(
75 new Bu::Socket( nSocket )
76 );
72 hClients.insert( nSocket, c ); 77 hClients.insert( nSocket, c );
73 78
74 onNewConnection( c, nPort ); 79 onNewConnection( c, nPort );
diff --git a/src/server.h b/src/server.h
index 942eb32..3331d2c 100644
--- a/src/server.h
+++ b/src/server.h
@@ -2,12 +2,16 @@
2#define SERVER_H 2#define SERVER_H
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include "bu/serversocket.h" 5
6#include "bu/fstring.h"
6#include "bu/list.h" 7#include "bu/list.h"
7#include "bu/client.h"
8 8
9namespace Bu 9namespace Bu
10{ 10{
11 class ServerSocket;
12 class Socket;
13 class Client;
14
11 /** 15 /**
12 * Core of a network server. This class is distinct from a ServerSocket in 16 * Core of a network server. This class is distinct from a ServerSocket in
13 * that a ServerSocket is one listening socket, nothing more. Socket will 17 * that a ServerSocket is one listening socket, nothing more. Socket will
diff --git a/src/socket.cpp b/src/socket.cpp
index 1832898..bd05024 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -109,13 +109,6 @@ void Bu::Socket::close()
109 ::close( nSocket ); 109 ::close( nSocket );
110 } 110 }
111 bActive = false; 111 bActive = false;
112 //xInputBuf.clearData();
113 //xOutputBuf.clearData();
114 //if( pProtocol != NULL )
115 //{
116 // delete pProtocol;
117 // pProtocol = NULL;
118 //}
119} 112}
120 113
121/* 114/*
@@ -218,15 +211,49 @@ bool Bu::Socket::isEOS()
218 211
219bool Bu::Socket::canRead() 212bool Bu::Socket::canRead()
220{ 213{
214 fd_set rfds;
215 FD_ZERO(&rfds);
216 FD_SET(nSocket, &rfds);
217 struct timeval tv = { 0, 0 };
218 int retval = select( nSocket+1, &rfds, NULL, NULL, &tv );
219 if( retval == -1 )
220 throw ConnectionException(
221 excodeBadReadError,
222 "Bad Read error"
223 );
224 if( !FD_ISSET( nSocket, &rfds ) )
225 return false;
221 return true; 226 return true;
222} 227}
223 228
224bool Bu::Socket::canWrite() 229bool Bu::Socket::canWrite()
225{ 230{
231 fd_set wfds;
232 FD_ZERO(&wfds);
233 FD_SET(nSocket, &wfds);
234 struct timeval tv = { 0, 0 };
235 int retval = select( nSocket+1, NULL, &wfds, NULL, &tv );
236 if( retval == -1 )
237 throw ConnectionException(
238 excodeBadReadError,
239 "Bad Read error"
240 );
241 if( !FD_ISSET( nSocket, &wfds ) )
242 return false;
226 return true; 243 return true;
227} 244}
228 245
229bool Bu::Socket::canSeek() 246bool Bu::Socket::isReadable()
247{
248 return true;
249}
250
251bool Bu::Socket::isWritable()
252{
253 return true;
254}
255
256bool Bu::Socket::isSeekable()
230{ 257{
231 return false; 258 return false;
232} 259}
@@ -244,3 +271,8 @@ void Bu::Socket::flush()
244{ 271{
245} 272}
246 273
274bool Bu::Socket::isOpen()
275{
276 return bActive;
277}
278
diff --git a/src/socket.h b/src/socket.h
index 30a43fb..c9dbd8d 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -28,12 +28,16 @@ namespace Bu
28 virtual void setPos( long pos ); 28 virtual void setPos( long pos );
29 virtual void setPosEnd( long pos ); 29 virtual void setPosEnd( long pos );
30 virtual bool isEOS(); 30 virtual bool isEOS();
31 virtual bool isOpen();
31 32
32 virtual void flush(); 33 virtual void flush();
33 34
34 virtual bool canRead(); 35 virtual bool canRead();
35 virtual bool canWrite(); 36 virtual bool canWrite();
36 virtual bool canSeek(); 37
38 virtual bool isReadable();
39 virtual bool isWritable();
40 virtual bool isSeekable();
37 41
38 virtual bool isBlocking(); 42 virtual bool isBlocking();
39 virtual void setBlocking( bool bBlocking=true ); 43 virtual void setBlocking( bool bBlocking=true );
diff --git a/src/stream.h b/src/stream.h
index a80586b..ba070d3 100644
--- a/src/stream.h
+++ b/src/stream.h
@@ -31,12 +31,45 @@ namespace Bu
31 virtual void setPos( long pos ) = 0; 31 virtual void setPos( long pos ) = 0;
32 virtual void setPosEnd( long pos ) = 0; 32 virtual void setPosEnd( long pos ) = 0;
33 virtual bool isEOS() = 0; 33 virtual bool isEOS() = 0;
34 virtual bool isOpen() = 0;
34 35
35 virtual void flush() = 0; 36 virtual void flush() = 0;
36 37
38 /**
39 * In non-blocking streams this indicates if a read operation will
40 * return data at the moment or not. In blocking streams this should
41 * return the same value as isEOS().
42 */
37 virtual bool canRead() = 0; 43 virtual bool canRead() = 0;
44
45 /**
46 * In non-blocking streams this indicates if a write operation will
47 * succeed or fail. In some cases writing is not allowed (e.g.
48 * internal buffers are full) temporarilly. In blocking streams this
49 * should return the same value as isWritable.
50 */
38 virtual bool canWrite() = 0; 51 virtual bool canWrite() = 0;
39 virtual bool canSeek() = 0; 52
53 /**
54 * Indicates if the stream is capable of read operations. This does not
55 * indicate if such operations will return useful data, see canRead for
56 * that.
57 */
58 virtual bool isReadable() = 0;
59
60 /**
61 * Indicates if the stream is capable of write operations. This does
62 * not indicate if such operations will succeed or fail, see canWrite
63 * for that.
64 */
65 virtual bool isWritable() = 0;
66
67 /**
68 * Indicates if the stream is capable of seek operations. This is
69 * generally false for non-blocking streams. Some buffered streams may
70 * support limited in-buffer seeking.
71 */
72 virtual bool isSeekable() = 0;
40 73
41 virtual bool isBlocking() = 0; 74 virtual bool isBlocking() = 0;
42 virtual void setBlocking( bool bBlocking=true ) = 0; 75 virtual void setBlocking( bool bBlocking=true ) = 0;