-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpcommand.cpp
202 lines (162 loc) · 4.06 KB
/
tcpcommand.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "tcpcommand.hpp"
#include "utils.hpp"
#include "player.hpp"
#include <memory>
#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
TCPCommand::TCPCommand(std::uint16_t port) : remote(nullptr)
{
struct addrinfo hints{}, *servinfo{nullptr};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(nullptr, stringify(port).c_str(), &hints, &servinfo) < 0)
throw std::runtime_error("getaddrinfo() failed.\n");
std::unique_ptr<struct addrinfo, std::function<void (struct addrinfo*)>> holder(servinfo,
[](struct addrinfo *info) {
freeaddrinfo(info);
});
fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if (fd < 0)
throw std::runtime_error("Failed to create socket.\n");
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(fd, servinfo->ai_addr, servinfo->ai_addrlen) < 0)
{
close(fd);
throw std::runtime_error("Failed to bind socket. umusd is probably already running.\n");
}
if (listen(fd, 10) < 0)
{
close(fd);
throw std::runtime_error("Failed to listen to socket.\n");
}
}
void TCPCommand::handle(EventHandler &handler)
{
int newfd = accept(fd, nullptr, nullptr);
if (newfd < 0)
return;
connections.erase(std::remove_if(std::begin(connections), std::end(connections),
[](std::shared_ptr<TCPSocket> sock) { return sock->dead(); }),
connections.end());
auto conn = std::make_shared<TCPSocket>(newfd);
handler.add(conn);
conn->set_remote(*remote);
connections.push_back(conn);
}
EventHandled::PollList TCPCommand::pollfds() const
{
return {{fd, EPOLLIN}};
}
void TCPCommand::set_remote(Remote &remote)
{
this->remote = &remote;
for (auto &sock : connections)
sock->set_remote(remote);
}
TCPCommand& TCPCommand::operator=(TCPCommand &&tcp)
{
connections = std::move(tcp.connections);
if (fd >= 0)
close(fd);
fd = -1;
std::swap(fd, tcp.fd);
remote = tcp.remote;
return *this;
}
TCPCommand::TCPCommand(TCPCommand &&tcp)
{
*this = std::move(tcp);
}
TCPSocket::TCPSocket(int fd) : fd(fd), is_dead(false)
{}
TCPSocket::~TCPSocket() { kill_sock(); }
EventHandled::PollList TCPSocket::pollfds() const
{
return {{fd, EPOLLIN}};
}
void TCPSocket::kill_sock()
{
if (fd >= 0)
close(fd);
fd = -1;
is_dead = true;
}
void TCPSocket::write_all(EventHandler &handler, std::string &&str)
{
handler.remove(*this);
reply = std::make_shared<SocketReply>(fd, std::move(str),
[this, &handler](bool success)
{
if (success)
handler.add(shared_from_this());
else
kill_sock();
});
handler.add(reply);
}
void TCPSocket::parse_commands(EventHandler &event)
{
for (;;)
{
auto first = command_buf.find("\r\n");
if (first == std::string::npos)
return;
auto cmd = command_buf.substr(0, first);
auto reply = parse_command(event, cmd);
reply += "\r\n";
write_all(event, std::move(reply));
command_buf.erase(0, first + 2);
}
}
void TCPSocket::handle(EventHandler &event)
{
flush_buffer();
try
{
parse_commands(event);
}
catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
kill_sock();
}
}
void TCPSocket::flush_buffer()
{
char buf[1024];
ssize_t ret = ::read(fd, buf, sizeof(buf));
if (ret <= 0)
{
kill_sock();
return;
}
command_buf.insert(command_buf.end(), buf, buf + ret);
}
bool TCPSocket::dead() const
{
return is_dead;
}
TCPSocket::TCPSocket(TCPSocket &&tcp)
{
*this = std::move(tcp);
}
TCPSocket& TCPSocket::operator=(TCPSocket &&tcp)
{
kill_sock();
std::swap(tcp.fd, fd);
std::swap(tcp.is_dead, is_dead);
remote = tcp.remote;
return *this;
}