-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.cpp
More file actions
157 lines (117 loc) · 2.95 KB
/
TcpServer.cpp
File metadata and controls
157 lines (117 loc) · 2.95 KB
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
#include <errno.h>
#include <string.h>
#include "TcpServer.h"
#include "Common.h"
#include "IOBuffer.h"
using namespace std;
TcpServer::TcpServer(int port)
:event_loop_(new EventLoop()),
acceptor_(new Acceptor(port,TcpServer::new_conn))
{
}
TcpServer::~TcpServer()
{
delete acceptor_;
acceptor_ = NULL;
delete event_loop_;
event_loop_ = NULL;
}
void TcpServer::start()
{
int lfd = this->acceptor_->listen();
this->event_loop_->set_fd_event(lfd,eREADABLE,
Acceptor::accept_handler,
(void*)this->acceptor_);
this->event_loop_->run();
}
void TcpServer::new_conn(EventLoop*el, Connctx* connctx)
{
int fd = connctx->get_fd();
if(0 != set_fd_nonblock(fd))
{
return ;
}
if(0 != set_tcp_nodelay(fd))
{
return ;
}
el->set_fd_event(fd, eREADABLE, TcpServer::on_msg, (void*)connctx);
}
void TcpServer::del_conn(EventLoop*el, Connctx* connctx)
{
//printf("release conn\n");
int fd = connctx->get_fd();
el->release_fd_event(fd, eREADABLE);
el->release_fd_event(fd, eWRITABLE);
close(fd);
delete connctx;
connctx = NULL;
}
void TcpServer::shutdown_conn(EventLoop*el, Connctx*connctx)
{
//we make the current conn can not be write
//and continue make the fd readable
//so that can make sure that we will NOT loss the data on wire
int fd = connctx->get_fd();
Event* event = el->get_event(fd);
if((event->get_mask()& eWRITABLE) !=0)
{
el->release_fd_event(fd, eWRITABLE);
shutdown(fd, SHUT_WR);
}
}
void TcpServer::on_msg(EventLoop*el, void* ctx )
{
Connctx* connctx =(Connctx*)ctx;
IOBuffer* buf = connctx->get_buf();
int nread = buf->read_to_buffer();
if (nread == -1)
{
if (errno == EAGAIN)
{
nread = 0;
}
else
{
del_conn(el, connctx);
return;
}
}
else if (nread == 0)
{
del_conn(el, connctx);
return;
}
//try best to write
buf->write_from_buffer();
int fd = connctx->get_fd();
if(buf->remain() > 0)
{//need to write again when th fd is writable
Event* event = el->get_event(fd);
int mask = event->get_mask();
if(0 == (mask & eWRITABLE))
{
el->set_fd_event(fd, eWRITABLE, TcpServer::on_can_write, (void*)connctx);
}
}
else
{
el->release_fd_event(fd, eWRITABLE);
}
}
void TcpServer::on_can_write(EventLoop*el, void* ctx )
{
Connctx* connctx =(Connctx*)ctx;
int fd = connctx->get_fd();
IOBuffer* buf = connctx->get_buf();
int nwrite = buf->write_from_buffer();
if(nwrite < 0)
{
shutdown_conn(el, connctx);
return;
}
if(0 == buf->remain())
{
el->release_fd_event(fd, eWRITABLE);
}
}