github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/net/middleware/net_nervure.h (about)

     1  /***********************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com>
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in
    14  all copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  THE SOFTWARE.
    23  *************************************************/
    24  #pragma once
    25  #include "ff/net/common/common.h"
    26  #include "ff/net/middleware/event_handler.h"
    27  #include "ff/net/middleware/pkg_handler.h"
    28  #include "ff/net/middleware/pkg_packer.h"
    29  #include "ff/net/network/end_point.h"
    30  #include "ff/net/network/events.h"
    31  #include "ff/net/network/tcp_connection_base.h"
    32  #include <condition_variable>
    33  #include <mutex>
    34  
    35  namespace ff {
    36  namespace net {
    37  class tcp_server;
    38  
    39  class udp_point;
    40  
    41  typedef std::shared_ptr<tcp_server> tcp_server_ptr;
    42  typedef std::shared_ptr<udp_point> udp_point_ptr;
    43  
    44  using boost::asio::io_service;
    45  using namespace event;
    46  
    47  class net_nervure {
    48  public:
    49    net_nervure(net_mode nm = real_net);
    50  
    51    void add_pkg_handler(tcp_pkg_handler *p_tcp_handler,
    52                         udp_pkg_handler *p_udp_handler);
    53  
    54    template <class HT_> void add_pkg_hub(HT_ &ht) {
    55      if (ht.get_tcp_pkg_handler() != NULL) {
    56        m_pTCPHandler.push_back(ht.get_tcp_pkg_handler());
    57      }
    58      if (ht.get_udp_pkg_handler() != NULL) {
    59        m_pUDPHandler.push_back(ht.get_udp_pkg_handler());
    60      }
    61    }
    62  
    63    virtual ~net_nervure();
    64  
    65    void run();
    66  
    67    //! This is thread safe!
    68    void stop();
    69  
    70    tcp_server_ptr add_tcp_server(const std::string &ip, uint16_t iTCPPort);
    71  
    72    tcp_server_ptr add_tcp_server(const tcp_endpoint &ep);
    73  
    74    udp_point_ptr add_udp_point(const std::string &ip, uint16_t iUDPPort);
    75  
    76    udp_point_ptr add_udp_point(const udp_endpoint &ep);
    77  
    78    tcp_connection_base_ptr add_tcp_client(const std::string &ip,
    79                                           uint16_t iTCPPort);
    80  
    81    tcp_connection_base_ptr add_tcp_client(const tcp_endpoint &ep);
    82  
    83    inline io_service &ioservice() { return m_oIOService; }
    84  
    85    inline pkg_packer *get_pkg_packer() { return m_pBS; }
    86  
    87    inline event_handler *get_event_handler() { return m_pEH; }
    88  
    89    inline std::vector<tcp_pkg_handler *> get_tcp_pkg_handler() {
    90      return m_pTCPHandler;
    91    }
    92  
    93    inline std::vector<udp_pkg_handler *> get_udp_pkg_handler() {
    94      return m_pUDPHandler;
    95    }
    96  
    97  protected:
    98    void on_tcp_server_accept_connect(tcp_connection_base_ptr pConn);
    99  
   100    void on_tcp_client_get_connect(tcp_connection_base *pClient);
   101  
   102    void on_conn_recv_or_send_error(tcp_connection_base *pConn,
   103                                    boost::system::error_code ec);
   104  
   105    void internal_stop();
   106  
   107  protected:
   108    pkg_packer *m_pBS;
   109    event_handler *m_pEH;
   110    std::vector<tcp_pkg_handler *> m_pTCPHandler;
   111    std::vector<udp_pkg_handler *> m_pUDPHandler;
   112    io_service m_oIOService;
   113  
   114    net_mode mi_mode;
   115    typedef std::vector<tcp_server_ptr> tcp_servers_t;
   116    typedef std::vector<tcp_connection_base_ptr> tcp_clients_t;
   117    typedef std::vector<udp_point_ptr> udp_points_t;
   118    typedef std::vector<tcp_connection_base_ptr> tcp_connections_t;
   119  
   120    tcp_servers_t m_oServers;
   121    tcp_clients_t m_oClients;
   122    udp_points_t m_oUDPPoints;
   123    tcp_connections_t m_oTCPConnections;
   124    bool m_safe_to_stop;
   125    std::mutex m_stop_mutex;
   126    std::condition_variable m_stop_cond;
   127    std::thread::id m_io_service_thrd;
   128  }; // end class
   129  
   130  } // namespace net
   131  } // namespace ff