xref: /f-stack/app/micro_thread/mt_session.cpp (revision 2d9fd380)
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  *  @time 20130924
23  **/
24 
25 #include "micro_thread.h"
26 #include "mt_session.h"
27 
28 using namespace std;
29 using namespace NS_MICRO_THREAD;
30 
31 ISession::~ISession()
32 {
33     if (_session_flg) {
34         SessionMgr* sessionmgr = SessionMgr::Instance();
35         sessionmgr->RemoveSession(_session_id);
36         _session_flg = (int)SESSION_IDLE;
37     }
38 }
39 
40 SessionMgr* SessionMgr::_instance = NULL;
41 SessionMgr* SessionMgr::Instance (void)
42 {
43     if (NULL == _instance)
44     {
45         _instance = new SessionMgr;
46     }
47 
48     return _instance;
49 }
50 
51 void SessionMgr::Destroy()
52 {
53     if( _instance != NULL )
54     {
55         delete _instance;
56         _instance = NULL;
57     }
58 }
59 
60 SessionMgr::SessionMgr()
61 {
62     _curr_session = 0;
63     _hash_map = new HashList(100000);
64 }
65 
66 SessionMgr::~SessionMgr()
67 {
68     if (_hash_map) {
69         delete _hash_map;
70         _hash_map = NULL;
71     }
72 }
73 
74 int SessionMgr::InsertSession(ISession* session)
75 {
76     if (!_hash_map || !session) {
77         MTLOG_ERROR("Mngr not init(%p), or session null(%p)", _hash_map, session);
78         return -100;
79     }
80 
81     int flag = session->GetSessionFlag();
82     if (flag & SESSION_INUSE) {
83         MTLOG_ERROR("Session already in hash, bugs, %p, %d", session, flag);
84         return -200;
85     }
86 
87     session->SetSessionFlag((int)SESSION_INUSE);
88     return _hash_map->HashInsert(session);
89 }
90 
91 ISession* SessionMgr::FindSession(int session_id)
92 {
93     if (!_hash_map) {
94         MTLOG_ERROR("Mngr not init(%p)", _hash_map);
95         return NULL;
96     }
97 
98     ISession key;
99     key.SetSessionId(session_id);
100     return dynamic_cast<ISession*>(_hash_map->HashFind(&key));
101 }
102 
103 void SessionMgr::RemoveSession(int session_id)
104 {
105     if (!_hash_map) {
106         MTLOG_ERROR("Mngr not init(%p)", _hash_map);
107         return;
108     }
109 
110     ISession key;
111     key.SetSessionId(session_id);
112     return _hash_map->HashRemove(&key);
113 }
114