最后一站 session相关


这里写图片描述


ucnet.h

#ifndef UCODE_UCNET_H_
#define UCODE_UCNET_H_

#include "ucbase.h"
#include "ucconfig.h"
#include "uclogger.h"

namespace UCODE{

/**
 * @brief UCNet组件模块版本
 */
static const SUCODEVersion UCNET_MODULE_VERSION = {1, 0, 0, 0};

/**
 * @brief UCNet module name
 */
const char UCNET_MODULENAME[] = "UCNet";

/**
 * @brief UCNet version
 */
const SUCODEVersion UCNET_VERSION = UCNET_MODULE_VERSION;

/**
 * @brief Error code of UCNet
 */
enum ESDNetErrCode{
    NET_BIND_FAIL   = -7,
    NET_CONNECT_FAIL    = -6,
    NET_SYSTEM_ERROR    = -5, 
    NET_RECV_ERROR      = -4, 
    NET_SEND_ERROR      = -3, 
    NET_SEND_OVERFLOW   = -2,
    NET_PACKET_ERROR    = -1,
    NET_SUCCESS         = 0
};


/**
 * @brief 创建ISDConnector或ISDListener时,用户需要指定网络I/O模型。
 */


/**
 * @brief 完成端口
 */
const UINT32 NETIO_COMPLETIONPORT   = 0x0001; 

/**
 * @brief 完成端口,使用sdgate组件
 */
const UINT32 NETIO_COMPLETIONPORT_GATE   = 0x0002;

/**
 * @brief 每次收到数据时,在网络线程中回调给应用
 */
const UINT32 NET_THREADED   = 0x0004;   

/**
 * @brief 每次收到数据时,将在主线程中回调给应用
 */
const UINT32 NET_NONETHREADED   = 0x0008;          

/**
 * @brief 异步I/O的select网络模型,客户端用。
 */
const UINT32 NETIO_ASYNCSELECT      = 2;

/**
 * @brief linux下epoll网络模型
 */
const UINT32 NETIO_EPOLL            = 0x0010;
/**
 * @brief linux下epoll网络模型,选用sdgate组件
 */
const UINT32 NETIO_EPOLL_GATE       = 0x0020;      

const UINT32 INVALID_IO_TYPE        = 0xFFFFFFFF;

const UINT32 UNKNOWN_SIZE   = 0xFFFFFFFF;

class ISDPacketParser;
class ISDSession;
class ISDSessionFactory;

/**
 * @brief An ISDConnection object represent an abstraction of a TCP connection
 */
class ISDConnection
{
public:
    /**
     * @brief Check if the connection is connected
     * @return true means connected, otherwise, false
     */
    virtual bool UCAPI IsConnected(void) = 0;

    /**
     * @brief Send data on the connection
     * @param pBuf : data buffer need to send
     * @param dwLen : data buffer length
     */
    virtual void UCAPI Send(const char* pBuf,UINT32 dwLen) = 0;

    /**
     * @brief Set extension options
     * @param dwType : option type
     * @param pOpt : option value
     */
    virtual void UCAPI SetOpt(UINT32 dwType, void* pOpt) = 0;

    /**
     * @brief Close the TCP connection
     */
    virtual void UCAPI Disconnect(void) = 0;

    /**
     * @brief Retrieves remote IP address for the connection
     * @return IP address four bytes unsigned integer
     */
    virtual const UINT32 UCAPI GetRemoteIP(void) = 0;

    /**
     * @brief Retrieves remote IP address in text format for the connection
     * @return IP address string
     */
    virtual const char* UCAPI GetRemoteIPStr(void) = 0;

    /**
     * @brief Retrieves remote port number for the connection
     * @return port number
     */
    virtual UINT16 UCAPI GetRemotePort(void) = 0;

    /**
     * @brief Retrieves local IP address for the connection
     * @return IP address four bytes unsigned integer
     */
    virtual const UINT32 UCAPI GetLocalIP(void) = 0;

    /**
     * @brief Retrieves local IP address in text format for the connection
     * @return IP address string
     */
    virtual const char* UCAPI GetLocalIPStr(void) = 0;

    /**
     * @brief Retrieves local port number for the connection
     * @return port number
     */
    virtual UINT16 UCAPI GetLocalPort(void) = 0;

    /**
     * @brief Retrieves local send buffer's free size
     * @return local send buffer's free size in bytes
     */
    virtual UINT32 UCAPI GetSendBufFree(void) = 0;
};

/**
 * @brief The interface provides functionality for TCP listening
 */
class ISDListener
{
public:
    /**
     * @brief Set an user implemented ISDPacketParser object, which process
     *        packet parse logic of the connection accepted by the ISDListener
     * @param poPacketParser ISDPacketParser instance pointer
     */
    virtual void UCAPI SetPacketParser(ISDPacketParser* poPacketParser) = 0;

    /**
     * @brief Set the user implemented ISDSessionFactory object to ISDListener
     *
     * When a TCP connection is accepted by ISDListener,
     * ISDSessionFactory object will be asked to create a ISDSession
     * @param poSessionFactory ISDSessionFactory instance factory
     */
    virtual void UCAPI SetSessionFactory(ISDSessionFactory* poSessionFactory) = 0;

    /**
     * @brief Set the send and receive buffer size of the connection accepted by the ISDListener object
     * @param dwRecvBufSize : receiving buffer size in bytes
     * @param dwSendBufSize : sending buffer size in bytes
     */
     virtual void UCAPI SetBufferSize(UINT32 dwRecvBufSize, UINT32 dwSendBufSize) = 0;

    /**
     * @brief Set extension options
     * @param dwType : option type
     * @param pOpt : option value
     */
    virtual void UCAPI SetOpt(UINT32 dwType, void* pOpt) = 0;

    /**
     * @brief Listen at specified IP and port
     * @param pszIP : IP string
     * @param wPort : port number
     * @param bReUseAddr : the flag for re-using same address
     * @return true means success, false means failure
     */
    virtual bool UCAPI Start(const char* pszIP, UINT16 wPort, bool bReUseAddr = true) = 0;

    /**
     * @brief Stop listening
     * @return true means success, false means failure
     */
    virtual bool UCAPI Stop(void) = 0;

    /**
     * @brief Release the ISDListener object
     */
    virtual void UCAPI Release(void) = 0;
};

/**
 * @brief The interface provides functionality for initiative TCP connection
 */
class ISDConnector
{
public:
    /**
     * @brief Set the user implemented ISDPacketParser object, which process
     *        packet parse logic of the connection connected by the ISDConnector
     *        object
     * @param poPakcetParser ISDPacketParser instance pointer
     */
    virtual void UCAPI SetPacketParser(ISDPacketParser* poPakcetParser) = 0;

    /**
     * @brief Set the user implemented ISDSession object, which will be bind
     *        to ISDConnection object when connection establishes
     * @param poSession ISDSession instance pointer
     */
     virtual void UCAPI SetSession(ISDSession* poSession) = 0;

    /**
     * @brief Set the send and receive buffer size for the connection connected
     *        by the ISDConnector object
     * @param dwRecvBufSize : receiving buffer size in bytes
     * @param dwSendBufSize : sending buffer size in bytes
     */
    virtual void UCAPI SetBufferSize(UINT32 dwRecvBufSize, UINT32 dwSendBufSize) = 0;

    /**
     * @brief Connect to the specified address
     * @param pszIP : IP string
     * @param wPort : port number
     * @return true means success, false means failure
     */
    virtual bool UCAPI Connect(const char* pszIP, UINT16 wPort) = 0;

    /**
     * @brief Reconnect to the address which is specified by the last Connect call
     * @return true means success, false means failure
     */
    virtual bool UCAPI ReConnect(void) = 0;

    /**
     * @brief Release the ISDConnector object
     */
    virtual void UCAPI Release(void) = 0;

    /**
     * @brief Set extension options
     * @param dwType : option type
     * @param pOpt : option value
     */
    virtual void UCAPI SetOpt(UINT32 dwType, void* pOpt) = 0;
};

/**
 * @brief Interface of SDNet module
 */
class IUCNet : public ISDBase
{
public:
    /**
     * @brief Create an ISDConnector object
     * @param dwNetIOType : Network I/O type
     * @return ISDConnector instance pointer
     */
    virtual ISDConnector* UCAPI CreateConnector(UINT32 dwNetIOType) = 0;

    /**
     * @brief Create an ISDListener object
     * @param dwNetIOType : Network I/O type
     * @return ISDListener instance pointer
     */
    virtual ISDListener* UCAPI CreateListener(UINT32 dwNetIOType)  = 0;

    /**
     * @brief Process network event
     * @param nCount : the count of loop
     * @return true means success, false means failure
     */
    virtual bool UCAPI Run(INT32 nCount = -1) = 0;
};

/**
 * @brief Break packet from TCP data stream. The interface need to be implemented by user
 */
class ISDPacketParser
{
public:
    /**
     * @brief Return the length in bytes of the packet in TCP data stream
     * @param pBuf : the data buffer need to parse
     * @param dwLen : the data buffer length
     */
    virtual INT32 UCAPI ParsePacket(const char* pBuf, UINT32 dwLen) = 0;
};

/**
 * @brief When network event happens, the related function of ISDSession
 *        will be callback. User need to implement ISDSession
 */
class ISDSession
{
public:
    /**
     * @brief Set the ISDConnection object to the ISDSession object
     * @param poConnection : ISDConnection instance pointer
     */
    virtual void UCAPI SetConnection(ISDConnection* poConnection) = 0;

    /**
     * @brief Callback when the connection is established
     */
    virtual void UCAPI OnEstablish(void) = 0;

    /**
     * @brief Callback when the connection is closed
     */
    virtual void UCAPI OnTerminate(void) = 0;

    /**
     * @brief Callback when TCP connection has error
     */
    virtual bool UCAPI OnError(INT32 nModuleErr, INT32 nSysErr) = 0;

    /**
     * @brief Callback when receive a packet, which is parsed by ISDPacketParser
     * @param pBuf : data buffer pointer which hold data
     * @param dwLen : buffer length
     */
    virtual void UCAPI OnRecv(const char* pBuf, UINT32 dwLen) = 0;

#ifdef SDNET_HAS_SEND_REPORT

    /**
     * @brief Callback when send  success, used in asynch mode
     * @param pBuf : left data in buffer
     * @param left : data length  in buffer
     * @warning don't send the data again
     */
    virtual void UCAPI OnSend(const char* pBuf, UINT32 dwLen) 
    {
    }
#endif

    /**
     * @brief Release ISDSession object
     */
    virtual void UCAPI Release(void) = 0;
};

/**
 * @brief Create user implemented ISDSession object. This interface need to be implemented by user too
 */
class ISDSessionFactory
{
public:
    /**
     * @brief Create an user implemented ISDSession object
     * @param poConnection : ISDConnection instance pointer
     * @return ISDSession instance pointer
     */
    virtual ISDSession* UCAPI CreateSession(ISDConnection* poConnection) = 0;
};

/**
 * @brief Get an SDNet object
 * @param pstVersion : the version of SDNet
 * @return IUCNet instance pointer
 */
UCODE_NET_FUN IUCNet* UCAPI SDNetGetModule(const SUCODEVersion* pstVersion);

/**
 * @brief Function pointer definition of SDNetGetModule
 */
typedef IUCNet* (UCAPI *PFN_SDNetGetModule)(const SUCODEVersion* pstVersion);

/**
 * @brief Set ISDLogger object to the SDNet. The log of SDNet will export to the ISDLogger object
 * @param poLogger : ISDLogger instance pointer
 * @param dwLevel : log level
 * @return true means success, false means failure
 */
UCODE_NET_FUN bool UCAPI SDNetSetLogger(ISDLogger* poLogger, UINT32 dwLevel);

/**
 * @brief Function pointer definition of SDNetSetLogger
 */
typedef bool (UCAPI *PFN_SDNetSetLogger)(ISDLogger* poLogger, UINT32 dwLevel);

/**
 * @brief Set extension options
 * @param dwType : option type
 * @param pOpt : option value
 */
UCODE_NET_FUN void UCAPI SDNetSetOpt(UINT32 dwType, void* pOpt);

}

#endif


INetSession.h

class INetSession : public ISDSession
{
public:
    INetSession();
    virtual ~INetSession();
public:
    UINT32 GetID() { return mID; }
    void SetID(UINT32 id) { mID=id; }
    UINT32 GetLogicID() { return mLogicID; }
    void SetLogicID(UINT32 logicId) { mLogicID = logicId; }
    UINT32 GetType() { return mType; }
    void SetType(UINT32 type) { mType = type; }
    void SetSendBufferMaxSize(UINT32 maxsize) { mSendBufferMaxSize = maxsize; }
    void Close();
    void DelayClose(UINT32 error_code,UINT32 delayCount=10);
    bool IsConned() { if(mConnection && mConnection->IsConnected()) return true; return false; }
    const char* GetRemoteIp() { return mConnection!=NULL?mConnection->GetRemoteIPStr():""; }
    UINT32 GetRemoteIpEx() { return mConnection!=NULL?mConnection->GetRemoteIP():0; }
    UINT32 GetRemotePort() { return mConnection!=NULL?mConnection->GetRemotePort():0; }

    bool IsClosed() { return mCloseTime>0; }
    void SetInited(bool isInited,bool isTrigger=false);//call this func will trigger any logic.
    bool Send(const char* pBuf,UINT32 dwLen);
    void SetMsgHandle(IMsgHandle* pMsgHandle) { mMsgHandle = pMsgHandle; }
    virtual void Update();
protected:
    bool IsHadRecon() { return mReconFlag; }
    void ResetRecon() { mReconFlag = false; }
    bool IsInited() { return mIsInited; }
    bool IsRemoteInited() { return mIsRemoteInited; }
    void SetRemoteInited(bool isInited) { mIsRemoteInited = isInited; }
protected:
    virtual void SendInitData() = 0;
    virtual void OnRealEstablish() = 0;
    virtual void OnClose() = 0;
public:
    virtual void UCAPI SetConnection(ISDConnection* poConnection);
    virtual void UCAPI OnEstablish(void);
    virtual void UCAPI OnTerminate(void);
    virtual bool UCAPI OnError(INT32 nModuleErr, INT32 nSysErr);
    virtual void UCAPI OnRecv(const char* pBuf, UINT32 dwLen);
    virtual void UCAPI Release(void) {}
protected:
    bool mIsInited;
    bool mIsRemoteInited;
    bool mReconFlag;
    bool mIsUseBuffer;
    bool mIsSrvCli;
    volatile bool mIsLogicClosed;
    UINT32 mType;
    UINT32 mLogicID;
    UINT32 mID;
    static UINT32 mMaxID;
    ISDConnection* mConnection;
    UINT32 mStartTime;
    UINT32 mCloseTime;
    vector<char> mBuffers;
    IMsgHandle* mMsgHandle;
    volatile UINT32 mCloseDelayCount;
    UINT32 mSendBufferMaxSize;//send buffer max size//
};


ICliSession.h


class ICliSession : public INetSession
{
public:
ICliSession();
virtual ~ICliSession();
public:
bool Reconnect();
bool Reconnect(const char* ip, UINT32 port);
void SetConnector(ISDConnector* connector) { m_poConnector=connector; }
void SetRemoteEndPointer(const char* ip,UINT32 port);
virtual void Update();
void SetCanReconnectTag(bool can){mReconnectTag = can;}
bool GetCanReconnectTag(){return mReconnectTag;}
public:
virtual void UCAPI OnEstablish(void);
virtual void UCAPI Release(void) {/触发重连,不释放该对象/}
private:
ISDConnector* m_poConnector;
UINT32 mReconTime;
string mRemoteEndPointer;
bool mReconnectTag;
};



ISrvCliSession .h



class ISrvCliSession : public INetSession
{
public:
    ISrvCliSession();
    virtual ~ISrvCliSession();
public:
    virtual void UCAPI Release(void) {delete this;/*释放该对象*/}
};


class ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* poConnection) = 0;
};

class CGateSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

下面是BSNetSessionMgr.cpp

#include "../stdafx.h"
#include "BSNetSessionMgr.h"
#include "B2LSession.h"
#include "GateSession.h"
#include "ClientSession.h"

CGateSessionFactory gGateSessionFactory;
CClientSessionFactory gClientSessionFactory;

CBSNetSessionMgr::CBSNetSessionMgr()
{

}

CBSNetSessionMgr::~CBSNetSessionMgr()
{

}

ICliSession* UCAPI CBSNetSessionMgr::CreateConnectorSession(SESSION_TYPE type)
{
    ICliSession* pSession = NULL;
    if (type==ST_CLIENT_B2L)
    {
        pSession = new CB2LSession();
        pSession->SetType(ST_CLIENT_B2L);
        pSession->SetMsgHandle(&CB2LSession::mHandles);
    }
    return pSession;
}

ISDSession* UCAPI CGateSessionFactory::CreateSession( ISDConnection* pConnection )
{
    CGateSession* pSession = new CGateSession();
    pSession->SetType(ST_SERVER_BS_OnlyGS);
    pSession->SetMsgHandle(&CGateSession::mHandles);
    return pSession;
}

ISDSession* UCAPI CClientSessionFactory::CreateSession( ISDConnection* pConnection )
{
    CClientSession* pSession = new CClientSession();
    pSession->SetType(ST_SERVER_BS_OnlyGC);
    pSession->SetMsgHandle(&CClientSession::mHandles);
    return pSession;
}

下面是BSNetSessionMgr.h

#pragma once

#include "../../../Share/Net/INetSessionMgr.h"

class CBSNetSessionMgr : public INetSessionMgr
{
public:
    CBSNetSessionMgr();
    virtual ~CBSNetSessionMgr();
public:
    bool getnetip(string& ipaddr,int pos)
    {
        char host_name[255];
        if (gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR) {
            return false;
        }
        struct hostent *phe = gethostbyname(host_name);
        if (phe == 0) {
            return false;
        }
        for (int i = 0; phe->h_addr_list[i] != 0; ++i) 
        {
            if (pos==i)
            {
                struct in_addr addr;
                memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
                ipaddr = inet_ntoa(addr);
                return true;
            }
        }
        return false;
    }
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection) { return NULL; /*重写*/}
    virtual ICliSession* UCAPI CreateConnectorSession(SESSION_TYPE type);
};

class CGateSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

class CClientSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

extern CGateSessionFactory gGateSessionFactory;
extern CClientSessionFactory gClientSessionFactory;

下面是CSNetSessionMgr.cpp

#include "../stdafx.h"
#include "CSNetSessionMgr.h"
#include "GateSession.h"
#include "SceneSession.h"
#include "RemoteConsoleSession.h"
#include "CSLogSession.h"
#include "RedisSession.h"
#include "LogicRedisSession.h"

CGateSessionFactory gGateSessionFactory;
CSceneSessionFactory gSceneSessionFactory;
CRemoteConsoleSessionFactory gRemoteConsoleFactory;

CSNetSessionMgr::CSNetSessionMgr()
{

}

CSNetSessionMgr::~CSNetSessionMgr()
{

}

ICliSession* UCAPI CSNetSessionMgr::CreateConnectorSession(SESSION_TYPE type){
    ICliSession* pSession = NULL;
    if (type==ST_CLIENT_C2R)
    {
        pSession = new CRedisSession();
        pSession->SetType(ST_CLIENT_C2R);
        pSession->SetMsgHandle(&CRedisSession::mHandles);
    }
    else if (type==ST_CLIENT_C2LogicRedis){
        pSession = new CLogicRedisSession();
        pSession->SetType(ST_CLIENT_C2LogicRedis);
    }
    else if (type == ST_CLIENT_C2Log){
        pSession = new CCSLogSession();
        pSession->SetType(ST_CLIENT_C2Log);
        pSession->SetMsgHandle(&CCSLogSession::mHandles);
    }
    return pSession;
}

bool CSNetSessionMgr::CreateConnector(SESSION_TYPE type, const char* ip, int port, int recvsize, int sendsize, int logicId)
{
    ISDConnector* pConnector = mNetModule->CreateConnector(NETIO_COMPLETIONPORT);
    ICliSession* pSession = CreateConnectorSession(type);
    UINT32 sId = AddSession(pSession);
    Assert(sId!=PP_INVALID);
    pSession->SetID(sId);
    pSession->SetLogicID(logicId);
    pSession->SetConnector(pConnector);
    pSession->SetRemoteEndPointer(ip,port);
    pConnector->SetSession(pSession);
    if (type == ST_CLIENT_C2R || type == ST_CLIENT_C2LogicRedis) 
        pConnector->SetPacketParser(&m_CSParser);
    else 
        pConnector->SetPacketParser(this);
    pConnector->SetBufferSize(recvsize,sendsize);
    return pConnector->Connect(ip,port);
}

ISDSession* UCAPI CGateSessionFactory::CreateSession( ISDConnection* pConnection )
{
    CGateSession* pSession = new CGateSession();
    pSession->SetType(ST_SERVER_CS_OnlyGS);
    pSession->SetMsgHandle(&CGateSession::mHandles);
    return pSession;
}

ISDSession* UCAPI CSceneSessionFactory::CreateSession( ISDConnection* pConnection )
{
    CSceneSession* pSession = new CSceneSession();
    pSession->SetType(ST_SERVER_CS_OnlySS);
    pSession->SetMsgHandle(&CSceneSession::mHandles);
    return pSession;
}

ISDSession* UCAPI CRemoteConsoleSessionFactory::CreateSession( ISDConnection* pConnection )
{
    CRemoteConsoleSession* pSession = new CRemoteConsoleSession();
    pSession->SetType(ST_SERVER_CS_OnlyRC);
    pSession->SetMsgHandle(&CRemoteConsoleSession::mHandles);
    return pSession;
}


下面是CSNetSessionMgr.h

#pragma once

#include "../../Share/Net/INetSessionMgr.h"
#include "ParsePacket.h"

class CSNetSessionMgr : public INetSessionMgr
{
public:
    CSNetSessionMgr();
    virtual ~CSNetSessionMgr();
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection) { return NULL; /*重写*/}
    virtual ICliSession* UCAPI CreateConnectorSession(SESSION_TYPE type);
    virtual bool CreateConnector(SESSION_TYPE type, const char* ip, int port, int recvsize, int sendsize, int logicId);

private:
    CSParser m_CSParser;
};

class CGateSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

class CSceneSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

class CRemoteConsoleSessionFactory : public ISDSessionFactory
{
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
};

extern CGateSessionFactory gGateSessionFactory;
extern CSceneSessionFactory gSceneSessionFactory;
extern CRemoteConsoleSessionFactory gRemoteConsoleFactory;


下面是GSNetSessionMgr.cpp



#include "stdafx.h"
#include "GSNetSessionMgr.h"
#include "ClientSession.h"
#include "M2CSession.h"
#include "M2SSession.h"
#include "M2BSession.h"

SProfileNet gProfileNet = {0};

GSNetSessionMgr::GSNetSessionMgr()
{

}

GSNetSessionMgr::~GSNetSessionMgr()
{

}

ISDSession* UCAPI GSNetSessionMgr::CreateSession( ISDConnection* pConnection )
{
    CClientSession* pSession = new CClientSession();
    pSession->SetType(ST_SERVER_GS);
    pSession->SetMsgHandle(&CClientSession::mHandles);
    return pSession;
}

ICliSession* UCAPI GSNetSessionMgr::CreateConnectorSession( SESSION_TYPE type )
{
    ICliSession* pSession = NULL;
    if (type==ST_CLIENT_G2C)
    {
        pSession = new CM2CSession();
        pSession->SetType(ST_CLIENT_G2C);
        pSession->SetMsgHandle(&CM2CSession::mHandles);
    }
    else if (type==ST_CLIENT_G2S)
    {
        pSession = new CM2SSession();
        pSession->SetType(ST_CLIENT_G2S);
        pSession->SetMsgHandle(&CM2SSession::mHandles);
    }
    else if (type==ST_CLIENT_G2B)
    {
        pSession = new CM2BSession();
        pSession->SetType(ST_CLIENT_G2B);
        pSession->SetMsgHandle(&CM2BSession::mHandles);
    }
    return pSession;
}


下面是GSNetSessionMgr .h

#pragma once

#include "../../Share/Net/INetSessionMgr.h"
#include "ClientSession.h"

struct SProfileNet
{
    UINT32 mClientCount;
    UINT32 mTimeForDealWithMsg;//处理消息时间//
    UINT32 mTimeForDealWithOther;//其他网络时间//
    std::map<UINT32,UINT32> mMessageCount;//消息数量//
};

extern SProfileNet gProfileNet;

class GSNetSessionMgr : public INetSessionMgr
{
public:
    GSNetSessionMgr();
    virtual ~GSNetSessionMgr();
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
    virtual ICliSession* UCAPI CreateConnectorSession(SESSION_TYPE type);
};


下面是SSBattleNetSessionMgr.h

#pragma once

#include "../../Share/Net/INetSessionMgr.h"

class SSBattleNetSessionMgr : public INetSessionMgr
{
public:
    SSBattleNetSessionMgr();
    virtual ~SSBattleNetSessionMgr();
public:
    virtual ISDSession* UCAPI CreateSession(ISDConnection* pConnection);
    virtual ICliSession* UCAPI CreateConnectorSession(SESSION_TYPE type);
};


SSBattleNetSessionMgr.cpp

#include "../stdafx.h"
#include "SSBattleNetSessionMgr.h"
#include "GateSession.h"
#include "CentralSession.h"
#include "SSLogSession.h"

SSBattleNetSessionMgr::SSBattleNetSessionMgr()
{
    mIsUnSafeSend = true;
}

SSBattleNetSessionMgr::~SSBattleNetSessionMgr()
{

}

ISDSession* UCAPI SSBattleNetSessionMgr::CreateSession( ISDConnection* pConnection )
{
    CGateSession* pSession = new CGateSession();
    pSession->SetType(ST_SERVER_SS);
    pSession->SetMsgHandle(&CGateSession::mHandles);
    return pSession;
}

ICliSession* UCAPI SSBattleNetSessionMgr::CreateConnectorSession( SESSION_TYPE type )
{
    ICliSession* pSession = NULL;
    if (type==ST_CLIENT_S2C)
    {
        pSession = new CCentralSession();
        pSession->SetType(ST_CLIENT_S2C);
        pSession->SetMsgHandle(&CCentralSession::mHandles);
    }
    else if (type == ST_CLIENT_S2Log){
        pSession = new CSSLogSession();
        pSession->SetType(ST_CLIENT_S2Log);
        pSession->SetMsgHandle(&CSSLogSession::mHandles);
    }
    return pSession;
}


下面是ucnet.h

class ISDSessionFactory
{
public:
    /**
     * @brief Create an user implemented ISDSession object
     * @param poConnection : ISDConnection instance pointer
     * @return ISDSession instance pointer
     */
    virtual ISDSession* UCAPI CreateSession(ISDConnection* poConnection) = 0;
};


FR:海涛高软(hunk Xu) QQ技术交流群:386476712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值