aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-01-14 23:14:13 +0000
committerMike Buland <eichlan@xagasoft.com>2011-01-14 23:14:13 +0000
commit9edd5b50c2b928be9fc7569170103a41cbc80e5f (patch)
tree6aca60610546ed3fc9c5d1a6669bb09c2b8c1203
parent8aa6cee7eed01384771e05ecb425e4df9da4687b (diff)
downloadlibbu++-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.
-rw-r--r--src/tcpsocket.h1
-rw-r--r--src/udpsocket.cpp164
-rw-r--r--src/udpsocket.h58
3 files changed, 222 insertions, 1 deletions
diff --git a/src/tcpsocket.h b/src/tcpsocket.h
index 3361e84..ce15172 100644
--- a/src/tcpsocket.h
+++ b/src/tcpsocket.h
@@ -65,7 +65,6 @@ namespace Bu
65 virtual ~TcpSocket(); 65 virtual ~TcpSocket();
66 66
67 virtual void close(); 67 virtual void close();
68 //virtual void read();
69 virtual size_t read( void *pBuf, size_t nBytes ); 68 virtual size_t read( void *pBuf, size_t nBytes );
70 virtual size_t read( void *pBuf, size_t nBytes, 69 virtual size_t read( void *pBuf, size_t nBytes,
71 uint32_t nSec, uint32_t nUSec=0 ); 70 uint32_t nSec, uint32_t nUSec=0 );
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
9namespace Bu { subExceptionDef( UdpSocketException ) }
10
11Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
12 iUdpSocket( iUdpSocket )
13{
14}
15
16Bu::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
80void Bu::UdpSocket::close()
81{
82 ::close( iUdpSocket );
83}
84
85size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes )
86{
87}
88
89size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes,
90 uint32_t nSec, uint32_t nUSec=0 )
91{
92}
93
94size_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
100size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes,
101 uint32_t nSec, uint32_t nUSec=0 )
102{
103}
104
105long Bu::UdpSocket::tell()
106{
107}
108
109void Bu::UdpSocket::seek( long offset )
110{
111}
112
113void Bu::UdpSocket::setPos( long pos )
114{
115}
116
117void Bu::UdpSocket::setPosEnd( long pos )
118{
119}
120
121bool Bu::UdpSocket::isEos()
122{
123}
124
125bool Bu::UdpSocket::isOpen()
126{
127}
128
129void Bu::UdpSocket::flush()
130{
131}
132
133bool Bu::UdpSocket::canRead()
134{
135}
136
137bool Bu::UdpSocket::canWrite()
138{
139}
140
141bool Bu::UdpSocket::isReadable()
142{
143}
144
145bool Bu::UdpSocket::isWritable()
146{
147}
148
149bool Bu::UdpSocket::isSeekable()
150{
151}
152
153bool Bu::UdpSocket::isBlocking()
154{
155}
156
157void Bu::UdpSocket::setBlocking( bool bBlocking=true )
158{
159}
160
161void Bu::UdpSocket::setSize( long iSize )
162{
163}
164
diff --git a/src/udpsocket.h b/src/udpsocket.h
new file mode 100644
index 0000000..4452458
--- /dev/null
+++ b/src/udpsocket.h
@@ -0,0 +1,58 @@
1#ifndef BU_UDP_SOCKET_H
2#define BU_UDP_SOCKET_H
3
4#include <stdint.h>
5
6#include "bu/stream.h"
7
8namespace Bu
9{
10 subExceptionDecl( UdpSocketException );
11
12 class UdpSocket : public Stream
13 {
14 public:
15 UdpSocket( int iUdpSocket );
16 UdpSocket( const Bu::FString &sAddr, int iPort, bool bBroadcast );
17 virtual ~UdpSocket();
18
19 virtual void close();
20 virtual size_t read( void *pBuf, size_t nBytes );
21 virtual size_t read( void *pBuf, size_t nBytes,
22 uint32_t nSec, uint32_t nUSec=0 );
23 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;
27
28 virtual long tell();
29 virtual void seek( long offset );
30 virtual void setPos( long pos );
31 virtual void setPosEnd( long pos );
32 virtual bool isEos();
33 virtual bool isOpen();
34
35 virtual void flush();
36
37 virtual bool canRead();
38 virtual bool canWrite();
39
40 virtual bool isReadable();
41 virtual bool isWritable();
42 virtual bool isSeekable();
43
44 virtual bool isBlocking();
45 virtual void setBlocking( bool bBlocking=true );
46
47 virtual void setSize( long iSize );
48
49 private:
50#ifdef WIN32
51 unsigned int iUdpSocket;
52#else
53 int iUdpSocket;
54#endif
55 };
56};
57
58#endif