File: socket.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (369 lines) | stat: -rw-r--r-- 8,357 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
	$Id: socket.cpp,v 1.17 2001/11/22 00:43:11 plasmoid Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#ifdef WIN32
#pragma warning (disable:4786)
	#ifdef IPV6
		#include <winsock2.h>
		#include <ws2tcpip.h>
  #endif
#else
#include <unistd.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#define INVALID_SOCKET -1
#endif

#include "socket_generic.h"
#include "API/Core/System/error.h"

/////////////////////////////////////////////////////////////////////////////
// CL_Socket construction:

CL_Socket::CL_Socket()
: impl(0)
{
}

CL_Socket::CL_Socket(int socket)
: impl(new CL_Socket_Generic)
{
	impl->add_ref();
	impl->sock = socket;

	input.socket = impl;
	output.socket = impl;
}

CL_Socket::CL_Socket(CL_Socket::Type type)
: impl(new CL_Socket_Generic)
{
	impl->add_ref();

	int in_type = SOCK_STREAM, in_proto = 0;
	switch (type)
	{
	case tcp: in_type = SOCK_STREAM; break;
	case udp: in_type = SOCK_DGRAM; break;
	}

#ifdef IPV6
	impl->sock = socket(AF_INET6, in_type, in_proto);
#else
	impl->sock = socket(AF_INET, in_type, in_proto);
#endif
	if (impl->sock < 0) throw CL_Error("CL_Socket::CL_Socket failed");

	input.socket = impl;
	output.socket = impl;
}

CL_Socket::CL_Socket(const CL_Socket &copy)
: impl(copy.impl)
{
	if (impl) impl->add_ref();
	input.socket = impl;
	output.socket = impl;
}

CL_Socket::~CL_Socket()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_Socket attributes:

int CL_Socket::get_socket() const
{
	return impl->sock;
}

CL_EventTrigger *CL_Socket::get_read_trigger() const
{
	if (impl->read == NULL) impl->read = impl->create_read_trigger();
	return impl->read;
}

CL_EventTrigger *CL_Socket::get_write_trigger() const
{
	if (impl->write == NULL) impl->write = impl->create_write_trigger();
	return impl->write;
}

CL_EventTrigger *CL_Socket::get_exception_trigger() const
{
	if (impl->exception == NULL) impl->exception = impl->create_exception_trigger();
	return impl->exception;
}

CL_IPAddress CL_Socket::get_source_address() const
{
	#ifdef IPV6
  sockaddr_in6 addr_in;
	#else
	sockaddr_in addr_in;
  #endif

	memset(&addr_in, 0, sizeof(addr_in));

  #ifdef IPV6
	addr_in.sin6_family = AF_INET6;
  #else
	addr_in.sin_family = AF_INET;
  #endif

	int size = sizeof(sockaddr_in);
	int result = getsockname(impl->sock, (sockaddr *) &addr_in, (socklen_t *) &size);
	if (result != 0) throw CL_Error("CL_Socket::get_source_address() failed.");

	return CL_Socket_Generic::create_ip_address(addr_in);
}

CL_IPAddress CL_Socket::get_dest_address() const
{
	#ifdef IPV6
  sockaddr_in6 addr_in;
	#else
	sockaddr_in addr_in;
  #endif

	memset(&addr_in, 0, sizeof(addr_in));

  #ifdef IPV6
	addr_in.sin6_family = AF_INET6;
  #else
	addr_in.sin_family = AF_INET;
  #endif

	int size = sizeof(sockaddr_in);
	int result = getpeername(impl->sock, (sockaddr *) &addr_in, (socklen_t *) &size);
	if (result != 0) throw CL_Error("CL_Socket::get_dest_address() failed.");

	return CL_Socket_Generic::create_ip_address(addr_in);
}

CL_Signal_v0 &CL_Socket::sig_read_triggered()
{
	return impl->sig_read_triggered;
}

CL_Signal_v0 &CL_Socket::sig_write_triggered()
{
	return impl->sig_write_triggered;
}

CL_Signal_v0 &CL_Socket::sig_exception_triggered()
{
	return impl->sig_exception_triggered;
}

/////////////////////////////////////////////////////////////////////////////
// CL_Socket operations:

CL_Socket &CL_Socket::operator =(const CL_Socket &other)
{
	if (impl) impl->release_ref();
	impl = other.impl;
	if (impl) impl->add_ref();
	input.socket = impl;
	output.socket = impl;
	return *this;
}

void CL_Socket::set_nonblocking(bool nonblocking)
{
#ifdef WIN32
	u_long argp = nonblocking ? 1 : 0;
	ioctlsocket(impl->sock, FIONBIO, &argp);
#else
	fcntl(impl->sock, F_SETFL, O_NONBLOCK, nonblocking ? 1 : 0);
#endif
}

void CL_Socket::set_nodelay(bool nodelay)
{
	static int sol_tcp = -1;
	if (sol_tcp == -1) sol_tcp = getprotobyname("tcp")->p_proto;

	int value = nodelay ? 1 : 0;
	setsockopt(impl->sock, sol_tcp, TCP_NODELAY, (const char *) &value, sizeof(int));
}

int CL_Socket::send(const void *data, int size)
{
	int result = ::send(
		impl->sock,
		(const char *) data,
		size,
		0);
	if (result < 0) throw CL_Error("CL_Socket::send failed");

	if (impl->write) impl->write->start_listen();
	return result;
}

int CL_Socket::send(const void *data, int size, const CL_IPAddress &dest)
{
	#ifdef IPV6
  sockaddr_in6 addr_in = CL_Socket_Generic::create_sockaddr_in6(dest);
  #else
	sockaddr_in addr_in = CL_Socket_Generic::create_sockaddr_in(dest);
  #endif

	int result = ::sendto(
		impl->sock,
		(const char *) data,
		size,
		0,
		(const sockaddr *) &addr_in,
		sizeof(addr_in));
	if (result < 0) throw CL_Error("CL_Socket::send failed");

	if (impl->write) impl->write->start_listen();
	return result;
}

int CL_Socket::send(const std::string &string)
{
	int result = ::send(
		impl->sock,
		(const char *) string.c_str(),
		string.size(),
		0);
	if (result < 0) throw CL_Error("CL_Socket::send failed");

	if (impl->write) impl->write->start_listen();
	return result;
}

int CL_Socket::recv(void *data, int size)
{
	int result = ::recv(impl->sock, (char *) data, size, 0);
	if (result < 0) throw CL_Error("CL_Socket::recv failed");

	if (impl->read) impl->read->start_listen();
	return result;
}

void CL_Socket::push(const std::string &string)
{
	impl->push_stack.push(string);
}

int CL_Socket::recv(void *data, int size, CL_IPAddress &from)
{
	char *_data = (char *) data;
	int total = 0;

	// Start filling data with pushed data (if any):
	while (!impl->push_stack.empty())
	{
		std::string &str = impl->push_stack.top();
		if (str.size() < (unsigned int)size)
		{
			memcpy(_data, str.data(), str.size());
			_data += str.size();
			total += str.size();
			size -= str.size();
			impl->push_stack.pop();
		}
		else
		{
			memcpy(_data, str.data(), size);
			str = str.substr(size);
			total += size;
			return total;
		}
	}

	// Fill with data from network socket:
	sockaddr_in addr_in;
	memset(&addr_in, 0, sizeof(addr_in));
	addr_in.sin_family = AF_INET;

	socklen_t addr_size = sizeof(addr_in);

	int result = ::recvfrom(
		impl->sock,
		_data,
		size,
		0,
		(sockaddr *) &addr_in,
		&addr_size);
	if (result < 0) throw CL_Error("CL_Socket::recv failed");
	total += result;

	if (impl->read) impl->read->start_listen();
	from = CL_Socket_Generic::create_ip_address(addr_in);
	return total;
}

void CL_Socket::connect(const CL_IPAddress &address)
{
	sockaddr_in addr_in = CL_Socket_Generic::create_sockaddr_in(address);
	int result = ::connect(impl->sock, (sockaddr *) &addr_in, sizeof(addr_in));
	if (result < 0) throw CL_Error("CL_Socket::connect failed");
	if (impl->read) impl->read->start_listen();
	if (impl->exception) impl->exception->start_listen();
}

void CL_Socket::shutdown(ShutdownHow how)
{
	int in_how = 0;
	switch (how)
	{
	case shutdown_receive: in_how = 0; break;
	case shutdown_send:    in_how = 1; break;
	}

	int result = ::shutdown(impl->sock, in_how);
	if (result < 0) throw CL_Error("CL_Socket::shutdown failed");
}

void CL_Socket::bind(const CL_IPAddress &address)
{
#ifdef IPV6
	sockaddr_in6 addr_in = CL_Socket_Generic::create_sockaddr_in6(address);
#else
	sockaddr_in addr_in = CL_Socket_Generic::create_sockaddr_in(address);
#endif
	int result = ::bind(impl->sock, (const sockaddr *) &addr_in, sizeof(addr_in));
	if (result < 0) throw CL_Error("CL_Socket::bind failed");
}

void CL_Socket::listen(int backlog)
{
	int result = ::listen(impl->sock, backlog);
	if (result < 0) throw CL_Error("CL_Socket::listen failed");
}

CL_Socket CL_Socket::accept()
{
	int new_sock = ::accept(impl->sock, NULL, NULL);
	if (new_sock == INVALID_SOCKET) throw CL_Error("CL_Socket::accept failed");
	if (impl->read) impl->read->start_listen();
	return CL_Socket(new_sock);
}

/////////////////////////////////////////////////////////////////////////////
// CL_Socket implementation:

CL_Socket::CL_Socket(CL_Socket_Generic *impl)
: impl(impl)
{
	impl->add_ref();
	input.socket = impl;
	output.socket = impl;
}