summaryrefslogtreecommitdiff
path: root/src/udpsocket.cpp
blob: b612b255b53894d39a543e710674167e996382b4 (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
163
164
#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 ) }

Bu::UdpSocket::UdpSocket( int iUdpSocket ) :
	iUdpSocket( iUdpSocket )
{
}

Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort,
		bool bBroadcast ) :
	iUdpSocket( 0 )
{
	struct sockaddr_in name;

	iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 );
	if( iUdpSocket < 0 )
	{
		throw UdpSocketException("Couldn't open udp socket: %s",
			strerror( errno )
			);
	}

	if( bBroadcast )
	{
		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 )
				);
		}
	}

	name.sin_family = AF_INET;
	name.sin_port = htons( iPort );
	name.sin_addr.s_addr = INADDR_ANY;
	memset( name.sin_zero, '\0', sizeof(name.sin_zero) );
	
	if( bind( iUdpSocket, (struct sockaddr*) &name, sizeof(name) ) == -1 )
	{
		throw UdpSocketException("Couldn't bind port to udp socket: %s",
			strerror( errno )
			);
	}
	
	name.sin_family = AF_INET;
	name.sin_port = htons( iPort );
	name.sin_addr.s_addr = inet_addr( sAddr.getStr() );
	memset( name.sin_zero, '\0', sizeof(name.sin_zero) );
/*
	while( true )
	{
		nbytes = sendto( iUdpSocket, "12345", 5, 0,
			(struct sockaddr *)&name, size );
		if( nbytes < 0 )
		{
			perror("sendto");
//			exit( 0 );
		}

		printf("Client wrote something\n");
		int nQueen = sockServe.accept( 3, 0 );
		if( nQueen >= 0 )
		{
			close( iUdpSocket );
			return nQueen;
		}
	}
	*/
}

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

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

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

size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes )
{
	return sendto( iUdpSocket, pBuf, nBytes, 0,
		(struct sockaddr *)&name, size );
}

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

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=true )
{
}

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