forked from sl950313/SEDA-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.cpp
106 lines (98 loc) · 3.03 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
#include "socket.h"
#include "log.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <unistd.h>
/*
* 完成所有的客户端socket初始化动作
*/
Socket::Socket(std::string ip, int port) {
connect(ip, port);
}
/*
* 完成所有的服务端socket初始化动作
*/
Socket::Socket(int listen_port) {
this->listen_port = listen_port;
bool ret = initServer();
if (ret) {
LogUtil::debug("Socket [initServer] error");
return ;
}
}
bool Socket::initServer() {
int ret = 0;
if ((ret = (listenfd = socket(AF_INET, SOCK_STREAM, 0))) == -1) {
// log here.
LogUtil::debug("Socket : [initServer] create socket error");
//fprintf(stderr, "create socket error\n");
return false;
}
struct sockaddr_in actual_addr;
actual_addr.sin_family = AF_INET;
actual_addr.sin_addr.s_addr = htonl(INADDR_ANY);
actual_addr.sin_port = htons(listen_port);
// set the listenfd socket to be nonblock.
fcntl(listenfd, F_SETFL, SOCK_NONBLOCK);
//set listenfd reused.
int option = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
struct sockaddr *addr = (struct sockaddr *)&actual_addr;
//addr.sa_family.
if ((ret = bind(listenfd, addr, sizeof(actual_addr))) == -1) {
fprintf(stderr, "bind socket error : %s\n", strerror(errno));
return false;
}
listen(listenfd, 50);
return true;
}
/*
void Socket::loop() {
epfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = EPOLLIN | EPOLLET;
event.data.fd = listenfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &event);
struct epoll_event events[MAXEVENTS];
//log->info("acceptor looping\n");
LogUtil::debug("Socket : [loop] running");
for (;;) {
int rd_fds = epoll_wait(epfd, events, MAXEVENTS, 1000);
for (int i = 0; i < rd_fds; ++i) {
if (events[i].data.fd == listenfd) {
//acceptor *ac = this;
accept_conn();
} else {
// TODO.
if (events[i].events & EPOLLIN) {
connection *conn = (connection *)events[i].data.ptr;
conn->cb = read_conn;
queue_element *qe = new queue_element();
qe->_cb = conn->cb;
qe->arg = (void *)conn;
tq->push((void *)qe);
log->debug("acceptor : [epoll_loop]. tq->push read_conn\n");
} else {
if (events[i].events & EPOLLOUT) {
connection *conn = (connection *)events[i].data.ptr;
conn->cb = write_conn;
queue_element *qe = new queue_element();
qe->_cb = write_conn;
qe->arg = (void *)conn;
tq->push((void *)qe);
log->debug("acceptor : [epoll_loop]. tq->push write_conn\n");
}
}
}
}
}
return true;
}
*/