summaryrefslogtreecommitdiff
path: root/src/unstable/udpsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/udpsocket.cpp')
-rw-r--r--src/unstable/udpsocket.cpp216
1 files changed, 108 insertions, 108 deletions
diff --git a/src/unstable/udpsocket.cpp b/src/unstable/udpsocket.cpp
index f554165..141c2cf 100644
--- a/src/unstable/udpsocket.cpp
+++ b/src/unstable/udpsocket.cpp
@@ -24,135 +24,135 @@ namespace Bu { subExceptionDef( UdpSocketException ) }
24#define saTarget ( *((struct sockaddr_in *)paTarget) ) 24#define saTarget ( *((struct sockaddr_in *)paTarget) )
25 25
26Bu::UdpSocket::UdpSocket( int iUdpSocket ) : 26Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
27 iUdpSocket( iUdpSocket ), 27 iUdpSocket( iUdpSocket ),
28 paTarget( NULL ), 28 paTarget( NULL ),
29 bBound( false ) 29 bBound( false )
30{ 30{
31} 31}
32 32
33Bu::UdpSocket::UdpSocket( const Bu::String &sAddr, int iPort, int iFlags ) : 33Bu::UdpSocket::UdpSocket( const Bu::String &sAddr, int iPort, int iFlags ) :
34 iUdpSocket( 0 ), 34 iUdpSocket( 0 ),
35 paTarget( NULL ), 35 paTarget( NULL ),
36 bBound( false ) 36 bBound( false )
37{ 37{
38 iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 ); 38 iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 );
39 if( iUdpSocket < 0 ) 39 if( iUdpSocket < 0 )
40 { 40 {
41 throw UdpSocketException("Couldn't open udp socket: %s", 41 throw UdpSocketException("Couldn't open udp socket: %s",
42 strerror( errno ) 42 strerror( errno )
43 ); 43 );
44 } 44 }
45 45
46 if( (iFlags&Broadcast) ) 46 if( (iFlags&Broadcast) )
47 { 47 {
48 int broadcast = 1; 48 int broadcast = 1;
49 if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST, 49 if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST,
50 &broadcast, sizeof(broadcast) )) == -1) 50 &broadcast, sizeof(broadcast) )) == -1)
51 { 51 {
52 throw UdpSocketException("Couldn't set udp socket to broadcast: %s", 52 throw UdpSocketException("Couldn't set udp socket to broadcast: %s",
53 strerror( errno ) 53 strerror( errno )
54 ); 54 );
55 } 55 }
56 } 56 }
57 57
58 paTarget = new struct sockaddr_in; 58 paTarget = new struct sockaddr_in;
59 saTarget.sin_family = AF_INET; 59 saTarget.sin_family = AF_INET;
60 saTarget.sin_port = htons( iPort ); 60 saTarget.sin_port = htons( iPort );
61 saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY; 61 saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY;
62 memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) ); 62 memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) );
63 63
64 if( (iFlags&Read) ) 64 if( (iFlags&Read) )
65 { 65 {
66 if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) ) 66 if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) )
67 == -1 ) 67 == -1 )
68 { 68 {
69 throw UdpSocketException("Couldn't bind port to udp socket: %s", 69 throw UdpSocketException("Couldn't bind port to udp socket: %s",
70 strerror( errno ) 70 strerror( errno )
71 ); 71 );
72 } 72 }
73 bBound = true; 73 bBound = true;
74 } 74 }
75} 75}
76 76
77Bu::UdpSocket::~UdpSocket() 77Bu::UdpSocket::~UdpSocket()
78{ 78{
79 close(); 79 close();
80 delete (struct sockaddr_in *)paTarget; 80 delete (struct sockaddr_in *)paTarget;
81 paTarget = NULL; 81 paTarget = NULL;
82} 82}
83 83
84Bu::String Bu::UdpSocket::addrToStr( const addr &a ) 84Bu::String Bu::UdpSocket::addrToStr( const addr &a )
85{ 85{
86 return Bu::String("%1.%2.%3.%4"). 86 return Bu::String("%1.%2.%3.%4").
87 arg( (a&0xff) ). 87 arg( (a&0xff) ).
88 arg( (a&0xff00)>>8 ). 88 arg( (a&0xff00)>>8 ).
89 arg( (a&0xff0000)>>16 ). 89 arg( (a&0xff0000)>>16 ).
90 arg( (a&0xff000000)>>24 ); 90 arg( (a&0xff000000)>>24 );
91} 91}
92 92
93void Bu::UdpSocket::close() 93void Bu::UdpSocket::close()
94{ 94{
95 ::close( iUdpSocket ); 95 ::close( iUdpSocket );
96} 96}
97 97
98Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes ) 98Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes )
99{ 99{
100 return recv( iUdpSocket, pBuf, nBytes, 0 ); 100 return recv( iUdpSocket, pBuf, nBytes, 0 );
101} 101}
102 102
103Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes, 103Bu::size Bu::UdpSocket::read( void *pBuf, Bu::size nBytes,
104 Bu::UdpSocket::addr &aHost, int &iPort ) 104 Bu::UdpSocket::addr &aHost, int &iPort )
105{ 105{
106 sockaddr_in name; 106 sockaddr_in name;
107 socklen_t size = sizeof(name); 107 socklen_t size = sizeof(name);
108 Bu::size ret = recvfrom( iUdpSocket, pBuf, nBytes, 0, 108 Bu::size ret = recvfrom( iUdpSocket, pBuf, nBytes, 0,
109 (struct sockaddr *)&name, &size ); 109 (struct sockaddr *)&name, &size );
110 aHost = name.sin_addr.s_addr; 110 aHost = name.sin_addr.s_addr;
111 iPort = ntohs(name.sin_port); 111 iPort = ntohs(name.sin_port);
112 return ret; 112 return ret;
113} 113}
114 114
115Bu::size Bu::UdpSocket::write( const void *pBuf, Bu::size nBytes ) 115Bu::size Bu::UdpSocket::write( const void *pBuf, Bu::size nBytes )
116{ 116{
117 if( bBound ) 117 if( bBound )
118 { 118 {
119 return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 ); 119 return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
120 } 120 }
121 else 121 else
122 { 122 {
123 return sendto( iUdpSocket, pBuf, nBytes, 0, 123 return sendto( iUdpSocket, pBuf, nBytes, 0,
124 (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) ); 124 (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) );
125 } 125 }
126} 126}
127 127
128Bu::size Bu::UdpSocket::tell() 128Bu::size Bu::UdpSocket::tell()
129{ 129{
130 throw Bu::UnsupportedException(); 130 throw Bu::UnsupportedException();
131} 131}
132 132
133void Bu::UdpSocket::seek( Bu::size ) 133void Bu::UdpSocket::seek( Bu::size )
134{ 134{
135 throw Bu::UnsupportedException(); 135 throw Bu::UnsupportedException();
136} 136}
137 137
138void Bu::UdpSocket::setPos( Bu::size ) 138void Bu::UdpSocket::setPos( Bu::size )
139{ 139{
140 throw Bu::UnsupportedException(); 140 throw Bu::UnsupportedException();
141} 141}
142 142
143void Bu::UdpSocket::setPosEnd( Bu::size ) 143void Bu::UdpSocket::setPosEnd( Bu::size )
144{ 144{
145 throw Bu::UnsupportedException(); 145 throw Bu::UnsupportedException();
146} 146}
147 147
148bool Bu::UdpSocket::isEos() 148bool Bu::UdpSocket::isEos()
149{ 149{
150 return false; 150 return false;
151} 151}
152 152
153bool Bu::UdpSocket::isOpen() 153bool Bu::UdpSocket::isOpen()
154{ 154{
155 return true; 155 return true;
156} 156}
157 157
158void Bu::UdpSocket::flush() 158void Bu::UdpSocket::flush()
@@ -161,79 +161,79 @@ void Bu::UdpSocket::flush()
161 161
162bool Bu::UdpSocket::canRead() 162bool Bu::UdpSocket::canRead()
163{ 163{
164 return bBound; 164 return bBound;
165} 165}
166 166
167bool Bu::UdpSocket::canWrite() 167bool Bu::UdpSocket::canWrite()
168{ 168{
169 return true; 169 return true;
170} 170}
171 171
172bool Bu::UdpSocket::isReadable() 172bool Bu::UdpSocket::isReadable()
173{ 173{
174 return bBound; 174 return bBound;
175} 175}
176 176
177bool Bu::UdpSocket::isWritable() 177bool Bu::UdpSocket::isWritable()
178{ 178{
179 return true; 179 return true;
180} 180}
181 181
182bool Bu::UdpSocket::isSeekable() 182bool Bu::UdpSocket::isSeekable()
183{ 183{
184 return false; 184 return false;
185} 185}
186 186
187bool Bu::UdpSocket::isBlocking() 187bool Bu::UdpSocket::isBlocking()
188{ 188{
189 return true; 189 return true;
190} 190}
191 191
192void Bu::UdpSocket::setBlocking( bool bBlocking ) 192void Bu::UdpSocket::setBlocking( bool bBlocking )
193{ 193{
194#ifndef WIN32 194#ifndef WIN32
195 if( bBlocking ) 195 if( bBlocking )
196 { 196 {
197 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) ); 197 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) );
198 } 198 }
199 else 199 else
200 { 200 {
201 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) | O_NONBLOCK ); 201 fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) | O_NONBLOCK );
202 } 202 }
203#else 203#else
204 u_long iMode; 204 u_long iMode;
205 if( bBlocking ) 205 if( bBlocking )
206 iMode = 0; 206 iMode = 0;
207 else 207 else
208 iMode = 1; 208 iMode = 1;
209 //------------------------- 209 //-------------------------
210 // Set the socket I/O mode: In this case FIONBIO 210 // Set the socket I/O mode: In this case FIONBIO
211 // enables or disables the blocking mode for the 211 // enables or disables the blocking mode for the
212 // socket based on the numerical value of iMode. 212 // socket based on the numerical value of iMode.
213 // If iMode = 0, blocking is enabled; 213 // If iMode = 0, blocking is enabled;
214 // If iMode != 0, non-blocking mode is enabled. 214 // If iMode != 0, non-blocking mode is enabled.
215 bu_ioctlsocket(iUdpSocket, FIONBIO, &iMode); 215 bu_ioctlsocket(iUdpSocket, FIONBIO, &iMode);
216#endif 216#endif
217} 217}
218 218
219void Bu::UdpSocket::setSize( Bu::size ) 219void Bu::UdpSocket::setSize( Bu::size )
220{ 220{
221 throw Bu::UnsupportedException(); 221 throw Bu::UnsupportedException();
222} 222}
223 223
224Bu::size Bu::UdpSocket::getSize() const 224Bu::size Bu::UdpSocket::getSize() const
225{ 225{
226 throw Bu::UnsupportedException(); 226 throw Bu::UnsupportedException();
227} 227}
228 228
229Bu::size Bu::UdpSocket::getBlockSize() const 229Bu::size Bu::UdpSocket::getBlockSize() const
230{ 230{
231 return 1500; 231 return 1500;
232} 232}
233 233
234Bu::String Bu::UdpSocket::getLocation() const 234Bu::String Bu::UdpSocket::getLocation() const
235{ 235{
236 throw Bu::UnsupportedException(); 236 throw Bu::UnsupportedException();
237} 237}
238 238
239#endif 239#endif