xref: /f-stack/app/micro_thread/mt_session.h (revision 7abd0fb2)
1 
2 /**
3  * Tencent is pleased to support the open source community by making MSEC available.
4  *
5  * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6  *
7  * Licensed under the GNU General Public License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License. You may
9  * obtain a copy of the License at
10  *
11  *     https://opensource.org/licenses/GPL-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the
14  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language governing permissions
16  * and limitations under the License.
17  */
18 
19 
20 /**
21  *  @file mt_session.h
22  *  @info ΢�̵߳��¼��Ự������, ÿ��������ӹ���һsession��Ϣ
23  *  @time 20130924
24  **/
25 
26 #ifndef __MT_SESSION_H__
27 #define __MT_SESSION_H__
28 
29 #include "hash_list.h"
30 
31 namespace NS_MICRO_THREAD {
32 
33 class MicroThread;
34 class IMtConnection;
35 
36 enum SESSION_FLAG
37 {
38     SESSION_IDLE    = 0,    ///< SESSION δ����hash����
39     SESSION_INUSE   = 1,    ///< SESSION �������״̬
40 };
41 
42 /**
43  * @brief ��������session�ӿڶ���, ����session��ӳ���thread,action��
44  */
45 class ISession : public HashKey
46 {
47 public:
48 
49     /**
50      * @brief ��������������
51      */
52     ISession() : _session_id(0), _session_flg(0), _thread(NULL), _connection(NULL) {};
53     virtual ~ISession();
54 
55 public:
56 
57     /**
58      * @brief �ỰID���������ȡ
59      */
60     void SetSessionId(int id) {
61         _session_id = id;
62     };
63     int GetSessionId() {
64         return _session_id;
65     };
66 
67     /**
68      * @brief �����̵߳��������ȡ
69      */
70     MicroThread* GetOwnerThread(){
71         return _thread;
72     };
73     void SetOwnerThread(MicroThread* thread) {
74         _thread = thread;
75     };
76 
77     /**
78      * @brief �������ӵ��������ȡ
79      */
80     IMtConnection* GetSessionConn(){
81         return _connection;
82     };
83     void SetSessionConn(IMtConnection* conn) {
84         _connection = conn;
85     };
86 
87     /**
88      * @brief �Ựflag���������ȡ
89      */
90     void SetSessionFlag(int flag) {
91         _session_flg = flag;
92     };
93     int GetSessionFlag() {
94         return _session_flg;
95     };
96 
97     /**
98      *  @brief �ڵ�Ԫ�ص�hash�㷨, ��ȡkey��hashֵ
99      *  @return �ڵ�Ԫ�ص�hashֵ
100      */
101     virtual uint32_t HashValue(){
102         return _session_id;
103     };
104 
105     /**
106      *  @brief �ڵ�Ԫ�ص�cmp����, ͬһͰID��, ��key�Ƚ�
107      *  @return �ڵ�Ԫ�ص�hashֵ
108      */
109     virtual int HashCmp(HashKey* rhs){
110         return this->_session_id - (int)rhs->HashValue();
111     };
112 
113 protected:
114 
115     int  _session_id;               // �Ựid��Ϣ
116     int  _session_flg;              // ��¼session״̬ 0 -����hash��, 1 -hash������
117     MicroThread* _thread;           // �Ự������session
118     IMtConnection* _connection;     // �Ự����������
119 };
120 
121 /**
122  * @brief ȫ�ֵ�session����ṹ
123  */
124 class SessionMgr
125 {
126 public:
127 
128     /**
129      * @brief �Ự�����ĵ�ȫ�ֹ������ӿ�
130      * @return ȫ�־��ָ��
131      */
132     static SessionMgr* Instance (void);
133 
134     /**
135      * @brief ȫ�ֵ�ɾ���ӿ�
136      */
137     static void Destroy();
138 
139     /**
140      * @brief ��ȡsessionid
141      * @return ȫ�־��ָ��
142      */
143     int GetSessionId(void) {
144         _curr_session++;
145         if (!_curr_session) {
146             _curr_session++;
147         }
148         return _curr_session;
149     };
150 
151     /**
152      * @brief Session���ݴ洢
153      */
154     int InsertSession(ISession* session);
155 
156     /**
157      * @brief ��ѯsession����
158      */
159     ISession* FindSession(int session_id);
160 
161     /**
162      * @brief ɾ��session����
163      */
164     void RemoveSession(int session_id);
165 
166     /**
167      * @brief ��������
168      */
169     ~SessionMgr();
170 
171 private:
172 
173     /**
174      * @brief ��Ϣbuff�Ĺ��캯��
175      */
176     SessionMgr();
177 
178     static SessionMgr * _instance;          ///<  ��������
179     int       _curr_session;                ///<  session����
180     HashList* _hash_map;                    ///<  ��sessionid hash�洢
181 };
182 
183 }
184 
185 #endif
186 
187 
188