rcpp_framework/core/net/tcp_server.cpp

194 lines
6.1 KiB
C++
Raw Normal View History

// This file is originally from Trantor - TcpServer.cc
// Copyright (c) 2016-2021, Tao An. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Tao An nor the names of other contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2021-06-17 14:43:29 +02:00
#include "core/net/tcp_server.h"
#include "core/log/logger.h"
#include "core/loops/acceptor.h"
#include "core/net/connections/tcp_connection_impl.h"
2021-06-17 14:43:29 +02:00
#include <functional>
#include <vector>
2022-02-10 12:07:47 +01:00
2021-06-17 14:43:29 +02:00
using namespace std::placeholders;
TcpServer::TcpServer(EventLoop *loop, const InetAddress &address, const std::string &name, bool reUseAddr, bool reUsePort) :
loop_(loop),
acceptorPtr_(new Acceptor(loop, address, reUseAddr, reUsePort)),
serverName_(name),
recvMessageCallback_([](const TcpConnectionPtr &, MsgBuffer *buffer) {
LOG_ERROR << "unhandled recv message [" << buffer->readableBytes()
<< " bytes]";
buffer->retrieveAll();
}) {
acceptorPtr_->setNewConnectionCallback(std::bind(&TcpServer::newConnection, this, _1, _2));
2021-06-17 14:43:29 +02:00
}
TcpServer::~TcpServer() {
// loop_->assertInLoopThread();
LOG_TRACE << "TcpServer::~TcpServer [" << serverName_ << "] destructing";
2021-06-17 14:43:29 +02:00
}
void TcpServer::newConnection(int sockfd, const InetAddress &peer) {
LOG_TRACE << "new connection:fd=" << sockfd
<< " address=" << peer.toIpPort();
// test code for blocking or nonblocking
// std::vector<char> str(1024*1024*100);
// for(int i=0;i<str.size();i++)
// str[i]='A';
// LOG_TRACE<<"vector size:"<<str.size();
// size_t n=write(sockfd,&str[0],str.size());
// LOG_TRACE<<"write "<<n<<" bytes";
loop_->assertInLoopThread();
EventLoop *ioLoop = NULL;
if (loopPoolPtr_ && loopPoolPtr_->size() > 0) {
ioLoop = loopPoolPtr_->getNextLoop();
}
if (ioLoop == NULL) {
ioLoop = loop_;
}
std::shared_ptr<TcpConnectionImpl> newPtr;
if (sslCtxPtr_) {
2021-06-17 14:43:29 +02:00
#ifdef USE_OPENSSL
2022-02-10 14:01:39 +01:00
newPtr = std::make_shared<TcpConnectionImpl>(ioLoop, sockfd, InetAddress(Socket::getLocalAddr(sockfd)), peer, sslCtxPtr_);
2021-06-17 14:43:29 +02:00
#else
LOG_FATAL << "OpenSSL is not found in your system!";
abort();
2021-06-17 14:43:29 +02:00
#endif
} else {
newPtr = std::make_shared<TcpConnectionImpl>(ioLoop, sockfd, InetAddress(Socket::getLocalAddr(sockfd)), peer);
}
2021-06-17 14:43:29 +02:00
if (idleTimeout_ > 0) {
assert(timingWheelMap_[ioLoop]);
newPtr->enableKickingOff(idleTimeout_, timingWheelMap_[ioLoop]);
}
newPtr->setRecvMsgCallback(recvMessageCallback_);
2021-06-17 14:43:29 +02:00
newPtr->setConnectionCallback(
[this](const TcpConnectionPtr &connectionPtr) {
if (connectionCallback_)
connectionCallback_(connectionPtr);
});
newPtr->setWriteCompleteCallback(
[this](const TcpConnectionPtr &connectionPtr) {
if (writeCompleteCallback_)
writeCompleteCallback_(connectionPtr);
});
newPtr->setCloseCallback(std::bind(&TcpServer::connectionClosed, this, _1));
connSet_.insert(newPtr);
newPtr->connectEstablished();
2021-06-17 14:43:29 +02:00
}
void TcpServer::start() {
loop_->runInLoop([this]() {
assert(!started_);
started_ = true;
2022-02-10 14:01:39 +01:00
if (idleTimeout_ > 0) {
2022-02-10 14:01:39 +01:00
timingWheelMap_[loop_] = std::make_shared<TimingWheel>(loop_, idleTimeout_, 1.0F, idleTimeout_ < 500 ? idleTimeout_ + 1 : 100);
if (loopPoolPtr_) {
auto loopNum = loopPoolPtr_->size();
while (loopNum > 0) {
// LOG_TRACE << "new Wheel loopNum=" << loopNum;
auto poolLoop = loopPoolPtr_->getNextLoop();
2022-02-10 14:01:39 +01:00
timingWheelMap_[poolLoop] = std::make_shared<TimingWheel>(poolLoop, idleTimeout_, 1.0F, idleTimeout_ < 500 ? idleTimeout_ + 1 : 100);
--loopNum;
}
}
}
2022-02-10 14:01:39 +01:00
LOG_TRACE << "map size=" << timingWheelMap_.size();
acceptorPtr_->listen();
});
2021-06-17 14:43:29 +02:00
}
void TcpServer::stop() {
loop_->runInLoop([this]() { acceptorPtr_.reset(); });
2022-02-10 14:01:39 +01:00
for (auto connection : connSet_) {
connection->forceClose();
}
2022-02-10 14:01:39 +01:00
loopPoolPtr_.reset();
2022-02-10 14:01:39 +01:00
for (auto &iter : timingWheelMap_) {
std::promise<int> pro;
auto f = pro.get_future();
2022-02-10 14:01:39 +01:00
iter.second->getLoop()->runInLoop([&iter, &pro]() mutable {
iter.second.reset();
pro.set_value(1);
});
2022-02-10 14:01:39 +01:00
f.get();
}
2021-06-17 14:43:29 +02:00
}
void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr) {
LOG_TRACE << "connectionClosed";
// loop_->assertInLoopThread();
loop_->runInLoop([this, connectionPtr]() {
2022-02-10 14:01:39 +01:00
size_t n = connSet_.erase(connectionPtr);
(void)n;
assert(n == 1);
2022-02-10 14:01:39 +01:00
});
2021-06-17 14:43:29 +02:00
static_cast<TcpConnectionImpl *>(connectionPtr.get())->connectDestroyed();
2021-06-17 14:43:29 +02:00
}
const std::string TcpServer::ipPort() const {
return acceptorPtr_->addr().toIpPort();
2021-06-17 14:43:29 +02:00
}
2022-02-10 12:07:47 +01:00
const InetAddress &TcpServer::address() const {
return acceptorPtr_->addr();
2021-06-17 14:43:29 +02:00
}
void TcpServer::enableSSL(const std::string &certPath, const std::string &keyPath, bool useOldTLS, const std::vector<std::pair<std::string, std::string> > &sslConfCmds) {
2021-06-17 14:43:29 +02:00
#ifdef USE_OPENSSL
/* Create a new OpenSSL context */
sslCtxPtr_ = newSSLServerContext(certPath, keyPath, useOldTLS, sslConfCmds);
2021-06-17 14:43:29 +02:00
#else
LOG_FATAL << "OpenSSL is not found in your system!";
abort();
2021-06-17 14:43:29 +02:00
#endif
}