-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat_server.cc
More file actions
136 lines (126 loc) · 3.86 KB
/
Copy pathchat_server.cc
File metadata and controls
136 lines (126 loc) · 3.86 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#define ENABLE_ELG_LOG
#include <elog/logger.h>
#include <netpoll/core.h>
#include <atomic>
#include <unordered_map>
#include "context.h"
#include "type.h"
#include "util.h"
namespace chat {
struct server
{
using ConnectionMapType =
std::unordered_map<int, std::weak_ptr<netpoll::TcpConnection>>;
NETPOLL_TCP_CONNECTION(conn)
{
if (conn->connected())
{
int id = gid_++;
conn->setContext(Context{id});
std::lock_guard<std::mutex> lockGuard(connMap_.mutex);
connMap_.value[id] = conn;
}
else
{
auto& ctx = netpoll::any_cast<Context&>(conn->getMutableContext());
std::lock_guard<std::mutex> lockGuard(connMap_.mutex);
connMap_.value.erase(ctx.getId());
}
}
NETPOLL_TCP_MESSAGE(conn, buffer)
{
flag:
auto& ctx = netpoll::any_cast<Context&>(conn->getMutableContext());
ctx.processCodec(conn, buffer);
if (ctx.parseOk())
{
onRequest(conn, ctx.getText());
if (buffer->readableBytes() > 0) { goto flag; }
}
}
netpoll::TcpConnectionPtr getConnById(int id)
{
netpoll::TcpConnectionPtr conn;
{
std::lock_guard<std::mutex> lockGuard(connMap_.mutex);
conn = connMap_.value[id].lock();
}
return conn;
}
void onRequest(netpoll::TcpConnectionPtr const& conn, const std::string& msg)
try
{
CommonData data;
ELG_TRACE("msg:{}", msg);
ejson::Parser::FromJSON(msg, data);
if (data.code == -1)
{
ELG_WARN("json parse error");
return;
}
auto& ctx = netpoll::any_cast<Context&>(conn->getMutableContext());
switch (data.code)
{
case kRequestInit: {
ELG_INFO("uid:{} request init", ctx.getId());
Context::Send(conn, {kResponseInit, std::to_string(ctx.getId())});
break;
}
case kRequestChat: {
ELG_INFO("uid:{} request chat", ctx.getId());
ChatMsg requestChat;
ejson::Parser::FromJSON(data.decodeText(), requestChat);
if (requestChat.sender == -1 || requestChat.receiver == -1)
{
ELG_WARN("request error");
return;
}
if (auto receiver = getConnById(requestChat.receiver); receiver)
{
Context::Send(receiver,
CommonData{kResponseChat,
ejson::base64_encode(
ejson::Parser::ToJSON(requestChat))});
}
else
{
connMap_.value.erase(requestChat.receiver);
Context::Send(conn,
CommonData{kBadResponse, "receiver is not exist"});
}
break;
}
case kRequestAllList: {
ELG_INFO("uid:{} request all connection list", ctx.getId());
ConnectionList response;
{
std::lock_guard<std::mutex> lockGuard(connMap_.mutex);
for (auto&& [k, _] : connMap_.value)
{
if (k == ctx.getId()) { continue; }
response.value.push_back(k);
}
}
Context::Send(conn, CommonData{kResponseAllList,
ejson::base64_encode(
ejson::Parser::ToJSON(response))});
break;
}
}
}
catch (const std::exception& e)
{
ELG_WARN("json parsing or serialization error:{}", e.what());
return;
}
std::atomic_int gid_{0};
MutexGuard<ConnectionMapType> connMap_;
};
} // namespace chat
int main()
{
auto loop = netpoll::NewEventLoop();
auto&& listener = netpoll::tcp::Listener::New({8080});
listener.bind<chat::server>();
loop.serve(listener);
}