f-stack/app/micro_thread/mt_session.h

188 lines
3.9 KiB
C
Raw Normal View History

2017-04-21 10:43:26 +00:00
/**
* Tencent is pleased to support the open source community by making MSEC available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the GNU General Public License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* https://opensource.org/licenses/GPL-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
/**
* @file mt_session.h
* @info ΢<EFBFBD>̵߳<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><EFBFBD><EFBFBD>һsession<EFBFBD><EFBFBD>Ϣ
* @time 20130924
**/
#ifndef __MT_SESSION_H__
#define __MT_SESSION_H__
#include "hash_list.h"
namespace NS_MICRO_THREAD {
class MicroThread;
class IMtConnection;
enum SESSION_FLAG
{
SESSION_IDLE = 0, ///< SESSION δ<><CEB4><EFBFBD><EFBFBD>hash<73><68><EFBFBD><EFBFBD>
SESSION_INUSE = 1, ///< SESSION <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>session<EFBFBD>ӿڶ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>session<EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>thread,action<EFBFBD><EFBFBD>
*/
class ISession : public HashKey
{
public:
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
ISession() : _session_id(0), _session_flg(0), _thread(NULL), _connection(NULL) {};
virtual ~ISession();
public:
/**
* @brief <EFBFBD>ID<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
*/
void SetSessionId(int id) {
_session_id = id;
};
int GetSessionId() {
return _session_id;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
*/
MicroThread* GetOwnerThread(){
return _thread;
};
void SetOwnerThread(MicroThread* thread) {
_thread = thread;
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
*/
IMtConnection* GetSessionConn(){
return _connection;
};
void SetSessionConn(IMtConnection* conn) {
_connection = conn;
};
/**
* @brief <EFBFBD>flag<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
*/
void SetSessionFlag(int flag) {
_session_flg = flag;
};
int GetSessionFlag() {
return _session_flg;
};
/**
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hash<EFBFBD>, <EFBFBD><EFBFBD>ȡkey<EFBFBD><EFBFBD>hashֵ
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
*/
virtual uint32_t HashValue(){
return _session_id;
};
/**
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>cmp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ͬһͰID<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>key<EFBFBD>Ƚ<EFBFBD>
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
*/
virtual int HashCmp(HashKey* rhs){
return this->_session_id - (int)rhs->HashValue();
};
protected:
int _session_id; // <20>Ựid<69><64>Ϣ
int _session_flg; // <20><>¼session״̬ 0 -<2D><><EFBFBD><EFBFBD>hash<73><68>, 1 -hash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MicroThread* _thread; // <20><EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>session
IMtConnection* _connection; // <20><EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
/**
* @brief ȫ<EFBFBD>ֵ<EFBFBD>session<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class SessionMgr
{
public:
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ȫ<EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
* @return ȫ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
*/
static SessionMgr* Instance (void);
/**
* @brief ȫ<EFBFBD>ֵ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*/
static void Destroy();
/**
* @brief <EFBFBD><EFBFBD>ȡsessionid
* @return ȫ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
*/
int GetSessionId(void) {
_curr_session++;
if (!_curr_session) {
_curr_session++;
}
return _curr_session;
};
/**
* @brief Session<EFBFBD><EFBFBD><EFBFBD>ݴ
*/
int InsertSession(ISession* session);
/**
* @brief <EFBFBD><EFBFBD>ѯsession<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
ISession* FindSession(int session_id);
/**
* @brief ɾ<EFBFBD><EFBFBD>session<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void RemoveSession(int session_id);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
~SessionMgr();
private:
/**
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
SessionMgr();
static SessionMgr * _instance; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int _curr_session; ///< session<6F><6E><EFBFBD><EFBFBD>
HashList* _hash_map; ///< <20><>sessionid hash<73>
};
}
#endif