summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-01-20 17:48:45 +0000
committerMike Buland <eichlan@xagasoft.com>2011-01-20 17:48:45 +0000
commitc259f95bd0e58b247940a339bb9b4b401b4e9438 (patch)
tree5ad7bc14bc3b70abbe700131ee519795fd83f150 /src
parentd5709e74f66a7a0f8585e8dadeae3bfcd55da09d (diff)
downloadlibbu++-c259f95bd0e58b247940a339bb9b4b401b4e9438.tar.gz
libbu++-c259f95bd0e58b247940a339bb9b4b401b4e9438.tar.bz2
libbu++-c259f95bd0e58b247940a339bb9b4b401b4e9438.tar.xz
libbu++-c259f95bd0e58b247940a339bb9b4b401b4e9438.zip
UdpSocket is pretty much working. Non-blocking mode isn't, which is odd, but
we can figure that out later.
Diffstat (limited to 'src')
-rw-r--r--src/tests/udpsocket.cpp43
-rw-r--r--src/udpsocket.cpp80
-rw-r--r--src/udpsocket.h8
3 files changed, 112 insertions, 19 deletions
diff --git a/src/tests/udpsocket.cpp b/src/tests/udpsocket.cpp
index 3038560..18c7bb9 100644
--- a/src/tests/udpsocket.cpp
+++ b/src/tests/udpsocket.cpp
@@ -2,22 +2,57 @@
2#include "bu/sio.h" 2#include "bu/sio.h"
3 3
4#include <errno.h> 4#include <errno.h>
5#include <arpa/inet.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <sys/utsname.h>
5 9
6using namespace Bu; 10using namespace Bu;
7 11
8int main( int argc, char *argv[] ) 12int main( int argc, char *argv[] )
9{ 13{
10 if( argv[1][0] == 'l' ) 14 sio << Fmt::hex(8) << INADDR_ANY << sio.nl << Fmt::hex(8) << inet_addr("0.0.0.0") << sio.nl;
15 if( argc == 1 )
16 {
17 sio << "Options are 'l' for listening and 'b' for broadcasting."
18 << sio.nl;
19 }
20 else if( argv[1][0] == 'l' )
11 { 21 {
12 sio << "Listening..." << sio.nl; 22 sio << "Listening..." << sio.nl;
13 Bu::UdpSocket udp( "255.255.255.255", 6688, UdpSocket::Read|UdpSocket::Broadcast ); 23 Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast );
14 24
15 for(;;) 25 for(;;)
16 { 26 {
17 char buf[1501]; 27 char buf[1501];
18 int iRead = udp.read( buf, 1500 ); 28 int iRead = udp.read( buf, 1500 );
19 buf[iRead] = '\0'; 29 if( iRead >= 0 )
20 sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl; 30 {
31 buf[iRead] = '\0';
32 sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl;
33 }
34 else
35 {
36 sio << "Got " << iRead << ": " << strerror( errno ) << sio.nl;
37 }
38 }
39 }
40 else if( argv[1][0] == 'L' )
41 {
42 sio << "Listening..." << sio.nl;
43 Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast );
44
45 for(;;)
46 {
47 char buf[1501];
48 Bu::UdpSocket::addr aHost;
49 int iPort;
50 int iRead = udp.read( buf, 1500, aHost, iPort );
51 if( iRead >= 0 )
52 {
53 buf[iRead] = '\0';
54 sio << "Read(" << iRead << ") from " << Bu::UdpSocket::addrToStr( aHost ) << ":" << iPort << ": '" << buf << "'" << sio.nl;
55 }
21 } 56 }
22 } 57 }
23 else if( argv[1][0] == 'b' ) 58 else if( argv[1][0] == 'b' )
diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp
index 7b03b78..da57b8d 100644
--- a/src/udpsocket.cpp
+++ b/src/udpsocket.cpp
@@ -1,5 +1,9 @@
1#include "bu/udpsocket.h" 1#include "bu/udpsocket.h"
2 2
3#include "bu/sio.h"
4using namespace Bu;
5#include <fcntl.h>
6
3#include <errno.h> 7#include <errno.h>
4#include <arpa/inet.h> 8#include <arpa/inet.h>
5#include <sys/socket.h> 9#include <sys/socket.h>
@@ -41,7 +45,7 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) :
41 ); 45 );
42 } 46 }
43 } 47 }
44 48
45 paTarget = new struct sockaddr_in; 49 paTarget = new struct sockaddr_in;
46 saTarget.sin_family = AF_INET; 50 saTarget.sin_family = AF_INET;
47 saTarget.sin_port = htons( iPort ); 51 saTarget.sin_port = htons( iPort );
@@ -63,10 +67,24 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) :
63 67
64Bu::UdpSocket::~UdpSocket() 68Bu::UdpSocket::~UdpSocket()
65{ 69{
70 close();
66 delete (struct sockaddr_in *)paTarget; 71 delete (struct sockaddr_in *)paTarget;
67 paTarget = NULL; 72 paTarget = NULL;
68} 73}
69 74
75Bu::FString Bu::UdpSocket::addrToStr( const addr &a )
76{
77 Bu::FString sOut;
78 sOut.format("%d.%d.%d.%d",
79 (a&0xff),
80 (a&0xff00)>>8,
81 (a&0xff0000)>>16,
82 (a&0xff000000)>>24
83 );
84
85 return sOut;
86}
87
70void Bu::UdpSocket::close() 88void Bu::UdpSocket::close()
71{ 89{
72 ::close( iUdpSocket ); 90 ::close( iUdpSocket );
@@ -74,12 +92,19 @@ void Bu::UdpSocket::close()
74 92
75size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) 93size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes )
76{ 94{
77 return recvfrom( iUdpSocket, pBuf, nBytes, 0, NULL, 0 ); 95 return recv( iUdpSocket, pBuf, nBytes, 0 );
78} 96}
79 97
80size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, 98size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes,
81 uint32_t nSec, uint32_t nUSec ) 99 Bu::UdpSocket::addr &aHost, int &iPort )
82{ 100{
101 sockaddr_in name;
102 size_t size = sizeof(name);
103 size_t ret = recvfrom( iUdpSocket, pBuf, nBytes, 0,
104 (struct sockaddr *)&name, &size );
105 aHost = name.sin_addr.s_addr;
106 iPort = ntohs(name.sin_port);
107 return ret;
83} 108}
84 109
85size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) 110size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes )
@@ -95,33 +120,34 @@ size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes )
95 } 120 }
96} 121}
97 122
98size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes,
99 uint32_t nSec, uint32_t nUSec )
100{
101}
102
103long Bu::UdpSocket::tell() 123long Bu::UdpSocket::tell()
104{ 124{
125 throw Bu::UnsupportedException();
105} 126}
106 127
107void Bu::UdpSocket::seek( long offset ) 128void Bu::UdpSocket::seek( long )
108{ 129{
130 throw Bu::UnsupportedException();
109} 131}
110 132
111void Bu::UdpSocket::setPos( long pos ) 133void Bu::UdpSocket::setPos( long )
112{ 134{
135 throw Bu::UnsupportedException();
113} 136}
114 137
115void Bu::UdpSocket::setPosEnd( long pos ) 138void Bu::UdpSocket::setPosEnd( long )
116{ 139{
140 throw Bu::UnsupportedException();
117} 141}
118 142
119bool Bu::UdpSocket::isEos() 143bool Bu::UdpSocket::isEos()
120{ 144{
145 return false;
121} 146}
122 147
123bool Bu::UdpSocket::isOpen() 148bool Bu::UdpSocket::isOpen()
124{ 149{
150 return true;
125} 151}
126 152
127void Bu::UdpSocket::flush() 153void Bu::UdpSocket::flush()
@@ -130,33 +156,63 @@ void Bu::UdpSocket::flush()
130 156
131bool Bu::UdpSocket::canRead() 157bool Bu::UdpSocket::canRead()
132{ 158{
159 return bBound;
133} 160}
134 161
135bool Bu::UdpSocket::canWrite() 162bool Bu::UdpSocket::canWrite()
136{ 163{
164 return true;
137} 165}
138 166
139bool Bu::UdpSocket::isReadable() 167bool Bu::UdpSocket::isReadable()
140{ 168{
169 return bBound;
141} 170}
142 171
143bool Bu::UdpSocket::isWritable() 172bool Bu::UdpSocket::isWritable()
144{ 173{
174 return true;
145} 175}
146 176
147bool Bu::UdpSocket::isSeekable() 177bool Bu::UdpSocket::isSeekable()
148{ 178{
179 return false;
149} 180}
150 181
151bool Bu::UdpSocket::isBlocking() 182bool Bu::UdpSocket::isBlocking()
152{ 183{
184 return true;
153} 185}
154 186
155void Bu::UdpSocket::setBlocking( bool bBlocking ) 187void Bu::UdpSocket::setBlocking( bool bBlocking )
156{ 188{
189#ifndef WIN32
190 if( bBlocking )
191 {
192 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) );
193 }
194 else
195 {
196 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) | O_NONBLOCK );
197 }
198#else
199 u_long iMode;
200 if( bBlocking )
201 iMode = 0;
202 else
203 iMode = 1;
204 //-------------------------
205 // Set the socket I/O mode: In this case FIONBIO
206 // enables or disables the blocking mode for the
207 // socket based on the numerical value of iMode.
208 // If iMode = 0, blocking is enabled;
209 // If iMode != 0, non-blocking mode is enabled.
210 bu_ioctlsocket(iUdpSocket, FIONBIO, &iMode);
211#endif
157} 212}
158 213
159void Bu::UdpSocket::setSize( long iSize ) 214void Bu::UdpSocket::setSize( long )
160{ 215{
216 throw Bu::UnsupportedException();
161} 217}
162 218
diff --git a/src/udpsocket.h b/src/udpsocket.h
index 0f81c38..253839a 100644
--- a/src/udpsocket.h
+++ b/src/udpsocket.h
@@ -16,13 +16,15 @@ namespace Bu
16 UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ); 16 UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags );
17 virtual ~UdpSocket(); 17 virtual ~UdpSocket();
18 18
19 typedef uint32_t addr;
20
21 static Bu::FString addrToStr( const addr &a );
22
19 virtual void close(); 23 virtual void close();
20 virtual size_t read( void *pBuf, size_t nBytes ); 24 virtual size_t read( void *pBuf, size_t nBytes );
21 virtual size_t read( void *pBuf, size_t nBytes, 25 virtual size_t read( void *pBuf, size_t nBytes,
22 uint32_t nSec, uint32_t nUSec=0 ); 26 addr &sHost, int &iPort );
23 virtual size_t write( const void *pBuf, size_t nBytes ); 27 virtual size_t write( const void *pBuf, size_t nBytes );
24 virtual size_t write( const void *pBuf, size_t nBytes,
25 uint32_t nSec, uint32_t nUSec=0 );
26 using Stream::write; 28 using Stream::write;
27 29
28 virtual long tell(); 30 virtual long tell();