-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
100 lines (89 loc) · 2.16 KB
/
server.cpp
File metadata and controls
100 lines (89 loc) · 2.16 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
#include "stdafx.h"
namespace local {
#if 0
Server::Server() : TcpServer(new uv::EventLoop()) {
Init();
}
Server::~Server() {
UnInit();
}
void Server::Init() {
onMessageCallback_ =
[this](uv::TcpConnectionPtr tcp_connection, const char* buffer, ssize_t buffer_size) {
m_SessionQ.search(tcp_connection->Name(),
[&](Taskman* pSession) {
pSession->Read(std::string((char*)buffer, buffer_size));
});
};
onNewConnectCallback_ =
[this](std::weak_ptr<uv::TcpConnection> tcp_connection) {
auto pSession = new Taskman(tcp_connection.lock().get());
const auto& identify = pSession->Identify();
m_SessionQ.pop(identify, [](const auto&, auto&) {});
m_SessionQ.push(identify, pSession);
};
onConnectCloseCallback_ =
[this](std::weak_ptr<uv::TcpConnection> tcp_connection) {
m_SessionQ.pop(tcp_connection.lock().get()->Name(),
[&](const auto&, auto& pSession) {
SK_DELETE_PTR(pSession);
});
};
}
void Server::UnInit() {
SK_DELETE_PTR(loop_);
}
bool Server::Start() {
do {
if (m_IsOpen.load())
break;
m_Threads.emplace_back(
[this]() {
uv::SocketAddr socket_addr("127.0.0.1", 13762, uv::SocketAddr::Ipv4);
do {
if (0 != bindAndListen(socket_addr))
break;
loop_->run();
loop_->runInThisLoop([]() {});
} while (0);
});
m_IsOpen.store(true);
m_Threads.emplace_back([this]() {Process(); });
} while (0);
return m_IsOpen.load();
}
void Server::Stop() {
do {
if (!m_IsOpen.load())
break;
__super::close(
[this]() {
std::thread(
[&]() {
loop_->runInThisLoop([&]() {loop_->stop(); });
}).join();
});
} while (0);
m_IsOpen.store(false);
for (auto& t : m_Threads)
t.join();
m_Threads.clear();
}
void Server::Process() {
do {
m_SessionQ.iterate(
[&](const auto&, Taskman* pSession, bool& itbreak) {
std::vector<std::string> recv_pak_s;
pSession->Read(recv_pak_s);
if (!recv_pak_s.empty()) {
auto sksksk = 0;
}
auto sk = 0;
});
if (!m_IsOpen.load())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (1);
}
#endif
}///namespace lcoal