-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpConnection.h
57 lines (41 loc) · 1.29 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
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include "Socket.h"
#include "Channel.h"
#include "Buffer.h"
#include <functional>
#include <vector>
#include <memory>
namespace net
{
class EventLoop;
class TcpConnection : public std::enable_shared_from_this<TcpConnection>
{
public:
typedef Buffer::BufferData Data;
typedef std::shared_ptr<TcpConnection> TcpConnPtr;
typedef std::weak_ptr<TcpConnection> TcpConnWeakPtr;
typedef std::function<void(TcpConnPtr &)> OnMessageCallBack;
typedef std::function<void(TcpConnPtr &)> OnCloseCallBack;
TcpConnection(EventLoop *loop, Socket connSock);
~TcpConnection();
void runInLoop();
Data recieve() { return inputBuffer_.getBufferData(); }
void send(Data data) { if(!closed_) outputBuffer_.appendBufferData(data); }
void setMessageCallBack(OnMessageCallBack cb) { messageCallback_ = cb; }
void setCloseCallBack(OnCloseCallBack cb) { closeCallback_ = cb; }
private:
static void HandleRead(TcpConnWeakPtr &);
static void HandleWrite(TcpConnWeakPtr &);
static void HandleClose(TcpConnWeakPtr &);
bool closed_;
EventLoop *loop_;
Socket connSock_;
Channel::ChannelPtr connChannel_;
Buffer inputBuffer_;
Buffer outputBuffer_;
OnMessageCallBack messageCallback_;
OnCloseCallBack closeCallback_;
};
}
#endif