//! 逻辑处理,转发消息到logic service int ffgate_t::route_logic_msg(const message_t& msg_, socket_ptr_t sock_) { session_data_t* session_data = sock_->get_data<session_data_t>(); LOGTRACE((FFGATE, "ffgate_t::route_logic_msg session_id[%s]", session_data->id())); client_info_t& client_info = m_client_set[session_data->id()]; if (client_info.request_queue.size() == MAX_MSG_QUEUE_SIZE) { //! 消息队列超限,关闭sock sock_->close(); return 0; } route_logic_msg_t::in_t msg; msg.session_id = session_data->id(); msg.cmd = msg_.get_cmd(); msg.body = msg_.get_body(); if (client_info.request_queue.empty()) { m_ffrpc->call(client_info.alloc_logic_service, msg, ffrpc_ops_t::gen_callback(&ffgate_t::route_logic_msg_callback, this, session_data->id(), sock_)); } else { client_info.request_queue.push(msg); } LOGTRACE((FFGATE, "ffgate_t::route_logic_msg end ok alloc_logic_service[%s]", client_info.alloc_logic_service)); return 0; }
//! 处理borker client 注册消息 int ffbroker_t::handle_client_register(register_broker_client_t::in_t& msg_, socket_ptr_t sock_) { LOGTRACE((BROKER, "ffbroker_t::handle_client_register begin")); if (msg_.service_name.empty() == false) { for (map<uint32_t, broker_client_info_t>::iterator it = m_broker_client_info.begin(); it != m_broker_client_info.end(); ++it) { broker_client_info_t& broker_client_info = it->second; if (broker_client_info.service_name == msg_.service_name) { sock_->close(); LOGTRACE((BROKER, "ffbroker_t::handle_client_register service<%s> has been registed", msg_.service_name.c_str())); return -1; } } } session_data_t* psession = new session_data_t(alloc_id()); sock_->set_data(psession); LOGTRACE((BROKER, "ffbroker_t::handle_client_register alloc node id<%u>,binder_broker[%u]", psession->get_node_id(), msg_.binder_broker_node_id)); broker_client_info_t& broker_client_info = m_broker_client_info[psession->get_node_id()]; broker_client_info.bind_broker_id = msg_.binder_broker_node_id; if (broker_client_info.bind_broker_id < 0) { broker_client_info.bind_broker_id = alloc_broker_id(); } broker_client_info.service_name = msg_.service_name; broker_client_info.sock = sock_; for (std::set<string>::iterator it = msg_.msg_names.begin(); it != msg_.msg_names.end(); ++it) { if (m_msg2id.find(*it) != m_msg2id.end()) { continue; } m_msg2id[*it] = alloc_id(); } sync_all_register_info(sock_); LOGTRACE((BROKER, "ffbroker_t::handle_client_register end ok")); return 0; }
//! 处理消息 int ffgate_t::handle_msg_impl(const message_t& msg_, socket_ptr_t sock_) { session_data_t* session_data = sock_->get_data<session_data_t>(); if (NULL == session_data)//! 还未验证sessionid { return verify_session_id(msg_, sock_); } else if (false == session_data->is_valid()) { //! sessionid再未验证之前,client是不能发送消息的 sock_->close(); return 0; } else { return route_logic_msg(msg_, sock_); } return 0; }
//! 验证sessionid 的回调函数 int ffgate_t::verify_session_callback(ffreq_t<session_verify_t::out_t>& req_, socket_ptr_t sock_) { LOGTRACE((FFGATE, "ffgate_t::verify_session_callback session_id[%s], err[%s]", req_.arg.session_id, req_.arg.err)); set<socket_ptr_t>::iterator it = m_wait_verify_set.find(sock_); if (it == m_wait_verify_set.end()) { //! 连接已经断开 return 0; } m_wait_verify_set.erase(it); if (false == req_.arg.err.empty() || req_.arg.session_id.empty()) { sock_->close(); return 0; } session_data_t* session_data = sock_->get_data<session_data_t>(); session_data->set_id(req_.arg.session_id); client_info_t& client_info = m_client_set[session_data->id()]; if (client_info.sock) { client_info.sock->close(); LOGINFO((FFGATE, "ffgate_t::verify_session_callback reconnect, close old session_id[%s]", req_.arg.session_id)); } client_info.sock = sock_; if (false == req_.arg.extra_data.empty()) { msg_sender_t::send(client_info.sock, 0, req_.arg.extra_data); } session_enter_scene_t::in_t enter_msg; enter_msg.session_id = session_data->id(); enter_msg.from_gate = m_gate_name; //enter_msg.from_scene = ""; enter_msg.to_scene = DEFAULT_LOGIC_SERVICE; //enter_msg.extra_data = ""; m_ffrpc->call(DEFAULT_LOGIC_SERVICE, enter_msg, ffrpc_ops_t::gen_callback(&ffgate_t::enter_scene_callback, this, session_data->id())); LOGTRACE((FFGATE, "ffgate_t::verify_session_callback end ok")); return 0; }
//! 验证sessionid int ffgate_t::verify_session_id(const message_t& msg_, socket_ptr_t sock_) { string ip = socket_op_t::getpeername(sock_->socket()); LOGTRACE((FFGATE, "ffgate_t::verify_session_id session_key[%s], ip[%s]", msg_.get_body(), ip)); if (ip.empty()) { sock_->close(); return -1; } session_data_t* session_data = new session_data_t(); sock_->set_data(session_data); //! 还未通过验证 m_wait_verify_set.insert(sock_); session_verify_t::in_t msg; msg.session_key = msg_.get_body(); msg.online_time = session_data->online_time; msg.gate_name = m_gate_name; msg.ip = ip; m_ffrpc->call(DEFAULT_LOGIC_SERVICE, msg, ffrpc_ops_t::gen_callback(&ffgate_t::verify_session_callback, this, sock_)); LOGTRACE((FFGATE, "ffgate_t::verify_session_id end ok")); return 0; }
//! 验证sessionid int ffgate_t::verify_session_id(const message_t& msg_, socket_ptr_t sock_) { string ip = socket_op_t::getpeername(sock_->socket()); LOGTRACE((FFGATE, "ffgate_t::verify_session_id session_key len=%u, ip[%s]", msg_.get_body().size(), ip)); if (ip.empty()) { sock_->close(); return -1; } session_data_t* session_data = new session_data_t(this->alloc_id()); sock_->set_data(session_data); m_wait_verify_set[session_data->socket_id].sock = sock_; session_first_entere_t::in_t msg; msg.cmd = msg_.get_cmd(); msg.socket_id = session_data->socket_id; msg.msg_body = msg_.get_body(); msg.gate_name = m_gate_name; msg.ip = ip; m_ffrpc->call(DEFAULT_LOGIC_SERVICE, msg, ffrpc_ops_t::gen_callback(&ffgate_t::verify_session_callback, this, session_data->socket_id)); LOGTRACE((FFGATE, "ffgate_t::verify_session_id end ok")); return 0; }
static void timer_close(socket_ptr_t s_) { s_->close(); }