xref: /f-stack/app/micro_thread/mt_session.h (revision 9bd490e8)
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  *  @time 20130924
23  **/
24 
25 #ifndef __MT_SESSION_H__
26 #define __MT_SESSION_H__
27 
28 #include "hash_list.h"
29 
30 namespace NS_MICRO_THREAD {
31 
32 class MicroThread;
33 class IMtConnection;
34 
35 enum SESSION_FLAG
36 {
37     SESSION_IDLE    = 0,
38     SESSION_INUSE   = 1,
39 };
40 
41 class ISession : public HashKey
42 {
43 public:
44 
45     ISession() : _session_id(0), _session_flg(0), _thread(NULL), _connection(NULL) {};
46     virtual ~ISession();
47 
48 public:
49 
50     void SetSessionId(int id) {
51         _session_id = id;
52     };
53     int GetSessionId() {
54         return _session_id;
55     };
56 
57     MicroThread* GetOwnerThread(){
58         return _thread;
59     };
60     void SetOwnerThread(MicroThread* thread) {
61         _thread = thread;
62     };
63 
64     IMtConnection* GetSessionConn(){
65         return _connection;
66     };
67     void SetSessionConn(IMtConnection* conn) {
68         _connection = conn;
69     };
70 
71     void SetSessionFlag(int flag) {
72         _session_flg = flag;
73     };
74     int GetSessionFlag() {
75         return _session_flg;
76     };
77 
78     virtual uint32_t HashValue(){
79         return _session_id;
80     };
81 
82     virtual int HashCmp(HashKey* rhs){
83         return this->_session_id - (int)rhs->HashValue();
84     };
85 
86 protected:
87 
88     int  _session_id;
89     int  _session_flg;
90     MicroThread* _thread;
91     IMtConnection* _connection;
92 };
93 
94 class SessionMgr
95 {
96 public:
97 
98     static SessionMgr* Instance (void);
99 
100     static void Destroy();
101 
102     int GetSessionId(void) {
103         _curr_session++;
104         if (!_curr_session) {
105             _curr_session++;
106         }
107         return _curr_session;
108     };
109 
110     int InsertSession(ISession* session);
111 
112     ISession* FindSession(int session_id);
113 
114     void RemoveSession(int session_id);
115 
116     ~SessionMgr();
117 
118 private:
119 
120     SessionMgr();
121 
122     static SessionMgr * _instance;
123     int       _curr_session;
124     HashList* _hash_map;
125 };
126 
127 }
128 
129 #endif
130