-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cc
84 lines (73 loc) · 2.4 KB
/
server.cc
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/TCPServerConnection.h>
#include <Poco/Net/TCPServerConnectionFactory.h>
#include <Poco/Net/TCPServerParams.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Exception.h>
#include <Poco/Util/ServerApplication.h>
#include <iostream>
#include <msgpack.hpp>
#include "p.hh"
using Poco::Net::ServerSocket;
using Poco::Net::StreamSocket;
using Poco::Net::TCPServerConnection;
using Poco::Net::TCPServerConnectionFactoryImpl;
using Poco::Net::TCPServer;
using Poco::Util::ServerApplication;
using Poco::Util::Application;
class MsgServerConnection: public TCPServerConnection {
public:
MsgServerConnection(const StreamSocket& s) : TCPServerConnection(s) {}
void run() {
Application& app = Application::instance();
app.logger().information("Request from " + this->socket().peerAddress().toString());
try {
size_t length;
socket().receiveBytes(&length, sizeof(length));
char* data = (char*)malloc(length);
socket().receiveBytes(data, length);
msgpack::unpacked msg;
msgpack::unpack(&msg, data, length);
msgpack::object obj = msg.get();
Arg a;
obj.convert(&a);
app.logger().information("content: " + a.name);
Result r;
r.count = strlen(a.name.c_str());
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, r);
length = sbuf.size();
socket().sendBytes(&length, sizeof(length));
socket().sendBytes(sbuf.data(), length);
// do something
} catch (Poco::Exception& exc) {
app.logger().log(exc);
}
}
};
class MsgServer: public Poco::Util::ServerApplication {
public:
MsgServer() {}
~MsgServer() {}
protected:
void initialize(Application& self) {
ServerApplication::initialize(self);
}
void uninitialize() {
ServerApplication::uninitialize();
}
int main(const std::vector<std::string>& args) {
unsigned short port = 8888;
ServerSocket svs(port);
TCPServer srv(new TCPServerConnectionFactoryImpl<MsgServerConnection>(), svs);
srv.start();
waitForTerminationRequest();
srv.stop();
return Application::EXIT_OK;
}
};
int main(int argc, char** argv) {
MsgServer app;
return app.run(argc, argv);
}