-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
70 lines (60 loc) · 1.63 KB
/
Copy pathserver.cpp
File metadata and controls
70 lines (60 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "./include/socketEventLoop.h"
#include <iostream>
#include <thread>
int light_job(sockpp::tcp_socket &sock, std::vector<char*> request)
{
sock.write_n(request[0], sizeof(request[0]));
return 0;
}
int heavy_job(sockpp::tcp_socket &sock, std::vector<char*> request)
{
/*
Placeholder for heavy io load
*/
for(int i; i < 100; i++)
{
sock.write_n(request[0], sizeof(request[0]));
}
return 0;
}
// void run_echo(sockpp::tcp_socket sock)
// {
// char buf[1024] = {0};
// ssize_t n;
// while ((n = sock.read(buf, sizeof(buf))) > 0)
// {
// std::cout << buf << std::endl;
// std::cout << "New Line" << std::endl;
// sock.write_n(buf, n);
// }
// std::cout << "Connection closed from " << sock.peer_address() << std::endl;
// }
int main()
{
kqueueEventLoop t;
std::thread thr(&kqueueEventLoop::runLoop, t, std::ref(light_job), std::ref(heavy_job));
int16_t port = 12345;
sockpp::tcp_acceptor acc(port);
if (!acc)
{
std::cerr << acc.last_error_str() << std::endl;
}
std::cout << "Listening on port " << port << std::endl;
// Accept a new client connection
sockpp::tcp_socket sock = acc.accept();
while (true)
{
// Accept a new client connection
sockpp::tcp_socket sock = acc.accept();
if (!sock)
{
std::cerr << "Error accepting incoming connection: " << acc.last_error_str() << std::endl;
}
else
{
// Create a thread and transfer the new stream to it.
t.addSocketEvent(sock);
// thr.detach();
}
}
}