aboutsummaryrefslogtreecommitdiff
path: root/src/udpsocket.cpp
blob: 7b03b7895d216da8b1dc1a63b5e0b5fb19c20b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "bu/udpsocket.h"

#include <errno.h>
#include <arpa/inet.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/utsname.h>

namespace Bu { subExceptionDef( UdpSocketException ) }

#define saTarget ( *((struct sockaddr_in *)paTarget) )

Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
	iUdpSocket( iUdpSocket ),
	paTarget( NULL ),
	bBound( false )
{
}

Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) :
	iUdpSocket( 0 ),
	paTarget( NULL ),
	bBound( false )
{
	iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 );
	if( iUdpSocket < 0 )
	{
		throw UdpSocketException("Couldn't open udp socket: %s",
			strerror( errno )
			);
	}

	if( (iFlags&Broadcast) )
	{
		int broadcast = 1;
		if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST,
			&broadcast, sizeof(broadcast) )) == -1)
		{
			throw UdpSocketException("Couldn't set udp socket to broadcast: %s",
				strerror( errno )
				);
		}
	}

	paTarget = new struct sockaddr_in;
	saTarget.sin_family = AF_INET;
	saTarget.sin_port = htons( iPort );
	saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY;
	memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) );
	
	if( (iFlags&Read) )
	{
		if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) )
				== -1 )
		{
			throw UdpSocketException("Couldn't bind port to udp socket: %s",
				strerror( errno )
				);
		}
		bBound = true;
	}
}

Bu::UdpSocket::~UdpSocket()
{
	delete (struct sockaddr_in *)paTarget;
	paTarget = NULL;
}

void Bu::UdpSocket::close()
{
	::close( iUdpSocket );
}

size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes )
{
	return recvfrom( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
}

size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes,
		uint32_t nSec, uint32_t nUSec )
{
}

size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes )
{
	if( bBound )
	{
		return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 );
	}
	else
	{
		return sendto( iUdpSocket, pBuf, nBytes, 0,
			(struct sockaddr*)paTarget, sizeof(struct sockaddr_in) );
	}
}

size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes,
		uint32_t nSec, uint32_t nUSec )
{
}

long Bu::UdpSocket::tell()
{
}

void Bu::UdpSocket::seek( long offset )
{
}

void Bu::UdpSocket::setPos( long pos )
{
}

void Bu::UdpSocket::setPosEnd( long pos )
{
}

bool Bu::UdpSocket::isEos()
{
}

bool Bu::UdpSocket::isOpen()
{
}

void Bu::UdpSocket::flush()
{
}

bool Bu::UdpSocket::canRead()
{
}

bool Bu::UdpSocket::canWrite()
{
}

bool Bu::UdpSocket::isReadable()
{
}

bool Bu::UdpSocket::isWritable()
{
}

bool Bu::UdpSocket::isSeekable()
{
}

bool Bu::UdpSocket::isBlocking()
{
}

void Bu::UdpSocket::setBlocking( bool bBlocking )
{
}

void Bu::UdpSocket::setSize( long iSize )
{
}