-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cxx
More file actions
112 lines (85 loc) · 2.18 KB
/
main.cxx
File metadata and controls
112 lines (85 loc) · 2.18 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <cstdlib>
#include <memory>
#include <exception>
#include <typeinfo>
#include "tcp_socket.hpp"
#include "raw_socket.hpp"
#include "pipe.hpp"
static void
callback(void*, void*)
{
}
static void
run(nt::http::interfaces::Socket* socket)
{
// socket->bind("0.0.0.0", "tcp"); // error
socket->bind("0.0.0.0", 8888);
socket->listen(8, callback);
socket->open();
// socket->close();
}
static void
_main()
{
auto socket = std::make_unique<nt::http::TcpSocket>();
auto s = dynamic_cast<nt::http::interfaces::Socket*>(socket.get());
if (s == nullptr) {
throw std::bad_cast();
}
run(s);
}
static inline bool
_is_set(std::vector<int> bits, int set)
{
bool is_set = false;
for (auto& bit :bits) {
is_set |= static_cast<bool>(bit & set);
if (is_set) break;
}
return is_set;
}
static void
_main_raw()
{
auto socket = std::make_unique<nt::http::RawSocket>();
auto event = std::make_unique<nt::http::OverlappedEvent>(socket.get());
socket->bind("0.0.0.0", 8888);
socket->listen(8);
event->set();
#ifdef LOSE
HANDLE handles[] = {event->handle};
::WaitForMultipleObjects(1, handles, false, INFINITE);
WSANETWORKEVENTS networkEvents;
if (::WSAEnumNetworkEvents(socket->socket, event->handle, &networkEvents) == SOCKET_ERROR) {
std::cout << "enumerate socket events error\n";
}
if (_is_set({FD_ACCEPT}, networkEvents.lNetworkEvents)) {
std::cout << "accept\n";
auto client = socket->accept();
char buffer[MAX_INPUT] = {0};
::recv(client->socket, buffer, sizeof(buffer) - 1, 0);
std::cout << buffer;
}
#else
auto client = socket->accept();
char buffer[MAX_INPUT] = {0};
::recv(client->socket, buffer, sizeof(buffer) - 1, 0);
std::cout << buffer;
#endif
}
int
main(int, char**)
{
try {
_main();
// _main_raw();
return EXIT_SUCCESS;
} catch (std::bad_cast& ex) {
std::cout << "dynamic cast failed" << std::endl;
return EXIT_FAILURE;
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
return EXIT_FAILURE;
}
}