File: socket_set.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (131 lines) | stat: -rw-r--r-- 3,242 bytes parent folder | download | duplicates (5)
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
/* M-runtime for c++
 * Copyright (C) 2005-2008 Vladimir Menshakov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "socket_set.h"
#include "tcp_socket.h"
#include "net_exception.h"
#include <assert.h>

#ifdef _WINDOWS
#	include <Winsock2.h>
#else
#	include <sys/select.h>
/* According to earlier standards */
#	include <sys/time.h>
#	include <sys/types.h>
#	include <unistd.h>
#	include <errno.h>
#endif

using namespace mrt;

const int SocketSet::Read = 1;
const int SocketSet::Write = 2;
const int SocketSet::Exception = 4;


SocketSet::SocketSet() : _r_set(new fd_set), _w_set(new fd_set), _e_set(new fd_set), _n(0) {
	reset();
}

void SocketSet::reset() {
	FD_ZERO((fd_set*)_r_set);
	FD_ZERO((fd_set*)_w_set);
	FD_ZERO((fd_set*)_e_set);
}


void SocketSet::add(const Socket &sock, const int how) {
	int fd = sock._sock;
	if (fd == -1)
		throw_ex(("attempt to add uninitialized socket to set"));
	if (!(how & (Read | Write | Exception))) {
		LOG_WARN(("skip add in set %d", how));
		return;
	}

	if (how & Read) {
		FD_SET(fd, (fd_set*)_r_set);
	}
	if (how & Write) {
		FD_SET(fd, (fd_set*)_w_set);
	}
	if (how & Exception) {
		FD_SET(fd, (fd_set*)_e_set);
	}
	if (fd >= _n) 
		_n = fd + 1;
}

void SocketSet::add(const Socket *sock, const int how) {
	if (sock == NULL)
		throw_ex(("attempt to add NULL socket to set"));
	add(*sock, how);
}


void SocketSet::remove(const Socket &sock) {
	if (sock._sock == -1)
		throw_ex(("attempt to remove uninitialized socket from set"));

	FD_CLR(sock._sock, (fd_set*)_r_set);
	FD_CLR(sock._sock, (fd_set*)_w_set);
	FD_CLR(sock._sock, (fd_set*)_e_set);
}

const int SocketSet::check(const unsigned int timeout) {
	struct timeval tv;
	tv.tv_sec = timeout / 1000;
	tv.tv_usec = ( timeout % 1000 ) * 1000;
	
	int r = select(_n, (fd_set*)_r_set, (fd_set*)_w_set, (fd_set*)_e_set, &tv);
	if (r == -1) {
#ifndef _WINDOWS
		if (errno == EINTR) 
			return 0;
#endif
		throw_net(("select"));
	}
	
	return r;
}

bool SocketSet::check(const Socket &sock, const int how) {
	int fd = sock._sock;
	if (fd == -1)
		throw_ex(("check on uninitialized socket"));
	
	if ((how & Read) != 0 && FD_ISSET(fd, ((fd_set*)_r_set)))
		return true;
	if ((how & Write) != 0 && FD_ISSET(fd, ((fd_set*)_w_set)))
		return true;
	if ((how & Exception) != 0 && FD_ISSET(fd, ((fd_set*)_e_set)))
		return true;
	return false;
}

bool SocketSet::check(const Socket *sock, const int how) {
	return check(*sock, how);
}

SocketSet::~SocketSet() {
	delete ((fd_set *)_r_set);
	delete ((fd_set *)_w_set);
	delete ((fd_set *)_e_set);
}