-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.h
123 lines (103 loc) · 2.09 KB
/
session.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
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
/**
***All Rights Reserved
*
*author frankiezhu
*date 2013-03-22
*/
#ifndef __SESSION_HEADER__
#define __SESSION_HEADER__
#include "mempool.h"
#include "message.h"
#include <time.h>
#include <unistd.h>
#include "timer.h"
#include <string>
using namespace std;
typedef enum session_ret
{
SESSION_RET_FAILED = -1,
SESSION_RET_SUCCESS = 0,
SESSION_RET_NO_DATA = 1,
SESSION_RET_NOTIFY_MASTER = 2,
SESSION_RET_MAX,
} session_ret_t;
class Session
{
public:
Session(int64_t life_span);
virtual ~Session();
virtual int on_connect();
virtual int on_read_event();
virtual int on_write_event();
virtual int on_close();
u32 get_cli_addr() const
{
return m_cli_addr;
}
void set_cli_addr(u32 ip)
{
m_cli_addr = ip;
struct in_addr addr;
addr.s_addr = htonl(m_cli_addr);
m_string_ip = string(inet_ntoa(addr));
}
int get_cli_port() const
{
return m_cli_port;
}
void set_cli_port(u32 port)
{
m_cli_port = port;
}
int get_sockfd() const
{
return m_sockfd;
}
void set_sockfd(int sockfd)
{
m_sockfd = sockfd;
}
int64_t get_active_time() const
{
return m_active_time;
}
void update_active_time()
{
m_active_time = get_cur_time_ms();
}
string& get_string_ip()
{
return m_string_ip;
}
int64_t get_life_span() const
{
return m_life_span;
}
void set_life_span(int64_t lifetime)
{
m_life_span = lifetime;
}
bool is_expired()
{
return ((get_cur_time_ms() - m_active_time) > (m_life_span*1000));
}
protected:
int write_msg(u_char *buffer, int buf_len);
int read_head();
int notify_master(struct commu_msg &msg);
void reset_buffer();
protected:
int m_sockfd;
u32 m_cli_addr;
u32 m_cli_port;
string m_string_ip;
int64_t m_start_time;
int64_t m_active_time;
u_char *m_buff;
int m_n_read;
int m_msg_len;
bool m_has_head;
msg_type_t m_msg_type;
int64_t m_life_span;
};
#endif