-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnection.h
More file actions
74 lines (63 loc) · 2.72 KB
/
connection.h
File metadata and controls
74 lines (63 loc) · 2.72 KB
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
#ifndef CONNECTION_H
#define CONNECTION_H
#include <stdio.h>
#include <string>
#include <functional>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>
class Connection{
friend class TcpServer;
public:
Connection(std::string peer_ip, uint16_t peer_port, uint16_t local_port = 0, uint16_t index = 0);
virtual ~Connection();
virtual void OnPeerRead();
virtual void OnPeerWrite();
virtual void OnPeerEvent(short events);
virtual void OnInnerConnectionRead(struct bufferevent*);
virtual void OnInnerConnectionEvent(struct bufferevent*, short events);
virtual bool Start();
void Initialize(struct event_base* base, evutil_socket_t fd=-1, bool write_callback_on=false);
//do not add 4-bytes-leng HEAD
void WriteRaw(char* buffer, int len);
//add 4-bytes-len HEAD
void Write(char* buffer, int len);
inline uint16_t GetIndex(){ return index_; };
inline void SetEventPriority(int priority){ priority_ = priority; };
inline uint16_t GetPeerPort(){ return peer_port_; };
inline uint16_t GetLocalPort(){ return local_port_; };
inline bool IsRecvRawMode(){ return recv_raw_mode_;};
inline bool IsSendRawMode(){return send_raw_mode_;};
inline void SetRecvRawMode(bool raw){ recv_raw_mode_ = raw;};
inline void SetSendRawMode(bool raw){ send_raw_mode_ = raw;};
inline void SetDelConnCallback(std::function<void(uint16_t)> cb) { delconn_cb_ = cb;};
inline std::function<void(uint16_t)> GetDelConnCallback(){return delconn_cb_;};
protected:
static void InnerConnectionReadCallback(struct bufferevent* , void*);
static void InnerConnectionEventCallback(struct bufferevent* , short events, void*);
static void PeerReadCallback(struct bufferevent*, void* arg);
static void PeerWriteCallback(struct bufferevent*, void* arg);
static void PeerEventCallback(struct bufferevent*, short events, void*);
std::function<void(uint16_t)> delconn_cb_;
struct bufferevent* peer_event_;
struct event_base* base_;
//for inner communication, when the listen connection process and connection's data process are in different event_base loop(in different thread)
//socket pair, used to send signal from server to conn
struct bufferevent* conn_2_server_pair_[2];
//socket pair, used to send signal from conn to server
struct bufferevent* server_2_conn_pair_[2];
int fd_;
std::string peer_ip_;
uint16_t peer_port_;
uint16_t local_port_;
uint16_t index_;
int priority_;
bool connected_;
char buffer_[0xffff];
int buffer_len_;
bool recv_raw_mode_;
bool send_raw_mode_;
};
#endif