void CDBServConn::_HandleAllUserResponse(CImPdu *pPdu) { IM::Buddy::IMAllUserRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); uint32_t user_id = msg.user_id(); uint32_t latest_update_time = msg.latest_update_time(); uint32_t user_cnt = msg.user_list_size(); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); uint32_t handle = attach_data.GetHandle(); log("HandleAllUserResponse, userId=%u, latest_update_time=%u, user_cnt=%u", user_id, latest_update_time, user_cnt); CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(user_id, handle); if (pMsgConn && pMsgConn->IsOpen()) { msg.clear_attach_data(); pPdu->SetPBMsg(&msg); pMsgConn->SendPdu(pPdu); } }
void CDBServConn::_HandleGetLatestMsgIDRsp(CImPdu *pPdu) { IM::Message::IMGetLatestMsgIdRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); uint32_t user_id = msg.user_id(); uint32_t session_id = msg.session_id(); uint32_t session_type = msg.session_type(); uint32_t latest_msg_id = msg.latest_msg_id(); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); uint32_t handle = attach_data.GetHandle(); log("HandleUnreadMsgCntResp, userId=%u, session_id=%u, session_type=%u, latest_msg_id=%u.", user_id, session_id, session_type, latest_msg_id); CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(user_id, handle); if (pMsgConn && pMsgConn->IsOpen()) { msg.clear_attach_data(); pPdu->SetPBMsg(&msg); pMsgConn->SendPdu(pPdu); } }
void CDBServConn::_HandlePushShieldResponse(CImPdu* pPdu) { IM::Login::IMPushShieldRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); uint32_t user_id = msg.user_id(); uint32_t result = msg.result_code(); log("_HandlePushShieldResponse: user_id=%u, result=%u.", user_id, result); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); uint32_t handle = attach_data.GetHandle(); CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(user_id, handle); if (pMsgConn && pMsgConn->IsOpen()) { msg.clear_attach_data(); pPdu->SetPBMsg(&msg); pMsgConn->SendPdu(pPdu); } else { log("_HandlePushShieldResponse: can't found msg_conn by user_id = %u, handle = %u", user_id, handle); } }
void CDBServConn::_HandleChangeSignInfoResponse(CImPdu* pPdu) { IM::Buddy::IMChangeSignInfoRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); uint32_t user_id = msg.user_id(); uint32_t result = msg.result_code(); log("HandleChangeSignInfoResp: user_id=%u, result=%u.", user_id, result); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); uint32_t handle = attach_data.GetHandle(); CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(user_id, handle); if (pMsgConn && pMsgConn->IsOpen()) { msg.clear_attach_data(); pPdu->SetPBMsg(&msg); pMsgConn->SendPdu(pPdu); } else { log("HandleChangeSignInfoResp: can't found msg_conn by user_id = %u, handle = %u", user_id, handle); } if (!result) { CRouteServConn* route_conn = get_route_serv_conn(); if (route_conn) { IM::Buddy::IMSignInfoChangedNotify notify_msg; notify_msg.set_changed_user_id(user_id); notify_msg.set_sign_info(msg.sign_info()); route_conn->SendPdu(SID_BUDDY_LIST, CID_BUDDY_LIST_SIGN_INFO_CHANGED_NOTIFY, notify_msg); } else { log("HandleChangeSignInfoResp: can't found route_conn"); } } }
void CDBServConn::_HandleMsgListResponse(CImPduMsgListResponse* pPdu) { uint32_t request_cmd_id = pPdu->GetRequestCmdId(); uint32_t from_user_id = pPdu->GetFromUserId(); uint32_t to_user_id = pPdu->GetToUserId(); uint32_t msg_cnt = pPdu->GetMsgCount(); server_msg_t* server_msg_list = pPdu->GetMsgList(); string to_id_url = idtourl(to_user_id); CDbAttachData attach_data(pPdu->GetAttachData(), pPdu->GetAttachLen()); uint32_t handle = attach_data.GetHandle(); CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(from_user_id, handle); log("HandleMsgListResp, req_cmd_id=%u, %u->%u, msg_cnt=%u\n", request_cmd_id, from_user_id, to_user_id, msg_cnt); if (pMsgConn && pMsgConn->IsOpen()) { uint32_t client_cmd_id = CID_MSG_UNREAD_MSG_RESPONSE; CImPduClientMsgListResponse pdu(client_cmd_id, to_id_url.c_str(), msg_cnt, server_msg_list); pdu.SetReserved(pPdu->GetReserved()); pMsgConn->SendPdu(&pdu); } }
void CDBServConn::_HandleValidateResponse(CImPduValidateResponse* pPdu) { string user_name(pPdu->GetUserName(), pPdu->GetUserNameLen()); uint32_t result = pPdu->GetResult(); CDbAttachData attach_data(pPdu->GetAttachData(), pPdu->GetAttachLen()); log("HandleValidateResp, user_name=%s, result=%d\n", user_name.c_str(), result); CImUser* pImUser = CImUserManager::GetInstance()->GetImUserByName(user_name); CMsgConn* pMsgConn = NULL; if (!pImUser) { // can not find the client connection, // maybe the client is closed before the DB response arrived // do nothing log("ImUser for user_name=%s not exist\n", user_name.c_str()); return; } else { pMsgConn = pImUser->GetUnValidateMsgConn(attach_data.GetHandle()); if (!pMsgConn || pMsgConn->IsOpen()) { log("no such connection or is validated, user_name=%s\n", user_name.c_str()); return; } } if (result != 0) { result = REFUSE_REASON_DB_VALIDATE_FAILED; } // validate OK, set client validate past, and send FriendListRequest to db storage server // else close the client connection if (result == 0) { user_info_t* user = pPdu->GetUserInfo(); pImUser->SetUser(user); pImUser->SetValidated(); uint32_t user_id = user->user_id; CImUserManager::GetInstance()->AddImUserById(user_id, pImUser); pImUser->KickOutSameClientType(pMsgConn->GetClientType(), pMsgConn); CRouteServConn* pRouteConn = get_route_serv_conn(); if (pRouteConn) { CImPduServerKickUser kickPdu(user_id, pMsgConn->GetClientType(), KICK_REASON_DUPLICATE_USER); pRouteConn->SendPdu(&kickPdu); } string token = create_uuid(); log("user_name: %s, uid: %d, token:%s\n", user_name.c_str(), user->user_id, token.c_str()); pMsgConn->SetToken(token); pMsgConn->SetOpen(); pMsgConn->SendUserActionLog(USER_ACTION_TYPE_LOGIN); pMsgConn->SendUserStatusUpdate(USER_STATUS_ONLINE); pImUser->ValidateMsgConn(token, pMsgConn); CImPduLoginResponse pduLR(result, pImUser->GetIMOnlineStatus(), user, (char*)token.c_str()); pduLR.SetReserved(pPdu->GetReserved()); pMsgConn->SendPdu(&pduLR); } else { CImPduLoginResponse pduLR(result); pduLR.SetReserved(pPdu->GetReserved()); pMsgConn->SendPdu(&pduLR); //pMsgConn->Close(); } }
void CDBServConn::_HandleValidateResponse(CImPdu* pPdu) { IM::Server::IMValidateRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); string login_name = msg.user_name(); uint32_t result = msg.result_code(); string result_string = msg.result_string(); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); log("HandleValidateResp, user_name=%s, result=%d", login_name.c_str(), result); CImUser* pImUser = CImUserManager::GetInstance()->GetImUserByLoginName(login_name); CMsgConn* pMsgConn = NULL; if (!pImUser) { log("ImUser for user_name=%s not exist", login_name.c_str()); return; } else { pMsgConn = pImUser->GetUnValidateMsgConn(attach_data.GetHandle()); if (!pMsgConn || pMsgConn->IsOpen()) { log("no such conn is validated, user_name=%s", login_name.c_str()); return; } } if (result != 0) { result = IM::BaseDefine::REFUSE_REASON_DB_VALIDATE_FAILED; } if (result == 0) { IM::BaseDefine::UserInfo user_info = msg.user_info(); uint32_t user_id = user_info.user_id(); CImUser* pUser = CImUserManager::GetInstance()->GetImUserById(user_id); if (pUser) { pUser->AddUnValidateMsgConn(pMsgConn); pImUser->DelUnValidateMsgConn(pMsgConn); if (pImUser->IsMsgConnEmpty()) { CImUserManager::GetInstance()->RemoveImUserByLoginName(login_name); delete pImUser; } } else { pUser = pImUser; } pUser->SetUserId(user_id); pUser->SetNickName(user_info.user_nick_name()); pUser->SetValidated(); //帐号验证通过 CImUserManager::GetInstance()->AddImUserById(user_id, pUser); pUser->KickOutSameClientType(pMsgConn->GetClientType(), IM::BaseDefine::KICK_REASON_DUPLICATE_USER, pMsgConn); CRouteServConn* pRouteConn = get_route_serv_conn(); if (pRouteConn) { IM::Server::IMServerKickUser msg2; msg2.set_user_id(user_id); msg2.set_client_type((::IM::BaseDefine::ClientType)pMsgConn->GetClientType()); msg2.set_reason(1); //1是什么鬼 pRouteConn->SendPdu(SID_OTHER, CID_OTHER_SERVER_KICK_USER, msg2); } log("user_name: %s, uid: %d", login_name.c_str(), user_id); pMsgConn->SetUserId(user_id); pMsgConn->SetOpen(); pMsgConn->SendUserStatusUpdate(IM::BaseDefine::USER_STATUS_ONLINE); pUser->ValidateMsgConn(pMsgConn->GetHandle(), pMsgConn); IM::Login::IMLoginRes msg3; msg3.set_server_time(time(NULL)); msg3.set_result_code(IM::BaseDefine::REFUSE_REASON_NONE); msg3.set_result_string(result_string); msg3.set_online_status((IM::BaseDefine::UserStatType)pMsgConn->GetOnlineStatus()); IM::BaseDefine::UserInfo* user_info_tmp = msg3.mutable_user_info(); user_info_tmp->set_user_id(user_info.user_id()); user_info_tmp->set_user_gender(user_info.user_gender()); user_info_tmp->set_user_nick_name(user_info.user_nick_name()); user_info_tmp->set_avatar_url(user_info.avatar_url()); user_info_tmp->set_sign_info(user_info.sign_info()); user_info_tmp->set_department_id(user_info.department_id()); user_info_tmp->set_email(user_info.email()); user_info_tmp->set_user_real_name(user_info.user_real_name()); user_info_tmp->set_user_tel(user_info.user_tel()); user_info_tmp->set_user_domain(user_info.user_domain()); user_info_tmp->set_status(user_info.status()); pMsgConn->SendPdu(SID_LOGIN, CID_LOGIN_RES_USERLOGIN, msg3); } else { IM::Login::IMLoginRes msg4; msg4.set_server_time(time(NULL)); msg4.set_result_code((IM::BaseDefine::ResultType)result); msg4.set_result_string(result_string); pMsgConn->SendPdu(SID_LOGIN, CID_LOGIN_RES_USERLOGIN, msg4); pMsgConn->Close(); } }
void CDBServConn::_HandleValidateResponse(CImPdu* pPdu) { IM::Server::IMValidateRsp msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); string login_name = msg.user_name(); uint32_t result = msg.result_code(); string result_string = msg.result_string(); CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length()); struct timeval now; gettimeofday(&now, NULL); uint32_t rand_num = rand(); string strKey = int2string(now.tv_usec % 10000) + int2string(now.tv_sec) + int2string(rand_num); strKey += strKey; uint32_t nKeyLen = strKey.length(); if (nKeyLen < 32) { for (uint32_t i=nKeyLen; i<32; i++) strKey += '0'; } else { strKey = strKey.substr(0, 32); } log("HandleValidateResp, user_name=%s, result=%d, key=%s", login_name.c_str(), result, strKey.c_str()); CImUser* pImUser = CImUserManager::GetInstance()->GetImUserByLoginName(login_name); CMsgConn* pMsgConn = NULL; if (!pImUser) { log("ImUser for user_name=%s not exist", login_name.c_str()); return; } else { pMsgConn = pImUser->GetUnValidateMsgConn(attach_data.GetHandle()); if (!pMsgConn || pMsgConn->IsOpen()) { log("no such conn is validated, user_name=%s", login_name.c_str()); return; } log("_HandleValidateResponse, user=%s(%p) conn=%p", login_name.c_str(), pImUser, pMsgConn); } if (result != 0) { result = IM::BaseDefine::REFUSE_REASON_DB_VALIDATE_FAILED; } log("_HandleValidateResponse, login_user=%s key=%s %p ", login_name.c_str(), strKey.c_str(), pMsgConn); total_users++; switch(pMsgConn->GetClientType()) { case IM::BaseDefine::CLIENT_TYPE_WINDOWS: case IM::BaseDefine::CLIENT_TYPE_MAC: pc_total_users++; break; case IM::BaseDefine::CLIENT_TYPE_ANDROID: android_total_users++; break; case IM::BaseDefine::CLIENT_TYPE_IOS: ios_total_users++; break; case IM::BaseDefine::CLIENT_TYPE_WEB: web_total_users++; break; } if (result == 0) { IM::BaseDefine::UserInfo user_info = msg.user_info(); uint32_t user_id = user_info.user_id(); CImUser* pUser = CImUserManager::GetInstance()->GetImUserById(user_id); if (pUser) { log("_HandleValidateResponse, get user %u(%p) key=%s %p", user_id, pUser, strKey.c_str(), pMsgConn); pUser->AddUnValidateMsgConn(pMsgConn); pImUser->DelUnValidateMsgConn(pMsgConn); if (pImUser->IsMsgConnEmpty()) { CImUserManager::GetInstance()->RemoveImUserByLoginName(login_name); delete pImUser; } } else { pUser = pImUser; log("_HandleValidateResponse, use login user %u(%p) key=%s %p", user_id, pUser, strKey.c_str(), pMsgConn); } pUser->SetUserId(user_id); pUser->SetNickName(user_info.user_nick_name()); pUser->SetValidated(); CImUserManager::GetInstance()->AddImUserById(user_id, pUser); log("user_name: %s, uid: %d(%p)", login_name.c_str(), user_id, pMsgConn); pMsgConn->SetKey(strKey); pMsgConn->SetUserId(user_id); pMsgConn->SetOpen(); pMsgConn->SendUserStatusUpdate(IM::BaseDefine::USER_STATUS_ONLINE); CRouteServConn* pRouteConn = get_route_serv_conn(); if (pRouteConn) { IM::Server::IMServerKickUser msg2; msg2.set_user_id(user_id); msg2.set_client_type((::IM::BaseDefine::ClientType)pMsgConn->GetClientType()); msg2.set_reason(1); CImPdu pdu; pdu.SetPBMsg(&msg2); pdu.SetServiceId(SID_OTHER); pdu.SetCommandId(CID_OTHER_SERVER_KICK_USER); pRouteConn->SendPdu(&pdu); } pUser->ValidateMsgConn(pMsgConn->GetHandle(), pMsgConn); //pUser->KickOutSameClientType(pMsgConn->GetClientType(), IM::BaseDefine::KICK_REASON_DUPLICATE_USER, user_id); pUser->KickOutSameClientType(pMsgConn->GetClientType(), IM::BaseDefine::KICK_REASON_DUPLICATE_USER, pMsgConn); IM::Login::IMLoginRes msg3; msg3.set_server_time(time(NULL)); msg3.set_result_code(IM::BaseDefine::REFUSE_REASON_NONE); msg3.set_result_string(result_string); msg3.set_online_status((IM::BaseDefine::UserStatType)pMsgConn->GetOnlineStatus()); msg3.set_client_key(strKey); IM::BaseDefine::UserInfo* user_info_tmp = msg3.mutable_user_info(); user_info_tmp->set_user_id(user_info.user_id()); user_info_tmp->set_user_gender(user_info.user_gender()); user_info_tmp->set_user_nick_name(user_info.user_nick_name()); user_info_tmp->set_avatar_url(user_info.avatar_url()); user_info_tmp->set_sign_info(user_info.sign_info()); user_info_tmp->set_department_id(user_info.department_id()); user_info_tmp->set_email(user_info.email()); user_info_tmp->set_user_real_name(user_info.user_real_name()); user_info_tmp->set_user_tel(user_info.user_tel()); user_info_tmp->set_user_domain(user_info.user_domain()); user_info_tmp->set_status(user_info.status()); user_info_tmp->set_user_type(user_info.user_type()); log("id:%u gender:%u nick:%s avatar:%s sign:%s depid:%u email:%s name:%s tel:%s uname:%s status:%u type:%u", user_info.user_id(), user_info.user_gender(), user_info.user_nick_name().c_str(), user_info.avatar_url().c_str(), user_info.sign_info().c_str(), user_info.department_id(), user_info.email().c_str(), user_info.user_real_name().c_str(), user_info.user_tel().c_str(), user_info.user_domain().c_str(), user_info.status(), user_info.user_type()); CImPdu pdu2; pdu2.SetPBMsg(&msg3); pdu2.SetServiceId(SID_LOGIN); pdu2.SetCommandId(CID_LOGIN_RES_USERLOGIN); pdu2.SetSeqNum(pPdu->GetSeqNum()); pMsgConn->SendPdu(&pdu2); } else { fail_users++; IM::Login::IMLoginRes msg4; msg4.set_server_time(time(NULL)); msg4.set_result_code((IM::BaseDefine::ResultType)result); msg4.set_result_string(result_string); CImPdu pdu3; pdu3.SetPBMsg(&msg4); pdu3.SetServiceId(SID_LOGIN); pdu3.SetCommandId(CID_LOGIN_RES_USERLOGIN); pdu3.SetSeqNum(pPdu->GetSeqNum()); pMsgConn->SendPdu(&pdu3); pMsgConn->Close(); } }