Skip to content

Commit c67ae7f

Browse files
committed
added multithread support
1 parent f1f7728 commit c67ae7f

File tree

5 files changed

+127
-88
lines changed

5 files changed

+127
-88
lines changed

Test/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ using namespace std::chrono_literals;
1111

1212
void wssautobahntest() {
1313
// auto listener = SL::WS_LITE::WSListener::CreateListener(3001, TEST_CERTIFICATE_PRIVATE_PASSWORD, TEST_CERTIFICATE_PRIVATE_PATH, TEST_CERTIFICATE_PUBLIC_PATH, TEST_DH_PATH);
14-
auto listener = SL::WS_LITE::WSListener::CreateListener(3001);
14+
SL::WS_LITE::ThreadCount thrdcount(1);
15+
SL::WS_LITE::PortNumber port(3001);
16+
auto listener = SL::WS_LITE::WSListener::CreateListener(thrdcount, port);
1517
listener.set_ReadTimeout(100);
1618
listener.set_WriteTimeout(100);
1719
auto lastheard = std::chrono::high_resolution_clock::now();
@@ -46,7 +48,9 @@ void wssautobahntest() {
4648
void generaltest() {
4749
std::cout << "Starting General test..." << std::endl;
4850
//auto listener = SL::WS_LITE::WSListener::CreateListener(3002, TEST_CERTIFICATE_PRIVATE_PASSWORD, TEST_CERTIFICATE_PRIVATE_PATH, TEST_CERTIFICATE_PUBLIC_PATH, TEST_DH_PATH);
49-
auto listener = SL::WS_LITE::WSListener::CreateListener(3002);
51+
SL::WS_LITE::ThreadCount thrdcount(1);
52+
SL::WS_LITE::PortNumber port(3001);
53+
auto listener = SL::WS_LITE::WSListener::CreateListener(thrdcount, port);
5054
auto lastheard = std::chrono::high_resolution_clock::now();
5155
listener.onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
5256
lastheard = std::chrono::high_resolution_clock::now();
@@ -65,7 +69,7 @@ void generaltest() {
6569
listener.startlistening();
6670

6771
//auto client = SL::WS_LITE::WSClient::CreateClient(TEST_CERTIFICATE_PUBLIC_PATH);
68-
auto client = SL::WS_LITE::WSClient::CreateClient();
72+
auto client = SL::WS_LITE::WSClient::CreateClient(thrdcount);
6973
client.onHttpUpgrade([&](const SL::WS_LITE::WSocket& socket) {
7074
lastheard = std::chrono::high_resolution_clock::now();
7175
SL_WS_LITE_LOG(SL::WS_LITE::Logging_Levels::INFO_log_level, "Client::onHttpUpgrade");

include/WS_Lite.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66

77
namespace SL {
88
namespace WS_LITE {
9+
template <typename T, typename Meaning>
10+
struct Explicit
11+
{
12+
Explicit() { }
13+
Explicit(T value) : value(value) { }
14+
inline operator T () const { return value; }
15+
T value;
16+
};
17+
namespace INTERNAL {
18+
struct PorNumbertTag { };
19+
struct ThreadCountTag { };
20+
}
21+
22+
typedef Explicit<unsigned short, INTERNAL::PorNumbertTag> PortNumber;
23+
typedef Explicit<unsigned short, INTERNAL::ThreadCountTag> ThreadCount;
924

1025
const auto HTTP_METHOD = "Method";
1126
const auto HTTP_PATH = "Path";
@@ -47,7 +62,8 @@ namespace SL {
4762
struct WSocketImpl;
4863
struct WSocket {
4964
std::shared_ptr<WSocketImpl> WSocketImpl_;
50-
65+
WSocket(const std::shared_ptr<WSocketImpl>& s) :WSocketImpl_(s){}
66+
WSocket() {}
5167
//can be used to compare two WSocket objects
5268
bool operator=(const WSocket& s) { return s.WSocketImpl_ == WSocketImpl_; }
5369
bool is_open();
@@ -57,8 +73,7 @@ namespace SL {
5773
bool is_v6();
5874
bool is_loopback();
5975
operator bool() const { return WSocketImpl_.operator bool(); }
60-
friend WSListener;
61-
friend WSClient;
76+
6277
};
6378
class WSListenerImpl;
6479
class WSListener {
@@ -107,10 +122,11 @@ namespace SL {
107122
//start the process to listen for clients. This is non-blocking and will return immediatly
108123
void startlistening();
109124
//factory to create listeners. Use this if you ARE NOT using TLS
110-
static WSListener CreateListener(unsigned short port);
125+
static WSListener CreateListener(ThreadCount threadcount, PortNumber port);
111126
//factory to create listeners. Use this if you ARE using TLS
112127
static WSListener CreateListener(
113-
unsigned short port,
128+
ThreadCount threadcount,
129+
PortNumber port,
114130
std::string Password,
115131
std::string Privatekey_File,
116132
std::string Publiccertificate_File,
@@ -161,11 +177,11 @@ namespace SL {
161177
//send a close message and close the socket
162178
void close(const WSocket& s, unsigned short code = 1000, const std::string& msg = "");
163179
//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.
164-
void connect(const char* host, unsigned short port);
180+
void connect(const char* host, PortNumber port);
165181
//factory to create clients. Use this if you ARE NOT using TLS
166-
static WSClient CreateClient();
182+
static WSClient CreateClient(ThreadCount threadcount);
167183
//factory to create clients. Use this if you ARE using TLS
168-
static WSClient CreateClient(std::string Publiccertificate_File);
184+
static WSClient CreateClient(ThreadCount threadcount,std::string Publiccertificate_File);
169185
};
170186
}
171187
}

0 commit comments

Comments
 (0)