-
Notifications
You must be signed in to change notification settings - Fork 10
/
TcpConnection.h
69 lines (51 loc) · 1.93 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
#ifndef TCP_CONNECTION_H
#define TCP_CONNECTION_H
#include "NonCopyable.h"
#include "InetAddress.h"
#include "Socket.h"
#include "Rio.h"
#include <string>
#include <memory>
#include <functional>
class TcpConnection;
typedef std::shared_ptr<TcpConnection> TcpConnectionPtr;
class TcpConnection : private NonCopyable,
public std::enable_shared_from_this<TcpConnection>
{
public:
typedef std::function<void(const TcpConnectionPtr &)> TcpConnectionCallback;
TcpConnection(int sockfd,
const InetAddress &localAddr,
const InetAddress &peerAddr);
~TcpConnection();
int fd() const
{ return sockfd_.fd();}
ssize_t readn(char *usrbuf, size_t n);
ssize_t readLine(char *usrbuf, size_t maxline);
ssize_t writen(const char *usrbuf, size_t n);
void send(const std::string &s);
std::string receive();
void shutdown();
void setConnectionCallback(const TcpConnectionCallback &cb)
{ onConnectionCallback_ = cb; }
void setMessageCallback(const TcpConnectionCallback &cb)
{ onMessageCallback_ = cb; }
void setCloseCallback(const TcpConnectionCallback &cb)
{ onCloseCallback_ = cb; }
void handleConnection();
void handleMessage();
void handleClose();
const InetAddress &getLocalAddr() const
{ return localAddr_; }
const InetAddress &getPeerAddr() const
{ return peerAddr_; }
private:
Socket sockfd_; //fd
Rio buffer_; //该连接的读写缓冲区
const InetAddress localAddr_; //Local
const InetAddress peerAddr_; //Peer
TcpConnectionCallback onConnectionCallback_; //连接建立时的回调
TcpConnectionCallback onMessageCallback_; //消息到达
TcpConnectionCallback onCloseCallback_; //连接关闭
};
#endif /*TCP_CONNECTION_H*/