xref: /f-stack/app/micro_thread/mt_session.cpp (revision a9643ea8)
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.cpp
22  *  @info ΢�̺߳�����ӻỰ����ʵ�ֲ���
23  *  @time 20130924
24  **/
25 
26 #include "micro_thread.h"
27 #include "mt_session.h"
28 
29 using namespace std;
30 using namespace NS_MICRO_THREAD;
31 
32 /**
33  * @brief session�ӿ���Դ�Ի��մ���
34  */
35 ISession::~ISession()
36 {
37     if (_session_flg) {
38         SessionMgr* sessionmgr = SessionMgr::Instance();
39         sessionmgr->RemoveSession(_session_id);
40         _session_flg = (int)SESSION_IDLE;   // ���⴦��, ��remove�����ڴ����Ӵ���
41     }
42 }
43 
44 
45 /**
46  * @brief sessionȫ�ֹ�����
47  * @return ȫ�־��ָ��
48  */
49 SessionMgr* SessionMgr::_instance = NULL;
50 SessionMgr* SessionMgr::Instance (void)
51 {
52     if (NULL == _instance)
53     {
54         _instance = new SessionMgr;
55     }
56 
57     return _instance;
58 }
59 
60 /**
61  * @brief session����ȫ�ֵ����ٽӿ�
62  */
63 void SessionMgr::Destroy()
64 {
65     if( _instance != NULL )
66     {
67         delete _instance;
68         _instance = NULL;
69     }
70 }
71 
72 /**
73  * @brief ��Ϣbuff�Ĺ��캯��
74  */
75 SessionMgr::SessionMgr()
76 {
77     _curr_session = 0;
78     _hash_map = new HashList(100000);
79 }
80 
81 /**
82  * @brief ��������, ��������Դ, ������������
83  */
84 SessionMgr::~SessionMgr()
85 {
86     if (_hash_map) {
87         delete _hash_map;
88         _hash_map = NULL;
89     }
90 }
91 
92 /**
93  * @brief Session���ݴ洢
94  */
95 int SessionMgr::InsertSession(ISession* session)
96 {
97     if (!_hash_map || !session) {
98         MTLOG_ERROR("Mngr not init(%p), or session null(%p)", _hash_map, session);
99         return -100;
100     }
101 
102     int flag = session->GetSessionFlag();
103     if (flag & SESSION_INUSE) {
104         MTLOG_ERROR("Session already in hash, bugs, %p, %d", session, flag);
105         return -200;
106     }
107 
108     session->SetSessionFlag((int)SESSION_INUSE);
109     return _hash_map->HashInsert(session);
110 }
111 
112 /**
113  * @brief ��ѯsession����
114  */
115 ISession* SessionMgr::FindSession(int session_id)
116 {
117     if (!_hash_map) {
118         MTLOG_ERROR("Mngr not init(%p)", _hash_map);
119         return NULL;
120     }
121 
122     ISession key;
123     key.SetSessionId(session_id);
124     return dynamic_cast<ISession*>(_hash_map->HashFind(&key));
125 }
126 
127 /**
128  * @brief ɾ��session����
129  */
130 void SessionMgr::RemoveSession(int session_id)
131 {
132     if (!_hash_map) {
133         MTLOG_ERROR("Mngr not init(%p)", _hash_map);
134         return;
135     }
136 
137     ISession key;
138     key.SetSessionId(session_id);
139     return _hash_map->HashRemove(&key);
140 }
141 
142 
143