github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/core/net_ipc/client/nipc_client.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #pragma once
    21  #include "common/common.h"
    22  #include "core/net_ipc/nipc_common.h"
    23  #include "core/net_ipc/nipc_pkg.h"
    24  #include "util/quitable_thread.h"
    25  #include "util/timer_loop.h"
    26  #include <ff/functionflow.h>
    27  #include <ff/network.h>
    28  
    29  namespace neb {
    30  namespace core {
    31  class nipc_client {
    32  public:
    33    nipc_client();
    34    ~nipc_client();
    35  
    36    //! The handler f will run in a thread pool.
    37    template <typename T, typename Func> void add_handler(Func &&f) {
    38      m_handlers.push_back([this, f](::ff::net::typed_pkg_hub &hub) {
    39        hub.to_recv_pkg<T>([f, this](std::shared_ptr<T> pkg) {
    40          LOG(INFO) << "recv pkg " << pkg_type_id_to_name(T().type_id());
    41          // No big pressure for NBRE
    42          m_to_recv_heart_beat_msg = 0;
    43          if (m_pkg_handler_thread->size() > ff::rt::concurrency()) {
    44            LOG(INFO) << "ignore pkg";
    45            return;
    46          }
    47  
    48          m_pkg_handler_thread->schedule([this, f, pkg]() {
    49            ff::para<> p;
    50            p([pkg, f, this]() {
    51              m_handling_pkg_num++;
    52              f(pkg);
    53              m_handling_pkg_num--;
    54            });
    55          });
    56        });
    57      });
    58    }
    59  
    60    bool start();
    61  
    62    void shutdown();
    63  
    64    inline ::ff::net::tcp_connection_base_ptr connection() { return m_conn; }
    65  
    66  protected:
    67    std::vector<std::function<void(::ff::net::typed_pkg_hub &)>> m_handlers;
    68    std::unique_ptr<std::thread> m_thread;
    69    std::unique_ptr<util::timer_loop> m_heart_bear_timer;
    70    ::ff::net::tcp_connection_base_ptr m_conn;
    71    std::atomic_bool m_is_connected;
    72    std::atomic_bool m_got_exception_when_start_ipc;
    73    int32_t m_to_recv_heart_beat_msg;
    74    std::atomic_int_fast64_t m_handling_pkg_num;
    75    std::unique_ptr<util::wakeable_thread> m_pkg_handler_thread;
    76    // std::unique_ptr<ipc_client_t> m_client;
    77  };
    78  } // namespace core
    79  } // namespace neb