Skip to content

Commit 58e1aec

Browse files
committed
Updating public API
1 parent b7d8137 commit 58e1aec

File tree

5 files changed

+106
-86
lines changed

5 files changed

+106
-86
lines changed

Test/main.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ using namespace std::chrono_literals;
1212

1313
void wssautobahntest() {
1414
// auto listener = SL::WS_LITE::WSListener::CreateListener(3001, TEST_CERTIFICATE_PRIVATE_PASSWORD, TEST_CERTIFICATE_PRIVATE_PATH, TEST_CERTIFICATE_PUBLIC_PATH, TEST_DH_PATH);
15-
SL::WS_LITE::ThreadCount thrdcount(1);
15+
1616
SL::WS_LITE::PortNumber port(3001);
17-
auto listener = SL::WS_LITE::WSListener::CreateListener(thrdcount, port);
17+
SL::WS_LITE::WSContext ctx(SL::WS_LITE::ThreadCount(1));
18+
19+
auto listener = ctx.CreateListener(port);
1820
listener.set_ReadTimeout(std::chrono::seconds(100));
1921
listener.set_WriteTimeout(std::chrono::seconds(100));
2022
auto lastheard = std::chrono::high_resolution_clock::now();
@@ -49,9 +51,10 @@ void wssautobahntest() {
4951
void generaltest() {
5052
std::cout << "Starting General test..." << std::endl;
5153
//auto listener = SL::WS_LITE::WSListener::CreateListener(3002, TEST_CERTIFICATE_PRIVATE_PASSWORD, TEST_CERTIFICATE_PRIVATE_PATH, TEST_CERTIFICATE_PUBLIC_PATH, TEST_DH_PATH);
52-
SL::WS_LITE::ThreadCount thrdcount(1);
54+
5355
SL::WS_LITE::PortNumber port(3002);
54-
auto listener = SL::WS_LITE::WSListener::CreateListener(thrdcount, port);
56+
SL::WS_LITE::WSContext ctx(SL::WS_LITE::ThreadCount(1));
57+
auto listener = ctx.CreateListener(port);
5558
auto lastheard = std::chrono::high_resolution_clock::now();
5659
listener.onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
5760
lastheard = std::chrono::high_resolution_clock::now();
@@ -80,7 +83,8 @@ void generaltest() {
8083
listener.startlistening();
8184

8285
//auto client = SL::WS_LITE::WSClient::CreateClient(TEST_CERTIFICATE_PUBLIC_PATH);
83-
auto client = SL::WS_LITE::WSClient::CreateClient(thrdcount);
86+
87+
auto client = ctx.CreateClient();
8488
client.onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
8589
lastheard = std::chrono::high_resolution_clock::now();
8690
SL_WS_LITE_LOG(SL::WS_LITE::Logging_Levels::INFO_log_level, "Client::onHttpUpgrade");
@@ -102,9 +106,11 @@ void generaltest() {
102106
}
103107
void multithreadtest() {
104108
std::cout << "Starting Multi threaded test..." << std::endl;
105-
SL::WS_LITE::ThreadCount thrdcount(4);
109+
106110
SL::WS_LITE::PortNumber port(3003);
107-
auto listener = SL::WS_LITE::WSListener::CreateListener(thrdcount, port);
111+
SL::WS_LITE::WSContext ctx(SL::WS_LITE::ThreadCount(4));
112+
113+
auto listener = ctx.CreateListener(port);
108114
auto lastheard = std::chrono::high_resolution_clock::now();
109115
listener.onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
110116
lastheard = std::chrono::high_resolution_clock::now();
@@ -127,9 +133,10 @@ void multithreadtest() {
127133
});
128134
listener.startlistening();
129135
std::vector<SL::WS_LITE::WSClient> clients;
130-
clients.reserve(25);
131-
for (auto i = 0; i < 25; i++) {
132-
clients.push_back(SL::WS_LITE::WSClient::CreateClient(thrdcount));
136+
clients.reserve(100);
137+
for (auto i = 0; i < 100; i++) {
138+
139+
clients.push_back(ctx.CreateClient());
133140
clients[i].onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
134141
lastheard = std::chrono::high_resolution_clock::now();
135142
});

include/WS_Lite.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ namespace SL {
5656
std::shared_ptr<unsigned char> Buffer;
5757
};
5858

59-
60-
61-
class WSListener;
62-
class WSClient;
59+
6360
struct WSocketImpl;
6461
struct WSocket {
6562
std::shared_ptr<WSocketImpl> WSocketImpl_;
@@ -76,6 +73,7 @@ namespace SL {
7673
operator bool() const { return WSocketImpl_.operator bool(); }
7774

7875
};
76+
class WSContext;
7977
class WSListenerImpl;
8078
class WSListener {
8179
std::shared_ptr<WSListenerImpl> Impl_;
@@ -122,16 +120,7 @@ namespace SL {
122120
void close(const WSocket& s, unsigned short code = 1000, const std::string& msg = "");
123121
//start the process to listen for clients. This is non-blocking and will return immediatly
124122
void startlistening();
125-
//factory to create listeners. Use this if you ARE NOT using TLS
126-
static WSListener CreateListener(ThreadCount threadcount, PortNumber port);
127-
//factory to create listeners. Use this if you ARE using TLS
128-
static WSListener CreateListener(
129-
ThreadCount threadcount,
130-
PortNumber port,
131-
std::string Password,
132-
std::string Privatekey_File,
133-
std::string Publiccertificate_File,
134-
std::string dh_File);
123+
friend WSContext;
135124
};
136125
class WSClientImpl;
137126
class WSClient {
@@ -179,10 +168,28 @@ namespace SL {
179168
void close(const WSocket& s, unsigned short code = 1000, const std::string& msg = "");
180169
//connect to an endpoint. This is non-blocking and will return immediatly. If the library is unable to establish a connection, ondisconnection will be called.
181170
void connect(const char* host, PortNumber port);
171+
friend WSContext;
172+
};
173+
class WSContextImpl;
174+
class WSContext {
175+
std::shared_ptr<WSContextImpl> Impl_;
176+
public:
177+
WSContext(ThreadCount threadcount);
178+
179+
//Use this if you ARE NOT using TLS
180+
WSListener CreateListener(PortNumber port);
181+
//Use this if you ARE using TLS
182+
WSListener CreateListener(
183+
PortNumber port,
184+
std::string Password,
185+
std::string Privatekey_File,
186+
std::string Publiccertificate_File,
187+
std::string dh_File);
182188
//factory to create clients. Use this if you ARE NOT using TLS
183-
static WSClient CreateClient(ThreadCount threadcount);
189+
WSClient CreateClient();
184190
//factory to create clients. Use this if you ARE using TLS
185-
static WSClient CreateClient(ThreadCount threadcount,std::string Publiccertificate_File);
191+
WSClient CreateClient(std::string Publiccertificate_File);
192+
186193
};
187194
}
188195
}

include/internal/WebSocketProtocol.h

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace SL {
9595
if (!ec) return rt.address().is_loopback();
9696
else return true;
9797
}
98-
struct SendQueueItem {
98+
struct SendQueueItem {
9999
WSMessage msg;
100100
bool compressmessage;
101101
};
@@ -133,18 +133,16 @@ namespace SL {
133133
ws->Socket = s;
134134
}
135135

136-
136+
137137
struct ThreadContext {
138138
std::unique_ptr<char[]> inflationBuffer;
139139
z_stream inflationStream = {};
140140
std::thread Thread;
141141
};
142142
const auto CONTROLBUFFERMAXSIZE = 125;
143-
class WSContext {
143+
class WSContextImpl {
144144
public:
145-
WSContext(ThreadCount threadcount) :
146-
work(std::make_unique<asio::io_service::work>(io_service)) ,
147-
sslcontext(asio::ssl::context::tlsv11){
145+
WSContextImpl(ThreadCount threadcount) : work(std::make_unique<asio::io_service::work>(io_service)) {
148146
Threads.resize(threadcount.value);
149147
for (auto& ctx : Threads) {
150148
inflateInit2(&ctx.inflationStream, -MAX_WBITS);
@@ -155,7 +153,7 @@ namespace SL {
155153
});
156154
}
157155
}
158-
~WSContext() {
156+
~WSContextImpl() {
159157
work.reset();
160158
io_service.stop();
161159
while (!io_service.stopped()) {
@@ -169,13 +167,20 @@ namespace SL {
169167
}
170168
Threads.clear();
171169
}
172-
std::chrono::seconds ReadTimeout = std::chrono::seconds(30);
173-
std::chrono::seconds WriteTimeout = std::chrono::seconds(30);
174-
size_t MaxPayload = 1024 * 1024 * 20;//20 MB
170+
175171
asio::io_service io_service;
176172
std::vector<ThreadContext> Threads;
177173
std::unique_ptr<asio::io_service::work> work;
174+
175+
};
176+
177+
struct WSInternal {
178+
WSInternal(std::shared_ptr<WSContextImpl>& p) :sslcontext(asio::ssl::context::tlsv11), WSContextImpl_(p) {}
179+
std::shared_ptr<WSContextImpl> WSContextImpl_;
178180
asio::ssl::context sslcontext;
181+
std::chrono::seconds ReadTimeout = std::chrono::seconds(30);
182+
std::chrono::seconds WriteTimeout = std::chrono::seconds(30);
183+
size_t MaxPayload = 1024 * 1024 * 20;//20 MB
179184
bool TLSEnabled = false;
180185

181186
std::function<void(WSocket&, const std::unordered_map<std::string, std::string>&)> onConnection;
@@ -186,10 +191,9 @@ namespace SL {
186191
std::function<void(WSocket&)> onHttpUpgrade;
187192

188193
};
189-
190-
class WSClientImpl : public WSContext {
194+
class WSClientImpl : public WSInternal {
191195
public:
192-
WSClientImpl(ThreadCount threadcount, std::string Publiccertificate_File) : WSClientImpl(threadcount)
196+
WSClientImpl(std::shared_ptr<WSContextImpl>& p, std::string Publiccertificate_File) : WSClientImpl(p)
193197
{
194198
TLSEnabled = true;
195199
std::ifstream file(Publiccertificate_File, std::ios::binary);
@@ -204,23 +208,25 @@ namespace SL {
204208
sslcontext.set_default_verify_paths(ec);
205209

206210
}
207-
WSClientImpl(ThreadCount threadcount) :WSContext(threadcount) { }
211+
WSClientImpl(std::shared_ptr<WSContextImpl>& p) :WSInternal(p)
212+
{
213+
}
208214
~WSClientImpl() {}
209215
};
210216

211-
class WSListenerImpl : public WSContext {
217+
class WSListenerImpl : public WSInternal {
212218
public:
213219

214220
asio::ip::tcp::acceptor acceptor;
215221

216222
WSListenerImpl(
217-
ThreadCount threadcount,
223+
std::shared_ptr<WSContextImpl>& p,
218224
PortNumber port,
219225
std::string Password,
220226
std::string Privatekey_File,
221227
std::string Publiccertificate_File,
222228
std::string dh_File) :
223-
WSListenerImpl(threadcount, port)
229+
WSListenerImpl(p, port)
224230
{
225231
TLSEnabled = true;
226232
sslcontext.set_options(
@@ -256,11 +262,9 @@ namespace SL {
256262

257263
}
258264

259-
WSListenerImpl(
260-
ThreadCount threadcount,
261-
PortNumber port) :
262-
WSContext(threadcount),
263-
acceptor(io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port.value)){
265+
WSListenerImpl(std::shared_ptr<WSContextImpl>& p, PortNumber port) :
266+
acceptor(p->io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port.value)) ,
267+
WSInternal(p) {
264268

265269
}
266270

@@ -320,7 +324,7 @@ namespace SL {
320324
}
321325
websocket->strand.post([websocket, msg, parent, compressmessage]() {
322326
websocket->SendMessageQueue.emplace_back(SendQueueItem{ msg, compressmessage });
323-
if (websocket->SendMessageQueue.size()==1) {
327+
if (websocket->SendMessageQueue.size() == 1) {
324328
SL::WS_LITE::startwrite(parent, websocket);
325329
}
326330
});
@@ -638,7 +642,7 @@ namespace SL {
638642
return closeImpl(parent, websocket, 1002, "Closing connection because mask requirement not met");
639643
}
640644

641-
if (getrsv2(websocket->ReceiveHeader) || getrsv3(websocket->ReceiveHeader) ||(getrsv1(websocket->ReceiveHeader) && !websocket->CompressionEnabled)) {
645+
if (getrsv2(websocket->ReceiveHeader) || getrsv3(websocket->ReceiveHeader) || (getrsv1(websocket->ReceiveHeader) && !websocket->CompressionEnabled)) {
642646
return closeImpl(parent, websocket, 1002, "Closing connection. rsv bit set");
643647
}
644648

src/internal/ClientImpl.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace SL {
5959

6060

6161
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Connected ");
62-
auto websocket = std::make_shared<WSocketImpl>(self->io_service);
62+
auto websocket = std::make_shared<WSocketImpl>(self->WSContextImpl_->io_service);
6363
if (header.find(PERMESSAGEDEFLATE) != header.end()) {
6464
websocket->CompressionEnabled = true;
6565
}
@@ -129,7 +129,7 @@ namespace SL {
129129

130130
auto socket = socketcreator(self);
131131
std::error_code ec;
132-
asio::ip::tcp::resolver resolver(self->io_service);
132+
asio::ip::tcp::resolver resolver(self->WSContextImpl_->io_service);
133133
auto portstr = std::to_string(port.value);
134134
asio::ip::tcp::resolver::query query(host, portstr.c_str());
135135

@@ -169,20 +169,10 @@ namespace SL {
169169

170170
}
171171

172-
WSClient WSClient::CreateClient(ThreadCount threadcount, std::string Publiccertificate_File) {
173-
WSClient c;
174-
c.Impl_ = std::make_shared<WSClientImpl>(threadcount, Publiccertificate_File);
175-
return c;
176-
}
177-
WSClient WSClient::CreateClient(ThreadCount threadcount) {
178-
WSClient c;
179-
c.Impl_ = std::make_shared<WSClientImpl>(threadcount);
180-
return c;
181-
}
182172
void WSClient::connect(const char* host, PortNumber port) {
183173
if (Impl_->TLSEnabled) {
184174
auto createsocket = [](auto c) {
185-
auto socket = std::make_shared<asio::ssl::stream<asio::ip::tcp::socket>>(c->io_service, c->sslcontext);
175+
auto socket = std::make_shared<asio::ssl::stream<asio::ip::tcp::socket>>(c->WSContextImpl_->io_service, c->sslcontext);
186176
socket->set_verify_mode(asio::ssl::verify_peer);
187177
socket->set_verify_callback(std::bind(&verify_certificate, std::placeholders::_1, std::placeholders::_2));
188178
return socket;
@@ -192,7 +182,7 @@ namespace SL {
192182
}
193183
else {
194184
auto createsocket = [](auto c) {
195-
return std::make_shared<asio::ip::tcp::socket>(c->io_service);
185+
return std::make_shared<asio::ip::tcp::socket>(c->WSContextImpl_->io_service);
196186
};
197187
Connect(Impl_, host, port, createsocket);
198188
}
@@ -288,5 +278,15 @@ namespace SL {
288278
if (WSocketImpl_->Socket) return SL::WS_LITE::is_loopback(WSocketImpl_->Socket);
289279
else return SL::WS_LITE::is_loopback(WSocketImpl_->TLSSocket);
290280
}
281+
WSClient WSContext::CreateClient(std::string Publiccertificate_File) {
282+
WSClient c;
283+
c.Impl_ = std::make_shared<WSClientImpl>(Impl_, Publiccertificate_File);
284+
return c;
285+
}
286+
WSClient WSContext::CreateClient() {
287+
WSClient c;
288+
c.Impl_ = std::make_shared<WSClientImpl>(Impl_);
289+
return c;
290+
}
291291
}
292292
}

0 commit comments

Comments
 (0)