Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/marian-nmt/Simple-WebSocket-Server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreidheim <eidheim@gmail.com>2019-12-29 12:34:33 +0300
committereidheim <eidheim@gmail.com>2019-12-29 12:34:33 +0300
commit971295b200b95a01c66520fd00dd61154e0e1f21 (patch)
tree5786120f8329da172bef22fe0a75a01082ad52cf
parentd2836f2d4a6837388294917e8b48f253efd5a5bb (diff)
Added callback to start and accept_and_run
-rw-r--r--client_ws.hpp8
-rw-r--r--server_ws.hpp27
2 files changed, 24 insertions, 11 deletions
diff --git a/client_ws.hpp b/client_ws.hpp
index 05a5e5b..26c845e 100644
--- a/client_ws.hpp
+++ b/client_ws.hpp
@@ -301,7 +301,9 @@ namespace SimpleWeb {
std::function<void(std::shared_ptr<Connection>)> on_ping;
std::function<void(std::shared_ptr<Connection>)> on_pong;
- void start() {
+ /// Start the client.
+ /// The callback parameter is called after the server is accepting connections.
+ void start(std::function<void()> callback = nullptr) {
{
std::lock_guard<std::mutex> lock(start_stop_mutex);
@@ -316,10 +318,14 @@ namespace SimpleWeb {
connect();
+ if(callback)
+ io_service->post(std::move(callback));
+
if(internal_io_service)
io_service->run();
}
+ /// Stop client, and close current connection
void stop() noexcept {
std::lock_guard<std::mutex> lock(start_stop_mutex);
diff --git a/server_ws.hpp b/server_ws.hpp
index fceca12..2fa86ec 100644
--- a/server_ws.hpp
+++ b/server_ws.hpp
@@ -250,7 +250,7 @@ namespace SimpleWeb {
public:
/// fin_rsv_opcode: 129=one fragment, text, 130=one fragment, binary, 136=close connection.
/// See http://tools.ietf.org/html/rfc6455#section-5.2 for more information.
- void send(const std::shared_ptr<OutMessage> &out_message, const std::function<void(const error_code &)> &callback = nullptr, unsigned char fin_rsv_opcode = 129) {
+ void send(std::shared_ptr<OutMessage> out_message, std::function<void(const error_code &)> callback = nullptr, unsigned char fin_rsv_opcode = 129) {
cancel_timeout();
set_timeout();
@@ -278,7 +278,7 @@ namespace SimpleWeb {
out_header->put(static_cast<char>(length));
LockGuard lock(send_queue_mutex);
- send_queue.emplace_back(out_header, out_message, callback);
+ send_queue.emplace_back(std::move(out_header), std::move(out_message), std::move(callback));
if(send_queue.size() == 1)
send_from_queue();
}
@@ -286,13 +286,13 @@ namespace SimpleWeb {
/// Convenience function for sending a string.
/// fin_rsv_opcode: 129=one fragment, text, 130=one fragment, binary, 136=close connection.
/// See http://tools.ietf.org/html/rfc6455#section-5.2 for more information.
- void send(string_view out_message_str, const std::function<void(const error_code &)> &callback = nullptr, unsigned char fin_rsv_opcode = 129) {
+ void send(string_view out_message_str, std::function<void(const error_code &)> callback = nullptr, unsigned char fin_rsv_opcode = 129) {
auto out_message = std::make_shared<OutMessage>();
out_message->write(out_message_str.data(), static_cast<std::streamsize>(out_message_str.size()));
- send(out_message, callback, fin_rsv_opcode);
+ send(out_message, std::move(callback), fin_rsv_opcode);
}
- void send_close(int status, const std::string &reason = "", const std::function<void(const error_code &)> &callback = nullptr) {
+ void send_close(int status, const std::string &reason = "", std::function<void(const error_code &)> callback = nullptr) {
// Send close only once (in case close is initiated by server)
if(closed)
return;
@@ -306,7 +306,7 @@ namespace SimpleWeb {
*send_stream << reason;
// fin_rsv_opcode=136: message close
- send(send_stream, callback, 136);
+ send(std::move(send_stream), std::move(callback), 136);
}
};
@@ -410,7 +410,8 @@ namespace SimpleWeb {
/// If you know the server port in advance, use start() instead.
/// Accept requests, and if io_service was not set before calling bind(), run the internal io_service instead.
/// Call after bind().
- void accept_and_run() {
+ /// The callback parameter is called after the server is accepting connections.
+ void accept_and_run(std::function<void()> callback = nullptr) {
acceptor->listen();
accept();
@@ -418,6 +419,9 @@ namespace SimpleWeb {
if(io_service->stopped())
restart(*io_service);
+ if(callback)
+ io_service->post(std::move(callback));
+
// If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
threads.clear();
for(std::size_t c = 1; c < config.thread_pool_size; c++) {
@@ -434,12 +438,15 @@ namespace SimpleWeb {
for(auto &t : threads)
t.join();
}
+ else if(callback)
+ io_service->post(std::move(callback));
}
- /// Start the server by calling bind() and accept_and_run()
- void start() {
+ /// Start the server by calling bind() and accept_and_run().
+ /// The callback parameter is called after the server is accepting connections.
+ void start(std::function<void()> callback = nullptr) {
bind();
- accept_and_run();
+ accept_and_run(std::move(callback));
}
/// Stop accepting new connections, and close current connections