Exemple #1
0
void MessageDispatcher::dispatchOnSessionEstablished(TcpSessionPtr  session)
{
    if (_vctOnSessionEstablished.empty())
    {
        return;
    }
    for (auto &fun : _vctOnSessionEstablished)
    {
        try
        {
            LCT("Entry dispatchOnSessionEstablished SessionID=" << session->getSessionID());
            fun(session);
            LCT("Leave dispatchOnSessionEstablished SessionID=" << session->getSessionID());
        }
        
        catch (std::runtime_error e)
        {
            LCE("Leave dispatchOnSessionEstablished Runtime Error: SessionID=" << session->getSessionID() << ", Error Message=\"" << e.what() << "\"");
        }
        catch (...)
        {
            LCE("Leave dispatchOnSessionEstablished Unknown Runtime Error: SessionID=" << session->getSessionID());
        }
    }
}
Exemple #2
0
bool MessageDispatcher::dispatchPreSessionMessage(TcpSessionPtr session, const char * blockBegin, zsummer::proto4z::Integer blockSize)
{
    if (_vctPreSessionDispatch.empty())
    {
        return true;
    }
    
    try
    {
        for (auto & fun : _vctPreSessionDispatch)
        {
            LCT("Entry dispatchOrgSessionMessage  SessionID=" << session->getSessionID() << ", blockSize=" << blockSize);
            if (!fun(session, blockBegin, blockSize))
            {
                return false;
            }
        }
    }
    catch (std::runtime_error e)
    {
        LCE("Leave dispatchOrgSessionMessage With Runtime Error:  SessionID=" << session->getSessionID() << ", Error Message=\"" << e.what() << "\"");
        return false;
    }
    catch (...)
    {
        LCE("Leave dispatchOrgSessionMessage With Unknown Runtime Error: SessionID=" << session->getSessionID());
        return false;
    }
    return true;
}
Exemple #3
0
void NetManager::event_onSessionDisconnect(TcpSessionPtr session)
{
	LOGT("NetManager::event_onSessionDisconnect. SessionID=" << session->getSessionID() << ", remoteIP=" << session->getRemoteIP() << ", remotePort=" << session->getRemotePort());

	if (isConnectID(session->getSessionID()))
	{
	}
	else
	{
		if (session->getUserParam() == SS_LOGINED)
		{
			auto info = UserManager::getRef().getInnerUserInfoBySID(session->getSessionID());
			if (info)
			{
				UserManager::getRef().userLogout(info);
				info->sID = InvalidSeesionID;
			}
		}

	}

	if (UserManager::getRef().getAllOnlineUserCount() == 0 && _onSafeClosed)
	{
		SessionManager::getRef().post(_onSafeClosed);
		_onSafeClosed = nullptr;
	}
}
Exemple #4
0
void NetMgr::msg_onHeartbeatEcho(TcpSessionPtr session, ReadStream & rs)
{
    auto status = session->getUserParamNumber(UPARAM_SESSION_STATUS);
    if (status != SSTATUS_UNKNOW && status != SSTATUS_PLAT_LOGINING)
    {
        session->setUserParam(UPARAM_LAST_ACTIVE_TIME, time(NULL));
    }
}
	void XAsioTCPServer::processAccept()
	{
		if ( m_acceptor )
		{
			TcpSessionPtr session = createTCPSession();
			m_acceptor->async_accept( *session->getSocket(), 
				m_strand.wrap( boost::bind( &XAsioTCPServer::onAcceptCallback, shared_from_this(), 
				session, boost::asio::placeholders::error ) ));
		}		
	}
 void OnSessionPulse(TcpSessionPtr session, unsigned int pulseInterval)
 {
     if (time(NULL) - session->getUserLParam() > pulseInterval/1000 * 3)
     {
         LOGI("remote session timeout. sID=" << session->getSessionID() << ", timeout=" << time(NULL) - session->getUserLParam());
         SessionManager::getRef().kickSession(session->getSessionID());
         return;
     }
     WriteStream pack(ID_Pulse);
     SessionManager::getRef().sendSessionData(session->getSessionID(), pack.getStream(), pack.getStreamLen());
 
 }
Exemple #7
0
void TcpServer::connectHandle(const error_code &err, const TcpSessionPtr &session)
{
    cout << err.message() << endl;
    if (!err)
    {
        m_tcpSessions.push_back(session);
        session->start();
        start();
    }
    else
    {
        session->close();
    }
}
Exemple #8
0
void SessionManager::removeSession(TcpSessionPtr session)
{
    _mapTcpSessionPtr.erase(session->getSessionID());
    if (session->getAcceptID() != InvalidAccepterID)
    {
        _mapAccepterOptions[session->getAcceptID()]._currentLinked--;
        _mapAccepterOptions[session->getAcceptID()]._totalAcceptCount++;
    }

    if (_stopClients == 2 || _stopServers == 2)
    {
        int clients = 0;
        int servers = 0;
        for (auto & kv : _mapTcpSessionPtr)
        {
            if (isSessionID(kv.first))
            {
                clients++;
            }
            else if (isConnectID(kv.first))
            {
                servers++;
            }
            else
            {
                LCE("error. invalid session id in _mapTcpSessionPtr");
            }
        }
        if (_stopClients == 2)
        {
            _stopClients = 3;
            if (_funClientsStop)
            {
                auto temp = std::move(_funClientsStop);
                temp();
            }
        }
        if (_stopServers == 2)
        {
            _stopServers = 3;
            if (_funServerStop)
            {
                auto temp = std::move(_funServerStop);
                temp();
            }
        }
    }
    
}
Exemple #9
0
void NetMgr::event_onSessionPulse(TcpSessionPtr session)
{
    if (isSessionID(session->getSessionID()))
    {
        if (time(NULL) - session->getUserParamNumber(UPARAM_LAST_ACTIVE_TIME) > session->getOptions()._sessionPulseInterval / 1000 * 2)
        {
            session->close();
            return;
        }
        Heartbeat hb;
        hb.timeStamp = (ui32)time(NULL);
        hb.timeTick = (unsigned int)getNowTick();
        sendMessage(session, hb);
    }
}
Exemple #10
0
static void _onSessionLinked(lua_State * L, TcpSessionPtr session)
{
    lua_pushcfunction(L, pcall_error);
    lua_rawgeti(L, LUA_REGISTRYINDEX, _linkedRef);
    lua_pushnumber(L, session->getSessionID());
    lua_pushstring(L, session->getRemoteIP().c_str());
    lua_pushnumber(L, session->getRemotePort());
    int status = lua_pcall(L, 3, 0, 1);
    lua_remove(L, 1);
    if (status)
    {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        LOGE(msg);
        lua_pop(L, 1);
    }
}
Exemple #11
0
void NetMgr::event_onClosed(TcpSessionPtr session)
{
    LOGD("NetMgr::event_onClosed. SessionID=" << session->getSessionID() << ", remoteIP=" << session->getRemoteIP() << ", remotePort=" << session->getRemotePort());

    if (isConnectID(session->getSessionID()))
    {
    }
    else
    {
        if (session->getUserParamNumber(UPARAM_SESSION_STATUS) == SSTATUS_LOGINED)
        {
            auto founder = _mapUserInfo.find(session->getUserParamNumber(UPARAM_USER_ID));
            if (founder == _mapUserInfo.end() || founder->second->sID != session->getSessionID())
            {
                _mapSession.erase(session->getSessionID());
                return;
            }
            
            event_onLogout(founder->second);
            founder->second->sID = InvalidSessionID;
        }
        _mapSession.erase(session->getSessionID());
    }

    if (_mapSession.size() == 0 && _onSafeClosed)
    {
        SessionManager::getRef().post(_onSafeClosed);
        _onSafeClosed = nullptr;
    }
}
Exemple #12
0
void NetMgr::msg_onAttachLogicReq(TcpSessionPtr session, ReadStream & rs)
{
    if (std::get<TupleParamNumber>(session->getUserParam(UPARAM_SESSION_STATUS)) != SSTATUS_UNKNOW)
    {
        return;
    }
    
    AttachLogicAck ack;
    ack.retCode = EC_SUCCESS;
    AttachLogicReq req;
    rs >> req;
    LOGD("enter msg_loginReq token=" << req.token << ", uID=" << req.uID);
    do 
    {
        auto info = getUserInfo(req.uID);
        if (!info)
        {
            ack.retCode = EC_TARGET_NOT_EXIST;
            break;
        }
        if (info->token.token != req.token)
        {
            ack.retCode = EC_PERMISSION_DENIED;
            break;
        }
        if (info->token.expire < time(NULL))
        {
            ack.retCode = EC_REQUEST_EXPIRE;
            break;
        }

        if (info->sID != InvalidSessionID)
        {
            event_onLogout(info);
            SessionManager::getRef().kickSession(info->sID);
            _mapSession.erase(info->sID);
        }

        info->sID = session->getSessionID();
        session->setUserParam(UPARAM_USER_ID, info->base.uID);
        session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_LOGINED);
        session->setUserParam(UPARAM_LAST_ACTIVE_TIME, time(NULL));
        session->setUserParam(UPARAM_LOGIN_TIME, time(NULL));
        _mapSession.insert(std::make_pair(session->getSessionID(), info));

        sendMessage(session, ack);
        session->setUserParam(UPARAM_LAST_ACTIVE_TIME, time(NULL));

        event_onLogin(info);
        
        return;
    } while (0);
    
    sendMessage(session, ack);
}
Exemple #13
0
//param: sID, {key=,value=}, {head kv}, body
static void _onWebMessage(lua_State * L, TcpSessionPtr session, const zsummer::network::PairString & commonLine,
                          const MapString &head, const std::string & body)
{
    lua_pushcfunction(L, pcall_error);
    lua_rawgeti(L, LUA_REGISTRYINDEX, _webMessageRef);
    lua_pushinteger(L, session->getSessionID());
    lua_newtable(L);
    lua_pushstring(L, commonLine.first.c_str());
    lua_setfield(L, -2, "key");
    lua_pushstring(L, commonLine.second.c_str());
    lua_setfield(L, -2, "value");
    lua_newtable(L);
    bool needUrlDecode = false;
    for (auto & hd : head)
    {
        lua_pushstring(L, hd.second.c_str());
        lua_setfield(L, -2, hd.first.c_str());
        if(hd.first.find("Content-Type") != std::string::npos
           && hd.second.find("application/x-www-form-urlencoded") != std::string::npos)
        {
            needUrlDecode = true;
        }
    }
    if (needUrlDecode)
    {
        std::string de = zsummer::proto4z::urlDecode(body);
        lua_pushlstring(L, de.c_str(), de.length());
    }
    else
    {
        lua_pushlstring(L, body.c_str(), body.length());
    }
    int status = lua_pcall(L, 4, 0, 1);
    lua_remove(L, 1);
    if (status)
    {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        LOGE("code crash when process web message. sID=" << session->getSessionID()
            << ", commond line=" << commonLine.first << " " << commonLine.second << ", body(max500byte)=" << body.substr(0, 500)
            << ", head=" << head);
        LOGE(msg);
        lua_pop(L, 1);
    }
}
Exemple #14
0
void NetManager::event_onSessionPulse(TcpSessionPtr session, unsigned int pulseInterval)
{
	if (isSessionID(session->getSessionID()))
	{
		if (session->getUserLParam() == SS_LOGINED || time(NULL) - session->getUserRParam() > pulseInterval * 2)
		{
			session->close();
			return;
		}

		WriteStream ws(ID_Heartbeat);
		Heartbeat hb;
		hb.timeStamp = (ui32)time(NULL);
		hb.timeTick = getNowTick();
		ws << hb;
		session->doSend(ws.getStream(), ws.getStreamLen());
	}
}
Exemple #15
0
    void msg_ResultSequence_fun(TcpSessionPtr session, ReadStream & rs)
    {
        if (g_hightBenchmark)
        {
            session->doSend(rs.getStream(), rs.getStreamLen());
        }
        else
        {
            EchoPack pack;
            rs >> pack;

            if (g_sendType == 0 || g_intervalMs == 0) //echo send
            {
                WriteStream ws(ID_EchoPack);
                ws << pack;
                session->doSend(ws.getStream(), ws.getStreamLen());
            }
        }
    };
Exemple #16
0
//param:sID, pID, content
static void _onMessage(lua_State * L, TcpSessionPtr session, const char * begin, unsigned int len)
{
    ReadStream rs(begin, len);
    lua_pushcfunction(L, pcall_error);
    lua_rawgeti(L, LUA_REGISTRYINDEX, _messageRef);
    lua_pushinteger(L, session->getSessionID());
    lua_pushinteger(L, rs.getProtoID());
    lua_pushlstring(L, rs.getStreamBody(), rs.getStreamBodyLen());
    int status = lua_pcall(L, 3, 0, 1);
    lua_remove(L, 1);
    if (status)
    {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        LOGE("code crash when process message. sID=" << session->getSessionID() << ", block len=" << len
            << ", block=" << zsummer::log4z::Log4zBinary(begin, len));
        LOGE(msg);
        lua_pop(L, 1);
    }
}
Exemple #17
0
void NetMgr::db_onFetchUsers(DBResultPtr result, TcpSessionPtr session)
{
    //loading guard over.
    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_LOGINED);

    PlatAuthAck ack;
    ack.retCode = EC_DB_ERROR;
    if (result->getErrorCode() != QEC_SUCCESS)
    {
        LOGE("Login::db_onFetchUsers have db error. error=" << result->getLastError() << ", sql=" << result->sqlString());
    }
    else
    {
        ack.retCode = EC_SUCCESS;
        try
        {
            BaseInfo info;
            while (result->haveRow())
            {
                *result >> info.uID;
                *result >> info.account;
                *result >> info.nickName;
                *result >> info.iconID;
                *result >> info.diamond;
                *result >> info.hisotryDiamond;
                *result >> info.giftDiamond;
                *result >> info.joinTime;
                ack.users.push_back(info);
                _mapAccounts[session->getUserParamString(UPARAM_ACCOUNT)][info.uID] = info;
            }

        }
        catch (std::runtime_error e)
        {
            ack.retCode = EC_DB_ERROR;
            LOGE("Login::db_onFetchUsers catch one exception. e=" << e.what() << ", sql=" << result->sqlString());
        }
    }
    sendMessage(session, ack);
}
Exemple #18
0
void NetMgr::msg_onSelectUserReq(TcpSessionPtr session, ReadStream & rs)
{
    if (session->getUserParamNumber(UPARAM_SESSION_STATUS) != SSTATUS_PLAT_LOGINED)
    {
        LOGE("NetMgr::msg_onSelectUserReq. status error. session id=" << session->getSessionID() << ", status = " << session->getUserParamNumber(UPARAM_SESSION_STATUS));
        session->close();
        return;
    }

    auto founder = _mapAccounts.find(session->getUserParamString(UPARAM_ACCOUNT));
    if (founder == _mapAccounts.end())
    {
        LOGW("Login::msg_onSelectUserReq session have no account info. sID=" << session->getSessionID());
        return;
    }

    SelectUserReq req;
    rs >> req;

    auto base = founder->second.find(req.uID);
    if (base == founder->second.end())
    {
        LOGW("Login::msg_onSelectUserReq session have no user info. sID=" << session->getSessionID());
        return;
    }


    MD5Data data;
    data.append(base->second.account);
    data.append(base->second.nickName);
    data.append(toString(rand()));

    //模拟通知logic刷新token
    auto ptr = getUserInfo(req.uID);
    if (!ptr)
    {
        return;
    }
    ptr->token.uID = req.uID;
    ptr->token.token = data.genMD5();
    ptr->token.expire = (unsigned int)time(NULL) + 600;

    //模拟断开连接
    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_UNKNOW);


    SelectUserAck ack;
    ack.retCode = EC_SUCCESS;
    ack.uID = req.uID;
    ack.token = data.genMD5();
    ack.ip = ServerConfig::getRef().getConfigListen(LogicServer)._wip;
    ack.port = ServerConfig::getRef().getConfigListen(LogicServer)._wport;

    sendMessage(session, ack);
}
Exemple #19
0
static void _onSessionClosed(lua_State * L, TcpSessionPtr session)
{
    if (_closedRef == LUA_NOREF)
    {
        LOGW("_onSessionClosed warning: cannot found ther callback.");
        return;
    }
    lua_pushcfunction(L, pcall_error);
    lua_rawgeti(L, LUA_REGISTRYINDEX, _closedRef);
    lua_pushnumber(L, session->getSessionID());
    lua_pushstring(L, session->getRemoteIP().c_str());
    lua_pushnumber(L, session->getRemotePort());
    int status = lua_pcall(L, 3, 0, 1);
    lua_remove(L, 1);
    if (status)
    {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        LOGE(msg);
        lua_pop(L, 1);
    }
}
Exemple #20
0
void MessageDispatcher::dispatchSessionMessage(TcpSessionPtr  session, ProtoID pID, zsummer::proto4z::ReadStream & msg)
{
    MapProtoDispatch::iterator iter = _mapSessionDispatch.find(pID);
    if ((iter == _mapSessionDispatch.end() || iter->second.empty()) && _vctDefaultSessionDispatch.empty())
    {
        LCW("Entry dispatchSessionMessage[" << pID << "] Failed: UNKNOWN ProtoID.  SessionID=" << session->getSessionID() << ", ProtoID=" << pID);
        return;
    }
    try
    {
        if (iter != _mapSessionDispatch.end() && !iter->second.empty())
        {
            for (auto & fun : iter->second)
            {
                LCT("Entry dispatchSessionMessage[" << pID << "]  SessionID=" << session->getSessionID());
                msg.resetMoveCursor();
                fun(session,  msg);
            }
        }
        else
        {
            for (auto & fun : _vctDefaultSessionDispatch)
            {
                msg.resetMoveCursor();
                fun(session, pID, msg);
            }
        }
        LCT("Leave dispatchSessionMessage[" << pID << "]  SessionID=" << session->getSessionID());
    }
    catch (std::runtime_error e)
    {
        LCE("Leave dispatchSessionMessage[" << pID << "] With Runtime Error: SessionID=" << session->getSessionID() << ", rsLen=" << msg.getStreamLen() << ", Error Message=\"" << e.what() << "\"");
    }
    catch (...)
    {
        LCE("Leave dispatchSessionMessage[" << pID << "] With Unknown Runtime Error: SessionID=" << session->getSessionID());
    }
}
Exemple #21
0
bool NetManager::on_preMessageProcess(TcpSessionPtr session, const char * blockBegin, zsummer::proto4z::Integer blockSize)
{
	ReadStream rs(blockBegin, blockSize);
	ProtoID pID = rs.getProtoID();
	if (pID >= 200)
	{
		if (session->getUserLParam() != SS_LOGINED)
		{
			LOGW("on_preMessageProcess check authorization failed. protoID=" << pID << ", session authorization status=" << session->getUserParam());
			return false;
		}
	}
	return true;
}
Exemple #22
0
bool  MessageDispatcher::dispatchSessionHTTPMessage(TcpSessionPtr session, const zsummer::proto4z::PairString & commonLine, const zsummer::proto4z::HTTPHeadMap &head, const std::string & body)
{
    try
    {
        for (auto & fun : _vctSessionHTTPMessage)
        {
            LCT("Entry dispatchSessionHTTPMessage  SessionID=" << session->getSessionID() << ", commond=" << commonLine.first << ", commondValue=" << commonLine.second
                << ", head count=" << head.size() << ", bodySize=" << body.length());
            fun(session, commonLine, head, body);
            LCT("Leave dispatchSessionHTTPMessage  SessionID=" << session->getSessionID());
        }
        return true;
    }
    catch (std::runtime_error e)
    {
        LCE("Leave dispatchSessionHTTPMessage With Runtime Error: SessionID=" << session->getSessionID() << ", Error Message=\"" << e.what() << "\"");
    }
    catch (...)
    {
        LCE("Leave dispatchSessionHTTPMessage With Unknown Runtime Error: SessionID=" << session->getSessionID());
    }
    return false;
}
Exemple #23
0
void MessageDispatcher::dispatchOnSessionPulse(TcpSessionPtr  session, unsigned int pulseInterval)
{
    if (_vctOnSessionPulse.empty())
    {
        return;
    }
    for (auto & fun : _vctOnSessionPulse)
    {
        try
        {
            LCT("Entry dispatchOnSessionPulse SessionID=" << session->getSessionID());
            fun(session, pulseInterval);
            LCT("Leave dispatchOnSessionPulse SessionID=" << session->getSessionID());
        }
        catch (std::runtime_error e)
        {
            LCE("Leave dispatchOnSessionPulse Runtime Error: SessionID=" << session->getSessionID() << ", Error Message=\"" << e.what() << "\"");
        }
        catch (...)
        {
            LCE("Leave dispatchOnSessionPulse Unknown Runtime Error: SessionID=" << session->getSessionID());
        }
    }
}
Exemple #24
0
void NetManager::msg_onLinkServerReq(TcpSessionPtr session, ProtoID pID, ReadStream & rs)
{
	LinkServerReq req;
	rs >> req;
	LOGD("enter msg_loginReq token=" << req.token << ", uID=" << req.uID);
	LinkServerAck ack;
	ack.retCode = EC_SUCCESS;

    auto inner = UserManager::getRef().getInnerUserInfo(req.uID);
    if (inner)
    {
		if (inner->token.token == req.token)
		{
			if (inner->token.tokenExpire > time(NULL))
			{
				ack.retCode = EC_SUCCESS;
				if (inner->sID != InvalidSeesionID)
				{
					UserManager::getRef().userLogout(inner);
				}

				inner->loginTime = time(NULL);
				inner->sID = session->getSessionID();

				session->setUserParam(inner->userInfo.uID);
				session->setUserLParam(SS_LOGINED);
				session->setUserRParam(time(NULL));
				WriteStream ws(ID_LinkServerAck);
				ws << ack;
				session->doSend(ws.getStream(), ws.getStreamLen());

				UserManager::getRef().userLogin(inner);
				return;
			}
			else
			{
				ack.retCode = EC_TOKEN_EXPIRE;
			}
		}
		else
		{
			ack.retCode = EC_INVALIDE_USER;
		}
		
    }
	else
	{
		ack.retCode = EC_INVALIDE_USER;
	}
    
	WriteStream ws(ID_LinkServerAck);
	ws << ack;
	session->doSend(ws.getStream(), ws.getStreamLen());
}
Exemple #25
0
    void SendFunc(TcpSessionPtr session, bool openTimer)
    {
        if (SessionManager::getRef()._totalSendMessages - SessionManager::getRef()._totalRecvMessages < 10000)
        {
            WriteStream ws(ID_EchoPack);
            EchoPack pack;
            TestIntegerData idata;
            idata._char = 'a';
            idata._uchar = 100;
            idata._short = 200;
            idata._ushort = 300;
            idata._int = 400;
            idata._uint = 500;
            idata._i64 = 600;
            idata._ui64 = 700;
            idata._ui128 = 255;

            TestFloatData fdata;
            fdata._float = (float)123123.111111;
            fdata._double = 12312312.88888;

            TestStringData sdata;
            sdata._string = "abcdefg";

            pack._iarray.push_back(idata);
            pack._iarray.push_back(idata);
            pack._farray.push_back(fdata);
            pack._farray.push_back(fdata);
            pack._sarray.push_back(sdata);
            pack._sarray.push_back(sdata);

            pack._imap.insert(std::make_pair("123", idata));
            pack._imap.insert(std::make_pair("223", idata));
            pack._fmap.insert(std::make_pair("323", fdata));
            pack._fmap.insert(std::make_pair("423", fdata));
            pack._smap.insert(std::make_pair("523", sdata));
            pack._smap.insert(std::make_pair("623", sdata));
            ws << pack;
            session->doSend(ws.getStream(), ws.getStreamLen());
        }
        if (openTimer)
        {
            SessionManager::getRef().createTimer(g_intervalMs, std::bind(&CStressClientHandler::SendFunc, this, session, !session->isInvalidSession()));
        }
    };
Exemple #26
0
void NetMgr::db_onCreateUser(DBResultPtr result, TcpSessionPtr session, const BaseInfo & info)
{
    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_LOGINED);
    CreateUserAck ack;
    ack.retCode = EC_SUCCESS;
    if (result->getErrorCode() != QEC_SUCCESS)
    {
        ack.retCode = EC_DB_ERROR;
        _mapAccounts[info.account].erase(info.uID);
    }
    else
    {
        //!模拟 通知logic服务器添加新的用户
        createUser(info);
        ack.users.push_back(info);
        //end
    }
    sendMessage(session, ack);
}
Exemple #27
0
static void _onSessionPulse(lua_State * L, TcpSessionPtr session)
{
    if (_pulseRef == LUA_NOREF)
    {
        LOGD("_onSessionPulse no callback.");
        return;
    }
    lua_pushcfunction(L, pcall_error);
    lua_rawgeti(L, LUA_REGISTRYINDEX, _pulseRef);
    lua_pushnumber(L, session->getSessionID());
    int status = lua_pcall(L, 1, 0, 1);
    lua_remove(L, 1);
    if (status)
    {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        LOGE(msg);
        lua_pop(L, 1);
    }
}
Exemple #28
0
void NetMgr::msg_onCreateUserReq(TcpSessionPtr session, ReadStream & rs)
{
    if (session->getUserParamNumber(UPARAM_SESSION_STATUS) != SSTATUS_PLAT_LOGINED)
    {
        LOGE("NetMgr::msg_onCreateUserReq. status error. session id=" << session->getSessionID() << ", status = " << session->getUserParamNumber(UPARAM_SESSION_STATUS));
        session->close();
        return;
    }

    auto founder = _mapAccounts.find(session->getUserParamString(UPARAM_ACCOUNT));
    if (founder != _mapAccounts.end() && founder->second.size() > MAX_ACCOUNT_USERS)
    {
        LOGW("Login::msg_onCreateUserReq  too many users. sID=" << session->getSessionID());
        return;
    }

    

    CreateUserReq req;
    rs >> req;
    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_CREATING);
    BaseInfo info;
    info.uID = _genID.genNewObjID();
    if (info.uID >= _genID.getMaxObjID() || info.uID < _genID.getMinObjID())
    {
        LOGE("gen UserID over the max user ID. cur ID=" << info.uID);
        return;
    }
    info.account = session->getUserParamString(UPARAM_ACCOUNT);
    info.nickName = req.nickName;
    info.iconID = req.iconID;
    info.joinTime = (ui32)time(NULL);
    _mapAccounts[session->getUserParamString(UPARAM_ACCOUNT)][info.uID] = info;

    std::string sql = BaseInfo_INSERT(info);
    DBMgr::getRef().infoAsyncQuery(sql, std::bind(&NetMgr::db_onCreateUser, this, _1, session, info));
}
Exemple #29
0
void TcpServer::notify(const FileDataPtr &pFileData, const TcpSessionPtr &pTcpSession)
{
    switch (pFileData->getFileType())
    {
    case FileData::VSERSION:
        {
            FileData data(FileData::VSERSION, getVersion());
            pTcpSession->sendData(data.dumps());
        }
        break;
    case FileData::FILEINFO:
        {
            FileData data(FileData::FILEINFO, getFileInfo());
            pTcpSession->sendData(data.dumps());
        }
        break;
    case FileData::FILE:
        {
            char buf[READ_FILE_SIZE] = {0};
            size_t size = 0;
            if (!readFile(buf, pTcpSession->getOffset(), READ_FILE_SIZE, size))
            {
                FileData data(FileData::CLOSE, "");
                pTcpSession->sendData(data.dumps());
            }
            else if (size > 0)
            {
                FileData data(FileData::FILE, size, buf);
                pTcpSession->sendData(data.dumps());
                pTcpSession->addOffset(size);
            }
        }
        break;
    case FileData::CLOSE:
        pTcpSession->close();
        break;
    default:
        break;
    }
}
Exemple #30
0
void NetMgr::msg_onPlatAuthReq(TcpSessionPtr session, ReadStream & rs)
{
    if (session->getUserParamNumber(UPARAM_SESSION_STATUS) != SSTATUS_UNKNOW)
    {
        LOGE("NetMgr::msg_onPlatAuthReq. status error .session id=" << session->getSessionID() << ", status = " << session->getUserParamNumber(UPARAM_SESSION_STATUS));
        session->close();
        return;
    }
    PlatAuthReq req;
    rs >> req;
    session->setUserParam(UPARAM_ACCOUNT, req.account);

    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_LOGINING);
    //goto plat auth
    //....
    //plat auth success.
    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_LOGINED);

    session->setUserParam(UPARAM_SESSION_STATUS, SSTATUS_PLAT_LOADING);
    zsummer::mysql::DBQuery q(" select `uID`,`account`,`nickName`,`iconID`,`diamond`,`hisotryDiamond`,`giftDiamond`,`joinTime` from `tb_BaseInfo` where `account` = ? ");
    q << req.account;
    DBMgr::getRef().infoAsyncQuery(q.popSQL(), std::bind(&NetMgr::db_onFetchUsers, this, _1, session));
}