github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/src/net/network/tcp_server.cpp (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  #include "ff/net/network/tcp_server.h"
    25  #include "ff/net/common/defines.h"
    26  #include "ff/net/middleware/event_handler.h"
    27  #include "ff/net/network/events.h"
    28  
    29  namespace ff {
    30  namespace net {
    31  using namespace ::ff::net::event;
    32  using namespace ::ff::net::event::more;
    33  
    34  net_tcp_connection::net_tcp_connection(
    35      io_service &ioservice, pkg_packer *bs, event_handler *eh,
    36      const std::vector<tcp_pkg_handler *> &rph, tcp_server *pSvr)
    37      : net_tcp_connection_base(ioservice, bs, eh, rph), m_pTCPServer(pSvr) {
    38    m_iPointState = state_valid;
    39  }
    40  
    41  void net_tcp_connection::start() {
    42    m_oRemoteEndpoint = m_oSocket.remote_endpoint();
    43    start_recv();
    44  }
    45  
    46  /////////
    47  tcp_server::tcp_server(io_service &ioservice, pkg_packer *bs, event_handler *eh,
    48                         const std::vector<tcp_pkg_handler *> &rph,
    49                         const tcp_endpoint &ep)
    50      : m_oIOService(ioservice), m_oAcceptEP(ep), m_pBS(bs), m_pEH(eh),
    51        m_pRPH(rph) {}
    52  
    53  net_tcp_server::net_tcp_server(io_service &ioservice, pkg_packer *bs,
    54                                 event_handler *eh,
    55                                 const std::vector<tcp_pkg_handler *> &rph,
    56                                 const tcp_endpoint &ep)
    57      : tcp_server(ioservice, bs, eh, rph, ep), m_oAcceptor(ioservice, ep) {}
    58  
    59  void net_tcp_server::start_accept() {
    60    auto pn = std::make_shared<net_tcp_connection>(m_oAcceptor.get_io_service(),
    61                                                   m_pBS, m_pEH, m_pRPH, this);
    62    tcp_connection_base_ptr pNewConn =
    63        std::dynamic_pointer_cast<tcp_connection_base>(pn);
    64    net_tcp_connection *ntc = static_cast<net_tcp_connection *>(pNewConn.get());
    65  
    66    // LOG(INFO) << "start_accept";
    67    m_pEH->triger<tcp_server_start_listen>(m_oAcceptor.local_endpoint());
    68  
    69    m_oAcceptor.async_accept(
    70        ntc->m_oSocket, [this, pNewConn](const boost::system::error_code &ec) {
    71          handle_accept(pNewConn, ec);
    72        });
    73  }
    74  
    75  void net_tcp_server::handle_accept(tcp_connection_base_ptr pNewConn,
    76                                     const boost::system::error_code &error) {
    77    if (!error) {
    78      net_tcp_connection *ntc = (net_tcp_connection *)pNewConn.get();
    79      // LOG(INFO) << "accept connection";
    80      ntc->start();
    81      m_pEH->triger<tcp_server_accept_connection>(pNewConn);
    82      start_accept();
    83    } else {
    84      m_pEH->triger<tcp_server_accept_error>(m_oAcceptEP, error);
    85    }
    86  }
    87  
    88  void net_tcp_server::close() {
    89    // m_oAcceptor.cancel();
    90    m_oAcceptor.close();
    91  }
    92  
    93  } // namespace net
    94  
    95  } // namespace ff