-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.cpp
115 lines (107 loc) · 2.57 KB
/
connection.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
#include "connection.h"
#include"connections_thread.h"
Connection::Connection(Rester* server)
:server_(server)
{
response_ptr_= make_shared<Response>();
on_write_=[](RequestPtr request_ptr,ResponsePtr response_ptr)
{
printf("null on_write triggered\n");
};
}
//Connection::Connection(const Connection &connection)
//{
// this->event_=connection.event_;
// this->ip_=connection.ip_;
// this->port_=connection.port_;
// this->is_on_=connection.is_on_;
// this->state_=connection.state_;
// this->thread_=connection.thread_;
// this->server_=connection.server_;
// this->connected_fd_=connection.connected_fd_;
// //return this;
//}
//
//Connection &Connection::operator=(const Connection &connection)
//{
// this->event_=connection.event_;
// this->ip_=connection.ip_;
// this->port_=connection.port_;
// this->is_on_=connection.is_on_;
// this->state_=connection.state_;
// this->thread_=connection.thread_;
// this->server_=connection.server_;
// this->connected_fd_=connection.connected_fd_;
// return *this;
//}
void Connection::Close()
{
printf("close\n");
OnClose();
thread_->DeleteConnection(GetShare() );
}
void Connection::AddToThread(ConnectionsThread* thread)
{
thread_=thread;
thread->AddConnection(GetShare());
}
bool Connection::operator==(const Connection &other)
{
return this->connected_fd_ == other.connected_fd_;
}
void Connection::SetOnWrite(GetCallBack on_write)
{
on_write_=on_write;
}
void Connection::SendOver()
{
printf("send over\n");
sent_size_=0;
read=false;
//request_ptr_= nullptr;
//response_ptr_= nullptr;
response_ptr_= make_shared<Response>();
RemoveEpollOut();
if(ShortConnection)
{
Close();
}
}
void Connection::AddEpollOut()
{
auto ev=event_.events;
ev=EPOLLIN|EPOLLET|EPOLLRDHUP|EPOLLERR|EPOLLOUT;
event_.events=ev;
auto ret=epoll_ctl(thread_->epoll_fd_, EPOLL_CTL_MOD, connected_fd_ , &event_);
if(ret<0)
{
perror("epoll mod err: ");
}
else
{
printf("add epollout\n");
}
}
void Connection::RemoveEpollOut()
{
auto ev=event_.events;
ev=EPOLLIN|EPOLLET|EPOLLRDHUP|EPOLLERR;
event_.events=ev;
auto ret=epoll_ctl(thread_->epoll_fd_, EPOLL_CTL_MOD, connected_fd_ , &event_);
if(ret<0)
{
perror("epoll mod err: ");
}
else
{
printf("remove epollout\n");
}
// if(event_.events & EPOLLOUT)
// {
// printf("EPOLLOUT not removed\n");
// }
// else
// {
// printf("EPOLLOUT is removed\n");
// }
}