-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpconnection.h
89 lines (61 loc) · 1.46 KB
/
tcpconnection.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include "subepoller.h"
#include <memory>
#include <unistd.h>
#include"connectionbuffer.h"
#include"httpcontext.h"
#include "entry.h"
#include "httpresponse.h"
#include <iostream>
class HttpContext;
class ConnectionBuffer;
class subepoller;
class Entry;
//将每个TCPConnection封装成单独的类
//当有新连接到来时就在
//析构时自动关闭连接
class TcpConnection : public std::enable_shared_from_this<TcpConnection>
{
public:
TcpConnection(int fd,subepoller* epoller) : fd_(fd),epoller_(epoller)
{
}
~TcpConnection()
{
// std::cout << "~TcpConnection fd = " << fd_ << std::endl;
//关闭连接
if(close(fd_) != 0)
{
std::cout << "close error" << std::endl;
}
}
void readtobuffer();
//destoryconnection实现的功能是取消epoll的关注并关闭连接文件描述符
void destoryconnection();
ConnectionBuffer* getreadbuffer()
{
return &readbuffer_;
}
void setentry(std::shared_ptr<Entry> en)
{
entry_ = en;
}
std::weak_ptr<Entry>& getentry()
{
return entry_;
}
void handleread();
void handlewrite();
private:
int fd_;
ConnectionBuffer readbuffer_;
ConnectionBuffer writebuffer_;
HttpContext context_;
//不掌管生命周期
subepoller* epoller_;
std::weak_ptr<Entry> entry_;
void request();
void send(ConnectionBuffer&);
};
#endif