2018-12-30 08:15:27 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketServer.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXWebSocketServer.h"
|
|
|
|
#include "IXWebSocketTransport.h"
|
|
|
|
#include "IXWebSocket.h"
|
2019-01-02 01:11:27 +01:00
|
|
|
#include "IXSocketConnect.h"
|
2018-12-31 07:00:49 +01:00
|
|
|
|
|
|
|
#include <sstream>
|
2019-01-01 22:47:25 +01:00
|
|
|
#include <future>
|
2018-12-30 08:15:27 +01:00
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
2019-01-02 02:13:26 +01:00
|
|
|
#include <string.h>
|
2018-12-30 08:15:27 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-01-02 04:23:27 +01:00
|
|
|
const int WebSocketServer::kDefaultPort(8080);
|
2019-01-01 23:28:41 +01:00
|
|
|
const std::string WebSocketServer::kDefaultHost("127.0.0.1");
|
2019-01-02 04:23:27 +01:00
|
|
|
const int WebSocketServer::kDefaultTcpBacklog(5);
|
2019-01-01 23:28:41 +01:00
|
|
|
|
|
|
|
WebSocketServer::WebSocketServer(int port, const std::string& host, int backlog) :
|
2018-12-31 07:00:49 +01:00
|
|
|
_port(port),
|
2019-01-01 23:28:41 +01:00
|
|
|
_host(host),
|
2019-01-01 23:52:14 +01:00
|
|
|
_backlog(backlog),
|
|
|
|
_stop(false)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketServer::~WebSocketServer()
|
|
|
|
{
|
2019-01-01 23:52:14 +01:00
|
|
|
stop();
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:12:13 +01:00
|
|
|
void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback)
|
|
|
|
{
|
|
|
|
_onConnectionCallback = callback;
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
void WebSocketServer::logError(const std::string& str)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_logMutex);
|
|
|
|
std::cerr << str << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketServer::logInfo(const std::string& str)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_logMutex);
|
|
|
|
std::cout << str << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
std::pair<bool, std::string> WebSocketServer::listen()
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2019-01-01 23:28:41 +01:00
|
|
|
struct sockaddr_in server; // server address information
|
2018-12-30 08:15:27 +01:00
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
// Get a socket for accepting connections.
|
2018-12-31 07:00:49 +01:00
|
|
|
if ((_serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::listen() error creating socket): "
|
|
|
|
<< strerror(errno);
|
|
|
|
|
|
|
|
return std::make_pair(false, ss.str());
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
// Make that socket reusable. (allow restarting this server at will)
|
2018-12-31 07:00:49 +01:00
|
|
|
int enable = 1;
|
|
|
|
if (setsockopt(_serverFd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2018-12-31 07:00:49 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::listen() error calling setsockopt(SO_REUSEADDR): "
|
|
|
|
<< strerror(errno);
|
|
|
|
|
|
|
|
return std::make_pair(false, ss.str());
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
// Bind the socket to the server address.
|
2018-12-30 08:15:27 +01:00
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(_port);
|
2018-12-31 20:48:49 +01:00
|
|
|
|
|
|
|
// Using INADDR_ANY trigger a pop-up box as binding to any address is detected
|
|
|
|
// by the osx firewall. We need to codesign the binary with a self-signed cert
|
|
|
|
// to allow that, but this is a bit of a pain. (this is what node or python would do).
|
|
|
|
//
|
|
|
|
// Using INADDR_LOOPBACK also does not work ... while it should.
|
2019-01-01 23:28:41 +01:00
|
|
|
// We default to 127.0.0.1 (localhost)
|
2018-12-31 20:48:49 +01:00
|
|
|
//
|
2019-01-01 23:28:41 +01:00
|
|
|
server.sin_addr.s_addr = inet_addr(_host.c_str());
|
2018-12-30 08:15:27 +01:00
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
if (bind(_serverFd, (struct sockaddr *)&server, sizeof(server)) < 0)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2018-12-31 07:00:49 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::listen() error calling bind: "
|
|
|
|
<< strerror(errno);
|
|
|
|
|
|
|
|
return std::make_pair(false, ss.str());
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-12-31 07:00:49 +01:00
|
|
|
* Listen for connections. Specify the tcp backlog.
|
2018-12-30 08:15:27 +01:00
|
|
|
*/
|
2018-12-31 07:00:49 +01:00
|
|
|
if (::listen(_serverFd, _backlog) != 0)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2018-12-31 07:00:49 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::listen() error calling listen: "
|
|
|
|
<< strerror(errno);
|
|
|
|
|
|
|
|
return std::make_pair(false, ss.str());
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:52:14 +01:00
|
|
|
void WebSocketServer::start()
|
|
|
|
{
|
|
|
|
if (_thread.joinable()) return; // we've already been started
|
|
|
|
|
|
|
|
_thread = std::thread(&WebSocketServer::run, this);
|
|
|
|
}
|
|
|
|
|
2019-01-02 01:11:27 +01:00
|
|
|
void WebSocketServer::wait()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_conditionVariableMutex);
|
|
|
|
_conditionVariable.wait(lock);
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:52:14 +01:00
|
|
|
void WebSocketServer::stop()
|
|
|
|
{
|
2019-01-02 01:14:46 +01:00
|
|
|
if (!_thread.joinable()) return; // nothing to do
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
auto clients = getClients();
|
|
|
|
for (auto client : clients)
|
|
|
|
{
|
|
|
|
client->close();
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:52:14 +01:00
|
|
|
_stop = true;
|
|
|
|
_thread.join();
|
|
|
|
_stop = false;
|
2019-01-02 01:11:27 +01:00
|
|
|
|
|
|
|
_conditionVariable.notify_one();
|
2019-01-01 23:52:14 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
void WebSocketServer::run()
|
|
|
|
{
|
2019-01-02 01:11:27 +01:00
|
|
|
// Set the socket to non blocking mode, so that accept calls are not blocking
|
|
|
|
SocketConnect::configure(_serverFd);
|
|
|
|
|
2019-01-02 07:21:07 +01:00
|
|
|
// Return value of std::async, ignored
|
2019-01-01 22:47:25 +01:00
|
|
|
std::future<void> f;
|
|
|
|
|
2019-01-02 07:21:07 +01:00
|
|
|
// Select arguments
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 10 * 1000; // 10ms
|
|
|
|
|
2018-12-31 06:16:05 +01:00
|
|
|
for (;;)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2019-01-02 01:11:27 +01:00
|
|
|
if (_stop) return;
|
|
|
|
|
2019-01-02 07:21:07 +01:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(_serverFd, &rfds);
|
|
|
|
select(_serverFd + 1, &rfds, nullptr, nullptr, &timeout);
|
|
|
|
|
|
|
|
if (!FD_ISSET(_serverFd, &rfds))
|
|
|
|
{
|
|
|
|
// We reached the select timeout, and no new connections are pending
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-31 21:43:47 +01:00
|
|
|
// Accept a connection.
|
|
|
|
struct sockaddr_in client; // client address information
|
|
|
|
int clientFd; // socket connected to client
|
2018-12-31 07:00:49 +01:00
|
|
|
socklen_t addressLen = sizeof(socklen_t);
|
2019-01-02 07:21:07 +01:00
|
|
|
memset(&client, 0, sizeof(client));
|
2018-12-31 06:16:05 +01:00
|
|
|
|
2019-01-02 01:11:27 +01:00
|
|
|
if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) < 0)
|
2018-12-31 06:16:05 +01:00
|
|
|
{
|
2019-01-02 07:21:07 +01:00
|
|
|
if (errno != EWOULDBLOCK)
|
2019-01-02 01:11:27 +01:00
|
|
|
{
|
|
|
|
// FIXME: that error should be propagated
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::run() error accepting connection: "
|
|
|
|
<< strerror(errno);
|
|
|
|
logError(ss.str());
|
|
|
|
}
|
2018-12-31 07:00:49 +01:00
|
|
|
continue;
|
2018-12-31 06:16:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 22:47:25 +01:00
|
|
|
// Launch the handleConnection work asynchronously in its own thread.
|
|
|
|
//
|
|
|
|
// the destructor of a future returned by std::async blocks,
|
|
|
|
// so we need to declare it outside of this loop
|
2019-01-01 23:28:41 +01:00
|
|
|
f = std::async(std::launch::async,
|
|
|
|
&WebSocketServer::handleConnection,
|
|
|
|
this,
|
|
|
|
clientFd);
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
2018-12-31 06:16:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketServer::handleConnection(int fd)
|
|
|
|
{
|
2019-01-01 22:53:13 +01:00
|
|
|
std::shared_ptr<WebSocket> webSocket(new WebSocket);
|
2018-12-31 07:12:13 +01:00
|
|
|
_onConnectionCallback(webSocket);
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
webSocket->disableAutomaticReconnection();
|
|
|
|
|
|
|
|
// Add this client to our client set
|
2019-01-01 23:28:41 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
|
|
|
_clients.insert(webSocket);
|
|
|
|
}
|
2019-01-01 22:47:25 +01:00
|
|
|
|
2019-01-01 22:53:13 +01:00
|
|
|
auto status = webSocket->connectToSocket(fd);
|
2019-01-03 01:08:32 +01:00
|
|
|
if (status.success)
|
|
|
|
{
|
|
|
|
// Process incoming messages and execute callbacks
|
|
|
|
// until the connection is closed
|
|
|
|
webSocket->run();
|
|
|
|
}
|
|
|
|
else
|
2019-01-01 22:47:25 +01:00
|
|
|
{
|
2019-01-01 23:28:41 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WebSocketServer::handleConnection() error: "
|
2019-01-03 01:08:32 +01:00
|
|
|
<< status.http_status
|
|
|
|
<< " error: "
|
2019-01-01 23:28:41 +01:00
|
|
|
<< status.errorStr;
|
|
|
|
logError(ss.str());
|
2019-01-01 22:47:25 +01:00
|
|
|
}
|
2018-12-31 21:43:47 +01:00
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
// Remove this client from our client set
|
2019-01-01 23:28:41 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
2019-01-02 06:25:15 +01:00
|
|
|
if (_clients.erase(webSocket) != 1)
|
|
|
|
{
|
|
|
|
logError("Cannot delete client");
|
|
|
|
}
|
2019-01-01 23:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logInfo("WebSocketServer::handleConnection() done");
|
|
|
|
}
|
2019-01-01 22:47:25 +01:00
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
std::set<std::shared_ptr<WebSocket>> WebSocketServer::getClients()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
|
|
|
return _clients;
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
}
|