-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcpchat.cpp
180 lines (146 loc) · 4.24 KB
/
tcpchat.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
#include <gflags/gflags.h>
#include <uthread/uthread.hpp>
using namespace uthread;
DEFINE_uint32(port, 8000, "The TCP port number for the server to listen on.");
namespace {
struct User {
// The name of the connected user.
std::string name;
// The log sequence number of the next message to send this user.
std::size_t seq = 0;
// The TCP connection for this user.
TcpStream conn;
User(std::string name, std::size_t seq, TcpStream conn)
: name{std::move(name)}, seq{seq}, conn{std::move(conn)} {}
};
struct Room {
// The log of all messages in the chat room.
std::vector<std::string> log;
// The task queue tasks sleep on when waiting for new messages from the log.
TaskQueue park;
// The queue of available names. Once the names are exhausted new connections
// are blocked.
MpmcQueue<std::string> names;
Room() {
names.push("Anonymous Iguana");
names.push("Anonymous Turtle");
names.push("Anonymous Bear");
names.push("Anonymous Whale");
names.push("Anonymous Shark");
names.push("Anonymous Elephant");
names.push("Anonymous Wolf");
names.push("Anonymous Moose");
}
};
struct Framer {
std::string buf;
void append(const char* buf, std::size_t bufLen) {
this->buf.append(buf, bufLen);
}
// Check if a line has been buffered.
bool has() {
auto p = buf.find('\n');
return (p != std::string::npos);
}
// Pop one line from the buffer.
std::string pop() {
auto p = buf.find('\n');
if (p == std::string::npos) {
return "";
}
auto s = buf.substr(0, p);
buf.erase(0, p + 1);
return s;
}
};
Room room;
void post(const User& user, std::string message) {
static const std::string kWhitespace = " \t\r\n";
// http://www.toptip.ca/2010/03/trim-leading-or-trailing-white-spaces.html
auto p = message.find_first_not_of(kWhitespace);
message.erase(0, p);
p = message.find_last_not_of(kWhitespace);
if (p != std::string::npos) {
message.erase(p + 1);
}
// Do not send empty messages.
if (message.empty()) {
return;
}
std::cerr << user.name + ": " + message << std::endl;
room.log.push_back(user.name + ": " + message + "\n");
while (room.park.unpark()) {
}
}
void work(User user) {
post(user, "<Joined>");
auto closed = false;
// Send the message log to this user.
Task forwarder{[&user, &closed]() {
while (!closed) {
if (user.seq < room.log.size()) {
const std::string& message = room.log[user.seq];
if (user.conn.send(message.c_str(), message.size())) {
user.seq++;
} else {
closed = true;
}
} else {
room.park.park();
}
}
}};
// Append messages from this user to the log.
Task publisher{[&user, &closed]() {
Framer f;
char buf[1024];
while (!closed) {
auto readLen = user.conn.read(buf, sizeof(buf));
if (readLen > 0) {
f.append(buf, readLen);
while (f.has()) {
post(user, f.pop());
}
} else {
closed = true;
while (room.park.unpark()) {
}
}
}
}};
publisher.join();
forwarder.join();
// Send a notification that the user has disconnected and reuse the name.
post(user, "<Left>");
room.names.push(user.name);
}
void runApp() {
TcpListener::Options options;
options.reuseAddr = true;
TcpListener listener{SocketAddrV4{"127.0.0.1", FLAGS_port}, options};
std::cout << "Running a chat server; Use 'ncat 127.0.0.1 " << FLAGS_port
<< "' to send messages." << std::endl;
while (true) {
// Wait for a name to become available. This is a simple way of limiting the
// number of concurrent users in our chat room.
std::string name;
room.names.pop(name);
// A name has been allocated, so now we just wait for a connection.
auto conn = listener.accept();
User user{std::move(name), room.log.size(), std::move(conn)};
Task{[user{std::move(user)}]() mutable { work(std::move(user)); }};
}
}
} // namespace
int main(int argc, char* argv[]) {
gflags::SetUsageMessage("A TCP chat server");
gflags::ParseCommandLineFlags(&argc, &argv, true);
TaskLoop taskLoop;
taskLoop.addTask(runApp);
taskLoop.runLoop();
return 0;
}