-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cpp
222 lines (190 loc) · 4.42 KB
/
socket.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// socket and epoll wrappers
//
#include "socket.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdexcept>
#include <sstream>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <netinet/tcp.h>
using namespace tcp;
static void set_fcntl(int fd, int flags)
{
int f = ::fcntl(fd, F_GETFL, 0);
if (f == -1) {
throw std::runtime_error("fcntl error, GETFL");
}
f |= flags;
f = ::fcntl(fd, F_SETFL, f);
if (f == -1) {
throw std::runtime_error("fcntl error, SETFL");
}
}
static void throw_error(const char* msg) {
std::stringstream se;
se << msg << ": " << errno;
throw std::runtime_error(se.str());
}
struct address
{
struct addrinfo *info_; //list of items
address(const std::string& ip, int port)
{
struct addrinfo v; //hints
memset (&v, 0, sizeof(struct addrinfo));
v.ai_family = AF_UNSPEC; // IPv4 and IPv6
v.ai_socktype = SOCK_STREAM; // TCP
v.ai_flags = AI_PASSIVE; // all interfaces
std::stringstream ss;
ss << port;
int err = ::getaddrinfo(!ip.empty()?ip.c_str():NULL, ss.str().c_str(), &v, &info_);
if (err) {
std::stringstream es;
es << "Unable to getaddrinfo: " << err;
throw std::runtime_error(es.str());
}
assert(info_);
}
~address()
{
::freeaddrinfo(info_);
}
address(const address&) = delete;
address& operator=(const address&) = delete;
};
socket::socket(const std::string& ip, int port)
:fd_(-1)
{
struct linger lng = {0, 0};
int flags =1;
address addr(ip, port);
for (struct addrinfo *info = addr.info_; info != nullptr; info = info->ai_next) {
int fd = ::socket (info->ai_family, info->ai_socktype, info->ai_protocol);
if (fd == -1)
continue;
int err = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
if (err != 0) {
std::cerr << "setsockopt error" << std::endl;
}
err = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
if (err != 0) {
std::cerr << "setsockopt error" << std::endl;
}
err = setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&lng, sizeof(lng));
if (err != 0) {
std::cerr << "setsockopt error" << std::endl;
}
err = ::bind(fd, info->ai_addr, info->ai_addrlen);
if (!err) //bind worked
{
fd_ = fd;
break;
}
::close(fd);
}
if (fd_ == -1) {
throw_error("socket bind error");
}
}
socket::~socket()
{
// count on RAII
::close(fd_);
}
void socket::set_non_blocking()
{
set_fcntl(fd_, O_NONBLOCK);
}
void socket::listen()
{
int err = ::listen(fd_, SOMAXCONN);
if (err == -1) {
throw_error("socket listen error");
}
}
// epoll
epoll::epoll(int max_events)
{
events_.resize(max_events); //allocate events buffer
for (auto& v : events_) {
v.data.ptr = nullptr;
}
fd_ = epoll_create1(0);
if (fd_ == -1) {
throw_error("epoll_create error");
}
}
epoll::~epoll()
{
::close(fd_);
}
void epoll::listen_socket(tcp::socket& s)
{
struct epoll_event event;
event.data.ptr = (void*)&s;
event.events = EPOLLIN | EPOLLET;
int err = epoll_ctl(fd_, EPOLL_CTL_ADD, s.fd_, &event);
if (err == -1) {
throw_error("listen epoll_ctl error");
}
s.listen();
}
void epoll::add_descriptor(int fd, void* user)
{
struct epoll_event event;
event.data.ptr = user;
event.events = EPOLLOUT | EPOLLIN | EPOLLET;
int err = epoll_ctl(fd_, EPOLL_CTL_ADD, fd, &event);
if (err == -1) {
throw_error("add epoll_ctl error");
}
}
void epoll::remove_descriptor(int fd)
{
struct epoll_event event;
event.events = EPOLLIN | EPOLLET | EPOLLOUT;
int err = epoll_ctl(fd_, EPOLL_CTL_DEL, fd, &event);
if (err == -1) {
throw_error("add epoll_ctl error");
}
}
int epoll::wait()
{
int n = ::epoll_wait(fd_, &events_.at(0), events_.size(), -1);
if (n == -1) {
throw_error("epoll wait error");
}
return n;
}
bool tcp::accept_connection(connection_info& info, tcp::socket& s, tcp::epoll& ep)
{
struct sockaddr in_addr;
socklen_t in_len = sizeof(in_addr);
info.fd_ = ::accept(s.fd_, &in_addr, &in_len);
if (info.fd_ == -1)
{
if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
// no incoming connections
return false;
}
else {
throw_error("incoming connection error");
}
}
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int err = ::getnameinfo(&in_addr, in_len
,hbuf, sizeof(hbuf)
,sbuf, sizeof(sbuf)
,NI_NUMERICHOST | NI_NUMERICSERV);
if (err)
throw_error("getnameinfo");
info.host_ = std::string(hbuf);
info.port_ = std::string(sbuf);
set_fcntl(info.fd_, O_NONBLOCK);
return true;
}