Beispiel #1
0
void Server::ReceiveSession(const SessionPtr& session, const boost::system::error_code& error)
{
    config_.Reload();

    if (!session) return;

    const auto address = session->tcp_socket().remote_endpoint().address();

    // 拒否IPでないか判定
    if(IsBlockedAddress(address)) {
        Logger::Info("Blocked IP Address: %s", address);
        session->Close();

    } else {
        session->set_on_receive(callback_);
        session->Start();
        sessions_.push_back(SessionWeakPtr(session));

        // クライアント情報を要求
        session->Send(ClientRequestedClientInfo());
    }

    auto new_session = boost::make_shared<ServerSession>(io_service_);
    acceptor_.async_accept(new_session->tcp_socket(),
                           boost::bind(&Server::ReceiveSession, this, new_session, boost::asio::placeholders::error));

    RefreshSession();
}
Beispiel #2
0
void Gateway::OnAccepted(SessionPtr pSession, const boost::system::error_code& error)
{
    if (!error)
    {
        boost::asio::ip::tcp::no_delay option(true);
        pSession->GetSocket().set_option(option);
        int64 sessionId = GenSessionId();
        int totalSessionNum = 0;
        pSession->SetSessionId(sessionId);
        {
            boost::mutex::scoped_lock lock(_mutexSessionMap);
            _sessions.insert(make_pair(sessionId, pSession));
            totalSessionNum = _sessions.size();
        }
        boost::system::error_code ec;
        ip::tcp::socket::endpoint_type endpoint = pSession->GetSocket().remote_endpoint(ec);
        pSession->Start();
        if (_onSessionOpenNew)
        {
            string ip = endpoint.address().to_string();
            _onSessionOpenNew(sessionId, ip.c_str(), totalSessionNum);
        }
    }

    if (!_bStopAccept)
        StartAccept();
}