summaryrefslogtreecommitdiff
path: root/src/udpsocket.cpp
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/udpsocket.cpp
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/udpsocket.cpp')
-rw-r--r--src/udpsocket.cpp80
1 files changed, 68 insertions, 12 deletions
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