aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.cpp54
-rw-r--r--src/client.h10
-rw-r--r--src/plugger.h6
-rw-r--r--src/socket.cpp12
-rw-r--r--src/socket.h2
5 files changed, 79 insertions, 5 deletions
diff --git a/src/client.cpp b/src/client.cpp
index 0e48285..63822ba 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -10,7 +10,8 @@
10 10
11Bu::Client::Client( Bu::Socket *pSocket ) : 11Bu::Client::Client( Bu::Socket *pSocket ) :
12 pSocket( pSocket ), 12 pSocket( pSocket ),
13 pProto( NULL ) 13 pProto( NULL ),
14 nRBOffset( 0 )
14{ 15{
15} 16}
16 17
@@ -103,3 +104,54 @@ void Bu::Client::write( const char *pData, int nBytes )
103 sWriteBuf.append( pData, nBytes ); 104 sWriteBuf.append( pData, nBytes );
104} 105}
105 106
107void Bu::Client::write( int8_t nData )
108{
109 sWriteBuf.append( (const char *)&nData, sizeof(nData) );
110}
111
112void Bu::Client::write( int16_t nData )
113{
114 sWriteBuf.append( (const char *)&nData, sizeof(nData) );
115}
116
117void Bu::Client::write( int32_t nData )
118{
119 sWriteBuf.append( (const char *)&nData, sizeof(nData) );
120}
121
122void Bu::Client::write( int64_t nData )
123{
124 sWriteBuf.append( (const char *)&nData, sizeof(nData) );
125}
126
127void Bu::Client::read( char *pData, int nBytes )
128{
129 memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes );
130 nRBOffset += nBytes;
131 if( sReadBuf.getSize()-nRBOffset == 0 )
132 {
133 sReadBuf.clear();
134 nRBOffset = 0;
135 }
136 // This is an experimental threshold, maybe I'll make this configurable
137 // later on.
138 else if(
139 (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) ||
140 (nRBOffset >= sReadBuf.getSize()/4)
141 )
142 {
143 sReadBuf.trimFront( nRBOffset );
144 nRBOffset = 0;
145 }
146}
147
148long Bu::Client::getInputSize()
149{
150 return sReadBuf.getSize()-nRBOffset;
151}
152
153const Bu::Socket *Bu::Client::getSocket() const
154{
155 return pSocket;
156}
157
diff --git a/src/client.h b/src/client.h
index f228e96..b400bb3 100644
--- a/src/client.h
+++ b/src/client.h
@@ -25,7 +25,12 @@ namespace Bu
25 Bu::FString &getInput(); 25 Bu::FString &getInput();
26 Bu::FString &getOutput(); 26 Bu::FString &getOutput();
27 void write( const char *pData, int nBytes ); 27 void write( const char *pData, int nBytes );
28 void read( const char *pData, int nBytes ); 28 void write( int8_t nData );
29 void write( int16_t nData );
30 void write( int32_t nData );
31 void write( int64_t nData );
32 void read( char *pData, int nBytes );
33 long getInputSize();
29 34
30 void setProtocol( Protocol *pProto ); 35 void setProtocol( Protocol *pProto );
31 Bu::Protocol *getProtocol(); 36 Bu::Protocol *getProtocol();
@@ -33,10 +38,13 @@ namespace Bu
33 38
34 bool isOpen(); 39 bool isOpen();
35 40
41 const Bu::Socket *getSocket() const;
42
36 private: 43 private:
37 Bu::Socket *pSocket; 44 Bu::Socket *pSocket;
38 Bu::Protocol *pProto; 45 Bu::Protocol *pProto;
39 Bu::FString sReadBuf; 46 Bu::FString sReadBuf;
47 int nRBOffset;
40 Bu::FString sWriteBuf; 48 Bu::FString sWriteBuf;
41 }; 49 };
42} 50}
diff --git a/src/plugger.h b/src/plugger.h
index 79a3271..615a662 100644
--- a/src/plugger.h
+++ b/src/plugger.h
@@ -37,7 +37,7 @@ namespace Bu
37 { \ 37 { \
38 delete pCls; \ 38 delete pCls; \
39 } \ 39 } \
40 PluginInfo classname = { \ 40 Bu::PluginInfo classname = { \
41 #classname, name, ver, rev, \ 41 #classname, name, ver, rev, \
42 create ##classname, destroy ##classname }; \ 42 create ##classname, destroy ##classname }; \
43 } 43 }
@@ -52,7 +52,7 @@ namespace Bu
52 { \ 52 { \
53 delete pCls; \ 53 delete pCls; \
54 } \ 54 } \
55 PluginInfo pluginname = { \ 55 Bu::PluginInfo pluginname = { \
56 #pluginname, name, ver, rev, \ 56 #pluginname, name, ver, rev, \
57 (void *(*)())(create ##classname), \ 57 (void *(*)())(create ##classname), \
58 (void (*)( void * ))(destroy ##classname) }; \ 58 (void (*)( void * ))(destroy ##classname) }; \
@@ -68,7 +68,7 @@ namespace Bu
68 { \ 68 { \
69 delete pCls; \ 69 delete pCls; \
70 } \ 70 } \
71 PluginInfo structname = { \ 71 Bu::PluginInfo structname = { \
72 #pluginname, name, ver, rev, \ 72 #pluginname, name, ver, rev, \
73 (void *(*)())(create ##classname), \ 73 (void *(*)())(create ##classname), \
74 (void (*)( void * ))(destroy ##classname) }; \ 74 (void (*)( void * ))(destroy ##classname) }; \
diff --git a/src/socket.cpp b/src/socket.cpp
index bd05024..5a3097c 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -276,3 +276,15 @@ bool Bu::Socket::isOpen()
276 return bActive; 276 return bActive;
277} 277}
278 278
279Bu::FString Bu::Socket::getAddress() const
280{
281 struct sockaddr_in addr;
282 socklen_t len = sizeof(addr);
283 addr.sin_family = AF_INET;
284 getsockname( nSocket, (sockaddr *)(&addr), &len );
285 char buf[150];
286 sprintf( buf, "%s", inet_ntoa( addr.sin_addr ) );
287
288 return buf;
289}
290
diff --git a/src/socket.h b/src/socket.h
index 0ccee3b..9e36041 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -42,6 +42,8 @@ namespace Bu
42 virtual bool isBlocking(); 42 virtual bool isBlocking();
43 virtual void setBlocking( bool bBlocking=true ); 43 virtual void setBlocking( bool bBlocking=true );
44 44
45 Bu::FString getAddress() const;
46
45 private: 47 private:
46 int nSocket; 48 int nSocket;
47 bool bActive; 49 bool bActive;