Skip to content

Commit 73b9c0b

Browse files
committed
(socket servers) merge the ConnectionInfo class with the ConnectionState one, which simplify all the server apis
1 parent 629c155 commit 73b9c0b

25 files changed

+97
-123
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ set( IXWEBSOCKET_SOURCES
6262
set( IXWEBSOCKET_HEADERS
6363
ixwebsocket/IXBench.h
6464
ixwebsocket/IXCancellationRequest.h
65-
ixwebsocket/IXConnectionInfo.h
6665
ixwebsocket/IXConnectionState.h
6766
ixwebsocket/IXDNSLookup.h
6867
ixwebsocket/IXExponentialBackoff.h

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All changes to this project will be documented in this file.
44

5+
## [10.3.1] - 2020-08-28
6+
7+
(socket servers) merge the ConnectionInfo class with the ConnectionState one, which simplify all the server apis
8+
59
## [10.3.0] - 2020-08-26
610

711
(ws) set the main thread name, to help with debugging in XCode, gdb, lldb etc...

docs/usage.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,9 @@ ix::WebSocketServer server(port);
280280

281281
server.setOnConnectionCallback(
282282
[&server](std::weak_ptr<WebSocket> webSocket,
283-
std::shared_ptr<ConnectionState> connectionState,
284-
std::unique_ptr<ConnectionInfo> connectionInfo)
283+
std::shared_ptr<ConnectionState> connectionState)
285284
{
286-
std::cout << "Remote ip: " << connectionInfo->remoteIp << std::endl;
285+
std::cout << "Remote ip: " << connectionState->remoteIp << std::endl;
287286

288287
auto ws = webSocket.lock();
289288
if (ws)
@@ -359,13 +358,12 @@ The webSocket reference is guaranteed to be always valid ; by design the callbac
359358
ix::WebSocketServer server(port);
360359

361360
server.setOnClientMessageCallback(std::shared_ptr<ConnectionState> connectionState,
362-
ConnectionInfo& connectionInfo,
363361
WebSocket& webSocket,
364362
const WebSocketMessagePtr& msg)
365363
{
366-
// The ConnectionInfo object contains information about the connection,
364+
// The ConnectionState object contains information about the connection,
367365
// at this point only the client ip address and the port.
368-
std::cout << "Remote ip: " << connectionInfo.remoteIp << std::endl;
366+
std::cout << "Remote ip: " << connectionState->getRemoteIp();
369367

370368
if (msg->type == ix::WebSocketMessageType::Open)
371369
{
@@ -519,12 +517,11 @@ If you want to handle how requests are processed, implement the setOnConnectionC
519517
```cpp
520518
setOnConnectionCallback(
521519
[this](HttpRequestPtr request,
522-
std::shared_ptr<ConnectionState> /*connectionState*/,
523-
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr
520+
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr
524521
{
525522
// Build a string for the response
526523
std::stringstream ss;
527-
ss << connectionInfo->remoteIp
524+
ss << connectionState->getRemoteIp();
528525
<< " "
529526
<< request->method
530527
<< " "

ixredis/ixredis/IXRedisServer.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ namespace ix
4545
}
4646

4747
void RedisServer::handleConnection(std::unique_ptr<Socket> socket,
48-
std::shared_ptr<ConnectionState> connectionState,
49-
std::unique_ptr<ConnectionInfo> connectionInfo)
48+
std::shared_ptr<ConnectionState> connectionState)
5049
{
51-
logInfo("New connection from remote ip " + connectionInfo->remoteIp);
50+
logInfo("New connection from remote ip " + connectionState->getRemoteIp());
5251

5352
_connectedClientsCount++;
5453

ixredis/ixredis/IXRedisServer.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ namespace ix
4444

4545
// Methods
4646
virtual void handleConnection(std::unique_ptr<Socket>,
47-
std::shared_ptr<ConnectionState> connectionState,
48-
std::unique_ptr<ConnectionInfo> connectionInfo) final;
47+
std::shared_ptr<ConnectionState> connectionState) final;
4948
virtual size_t getConnectedClientsCount() final;
5049

5150
bool startsWith(const std::string& str, const std::string& start);

ixsnake/ixsnake/IXSnakeServer.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ namespace snake
6161

6262
_server.setOnClientMessageCallback(
6363
[this](std::shared_ptr<ix::ConnectionState> connectionState,
64-
ix::ConnectionInfo& connectionInfo,
6564
ix::WebSocket& webSocket,
6665
const ix::WebSocketMessagePtr& msg) {
6766
auto state = std::dynamic_pointer_cast<SnakeConnectionState>(connectionState);
68-
auto remoteIp = connectionInfo.remoteIp;
67+
auto remoteIp = connectionState->getRemoteIp();
6968

7069
std::stringstream ss;
7170
ss << "[" << state->getId() << "] ";

ixwebsocket/IXConnectionInfo.h

-25
This file was deleted.

ixwebsocket/IXConnectionState.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ namespace ix
5050
_onSetTerminatedCallback();
5151
}
5252
}
53+
54+
const std::string& ConnectionState::getRemoteIp()
55+
{
56+
return _remoteIp;
57+
}
58+
59+
int ConnectionState::getRemotePort()
60+
{
61+
return _remotePort;
62+
}
63+
64+
void ConnectionState::setRemoteIp(const std::string& remoteIp)
65+
{
66+
_remoteIp = remoteIp;
67+
}
68+
69+
void ConnectionState::setRemotePort(int remotePort)
70+
{
71+
_remotePort = remotePort;
72+
}
5373
} // namespace ix

ixwebsocket/IXConnectionState.h

+9
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@ namespace ix
2828
void setTerminated();
2929
bool isTerminated() const;
3030

31+
const std::string& getRemoteIp();
32+
int getRemotePort();
33+
3134
static std::shared_ptr<ConnectionState> createConnectionState();
3235

3336
private:
3437
void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback);
3538

39+
void setRemoteIp(const std::string& remoteIp);
40+
void setRemotePort(int remotePort);
41+
3642
protected:
3743
std::atomic<bool> _terminated;
3844
std::string _id;
3945
OnSetTerminatedCallback _onSetTerminatedCallback;
4046

4147
static std::atomic<uint64_t> _globalId;
4248

49+
std::string _remoteIp;
50+
int _remotePort;
51+
4352
friend class SocketServer;
4453
};
4554
} // namespace ix

ixwebsocket/IXHttpServer.cpp

+10-13
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ namespace ix
120120
}
121121

122122
void HttpServer::handleConnection(std::unique_ptr<Socket> socket,
123-
std::shared_ptr<ConnectionState> connectionState,
124-
std::unique_ptr<ConnectionInfo> connectionInfo)
123+
std::shared_ptr<ConnectionState> connectionState)
125124
{
126125
_connectedClientsCount++;
127126

@@ -130,8 +129,7 @@ namespace ix
130129

131130
if (std::get<0>(ret))
132131
{
133-
auto response =
134-
_onConnectionCallback(std::get<2>(ret), connectionState, std::move(connectionInfo));
132+
auto response = _onConnectionCallback(std::get<2>(ret), connectionState);
135133
if (!Http::sendResponse(response, socket))
136134
{
137135
logError("Cannot send response");
@@ -151,8 +149,7 @@ namespace ix
151149
{
152150
setOnConnectionCallback(
153151
[this](HttpRequestPtr request,
154-
std::shared_ptr<ConnectionState> /*connectionState*/,
155-
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr {
152+
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
156153
std::string uri(request->uri);
157154
if (uri.empty() || uri == "/")
158155
{
@@ -184,8 +181,8 @@ namespace ix
184181

185182
// Log request
186183
std::stringstream ss;
187-
ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " "
188-
<< request->method << " " << request->headers["User-Agent"] << " "
184+
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
185+
<< " " << request->method << " " << request->headers["User-Agent"] << " "
189186
<< request->uri << " " << content.size();
190187
logInfo(ss.str());
191188

@@ -209,16 +206,16 @@ namespace ix
209206
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
210207
//
211208
setOnConnectionCallback(
212-
[this, redirectUrl](HttpRequestPtr request,
213-
std::shared_ptr<ConnectionState> /*connectionState*/,
214-
std::unique_ptr<ConnectionInfo> connectionInfo) -> HttpResponsePtr {
209+
[this,
210+
redirectUrl](HttpRequestPtr request,
211+
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
215212
WebSocketHttpHeaders headers;
216213
headers["Server"] = userAgent();
217214

218215
// Log request
219216
std::stringstream ss;
220-
ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " "
221-
<< request->method << " " << request->headers["User-Agent"] << " "
217+
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
218+
<< " " << request->method << " " << request->headers["User-Agent"] << " "
222219
<< request->uri;
223220
logInfo(ss.str());
224221

ixwebsocket/IXHttpServer.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ namespace ix
2323
{
2424
public:
2525
using OnConnectionCallback =
26-
std::function<HttpResponsePtr(HttpRequestPtr,
27-
std::shared_ptr<ConnectionState>,
28-
std::unique_ptr<ConnectionInfo> connectionInfo)>;
26+
std::function<HttpResponsePtr(HttpRequestPtr, std::shared_ptr<ConnectionState>)>;
2927

3028
HttpServer(int port = SocketServer::kDefaultPort,
3129
const std::string& host = SocketServer::kDefaultHost,
@@ -46,8 +44,7 @@ namespace ix
4644

4745
// Methods
4846
virtual void handleConnection(std::unique_ptr<Socket>,
49-
std::shared_ptr<ConnectionState> connectionState,
50-
std::unique_ptr<ConnectionInfo> connectionInfo) final;
47+
std::shared_ptr<ConnectionState> connectionState) final;
5148
virtual size_t getConnectedClientsCount() final;
5249

5350
void setDefaultConnectionCallback();

ixwebsocket/IXSocketServer.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,13 @@ namespace ix
332332
}
333333

334334
// Retrieve connection info, the ip address of the remote peer/client)
335-
std::unique_ptr<ConnectionInfo> connectionInfo;
335+
std::string remoteIp;
336+
int remotePort;
336337

337338
if (_addressFamily == AF_INET)
338339
{
339-
char remoteIp[INET_ADDRSTRLEN];
340-
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp, INET_ADDRSTRLEN) == nullptr)
340+
char remoteIp4[INET_ADDRSTRLEN];
341+
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
341342
{
342343
int err = Socket::getErrno();
343344
std::stringstream ss;
@@ -350,12 +351,13 @@ namespace ix
350351
continue;
351352
}
352353

353-
connectionInfo = std::make_unique<ConnectionInfo>(remoteIp, client.sin_port);
354+
remotePort = client.sin_port;
355+
remoteIp = remoteIp4;
354356
}
355357
else // AF_INET6
356358
{
357-
char remoteIp[INET6_ADDRSTRLEN];
358-
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp, INET6_ADDRSTRLEN) == nullptr)
359+
char remoteIp6[INET6_ADDRSTRLEN];
360+
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) == nullptr)
359361
{
360362
int err = Socket::getErrno();
361363
std::stringstream ss;
@@ -368,7 +370,8 @@ namespace ix
368370
continue;
369371
}
370372

371-
connectionInfo = std::make_unique<ConnectionInfo>(remoteIp, client.sin_port);
373+
remotePort = client.sin_port;
374+
remoteIp = remoteIp6;
372375
}
373376

374377
std::shared_ptr<ConnectionState> connectionState;
@@ -377,6 +380,8 @@ namespace ix
377380
connectionState = _connectionStateFactory();
378381
}
379382
connectionState->setOnSetTerminatedCallback([this] { onSetTerminatedCallback(); });
383+
connectionState->setRemoteIp(remoteIp);
384+
connectionState->setRemotePort(remotePort);
380385

381386
if (_stop) return;
382387

@@ -404,13 +409,10 @@ namespace ix
404409

405410
// Launch the handleConnection work asynchronously in its own thread.
406411
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
407-
_connectionsThreads.push_back(
408-
std::make_pair(connectionState,
409-
std::thread(&SocketServer::handleConnection,
410-
this,
411-
std::move(socket),
412-
connectionState,
413-
std::move(connectionInfo))));
412+
_connectionsThreads.push_back(std::make_pair(
413+
connectionState,
414+
std::thread(
415+
&SocketServer::handleConnection, this, std::move(socket), connectionState)));
414416
}
415417
}
416418

ixwebsocket/IXSocketServer.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#pragma once
88

9-
#include "IXConnectionInfo.h"
109
#include "IXConnectionState.h"
1110
#include "IXSelectInterrupt.h"
1211
#include "IXSocketTLSOptions.h"
@@ -105,8 +104,7 @@ namespace ix
105104
ConnectionStateFactory _connectionStateFactory;
106105

107106
virtual void handleConnection(std::unique_ptr<Socket>,
108-
std::shared_ptr<ConnectionState> connectionState,
109-
std::unique_ptr<ConnectionInfo> connectionInfo) = 0;
107+
std::shared_ptr<ConnectionState> connectionState) = 0;
110108
virtual size_t getConnectedClientsCount() = 0;
111109

112110
// Returns true if all connection threads are joined

ixwebsocket/IXWebSocketProxyServer.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ namespace ix
5656

5757
server.setOnConnectionCallback(
5858
[remoteUrl, remoteUrlsMapping](std::weak_ptr<ix::WebSocket> webSocket,
59-
std::shared_ptr<ConnectionState> connectionState,
60-
std::unique_ptr<ConnectionInfo> connectionInfo) {
59+
std::shared_ptr<ConnectionState> connectionState) {
6160
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
62-
auto remoteIp = connectionInfo->remoteIp;
61+
auto remoteIp = connectionState->getRemoteIp();
6362

6463
// Server connection
6564
state->webSocket().setOnMessageCallback(

ixwebsocket/IXWebSocketServer.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ namespace ix
7777
}
7878

7979
void WebSocketServer::handleConnection(std::unique_ptr<Socket> socket,
80-
std::shared_ptr<ConnectionState> connectionState,
81-
std::unique_ptr<ConnectionInfo> connectionInfo)
80+
std::shared_ptr<ConnectionState> connectionState)
8281
{
8382
setThreadName("WebSocketServer::" + connectionState->getId());
8483

8584
auto webSocket = std::make_shared<WebSocket>();
8685
if (_onConnectionCallback)
8786
{
88-
_onConnectionCallback(webSocket, connectionState, std::move(connectionInfo));
87+
_onConnectionCallback(webSocket, connectionState);
8988

9089
if (!webSocket->isOnMessageCallbackRegistered())
9190
{
@@ -99,9 +98,8 @@ namespace ix
9998
else if (_onClientMessageCallback)
10099
{
101100
webSocket->setOnMessageCallback(
102-
[this, &ws = *webSocket.get(), connectionState, &ci = *connectionInfo.get()](
103-
const WebSocketMessagePtr& msg) {
104-
_onClientMessageCallback(connectionState, ci, ws, msg);
101+
[this, &ws = *webSocket.get(), connectionState](const WebSocketMessagePtr& msg) {
102+
_onClientMessageCallback(connectionState, ws, msg);
105103
});
106104
}
107105
else

0 commit comments

Comments
 (0)