(socket servers) merge the ConnectionInfo class with the ConnectionState one, which simplify all the server apis
This commit is contained in:
parent
629c155044
commit
73b9c0b89b
@ -62,7 +62,6 @@ set( IXWEBSOCKET_SOURCES
|
|||||||
set( IXWEBSOCKET_HEADERS
|
set( IXWEBSOCKET_HEADERS
|
||||||
ixwebsocket/IXBench.h
|
ixwebsocket/IXBench.h
|
||||||
ixwebsocket/IXCancellationRequest.h
|
ixwebsocket/IXCancellationRequest.h
|
||||||
ixwebsocket/IXConnectionInfo.h
|
|
||||||
ixwebsocket/IXConnectionState.h
|
ixwebsocket/IXConnectionState.h
|
||||||
ixwebsocket/IXDNSLookup.h
|
ixwebsocket/IXDNSLookup.h
|
||||||
ixwebsocket/IXExponentialBackoff.h
|
ixwebsocket/IXExponentialBackoff.h
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
All changes to this project will be documented in this file.
|
All changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [10.3.1] - 2020-08-28
|
||||||
|
|
||||||
|
(socket servers) merge the ConnectionInfo class with the ConnectionState one, which simplify all the server apis
|
||||||
|
|
||||||
## [10.3.0] - 2020-08-26
|
## [10.3.0] - 2020-08-26
|
||||||
|
|
||||||
(ws) set the main thread name, to help with debugging in XCode, gdb, lldb etc...
|
(ws) set the main thread name, to help with debugging in XCode, gdb, lldb etc...
|
||||||
|
@ -280,10 +280,9 @@ ix::WebSocketServer server(port);
|
|||||||
|
|
||||||
server.setOnConnectionCallback(
|
server.setOnConnectionCallback(
|
||||||
[&server](std::weak_ptr<WebSocket> webSocket,
|
[&server](std::weak_ptr<WebSocket> webSocket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)
|
|
||||||
{
|
{
|
||||||
std::cout << "Remote ip: " << connectionInfo->remoteIp << std::endl;
|
std::cout << "Remote ip: " << connectionState->remoteIp << std::endl;
|
||||||
|
|
||||||
auto ws = webSocket.lock();
|
auto ws = webSocket.lock();
|
||||||
if (ws)
|
if (ws)
|
||||||
@ -359,13 +358,12 @@ The webSocket reference is guaranteed to be always valid ; by design the callbac
|
|||||||
ix::WebSocketServer server(port);
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionState,
|
server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const WebSocketMessagePtr& msg)
|
const WebSocketMessagePtr& msg)
|
||||||
{
|
{
|
||||||
// The ConnectionInfo object contains information about the connection,
|
// The ConnectionState object contains information about the connection,
|
||||||
// at this point only the client ip address and the port.
|
// at this point only the client ip address and the port.
|
||||||
std::cout << "Remote ip: " << connectionInfo.remoteIp << std::endl;
|
std::cout << "Remote ip: " << connectionState->getRemoteIp();
|
||||||
|
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
@ -519,12 +517,11 @@ If you want to handle how requests are processed, implement the setOnConnectionC
|
|||||||
```cpp
|
```cpp
|
||||||
setOnConnectionCallback(
|
setOnConnectionCallback(
|
||||||
[this](HttpRequestPtr request,
|
[this](HttpRequestPtr request,
|
||||||
std::shared_ptr<ConnectionState> /*connectionState*/,
|
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr
|
|
||||||
{
|
{
|
||||||
// Build a string for the response
|
// Build a string for the response
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << connectionInfo->remoteIp
|
ss << connectionState->getRemoteIp();
|
||||||
<< " "
|
<< " "
|
||||||
<< request->method
|
<< request->method
|
||||||
<< " "
|
<< " "
|
||||||
|
@ -45,10 +45,9 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RedisServer::handleConnection(std::unique_ptr<Socket> socket,
|
void RedisServer::handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)
|
|
||||||
{
|
{
|
||||||
logInfo("New connection from remote ip " + connectionInfo->remoteIp);
|
logInfo("New connection from remote ip " + connectionState->getRemoteIp());
|
||||||
|
|
||||||
_connectedClientsCount++;
|
_connectedClientsCount++;
|
||||||
|
|
||||||
|
@ -44,8 +44,7 @@ namespace ix
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
virtual void handleConnection(std::unique_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState) final;
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) final;
|
|
||||||
virtual size_t getConnectedClientsCount() final;
|
virtual size_t getConnectedClientsCount() final;
|
||||||
|
|
||||||
bool startsWith(const std::string& str, const std::string& start);
|
bool startsWith(const std::string& str, const std::string& start);
|
||||||
|
@ -61,11 +61,10 @@ namespace snake
|
|||||||
|
|
||||||
_server.setOnClientMessageCallback(
|
_server.setOnClientMessageCallback(
|
||||||
[this](std::shared_ptr<ix::ConnectionState> connectionState,
|
[this](std::shared_ptr<ix::ConnectionState> connectionState,
|
||||||
ix::ConnectionInfo& connectionInfo,
|
|
||||||
ix::WebSocket& webSocket,
|
ix::WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto state = std::dynamic_pointer_cast<SnakeConnectionState>(connectionState);
|
auto state = std::dynamic_pointer_cast<SnakeConnectionState>(connectionState);
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "[" << state->getId() << "] ";
|
ss << "[" << state->getId() << "] ";
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXConnectionInfo.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
struct ConnectionInfo
|
|
||||||
{
|
|
||||||
std::string remoteIp;
|
|
||||||
int remotePort;
|
|
||||||
|
|
||||||
ConnectionInfo(const std::string& r = std::string(), int p = 0)
|
|
||||||
: remoteIp(r)
|
|
||||||
, remotePort(p)
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -50,4 +50,24 @@ namespace ix
|
|||||||
_onSetTerminatedCallback();
|
_onSetTerminatedCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string& ConnectionState::getRemoteIp()
|
||||||
|
{
|
||||||
|
return _remoteIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ConnectionState::getRemotePort()
|
||||||
|
{
|
||||||
|
return _remotePort;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionState::setRemoteIp(const std::string& remoteIp)
|
||||||
|
{
|
||||||
|
_remoteIp = remoteIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionState::setRemotePort(int remotePort)
|
||||||
|
{
|
||||||
|
_remotePort = remotePort;
|
||||||
|
}
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -28,11 +28,17 @@ namespace ix
|
|||||||
void setTerminated();
|
void setTerminated();
|
||||||
bool isTerminated() const;
|
bool isTerminated() const;
|
||||||
|
|
||||||
|
const std::string& getRemoteIp();
|
||||||
|
int getRemotePort();
|
||||||
|
|
||||||
static std::shared_ptr<ConnectionState> createConnectionState();
|
static std::shared_ptr<ConnectionState> createConnectionState();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback);
|
void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback);
|
||||||
|
|
||||||
|
void setRemoteIp(const std::string& remoteIp);
|
||||||
|
void setRemotePort(int remotePort);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::atomic<bool> _terminated;
|
std::atomic<bool> _terminated;
|
||||||
std::string _id;
|
std::string _id;
|
||||||
@ -40,6 +46,9 @@ namespace ix
|
|||||||
|
|
||||||
static std::atomic<uint64_t> _globalId;
|
static std::atomic<uint64_t> _globalId;
|
||||||
|
|
||||||
|
std::string _remoteIp;
|
||||||
|
int _remotePort;
|
||||||
|
|
||||||
friend class SocketServer;
|
friend class SocketServer;
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -120,8 +120,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HttpServer::handleConnection(std::unique_ptr<Socket> socket,
|
void HttpServer::handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)
|
|
||||||
{
|
{
|
||||||
_connectedClientsCount++;
|
_connectedClientsCount++;
|
||||||
|
|
||||||
@ -130,8 +129,7 @@ namespace ix
|
|||||||
|
|
||||||
if (std::get<0>(ret))
|
if (std::get<0>(ret))
|
||||||
{
|
{
|
||||||
auto response =
|
auto response = _onConnectionCallback(std::get<2>(ret), connectionState);
|
||||||
_onConnectionCallback(std::get<2>(ret), connectionState, std::move(connectionInfo));
|
|
||||||
if (!Http::sendResponse(response, socket))
|
if (!Http::sendResponse(response, socket))
|
||||||
{
|
{
|
||||||
logError("Cannot send response");
|
logError("Cannot send response");
|
||||||
@ -151,8 +149,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
setOnConnectionCallback(
|
setOnConnectionCallback(
|
||||||
[this](HttpRequestPtr request,
|
[this](HttpRequestPtr request,
|
||||||
std::shared_ptr<ConnectionState> /*connectionState*/,
|
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr {
|
|
||||||
std::string uri(request->uri);
|
std::string uri(request->uri);
|
||||||
if (uri.empty() || uri == "/")
|
if (uri.empty() || uri == "/")
|
||||||
{
|
{
|
||||||
@ -184,8 +181,8 @@ namespace ix
|
|||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " "
|
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
|
||||||
<< request->method << " " << request->headers["User-Agent"] << " "
|
<< " " << request->method << " " << request->headers["User-Agent"] << " "
|
||||||
<< request->uri << " " << content.size();
|
<< request->uri << " " << content.size();
|
||||||
logInfo(ss.str());
|
logInfo(ss.str());
|
||||||
|
|
||||||
@ -209,16 +206,16 @@ namespace ix
|
|||||||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
|
||||||
//
|
//
|
||||||
setOnConnectionCallback(
|
setOnConnectionCallback(
|
||||||
[this, redirectUrl](HttpRequestPtr request,
|
[this,
|
||||||
std::shared_ptr<ConnectionState> /*connectionState*/,
|
redirectUrl](HttpRequestPtr request,
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr {
|
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
|
||||||
WebSocketHttpHeaders headers;
|
WebSocketHttpHeaders headers;
|
||||||
headers["Server"] = userAgent();
|
headers["Server"] = userAgent();
|
||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " "
|
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
|
||||||
<< request->method << " " << request->headers["User-Agent"] << " "
|
<< " " << request->method << " " << request->headers["User-Agent"] << " "
|
||||||
<< request->uri;
|
<< request->uri;
|
||||||
logInfo(ss.str());
|
logInfo(ss.str());
|
||||||
|
|
||||||
|
@ -23,9 +23,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using OnConnectionCallback =
|
using OnConnectionCallback =
|
||||||
std::function<HttpResponsePtr(HttpRequestPtr,
|
std::function<HttpResponsePtr(HttpRequestPtr, std::shared_ptr<ConnectionState>)>;
|
||||||
std::shared_ptr<ConnectionState>,
|
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)>;
|
|
||||||
|
|
||||||
HttpServer(int port = SocketServer::kDefaultPort,
|
HttpServer(int port = SocketServer::kDefaultPort,
|
||||||
const std::string& host = SocketServer::kDefaultHost,
|
const std::string& host = SocketServer::kDefaultHost,
|
||||||
@ -46,8 +44,7 @@ namespace ix
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
virtual void handleConnection(std::unique_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState) final;
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) final;
|
|
||||||
virtual size_t getConnectedClientsCount() final;
|
virtual size_t getConnectedClientsCount() final;
|
||||||
|
|
||||||
void setDefaultConnectionCallback();
|
void setDefaultConnectionCallback();
|
||||||
|
@ -332,12 +332,13 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve connection info, the ip address of the remote peer/client)
|
// Retrieve connection info, the ip address of the remote peer/client)
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo;
|
std::string remoteIp;
|
||||||
|
int remotePort;
|
||||||
|
|
||||||
if (_addressFamily == AF_INET)
|
if (_addressFamily == AF_INET)
|
||||||
{
|
{
|
||||||
char remoteIp[INET_ADDRSTRLEN];
|
char remoteIp4[INET_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp, INET_ADDRSTRLEN) == nullptr)
|
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
|
||||||
{
|
{
|
||||||
int err = Socket::getErrno();
|
int err = Socket::getErrno();
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -350,12 +351,13 @@ namespace ix
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionInfo = std::make_unique<ConnectionInfo>(remoteIp, client.sin_port);
|
remotePort = client.sin_port;
|
||||||
|
remoteIp = remoteIp4;
|
||||||
}
|
}
|
||||||
else // AF_INET6
|
else // AF_INET6
|
||||||
{
|
{
|
||||||
char remoteIp[INET6_ADDRSTRLEN];
|
char remoteIp6[INET6_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp, INET6_ADDRSTRLEN) == nullptr)
|
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) == nullptr)
|
||||||
{
|
{
|
||||||
int err = Socket::getErrno();
|
int err = Socket::getErrno();
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -368,7 +370,8 @@ namespace ix
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionInfo = std::make_unique<ConnectionInfo>(remoteIp, client.sin_port);
|
remotePort = client.sin_port;
|
||||||
|
remoteIp = remoteIp6;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<ConnectionState> connectionState;
|
std::shared_ptr<ConnectionState> connectionState;
|
||||||
@ -377,6 +380,8 @@ namespace ix
|
|||||||
connectionState = _connectionStateFactory();
|
connectionState = _connectionStateFactory();
|
||||||
}
|
}
|
||||||
connectionState->setOnSetTerminatedCallback([this] { onSetTerminatedCallback(); });
|
connectionState->setOnSetTerminatedCallback([this] { onSetTerminatedCallback(); });
|
||||||
|
connectionState->setRemoteIp(remoteIp);
|
||||||
|
connectionState->setRemotePort(remotePort);
|
||||||
|
|
||||||
if (_stop) return;
|
if (_stop) return;
|
||||||
|
|
||||||
@ -404,13 +409,10 @@ namespace ix
|
|||||||
|
|
||||||
// Launch the handleConnection work asynchronously in its own thread.
|
// Launch the handleConnection work asynchronously in its own thread.
|
||||||
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
|
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
|
||||||
_connectionsThreads.push_back(
|
_connectionsThreads.push_back(std::make_pair(
|
||||||
std::make_pair(connectionState,
|
connectionState,
|
||||||
std::thread(&SocketServer::handleConnection,
|
std::thread(
|
||||||
this,
|
&SocketServer::handleConnection, this, std::move(socket), connectionState)));
|
||||||
std::move(socket),
|
|
||||||
connectionState,
|
|
||||||
std::move(connectionInfo))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXConnectionInfo.h"
|
|
||||||
#include "IXConnectionState.h"
|
#include "IXConnectionState.h"
|
||||||
#include "IXSelectInterrupt.h"
|
#include "IXSelectInterrupt.h"
|
||||||
#include "IXSocketTLSOptions.h"
|
#include "IXSocketTLSOptions.h"
|
||||||
@ -105,8 +104,7 @@ namespace ix
|
|||||||
ConnectionStateFactory _connectionStateFactory;
|
ConnectionStateFactory _connectionStateFactory;
|
||||||
|
|
||||||
virtual void handleConnection(std::unique_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState) = 0;
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) = 0;
|
|
||||||
virtual size_t getConnectedClientsCount() = 0;
|
virtual size_t getConnectedClientsCount() = 0;
|
||||||
|
|
||||||
// Returns true if all connection threads are joined
|
// Returns true if all connection threads are joined
|
||||||
|
@ -56,10 +56,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnConnectionCallback(
|
server.setOnConnectionCallback(
|
||||||
[remoteUrl, remoteUrlsMapping](std::weak_ptr<ix::WebSocket> webSocket,
|
[remoteUrl, remoteUrlsMapping](std::weak_ptr<ix::WebSocket> webSocket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState) {
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) {
|
|
||||||
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
|
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
|
||||||
auto remoteIp = connectionInfo->remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
|
|
||||||
// Server connection
|
// Server connection
|
||||||
state->webSocket().setOnMessageCallback(
|
state->webSocket().setOnMessageCallback(
|
||||||
|
@ -77,15 +77,14 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketServer::handleConnection(std::unique_ptr<Socket> socket,
|
void WebSocketServer::handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)
|
|
||||||
{
|
{
|
||||||
setThreadName("WebSocketServer::" + connectionState->getId());
|
setThreadName("WebSocketServer::" + connectionState->getId());
|
||||||
|
|
||||||
auto webSocket = std::make_shared<WebSocket>();
|
auto webSocket = std::make_shared<WebSocket>();
|
||||||
if (_onConnectionCallback)
|
if (_onConnectionCallback)
|
||||||
{
|
{
|
||||||
_onConnectionCallback(webSocket, connectionState, std::move(connectionInfo));
|
_onConnectionCallback(webSocket, connectionState);
|
||||||
|
|
||||||
if (!webSocket->isOnMessageCallbackRegistered())
|
if (!webSocket->isOnMessageCallbackRegistered())
|
||||||
{
|
{
|
||||||
@ -99,9 +98,8 @@ namespace ix
|
|||||||
else if (_onClientMessageCallback)
|
else if (_onClientMessageCallback)
|
||||||
{
|
{
|
||||||
webSocket->setOnMessageCallback(
|
webSocket->setOnMessageCallback(
|
||||||
[this, &ws = *webSocket.get(), connectionState, &ci = *connectionInfo.get()](
|
[this, &ws = *webSocket.get(), connectionState](const WebSocketMessagePtr& msg) {
|
||||||
const WebSocketMessagePtr& msg) {
|
_onClientMessageCallback(connectionState, ws, msg);
|
||||||
_onClientMessageCallback(connectionState, ci, ws, msg);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -23,14 +23,10 @@ namespace ix
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using OnConnectionCallback =
|
using OnConnectionCallback =
|
||||||
std::function<void(std::weak_ptr<WebSocket>,
|
std::function<void(std::weak_ptr<WebSocket>, std::shared_ptr<ConnectionState>)>;
|
||||||
std::shared_ptr<ConnectionState>,
|
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo)>;
|
|
||||||
|
|
||||||
using OnClientMessageCallback = std::function<void(std::shared_ptr<ConnectionState>,
|
using OnClientMessageCallback = std::function<void(
|
||||||
ConnectionInfo&,
|
std::shared_ptr<ConnectionState>, WebSocket&, const WebSocketMessagePtr&)>;
|
||||||
WebSocket&,
|
|
||||||
const WebSocketMessagePtr&)>;
|
|
||||||
|
|
||||||
WebSocketServer(int port = SocketServer::kDefaultPort,
|
WebSocketServer(int port = SocketServer::kDefaultPort,
|
||||||
const std::string& host = SocketServer::kDefaultHost,
|
const std::string& host = SocketServer::kDefaultHost,
|
||||||
@ -69,8 +65,7 @@ namespace ix
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
virtual void handleConnection(std::unique_ptr<Socket> socket,
|
virtual void handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState);
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo);
|
|
||||||
virtual size_t getConnectedClientsCount() final;
|
virtual size_t getConnectedClientsCount() final;
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -6,4 +6,4 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define IX_WEBSOCKET_VERSION "10.3.0"
|
#define IX_WEBSOCKET_VERSION "10.3.1"
|
||||||
|
@ -95,15 +95,14 @@ TEST_CASE("Cobra_to_sentry_bot", "[cobra_bots]")
|
|||||||
|
|
||||||
sentryServer.setOnConnectionCallback(
|
sentryServer.setOnConnectionCallback(
|
||||||
[](HttpRequestPtr request,
|
[](HttpRequestPtr request,
|
||||||
std::shared_ptr<ConnectionState> /*connectionState*/,
|
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
|
||||||
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr {
|
|
||||||
WebSocketHttpHeaders headers;
|
WebSocketHttpHeaders headers;
|
||||||
headers["Server"] = userAgent();
|
headers["Server"] = userAgent();
|
||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " "
|
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
|
||||||
<< request->method << " " << request->headers["User-Agent"] << " "
|
<< " " << request->method << " " << request->headers["User-Agent"] << " "
|
||||||
<< request->uri;
|
<< request->uri;
|
||||||
|
|
||||||
if (request->method == "POST")
|
if (request->method == "POST")
|
||||||
|
@ -85,11 +85,10 @@ namespace ix
|
|||||||
bool startWebSocketEchoServer(ix::WebSocketServer& server)
|
bool startWebSocketEchoServer(ix::WebSocketServer& server)
|
||||||
{
|
{
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server](std::shared_ptr<ConnectionState> /*connectionState*/,
|
[&server](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
TLogger() << "New connection";
|
TLogger() << "New connection";
|
||||||
|
@ -191,11 +191,9 @@ namespace
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
|
[&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
|
|
||||||
|
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
|
@ -195,10 +195,9 @@ namespace
|
|||||||
{
|
{
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server](std::shared_ptr<ConnectionState> connectionState,
|
[&server](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
TLogger() << "New connection";
|
TLogger() << "New connection";
|
||||||
|
@ -171,10 +171,9 @@ namespace
|
|||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite](
|
[&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite](
|
||||||
std::shared_ptr<ConnectionState> connectionState,
|
std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& /*webSocket*/,
|
WebSocket& /*webSocket*/,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
TLogger() << "New server connection";
|
TLogger() << "New server connection";
|
||||||
|
@ -35,11 +35,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
|
[&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
|
|
||||||
|
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
|
@ -18,10 +18,9 @@ bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
|
|||||||
{
|
{
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server, &subProtocols](std::shared_ptr<ConnectionState> connectionState,
|
[&server, &subProtocols](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const ix::WebSocketMessagePtr& msg) {
|
const ix::WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
TLogger() << "New connection";
|
TLogger() << "New connection";
|
||||||
|
12
ws/ws.cpp
12
ws/ws.cpp
@ -412,10 +412,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server](std::shared_ptr<ConnectionState> connectionState,
|
[&server](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const WebSocketMessagePtr& msg) {
|
const WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
spdlog::info("New connection");
|
spdlog::info("New connection");
|
||||||
@ -1240,10 +1239,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[greetings](std::shared_ptr<ConnectionState> connectionState,
|
[greetings](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const WebSocketMessagePtr& msg) {
|
const WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
spdlog::info("New connection");
|
spdlog::info("New connection");
|
||||||
@ -1674,10 +1672,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[greetings, &sendMsg](std::shared_ptr<ConnectionState> connectionState,
|
[greetings, &sendMsg](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const WebSocketMessagePtr& msg) {
|
const WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
spdlog::info("New connection");
|
spdlog::info("New connection");
|
||||||
@ -2613,10 +2610,9 @@ namespace ix
|
|||||||
|
|
||||||
server.setOnClientMessageCallback(
|
server.setOnClientMessageCallback(
|
||||||
[&server](std::shared_ptr<ConnectionState> connectionState,
|
[&server](std::shared_ptr<ConnectionState> connectionState,
|
||||||
ConnectionInfo& connectionInfo,
|
|
||||||
WebSocket& webSocket,
|
WebSocket& webSocket,
|
||||||
const WebSocketMessagePtr& msg) {
|
const WebSocketMessagePtr& msg) {
|
||||||
auto remoteIp = connectionInfo.remoteIp;
|
auto remoteIp = connectionState->getRemoteIp();
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
spdlog::info("ws_transfer: New connection");
|
spdlog::info("ws_transfer: New connection");
|
||||||
|
Loading…
Reference in New Issue
Block a user