-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cpp
More file actions
52 lines (43 loc) · 1.4 KB
/
server.cpp
File metadata and controls
52 lines (43 loc) · 1.4 KB
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
/** prototls - Portable asynchronous client/server communications C++ library
See LICENSE for copyright information.
*/
#include "prototls.hpp"
#include "my_protocol.pb.h"
class MyPeer : public prototls::Peer {
public:
// additional connection-specific state and routines
};
class MyServer : public prototls::Server<MyPeer> {
public:
// start 8 threads to perform TLS handshakes simultaneously
MyServer() : prototls::Server<MyPeer>(8) {}
void onPacket(MyPeer&p) {
my_protocol::ClientMessage msg;
p.recv(msg);
std::cout << msg.hello().greeting() << std::endl;
// handle the message
my_protocol::ServerMessage reply;
p.send(reply);
p.flush();
}
void onJoin(MyPeer&p) {
my_protocol::ServerMessage msg;
my_protocol::Hello* hello = msg.mutable_hello();
hello->set_greeting("Welcome!");
p.send(msg);
p.flush();
}
void onLeave(MyPeer&p) {
// clean up
}
};
// server main function
int main(int argc, char** argv) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
prototls::Socket::init();
prototls::TLSSocket::init("", "", "cert.pem", "key.pem");
MyServer server;
// encryption on, serve on port 1234 for at most 1024 peers
server.serve(true, 1234, 1024);
return 0;
}