diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-01-14 23:14:13 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-01-14 23:14:13 +0000 |
commit | 9edd5b50c2b928be9fc7569170103a41cbc80e5f (patch) | |
tree | 6aca60610546ed3fc9c5d1a6669bb09c2b8c1203 /src/udpsocket.cpp | |
parent | 8aa6cee7eed01384771e05ecb425e4df9da4687b (diff) | |
download | libbu++-9edd5b50c2b928be9fc7569170103a41cbc80e5f.tar.gz libbu++-9edd5b50c2b928be9fc7569170103a41cbc80e5f.tar.bz2 libbu++-9edd5b50c2b928be9fc7569170103a41cbc80e5f.tar.xz libbu++-9edd5b50c2b928be9fc7569170103a41cbc80e5f.zip |
Here's a udp socket. It's not done yet, it can just about send data.
Diffstat (limited to 'src/udpsocket.cpp')
-rw-r--r-- | src/udpsocket.cpp | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp new file mode 100644 index 0000000..b612b25 --- /dev/null +++ b/src/udpsocket.cpp | |||
@@ -0,0 +1,164 @@ | |||
1 | #include "bu/udpsocket.h" | ||
2 | |||
3 | #include <errno.h> | ||
4 | #include <arpa/inet.h> | ||
5 | #include <sys/socket.h> | ||
6 | #include <netinet/in.h> | ||
7 | #include <sys/utsname.h> | ||
8 | |||
9 | namespace Bu { subExceptionDef( UdpSocketException ) } | ||
10 | |||
11 | Bu::UdpSocket::UdpSocket( int iUdpSocket ) : | ||
12 | iUdpSocket( iUdpSocket ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, | ||
17 | bool bBroadcast ) : | ||
18 | iUdpSocket( 0 ) | ||
19 | { | ||
20 | struct sockaddr_in name; | ||
21 | |||
22 | iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 ); | ||
23 | if( iUdpSocket < 0 ) | ||
24 | { | ||
25 | throw UdpSocketException("Couldn't open udp socket: %s", | ||
26 | strerror( errno ) | ||
27 | ); | ||
28 | } | ||
29 | |||
30 | if( bBroadcast ) | ||
31 | { | ||
32 | int broadcast = 1; | ||
33 | if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST, | ||
34 | &broadcast, sizeof(broadcast) )) == -1) | ||
35 | { | ||
36 | throw UdpSocketException("Couldn't set udp socket to broadcast: %s", | ||
37 | strerror( errno ) | ||
38 | ); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | name.sin_family = AF_INET; | ||
43 | name.sin_port = htons( iPort ); | ||
44 | name.sin_addr.s_addr = INADDR_ANY; | ||
45 | memset( name.sin_zero, '\0', sizeof(name.sin_zero) ); | ||
46 | |||
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 | |||
54 | name.sin_family = AF_INET; | ||
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 | { | ||
61 | nbytes = sendto( iUdpSocket, "12345", 5, 0, | ||
62 | (struct sockaddr *)&name, size ); | ||
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 | { | ||
73 | close( iUdpSocket ); | ||
74 | return nQueen; | ||
75 | } | ||
76 | } | ||
77 | */ | ||
78 | } | ||
79 | |||
80 | void Bu::UdpSocket::close() | ||
81 | { | ||
82 | ::close( iUdpSocket ); | ||
83 | } | ||
84 | |||
85 | size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) | ||
86 | { | ||
87 | } | ||
88 | |||
89 | size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, | ||
90 | uint32_t nSec, uint32_t nUSec=0 ) | ||
91 | { | ||
92 | } | ||
93 | |||
94 | size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) | ||
95 | { | ||
96 | return sendto( iUdpSocket, pBuf, nBytes, 0, | ||
97 | (struct sockaddr *)&name, size ); | ||
98 | } | ||
99 | |||
100 | size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes, | ||
101 | uint32_t nSec, uint32_t nUSec=0 ) | ||
102 | { | ||
103 | } | ||
104 | |||
105 | long Bu::UdpSocket::tell() | ||
106 | { | ||
107 | } | ||
108 | |||
109 | void Bu::UdpSocket::seek( long offset ) | ||
110 | { | ||
111 | } | ||
112 | |||
113 | void Bu::UdpSocket::setPos( long pos ) | ||
114 | { | ||
115 | } | ||
116 | |||
117 | void Bu::UdpSocket::setPosEnd( long pos ) | ||
118 | { | ||
119 | } | ||
120 | |||
121 | bool Bu::UdpSocket::isEos() | ||
122 | { | ||
123 | } | ||
124 | |||
125 | bool Bu::UdpSocket::isOpen() | ||
126 | { | ||
127 | } | ||
128 | |||
129 | void Bu::UdpSocket::flush() | ||
130 | { | ||
131 | } | ||
132 | |||
133 | bool Bu::UdpSocket::canRead() | ||
134 | { | ||
135 | } | ||
136 | |||
137 | bool Bu::UdpSocket::canWrite() | ||
138 | { | ||
139 | } | ||
140 | |||
141 | bool Bu::UdpSocket::isReadable() | ||
142 | { | ||
143 | } | ||
144 | |||
145 | bool Bu::UdpSocket::isWritable() | ||
146 | { | ||
147 | } | ||
148 | |||
149 | bool Bu::UdpSocket::isSeekable() | ||
150 | { | ||
151 | } | ||
152 | |||
153 | bool Bu::UdpSocket::isBlocking() | ||
154 | { | ||
155 | } | ||
156 | |||
157 | void Bu::UdpSocket::setBlocking( bool bBlocking=true ) | ||
158 | { | ||
159 | } | ||
160 | |||
161 | void Bu::UdpSocket::setSize( long iSize ) | ||
162 | { | ||
163 | } | ||
164 | |||