-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCP_interface.h
More file actions
150 lines (134 loc) · 3.56 KB
/
TCP_interface.h
File metadata and controls
150 lines (134 loc) · 3.56 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef _TCP_interface_H_
#define _TCP_interface_H_
namespace ftp_server
{
class TCP_interface
{
public:
// int listen_socket; // FTP监听句柄
struct event_base *base;
private:
TCP_manager *p_TCP_manager;
/* public:
const static int RET_CTRL_WRITE = 0x0001;
const static int DATA_WRITE = 0x0004;
*/
public:
TCP_interface()
{
p_TCP_manager = new TCP_manager();
p_TCP_manager -> set_ctrl_port(SERVERPORT);
// 若配置项无设置,则使用默认端口
};
~TCP_interface()
{
delete p_TCP_manager;
p_TCP_manager = NULL;
};
public:
void set_ctrl_port(int port)
{
p_TCP_manager -> set_ctrl_port(port);
}
int get_ctrl_port()
{
return p_TCP_manager -> get_ctrl_port();
}
TCP_manager *get_TCP_manager()
{
return p_TCP_manager;
};
bool start_proc();
public:
int init_TCP_event(TCP_interface *p);
int init_TCP_data_event(TCP_client *data_client);
public:
/** 设置检查超时的间隔时间和会话寿命 */
void set_timeout( int chk_span , int session_life )
{
if(p_TCP_manager == NULL)
{
LOG(FATAL,"set socket time out fail for TCP_Manger is not initial");
// cout<<"set socket time out fail for TCP_Manger is not initial"<<endl;
}
else
{
p_TCP_manager -> set_timeout(chk_span, session_life);
}
};
void update_last_access(int fd)
{
TCP_client *client = NULL;
client = p_TCP_manager -> seek_client( fd );
if(client == NULL)
{
LOG(FATAL,"Update client : "<<fd <<" is not exist");
}
else
{
client -> update_last_access();
}
return ;
}
private:
/** 控制连接事件:接受、通知、读、写、超时 */
static void on_read(int fd, short ev, void *arg);
static void on_write(int fd, short ev, void *arg);
static void on_accept(int fd, short ev, void *arg);
static void on_notify(int fd, short ev, void *arg);
static void on_timeout(int fd, short ev, void *arg);
/** 数据连接事件:接受、读、写 */
static void data_accept(int fd, short ev, void *arg);
static void data_read(int fd, short ev, void *arg);
static void data_write(int fd, short ev, void *arg);
static void data_timeout(int fd, short ev, void *arg);
public:
void post(int fd, char *buf, int len )
{
_event_notify _notify;
_notify.fd = fd;
_notify.action = EVENT_NOTIFY_CTRL_WRITE;
_notify.buf = new char[COMMANDSIZE];
strncpy(_notify.buf, buf, COMMANDSIZE);
_notify.len = len;
p_TCP_manager -> notify( _notify );
};
void post_transfer_ok(int fd, int action, char *buf, int len )
{
_event_notify _notify;
_notify.fd = fd;
_notify.action = action;
_notify.buf = new char[COMMANDSIZE];
strncpy(_notify.buf, buf, COMMANDSIZE);
_notify.len = len;
p_TCP_manager -> notify( _notify );
};
void post_action(int fd, int action)
{
_event_notify _notify;
_notify.fd = fd;
_notify.action = action;
p_TCP_manager -> notify( _notify );
};
void data_post(int cmd_fd, char *buf, int len )
{
_event_notify _notify;
_notify.fd = cmd_fd;
_notify.action = EVENT_NOTIFY_DATA_WRITE;
_notify.len = len;
_notify.buf = new char[len];
memcpy(_notify.buf, buf, len);
p_TCP_manager -> notify( _notify );
};
/* void close_fd(int fd)
{
_event_notify _notify;
_notify.fd = fd;
_notify.action = TCP_ACTION_DELETE;
_notify.buf = NULL;
_notify.len = 0;
p_TCP_manager -> notify( _notify );
};
*/ };
}
#endif