github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/core/net_ipc/server/nipc_server.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 "core/net_ipc/nipc_common.h"
    22  #include "core/net_ipc/nipc_pkg.h"
    23  #include "core/net_ipc/server/api_request_timer.h"
    24  #include "core/net_ipc/server/ipc_callback_holder.h"
    25  #include "core/net_ipc/server/ipc_client_watcher.h"
    26  namespace neb {
    27  namespace core {
    28  
    29  class nipc_server {
    30  public:
    31    nipc_server();
    32    ~nipc_server();
    33  
    34    void init_params(const nbre_params_t &params);
    35    bool start();
    36  
    37    template <typename PkgType>
    38    int send_api_pkg(void *holder, const std::shared_ptr<PkgType> &pkg) {
    39      if (m_request_timer == nullptr) {
    40        return ipc_status_fail;
    41      }
    42  
    43      m_request_timer->issue_api(
    44          reinterpret_cast<uint64_t>(holder),
    45          [this, pkg](::ff::net::tcp_connection_base_ptr conn) {
    46            if (conn) {
    47              conn->send(pkg);
    48            };
    49          },
    50          ipc_callback_holder::instance().get_callback(
    51              typename get_pkg_ack_type<PkgType>::type().type_id()));
    52      return ipc_status_succ;
    53    }
    54  
    55    void shutdown();
    56  
    57  protected:
    58    template <typename PkgType> void to_recv_pkg() {
    59      m_pkg_hub->to_recv_pkg<PkgType>([this](std::shared_ptr<PkgType> pkg) {
    60        m_last_heart_beat_time = std::chrono::steady_clock::now();
    61        ipc_callback_holder::instance().call_callback<PkgType>(pkg);
    62        m_request_timer->remove_api(pkg->template get<p_holder>());
    63      });
    64    }
    65  
    66  private:
    67    bool check_path_exists();
    68    void add_all_callbacks();
    69  
    70  protected:
    71    std::unique_ptr<::ff::net::net_nervure> m_server;
    72    ::ff::net::tcp_connection_base_ptr m_conn;
    73    std::unique_ptr<std::thread> m_thread;
    74  
    75    std::unique_ptr<::ff::net::typed_pkg_hub> m_pkg_hub;
    76    ipc_callback_holder *m_callbacks;
    77    std::atomic_bool m_got_exception_when_start_ipc;
    78    std::unique_ptr<api_request_timer> m_request_timer;
    79    std::condition_variable m_start_complete_cond_var;
    80    std::mutex m_mutex;
    81    bool m_is_started;
    82    std::unique_ptr<ipc_client_watcher> m_client_watcher;
    83    std::chrono::steady_clock::time_point m_last_heart_beat_time;
    84    std::unique_ptr<util::timer_loop> m_heart_beat_watcher;
    85  };
    86  
    87  } // namespace core
    88  } // namespace neb