• 奇迹世界服务器架构(2)


    b、代码实现连接

    连接每个服务器都用继承自ServerSession 的类实现

    有如下类

    AgentServerSession

    BattleServerSession

    FieldServerSession

    GameDBProxySession

    GuildServerSession

    MasterServerSession

    基类ServerSession 有 update 实现心跳连接

    VOID ServerSession::Update()
    {
    if( IsForConnect() )
    {
       // heartbeat 焊郴扁
       DWORD dwCurTick = GetTickCount();
       if( dwCurTick - m_dwLastHeartbeatTick > 10000 )
       {
        m_dwLastHeartbeatTick = dwCurTick;

        MSG_HEARTBEAT msg;
        msg.m_byCategory   = 0;
        msg.m_byProtocol   = SERVERCOMMON_HEARTBEAT;
        Send( (BYTE*)&msg, sizeof(MSG_HEARTBEAT) );
       }
    }
    }

    每个Session要连接服务器的时候

    VOID GameDBProxySession::OnConnect( BOOL bSuccess, DWORD dwSessionIndex )
    {
    ServerSession::OnConnect( bSuccess, dwSessionIndex );

    if( bSuccess )
    {
       ServerSession::SendServerType();

       g_pGameServer->ConnectTo( AGENT_SERVER );
    }
    else
    {
       //SUNLOG( eFULL_LOG, "Can't connect to game DB proxy." );
    }
    }

    VOID GameServer::ConnectTo( eSERVER_TYPE eServerType )
    {
    switch( eServerType )
    {
    case MASTER_SERVER:
       ConnectToServer( m_pMasterServerSession,
        (char*)m_pMasterServerSession->GetConnectIP().c_str(), m_pMasterServerSession->GetConnectPort() );
            break;

    case GAME_DBPROXY:
       ConnectToServer( m_pGameDBProxySession,
        (char*)m_pGameDBProxySession->GetConnectIP().c_str(), m_pGameDBProxySession->GetConnectPort() );
       break;

    case AGENT_SERVER:
       ConnectToServer( m_pAgentServerSession,
        (char*)m_pAgentServerSession->GetConnectIP().c_str(), m_pAgentServerSession->GetConnectPort() );
       break;
    case GUILD_SERVER:
       ConnectToServer( m_pGuildServerSession,
        (char*)m_pGuildServerSession->GetConnectIP().c_str(), m_pGuildServerSession->GetConnectPort() );
       break;

    default:
       ASSERT( !"弊繁 辑滚 鸥涝篮 绝绢夸" );
    }
    }

    DWORD GameServer::ConnectToServer( NetworkObject * pNetworkObject, char * pszIP, WORD wPort )
    {
    return m_pIOCPServer->Connect( SERVER_IOHANDLER, pNetworkObject, pszIP, wPort );
    }

    DWORD IOCPServer::Connect( DWORD dwIoHandlerKey, NetworkObject *pNetworkObject, char *pszIP, WORD wPort )
    {
    if( pNetworkObject == NULL ) return 0;

    IOHANDLER_MAP_ITER it = m_mapIoHandlers.find( dwIoHandlerKey );

    assert( it != m_mapIoHandlers.end() );

    return it->second->Connect( pNetworkObject, pszIP, wPort );
    }

    c、代码实现监听

    VOID GameServer::StartListen()
    {
    SERVER_ENV * pServerEnv = m_pFileParser->GetServerEnv();

    if( !m_pIOCPServer->IsListening( SERVER_IOHANDLER ) )
    {
       DISPMSG( "[GameServer::StartListen] Starting listen(%s:%d)...\n", pServerEnv->ServerIoHandler.szIP, pServerEnv->ServerIoHandler.wPort );
       if( !m_pIOCPServer->StartListen( SERVER_IOHANDLER, pServerEnv->ServerIoHandler.szIP, pServerEnv->ServerIoHandler.wPort ) )
       {
        DISP_FAIL;
        return ;
       }
       DISP_OK;
    }
    }

    BOOL IOCPServer::StartListen( DWORD dwIoHandlerKey, char *pIP, WORD wPort )
    {
    IOHANDLER_MAP_ITER it = m_mapIoHandlers.find( dwIoHandlerKey );

    assert( it != m_mapIoHandlers.end() );

    return it->second->StartListen( pIP, wPort );
    }

  • 相关阅读:
    Agile PLM opmn can not started
    参数化模块设计技术
    Enable the Load to CAD Action menu
    Agile Product Collaboration
    Configuration Handling SolidWorks
    SolidWorks 动画模拟仿真
    Agile PLM PC 模块实施方法论
    单因素方差分析的SAS实现
    非参中多样本的趋势秩检验的计算机实现
    游戏数据分析中“次日留存率”与“游戏生命周期第N天上线率”的SAS实现
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1366584.html
Copyright © 2020-2023  润新知