@@ -35,10 +35,6 @@ namespace curan
3535 {
3636 }
3737
38- Server (asio::io_context &io_context, unsigned short port, std::function<bool (std::error_code ec)> connection_callback) : _cxt{io_context}, acceptor_{_cxt, asio::ip::tcp::endpoint (asio::ip::tcp::v4 (), port)}
39- {
40- }
41-
4238 public:
4339 static inline std::shared_ptr<Server<protocol>> make (asio::io_context &io_context, unsigned short port)
4440 {
@@ -55,7 +51,7 @@ namespace curan
5551 */
5652 static inline std::shared_ptr<Server<protocol>> make (asio::io_context &io_context, unsigned short port, std::function<bool (std::error_code ec)> connection_callback)
5753 {
58- std::shared_ptr<Server<protocol>> server = std::shared_ptr<Server<protocol>>(new Server{io_context, port, connection_callback });
54+ std::shared_ptr<Server<protocol>> server = std::shared_ptr<Server<protocol>>(new Server{io_context, port});
5955 server->accept (connection_callback);
6056 return server;
6157 }
@@ -98,17 +94,20 @@ namespace curan
9894 acceptor_.cancel ();
9995 }
10096
101- void write (std::shared_ptr<utilities::MemoryBuffer> buffer)
97+ bool write (std::shared_ptr<utilities::MemoryBuffer> buffer)
10298 {
10399 std::lock_guard<std::mutex> g{mut};
100+ bool send_info = false ;
104101 if (list_of_clients.size () == 0 )
105- return ;
106- list_of_clients.remove_if ([buffer](std::shared_ptr<Client<protocol>> &client)
107- {
108- if (!client->get_socket ().sendable ())
109- return true ;
110- client->write (buffer);
111- return false ; });
102+ return send_info;
103+
104+ list_of_clients.remove_if ([buffer,&send_info](std::shared_ptr<Client<protocol>> &client){
105+ if (!client->get_socket ().sendable ())
106+ return true ;
107+ client->write (buffer);
108+ send_info = true ;
109+ return false ; });
110+ return send_info;
112111 }
113112
114113 inline asio::io_context &get_context ()
@@ -129,10 +128,8 @@ namespace curan
129128 for (auto &submitted_callables : callables)
130129 client_ptr->connect (submitted_callables);
131130 list_of_clients.push_back (client_ptr);
132-
133131 }
134- else
135- {
132+ else { // what else to do if the user did not provide any callback mechanism?
136133 throw std::runtime_error (" cannot wait for client" );
137134 }
138135 accept ();
@@ -156,12 +153,8 @@ namespace curan
156153 else
157154 client_ptr->get_socket ().close ();
158155 }
159- else
160- {
161- connection_callback (ec);
162- }
163-
164- accept (connection_callback);
156+ else if (connection_callback (ec))
157+ accept (connection_callback);
165158 });
166159 }
167160 };
0 commit comments