summaryrefslogtreecommitdiffstats
path: root/src/uscxml/server/Socket.cpp
blob: 9c844e55663f9c481ea2b8ff4ddf0657728ab320 (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
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
/**
 *  @file
 *  @author     2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program 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.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "Socket.h"

#include "uscxml/Message.h"             // for Data, Event
#include "uscxml/config.h"              // for OPENSSL_FOUND

#ifndef _WIN32
#include <sys/socket.h> /* For socket functions */
#include <arpa/inet.h> // inet_addr
#endif

#include <fcntl.h> /* For fcntl */
#include <iostream>

namespace uscxml {

// see: http://codepad.org/XRJAVg5m
Socket::Socket(int domain, int type, int protocol) {

	_base = EventBase::get("sockets");
	_blockSizeRead = 1024;

	if (!_base)
		throw std::runtime_error("Cannot get eventbase");

	_sin.sin_family = domain;
	_socketFD = socket(domain, type, protocol);

	if (_socketFD == -1)
		throw std::runtime_error(std::string("socket: ") + strerror(errno));
#ifndef WIN32
	{
		int one = 1;
		if (setsockopt(_socketFD, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
			throw std::runtime_error(std::string("setsockopt: ") + strerror(errno));
		}
	}
#endif

}

Socket::~Socket() {
	if (_socketFD > 0)
#ifdef WIN32
		closesocket(_socketFD);
#else
		close(_socketFD);
#endif
}

void Socket::setupSockAddr(const std::string& address, int port) {
	if (address == "*") {
		_sin.sin_addr.s_addr = 0;
	} else {
		_sin.sin_addr.s_addr = inet_addr(address.c_str());
		if (_sin.sin_addr.s_addr == INADDR_NONE)
			throw std::runtime_error(std::string("inet_addr: ") + strerror(errno));
	}

	_sin.sin_port = htons(port);
}

void Socket::setBlockSizeRead(size_t size) {
//	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	_blockSizeRead = size;
}

ClientSocket::ClientSocket(int domain, int type, int protocol) : Socket(domain, type, protocol), _clientEvent(NULL) {
}


ClientSocket::~ClientSocket() {
	if (_clientEvent) {
		bufferevent_enable(_clientEvent, 0);
		bufferevent_free(_clientEvent);
	}
}

void ClientSocket::errorCallback(struct bufferevent *bev, short error, void *ctx) {
	ClientSocket* instance = (ClientSocket*)ctx;
	//	tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);

	if (error & BEV_EVENT_READING) {
		std::cout << "ClientSocket: error encountered while reading" << std::endl;
	} else if (error & BEV_EVENT_WRITING) {
		std::cout << "ClientSocket: error encountered while writing" << std::endl;
	} else if (error & BEV_EVENT_EOF) {
		std::cout << "ClientSocket: eof file reached" << std::endl;
	} else if (error & BEV_EVENT_ERROR) {
		std::cout << "ClientSocket: unrecoverable error encountered" << std::endl;
	} else if (error & BEV_EVENT_TIMEOUT) {
		std::cout << "ClientSocket: user-specified timeout reached" << std::endl;
	} else if (error & BEV_EVENT_CONNECTED) {
		std::cout << "ClientSocket: connect operation finished" << std::endl;
	}

	//	bufferevent_free(bev);
}

void ClientSocket::connect(const std::string& address, int port) {
//	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	setupSockAddr(address, port);
	if(::connect(_socketFD, (struct sockaddr *)&_sin, sizeof _sin) != 0) {
		throw std::runtime_error(std::string("connect: ") + strerror(errno));
	}

	_clientEvent = bufferevent_socket_new(_base->base, _socketFD, 0); //BEV_OPT_THREADSAFE);
	bufferevent_setcb(_clientEvent, ClientSocket::readCallback, NULL, ClientSocket::errorCallback, this);
	bufferevent_enable(_clientEvent, EV_READ|EV_WRITE);
}

int ClientSocket::write(const char* data, size_t size) {
//	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	bufferevent_write(_clientEvent, data, size);
	return size;
}

void ClientSocket::readCallback(struct bufferevent *bev, void *ctx) {
	ClientSocket* instance = (ClientSocket*)ctx;
//	tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);

	size_t n;
	struct evbuffer* input;
	char* data = (char*)malloc(instance->_blockSizeRead);

	input = bufferevent_get_input(bev);
	n = evbuffer_remove(input, data, instance->_blockSizeRead);

	instance->readCallback(data, n);
	free(data);
}

std::set<ServerSocket*> ServerSocket::_instances;

ServerSocket::ServerSocket(int domain, int type, int protocol) : Socket(domain, type, protocol), _listenerEvent(NULL) {
	_instances.insert(this);
}

ServerSocket::~ServerSocket() {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	std::map<struct bufferevent*, Connection>::iterator connIter = _connections.begin();
	while(connIter != _connections.end()) {
		bufferevent_enable(connIter->second.bufferEvent, 0);
		bufferevent_setcb(connIter->second.bufferEvent, NULL, NULL, NULL, 0);

		bufferevent_free(connIter->second.bufferEvent);
#ifdef WIN32
		closesocket(connIter->second.fd);
#else
		close(connIter->second.fd);
#endif

		connIter++;
	}

	if (_listenerEvent) {
		event_del(_listenerEvent);
		event_free(_listenerEvent);
	}

	_instances.erase(this);

}

void ServerSocket::errorCallback(struct bufferevent *bev, short error, void *ctx) {
	ServerSocket* instance = (ServerSocket*)ctx;
	tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);

	if (_instances.find(instance) == _instances.end())
		return;

	if (error & BEV_EVENT_READING || error & BEV_EVENT_WRITING) {
		// remote end close the connection
		tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);
		std::map<struct bufferevent*, Connection>::iterator conn = instance->_connections.find(bev);
		if (conn != instance->_connections.end()) {
			bufferevent_enable(conn->second.bufferEvent, 0);
			bufferevent_free(conn->second.bufferEvent);
#ifdef WIN32
			closesocket(conn->second.fd);
#else
			close(conn->second.fd);
#endif

			instance->_connections.erase(conn);
		}
	} else if (error & BEV_EVENT_EOF) {
		std::cout << "ServerSocket: eof file reached" << std::endl;
	} else if (error & BEV_EVENT_ERROR) {
		std::cout << "ServerSocket: unrecoverable error encountered" << std::endl;
	} else if (error & BEV_EVENT_TIMEOUT) {
		std::cout << "ServerSocket: user-specified timeout reached" << std::endl;
	} else if (error & BEV_EVENT_CONNECTED) {
		std::cout << "ServerSocket: connect operation finished" << std::endl;
	}
	//	bufferevent_free(bev);
}

void ServerSocket::readCallback(struct bufferevent *bev, void *ctx) {
	ServerSocket* instance = (ServerSocket*)ctx;
	tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);

	// instance is already gone
	if (_instances.find(instance) == _instances.end())
		return;

	size_t n;
	struct evbuffer* input;
	char* data = (char*)malloc(instance->_blockSizeRead);

	input = bufferevent_get_input(bev);
	n = evbuffer_remove(input, data, instance->_blockSizeRead);

	instance->readCallback(data, n, instance->_connections[bev]);
	free(data);
}

void ServerSocket::bind() {
	if (::bind(_socketFD, (struct sockaddr*)&_sin, sizeof(_sin)) < 0) {
		throw std::runtime_error(std::string("bind: ") + strerror(errno));
	}
}

void ServerSocket::listen(const std::string& address, int port) {
//	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	setupSockAddr(address, port);
	bind();

	_listenerEvent = event_new(_base->base, _socketFD, EV_READ|EV_PERSIST, acceptCallback, (void*)this);
	/*XXX check it */
	event_add(_listenerEvent, NULL);

	if (::listen(_socketFD, 16)<0) {
		throw std::runtime_error(std::string("listen: ") + strerror(errno));
	}
}

void ServerSocket::acceptCallback(evutil_socket_t listener, short event, void *ctx) {
	ServerSocket* instance = (ServerSocket*)ctx;
//	tthread::lock_guard<tthread::recursive_mutex> lock(instance->_mutex);

	struct sockaddr_storage ss;
	socklen_t slen = sizeof(ss);
	int fd = accept(listener, (struct sockaddr*)&ss, &slen);
	if (fd < 0) {
		throw std::runtime_error(std::string("accept: ") + strerror(errno));
	} else if (fd > FD_SETSIZE) {
#ifdef WIN32
		closesocket(fd);
#else
		close(fd);
#endif

		throw std::runtime_error(std::string("accept: ") + strerror(errno));
	} else {
		struct bufferevent *bev;
		evutil_make_socket_nonblocking(fd);
		bev = bufferevent_socket_new(instance->_base->base, fd, BEV_OPT_THREADSAFE);
		bufferevent_setcb(bev, ServerSocket::readCallback, NULL, ServerSocket::errorCallback, ctx);
		bufferevent_enable(bev, EV_READ|EV_WRITE);

		instance->_connections[bev].bufferEvent = bev;
		instance->_connections[bev].fd = fd;
	}
}

void ServerSocket::Connection::reply(const char* data, size_t size) {
	bufferevent_write(bufferEvent, data, size);
}

}