-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_server.cpp
More file actions
29 lines (28 loc) · 841 Bytes
/
Copy pathmessage_server.cpp
File metadata and controls
29 lines (28 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "server.h"
#include <memory>
class message_server {
public:
message_server() {};
void handle_message(boost::asio::ip::tcp::socket&& sock) {
std::cout<<"Received connect from client\n";
sock_ = std::make_unique<utils::socket_with_cb>(sock, [this](const char* buf, int l) {
process_message(buf, l);
});
const char* test_msg = "Hello this is server you are connected\n";
sock_->send_message(test_msg, strlen(test_msg));
}
void process_message(const char* msg, int len) {
std::cout<<"processing message, read "<<len<<" bytes\n";
}
private:
std::unique_ptr<utils::socket_with_cb> sock_;
};
int main() {
boost::asio::io_service ios;
message_server ms;
server sv{ios, "127.0.0.1", 8080, [&ms](auto&& sock) {
ms.handle_message(std::move(sock));
}};
ios.run();
return 0;
}