-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConnHandler.cpp
152 lines (133 loc) · 4.28 KB
/
ConnHandler.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
//
// ConnHandler.cpp
// TYDB
//
// Created by TYPCN on 2016/9/19.
//
//
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <string.h>
#include "ConnHandler.hpp"
#include "Responder.hpp"
#include "Logger.h"
ConnHandler::ConnHandler(int s) : sfd(s) {
alive = true;
is_writeable = true;
initmtxlock:
int rv = pthread_mutex_init(&queue_lock, NULL);
if(rv != 0){
if(errno == EAGAIN){
goto initmtxlock;
}
}
initspinlock:
rv = pthread_spin_init(&writeable_lock, PTHREAD_PROCESS_PRIVATE);
if(rv != 0){
if(errno == EAGAIN){
goto initspinlock;
}
}
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
io_read.set<ConnHandler, &ConnHandler::read_cb>(this);
io_read.start(s, ev::READ);
io_write.set<ConnHandler, &ConnHandler::write_cb>(this);
Buffer_size = sizeof(Buffer);
}
void ConnHandler::read_cb(ev::io &watcher, int revents) {
uint8_t *data = (uint8_t *)malloc(1000);
ssize_t nread = recv(watcher.fd, data, 1000, 0);
if(nread < 0){
free(data);
LOG(WARNING) << (void *)this << ": Read fd " << watcher.fd << " failed with errno " << errno;
return;
}else if(nread == 0){
free(data);
LOG(VERBOSE) << (void *)this << ": Closing connection " << watcher.fd;
this->shutdown();
}else{
LOG(VERBOSE) << (void *)this << ": Data read complete";
Responder hdl(this);
hdl.send_result(data,nread);
}
}
void ConnHandler::write_cb(ev::io &watcher, int revents) {
pthread_mutex_lock(&queue_lock);
while (!write_queue.empty()) {
Buffer* buffer = write_queue.front();
ssize_t written = ::send(sfd, (const char *) buffer->data + buffer->wrote_len, buffer->len - buffer->wrote_len, 0);
if (written < 0) {
LOG(WARNING) << "Send fd " << watcher.fd << " failed with errno " << errno;
return;
}
buffer->wrote_len += written;
if (buffer->wrote_len != buffer->len) {
pthread_mutex_unlock(&queue_lock);
return;
}
write_queue.pop_front();
free(buffer->data);
free(buffer);
}
pthread_mutex_unlock(&queue_lock);
io_write.stop();
pthread_spin_lock(&writeable_lock);
is_writeable = true;
pthread_spin_unlock(&writeable_lock);
}
void ConnHandler::send(const uint8_t *data, int len){
pthread_spin_lock(&writeable_lock);
bool writeable = is_writeable;
pthread_spin_unlock(&writeable_lock);
if(writeable){
// 可写直接发送
ssize_t written = ::send(sfd, (const char *) data, len, 0);
if(written < 0){
if(errno == EAGAIN || errno == EWOULDBLOCK){
// Buffer 满了,设置为不可写
pthread_spin_lock(&writeable_lock);
is_writeable = false;
pthread_spin_unlock(&writeable_lock);
// 加入队列并等待可写事件回调
LOG(VERBOSE) << (void *)this << ": Blocked, Queued up packet";
this->add_queue(data, len);
io_write.start(sfd, ev::WRITE);
}else{
LOG(VERBOSE) << (void *)this << ": Connection closed " << sfd;
this->shutdown();
}
}else if(written < len){
// 只发了一部分
pthread_spin_lock(&writeable_lock);
is_writeable = false;
pthread_spin_unlock(&writeable_lock);
this->add_queue(data + written, len - written);
io_write.start(sfd, ev::WRITE);
}
}else{
this->add_queue(data, len);
}
}
void ConnHandler::add_queue(const uint8_t *data, int len){
pthread_mutex_lock(&queue_lock);
void *mem = malloc(len);
memcpy(mem, data, len);
Buffer *buf = (Buffer *)malloc(Buffer_size);
buf->data = (uint8_t *)mem;
buf->wrote_len = 0;
buf->len = len;
write_queue.push_back(buf);
pthread_mutex_unlock(&queue_lock);
}
void ConnHandler::shutdown(){
LOG(VERBOSE) << (void *)this << ": Shutdown connection " << sfd;
alive = false;
::shutdown(sfd, SHUT_RDWR);
delete this;
}