aboutsummaryrefslogtreecommitdiff
path: root/src/udpsocket.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-01-20 15:36:24 +0000
committerMike Buland <eichlan@xagasoft.com>2011-01-20 15:36:24 +0000
commit400f12a9d100d48509a48188fcff7f1b820d5826 (patch)
tree4f83145ea62926da06561c287d9a78ffd6f36bba /src/udpsocket.cpp
parente3bd8a39947097a3130a4064a4b1ff6981a03c63 (diff)
downloadlibbu++-400f12a9d100d48509a48188fcff7f1b820d5826.tar.gz
libbu++-400f12a9d100d48509a48188fcff7f1b820d5826.tar.bz2
libbu++-400f12a9d100d48509a48188fcff7f1b820d5826.tar.xz
libbu++-400f12a9d100d48509a48188fcff7f1b820d5826.zip
Ummm...it's a udpsocket, mostly. It seems like it should work, but early tests
are failing...
Diffstat (limited to '')
-rw-r--r--src/udpsocket.cpp79
1 files changed, 38 insertions, 41 deletions
diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp
index d836690..7b03b78 100644
--- a/src/udpsocket.cpp
+++ b/src/udpsocket.cpp
@@ -8,17 +8,20 @@
8 8
9namespace Bu { subExceptionDef( UdpSocketException ) } 9namespace Bu { subExceptionDef( UdpSocketException ) }
10 10
11#define saTarget ( *((struct sockaddr_in *)paTarget) )
12
11Bu::UdpSocket::UdpSocket( int iUdpSocket ) : 13Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
12 iUdpSocket( iUdpSocket ) 14 iUdpSocket( iUdpSocket ),
15 paTarget( NULL ),
16 bBound( false )
13{ 17{
14} 18}
15 19
16Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, 20Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) :
17 bool bBroadcast ) : 21 iUdpSocket( 0 ),
18 iUdpSocket( 0 ) 22 paTarget( NULL ),
23 bBound( false )
19{ 24{
20 struct sockaddr_in name;
21
22 iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 ); 25 iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 );
23 if( iUdpSocket < 0 ) 26 if( iUdpSocket < 0 )
24 { 27 {
@@ -27,7 +30,7 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort,
27 ); 30 );
28 } 31 }
29 32
30 if( bBroadcast ) 33 if( (iFlags&Broadcast) )
31 { 34 {
32 int broadcast = 1; 35 int broadcast = 1;
33 if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST, 36 if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST,
@@ -39,42 +42,29 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort,
39 } 42 }
40 } 43 }
41 44
42 name.sin_family = AF_INET; 45 paTarget = new struct sockaddr_in;
43 name.sin_port = htons( iPort ); 46 saTarget.sin_family = AF_INET;
44 name.sin_addr.s_addr = INADDR_ANY; 47 saTarget.sin_port = htons( iPort );
45 memset( name.sin_zero, '\0', sizeof(name.sin_zero) ); 48 saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY;
46 49 memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) );
47 if( bind( iUdpSocket, (struct sockaddr*) &name, sizeof(name) ) == -1 )
48 {
49 throw UdpSocketException("Couldn't bind port to udp socket: %s",
50 strerror( errno )
51 );
52 }
53 50
54 name.sin_family = AF_INET; 51 if( (iFlags&Read) )
55 name.sin_port = htons( iPort );
56 name.sin_addr.s_addr = inet_addr( sAddr.getStr() );
57 memset( name.sin_zero, '\0', sizeof(name.sin_zero) );
58/*
59 while( true )
60 { 52 {
61 nbytes = sendto( iUdpSocket, "12345", 5, 0, 53 if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) )
62 (struct sockaddr *)&name, size ); 54 == -1 )
63 if( nbytes < 0 )
64 {
65 perror("sendto");
66// exit( 0 );
67 }
68
69 printf("Client wrote something\n");
70 int nQueen = sockServe.accept( 3, 0 );
71 if( nQueen >= 0 )
72 { 55 {
73 close( iUdpSocket ); 56 throw UdpSocketException("Couldn't bind port to udp socket: %s",
74 return nQueen; 57 strerror( errno )
58 );
75 } 59 }
60 bBound = true;
76 } 61 }
77 */ 62}
63
64Bu::UdpSocket::~UdpSocket()
65{
66 delete (struct sockaddr_in *)paTarget;
67 paTarget = NULL;
78} 68}
79 69
80void Bu::UdpSocket::close() 70void Bu::UdpSocket::close()
@@ -84,6 +74,7 @@ void Bu::UdpSocket::close()
84 74
85size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) 75size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes )
86{ 76{
77 return recvfrom( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
87} 78}
88 79
89size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, 80size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes,
@@ -93,9 +84,15 @@ size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes,
93 84
94size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) 85size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes )
95{ 86{
96// name, the destination address, needs to be a class variable... 87 if( bBound )
97// return sendto( iUdpSocket, pBuf, nBytes, 0, 88 {
98// (struct sockaddr *)&name, size ); 89 return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
90 }
91 else
92 {
93 return sendto( iUdpSocket, pBuf, nBytes, 0,
94 (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) );
95 }
99} 96}
100 97
101size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes, 98size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes,