Beispiel #1
0
//! 当有连接断开,则被回调
int ffbroker_t::handle_broken_impl(socket_ptr_t sock_)
{
    if (NULL == sock_->get_data<session_data_t>())
    {
        sock_->safe_delete();
        return 0;
    }
    uint32_t node_id = sock_->get_data<session_data_t>()->get_node_id();
    if (node_id == BROKER_MASTER_NODE_ID && false == is_master())
    {
        //! slave 连接到master 的连接断开,重连
        //! 设置定时器重连
        m_broker_client_info.clear();
        m_timer.once_timer(RECONNECT_TO_BROKER_TIMEOUT, task_binder_t::gen(&route_call_reconnect, this));
    }
    else
    {
        m_slave_broker_sockets.erase(node_id);
        m_broker_client_info.erase(node_id);
    }

    if (is_master())
    {
        //! 如果是master, //!如果是slave broker断开连接,需要为原来的service重新分配slave broker
        sync_all_register_info(NULL);
    }
    delete sock_->get_data<session_data_t>();
    sock_->set_data(NULL);
    sock_->safe_delete();
    return 0;
}
Beispiel #2
0
//! 处理连接断开
int ffgate_t::handle_broken_impl(socket_ptr_t sock_)
{
    LOGTRACE((FFGATE, "ffgate_t::broken begin"));
    session_data_t* session_data = sock_->get_data<session_data_t>();
    if (NULL == session_data)
    {
        sock_->safe_delete();
        return 0;
    }
    
    if (false == session_data->is_valid())
    {
        //! 还未通过验证
        m_wait_verify_set.erase(sock_);
    }
    else
    {
        client_info_t& client_info = m_client_set[session_data->id()];
        if (client_info.sock == sock_)
        {
            session_offline_t::in_t msg;
            msg.session_id  = session_data->id();
            msg.online_time = session_data->online_time;
            m_ffrpc->call(DEFAULT_LOGIC_SERVICE, msg);
            m_client_set.erase(session_data->id());
        }
    }
    LOGTRACE((FFGATE, "ffgate_t::broken session_id[%s]", session_data->id()));
    delete session_data;
    sock_->set_data(NULL);
    sock_->safe_delete();
    return 0;
}
Beispiel #3
0
//! 处理连接断开
int ffgate_t::handle_broken(socket_ptr_t sock_)
{
    session_data_t* session_data = sock_->get_data<session_data_t>();
    if (NULL == session_data)
    {
        LOGDEBUG((FFGATE, "ffgate_t::broken ignore"));
        sock_->safe_delete();
        return 0;
    }
    
    LOGTRACE((FFGATE, "ffgate_t::broken begin"));
    if (false == session_data->is_valid())
    {
        //! 还未通过验证
        m_wait_verify_set.erase(session_data->socket_id);
    }
    else
    {
        client_info_t& client_info = m_client_set[session_data->id()];
        if (client_info.sock == sock_)
        {
            session_offline_t::in_t msg;
            msg.session_id  = session_data->id();
            m_ffrpc->call(client_info.alloc_logic_service, msg);
            m_client_set.erase(session_data->id());
        }
    }
    LOGTRACE((FFGATE, "ffgate_t::broken session_id[%ld]", session_data->id()));
    delete session_data;
    sock_->set_data(NULL);
    sock_->safe_delete();
    return 0;
}
Beispiel #4
0
//! 逻辑处理,转发消息到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;
}
Beispiel #5
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;
}
Beispiel #6
0
int msg_bus_t::handle_broken(socket_ptr_t sock_)
{
    lock_guard_t lock(m_mutex);
    if (m_socket == sock_)
    {
        m_socket = NULL;
    }

    sock_->safe_delete();
    return 0;    
}
Beispiel #7
0
//! 处理borker slave 注册消息
int ffbroker_t::handle_slave_register(register_slave_broker_t::in_t& msg_, socket_ptr_t sock_)
{
    LOGTRACE((BROKER, "ffbroker_t::handle_slave_register begin"));
    session_data_t* psession = new session_data_t(alloc_id());
    sock_->set_data(psession);
    slave_broker_info_t& slave_broker_info = m_slave_broker_sockets[psession->get_node_id()];
    slave_broker_info.host = msg_.host;
    slave_broker_info.sock = sock_;
    sync_all_register_info(sock_);
    LOGTRACE((BROKER, "ffbroker_t::handle_slave_register end ok new node id[%u]", psession->get_node_id()));
    return 0;
}
Beispiel #8
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;
}
Beispiel #9
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;
}
Beispiel #10
0
int ffrpc_t::handle_broken_impl(socket_ptr_t sock_)
{
    if (NULL == sock_->get_data<session_data_t>())
    {
        sock_->safe_delete();
        return 0;
    }
    if (BROKER_MASTER_NODE_ID == sock_->get_data<session_data_t>()->get_node_id())
    {
        m_master_broker_sock = NULL;
        //! 连接到broker master的连接断开了
        map<uint32_t, slave_broker_info_t>::iterator it = m_slave_broker_sockets.begin();//! node id -> info
        for (; it != m_slave_broker_sockets.end(); ++it)
        {
            slave_broker_info_t& slave_broker_info = it->second;
            delete slave_broker_info.sock->get_data<session_data_t>();
            slave_broker_info.sock->set_data(NULL);
            slave_broker_info.sock->close();
        }
        m_slave_broker_sockets.clear();//! 所有连接到broker slave的连接断开
        m_ffslot_interface.clear();//! 注册的接口清除
        m_ffslot_callback.clear();//! 回调函数清除
        m_msg2id.clear();//! 消息映射表清除
        m_broker_client_info.clear();//! 各个服务的记录表清除
        m_broker_client_name2nodeid.clear();//! 服务名到node id的映射
        //! 设置定时器重练
        m_timer.once_timer(RECONNECT_TO_BROKER_TIMEOUT, task_binder_t::gen(&route_call_reconnect, this));
    }
    else
    {
        m_slave_broker_sockets.erase(sock_->get_data<session_data_t>()->get_node_id());
    }
    delete sock_->get_data<session_data_t>();
    sock_->set_data(NULL);
    sock_->safe_delete();
    return 0;
}
Beispiel #11
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;
}
Beispiel #12
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;
}
Beispiel #13
0
static void timer_close(socket_ptr_t s_)
{
    s_->close();
}
Beispiel #14
0
int broker_service_t::handle_broken(socket_ptr_t sock_)
{
    logwarn((BROKER, "broker_service_t::handle_broken bagin soket_ptr<%p>", sock_));
    lock_guard_t lock(m_mutex);

    vector<uint32_t> del_sgid;
    vector<uint32_t> del_sid;
    vector<uint32_t> del_callback_uuid;

    service_obj_map_t::iterator it = m_service_obj_mgr.begin();
    for (; it != m_service_obj_mgr.end(); ++it)
    {
        map<uint16_t, service_obj_t>::iterator it2 = it->second.service_objs.begin();
        for (; it2 != it->second.service_objs.end(); ++it2)
        {
            callback_map_t::iterator uuid_it = it2->second.m_callback_map.begin();
            for (; uuid_it != it2->second.m_callback_map.end(); ++uuid_it)
            {
                if (uuid_it->second.socket_ptr == sock_)
                {
                    del_callback_uuid.push_back(uuid_it->first);
                }
            }
            
            if (it2->second.socket_ptr == sock_)
            {
                del_sgid.push_back(it->first);
                del_sid.push_back(it2->first);
            }
            
            //! del all tmp callback uuid
            for (size_t i = 0; i < del_callback_uuid.size(); ++i)
            {
                it2->second.m_callback_map.erase(del_callback_uuid[i]);
            }
            del_callback_uuid.clear();
        }
    }
    
    for (size_t i = 0; i < del_sgid.size(); ++i)
    {
        logwarn((BROKER, "broker_service_t::handle_broken del sgid[%u], sid[%u]", del_sgid[i], del_sid[i]));
        m_service_obj_mgr[del_sgid[i]].service_objs.erase(del_sid[i]);
        if (m_service_obj_mgr[del_sgid[i]].service_objs.empty())
        {
            m_service_obj_mgr.erase(del_sgid[i]);
            logwarn((BROKER, "broker_service_t::handle_broken del sgid[%u]", del_sgid[i]));
        }
    }
    
    if (NULL != sock_->get_data<socket_session_info_t>())
    {
        for (vector<string>::iterator vit = m_all_slave_host.begin(); vit != m_all_slave_host.end(); ++vit)
        {
            if (*vit == sock_->get_data<socket_session_info_t>()->slave_host)
            {
                m_all_slave_host.erase(vit);
                break;
            }
        }

        delete sock_->get_data<socket_session_info_t>();
        sock_->set_data(NULL);
    }

    sock_->safe_delete();
    m_all_sockets.erase(sock_);

    logwarn((BROKER, "broker_service_t::handle_broken en ok"));
    return 0;
}
Beispiel #15
0
int broker_service_t::handle_msg(const message_t& msg_, socket_ptr_t sock_)
{
    lock_guard_t lock(m_mutex);

    msg_tool_t msg_tool;
    try{
        msg_tool.decode(msg_.get_body());
    }catch(exception& e_)
    {
        logerror((BROKER, "broker_service_t::handle_msg except<%s>", e_.what()));
        return -1;
    }

    logtrace((BROKER, "broker_service_t::handle_msg begin... cmd[%u], name[%s], sgid[%u], sid[%u], msgid[%u]",
                      msg_.get_cmd(), msg_tool.get_name().c_str(), msg_tool.get_group_id(), msg_tool.get_service_id(), msg_tool.get_msg_id()));
    
    if (msg_tool.get_group_id() == 0 && msg_tool.get_service_id() == 0)
    {
        if (msg_tool.get_msg_id() == rpc_msg_cmd_e::CREATE_SERVICE_GROUP)
        {
            create_service_group_t::in_t in;
            in.decode(msg_.get_body());
            rpc_callcack_t<create_service_group_t::out_t> rcb;
            rcb.init_data(rpc_msg_cmd_e::INTREFACE_CALLBACK, 0, 0, in.get_uuid());
            rcb.set_socket(sock_);
            create_service_group(in, rcb);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::CREATE_SERVICE)
        {
            create_service_t::in_t in;
            in.decode(msg_.get_body());
            rpc_callcack_t<create_service_t::out_t> rcb;
            rcb.init_data(rpc_msg_cmd_e::INTREFACE_CALLBACK, 0, 0, in.get_uuid());
            rcb.set_socket(sock_);
            create_service(in, rcb);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::REG_INTERFACE)
        {
            reg_interface_t::in_t in;
            in.decode(msg_.get_body());
            
            rpc_callcack_t<reg_interface_t::out_t> rcb;
            rcb.init_data(rpc_msg_cmd_e::INTREFACE_CALLBACK, 0, 0, in.get_uuid());
            rcb.set_socket(sock_);

            reg_interface(in, rcb);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::SYNC_ALL_SERVICE)
        {
            loginfo((BROKER, "broker_service_t::handle_msg begin... cmd[%u], name[%s], sgid[%u], sid[%u], msgid[%u] sock[%p]",
                      msg_.get_cmd(), msg_tool.get_name().c_str(), msg_tool.get_group_id(), msg_tool.get_service_id(), msg_tool.get_msg_id(), sock_));

            sync_all_service_t::in_t in;
            in.decode(msg_.get_body());
            
            rpc_callcack_t<sync_all_service_t::out_t> rcb;
            rcb.init_data(rpc_msg_cmd_e::INTREFACE_CALLBACK, 0, 0, in.get_uuid());
            rcb.set_socket(sock_);
            
            sync_all_service(in, rcb);
            m_all_sockets.insert(sock_);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::PUSH_INIT_DATA)
        {
            push_init_data_t::in_t in;
            in.decode(msg_.get_body());
            process_sync_data(in);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::REG_SLAVE_BROKER)
        {
            reg_slave_broker_t::in_t in;
            in.decode(msg_.get_body());
            loginfo((BROKER, "broker_service_t::handle_msg REG_SLAVE_BROKER node_id[%u]", in.node_id));
            
            service_obj_map_t::iterator it = m_service_obj_mgr.begin();
            
            for (; it != m_service_obj_mgr.end(); ++it)
            {
                map<uint16_t, service_obj_t>::iterator it2 = it->second.service_objs.begin();
                for (; it2 != it->second.service_objs.end(); ++it2)
                {
                    if (it2->second.node_id == in.node_id)
                    {
                        it2->second.socket_ptr = sock_;
                    }
                }
            }
            sock_->set_data(new socket_session_info_t(in.node_id));
            m_all_sockets.insert(sock_);
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::PUSH_ADD_SERVICE_GROUP)
        {
            push_add_service_group_t::in_t in;
            in.decode(msg_.get_body());
            
            service_obj_mgr_t obj_mgr;
            obj_mgr.id     = in.sgid;
            obj_mgr.name   = in.name;
            
            m_service_obj_mgr.insert(make_pair(obj_mgr.id, obj_mgr));
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::PUSH_ADD_SERVICE)
        {
            push_add_service_t::in_t in;
            in.decode(msg_.get_body());
            
            loginfo((BROKER, "broker_service_t::handle_msg PUSH_ADD_SERVICE node_id[%u]", in.node_id));

            map<uint16_t, service_obj_mgr_t>::iterator it = m_service_obj_mgr.find(in.sgid);
            if (it == m_service_obj_mgr.end())
            {
                logerror((BROKER, "broker_service_t::handle_msg failed... sgid<%u> not exist", in.sgid));
            }
            else
            {
                service_obj_t obj;
                obj.name       = it->second.name;
                obj.group_id   = in.sgid;
                obj.id         = in.sid;
                obj.socket_ptr = find_socket_by_node(in.node_id);
                obj.node_id    = in.node_id;
                
                it->second.service_objs[in.sid] = obj;
            }
        }
        else if (msg_tool.get_msg_id() == rpc_msg_cmd_e::PUSH_ADD_MSG)
        {
            push_add_msg_t::in_t in;
            in.decode(msg_.get_body());
            singleton_t<msg_name_store_t>::instance().add_msg(in.name, in.msg_id);
        }
    }
    else
    {
        service_obj_map_t::iterator obj_mgr_it = m_service_obj_mgr.find(msg_tool.get_group_id());
        if (obj_mgr_it == m_service_obj_mgr.end())
        {
            logerror((BROKER, "broker_service_t::handle_msg sgid not found cmd[%u], name[%s], sgid[%u], sid[%u], msgid[%u]",
                              msg_.get_cmd(), msg_tool.get_name().c_str(), msg_tool.get_group_id(), msg_tool.get_service_id(), msg_tool.get_msg_id()));
            return -1;
        }

        map<uint16_t, service_obj_t>::iterator sobj_it = obj_mgr_it->second.service_objs.find(msg_tool.get_service_id());
        if (sobj_it == obj_mgr_it->second.service_objs.end())
        {
            logerror((BROKER, "broker_service_t::handle_msg sid not found cmd[%u], name[%s], sgid[%u], sid[%u], msgid[%u]",
                      msg_.get_cmd(), msg_tool.get_name().c_str(), msg_tool.get_group_id(), msg_tool.get_service_id(), msg_tool.get_msg_id()));
            return -1;
        }

        switch (msg_.get_cmd())
        {
            case rpc_msg_cmd_e::CALL_INTERFACE:
            {
                sobj_it->second.async_call(msg_tool, msg_.get_body(), sock_);
            }break;
            case rpc_msg_cmd_e::INTREFACE_CALLBACK:
            {
                sobj_it->second.interface_callback(msg_tool, msg_.get_body());
            }break;
        }
    }

    return 0;
}