-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.h
67 lines (50 loc) · 1.04 KB
/
socket.h
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
#ifndef SOCKET_WRAP_H
#define SOCKET_WRAP_H
#include <sys/types.h>
#include <vector>
#include <string>
#include "kqepoll.h"
namespace tcp
{
struct socket
{
int fd_; //socket
explicit socket(const std::string& ip, int port);
~socket();
void set_non_blocking();
void listen();
protected:
socket() //used by derived types
:fd_(-1)
{}
private:
//cannot copy
socket(const socket&) = delete;
socket& operator=(const socket&) = delete;
};
struct epoll
{
typedef std::vector<epoll_event> events;
int fd_;
events events_;
explicit epoll(int max_events);
~epoll();
void listen_socket(socket& s);
void add_descriptor(int fd, void* user);
void remove_descriptor(int fd);
int wait();
private:
//cannot copy
epoll(const epoll&) = delete;
epoll& operator=(const epoll&) = delete;
};
// returns false if no connections on this socket
struct connection_info
{
int fd_;
std::string host_;
std::string port_;
};
bool accept_connection(connection_info& info, tcp::socket& s, tcp::epoll& ep);
}
#endif