rcpp_framework/libs/brynet/net/TcpConnection.hpp

1235 lines
33 KiB
C++
Raw Normal View History

2020-11-24 15:41:18 +01:00
#pragma once
#include <brynet/base/Any.hpp>
2021-04-30 16:10:14 +02:00
#include <brynet/base/Buffer.hpp>
2020-11-24 15:41:18 +01:00
#include <brynet/base/Noexcept.hpp>
2021-04-30 16:10:14 +02:00
#include <brynet/base/NonCopyable.hpp>
2021-01-29 17:26:17 +01:00
#include <brynet/base/Packet.hpp>
2021-04-30 16:10:14 +02:00
#include <brynet/base/Timer.hpp>
2020-11-24 15:41:18 +01:00
#include <brynet/net/Channel.hpp>
#include <brynet/net/EventLoop.hpp>
#include <brynet/net/SSLHelper.hpp>
2021-04-30 16:10:14 +02:00
#include <brynet/net/SendableMsg.hpp>
#include <brynet/net/Socket.hpp>
#include <brynet/net/SocketLibFunction.hpp>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <memory>
#include <type_traits>
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
#ifdef __cplusplus
2020-11-24 15:41:18 +01:00
extern "C" {
#endif
#include <openssl/err.h>
2021-04-30 16:10:14 +02:00
#include <openssl/ssl.h>
#ifdef __cplusplus
2020-11-24 15:41:18 +01:00
}
#endif
#endif
namespace brynet { namespace net {
2021-04-30 16:10:14 +02:00
class TcpConnection : public Channel,
public brynet::base::NonCopyable,
public std::enable_shared_from_this<TcpConnection>
{
public:
using Ptr = std::shared_ptr<TcpConnection>;
using EnterCallback = std::function<void(Ptr)>;
using DataCallback = std::function<void(brynet::base::BasePacketReader&)>;
using DisconnectedCallback = std::function<void(Ptr)>;
using PacketSendedCallback = std::function<void(void)>;
using HighWaterCallback = std::function<void(void)>;
public:
Ptr static Create(TcpSocket::Ptr socket,
size_t maxRecvBufferSize,
EnterCallback&& enterCallback,
const EventLoop::Ptr& eventLoop,
const SSLHelper::Ptr& sslHelper = nullptr)
2021-01-29 17:26:17 +01:00
{
2021-04-30 16:10:14 +02:00
class make_shared_enabler : public TcpConnection
2021-01-29 17:26:17 +01:00
{
2021-04-30 16:10:14 +02:00
public:
make_shared_enabler(TcpSocket::Ptr socket,
size_t maxRecvBufferSize,
EnterCallback&& enterCallback,
EventLoop::Ptr eventLoop)
: TcpConnection(std::move(socket),
maxRecvBufferSize,
std::move(enterCallback),
std::move(eventLoop))
{}
};
2020-11-25 00:20:41 +01:00
2021-04-30 16:10:14 +02:00
const auto isServerSide = socket->isServerSide();
auto session = std::make_shared<make_shared_enabler>(
std::move(socket),
maxRecvBufferSize,
std::move(enterCallback),
eventLoop);
(void) isServerSide;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if (sslHelper != nullptr)
{
if (isServerSide)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
if (sslHelper->getOpenSSLCTX() == nullptr ||
!session->initAcceptSSL(sslHelper->getOpenSSLCTX()))
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
throw std::runtime_error("init ssl failed");
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
else
{
if (!session->initConnectSSL())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
throw std::runtime_error("init ssl failed");
2020-11-24 15:41:18 +01:00
}
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#else
2021-04-30 16:10:14 +02:00
if (sslHelper != nullptr)
{
throw std::runtime_error("not enable ssl");
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
eventLoop->runAsyncFunctor([session]() {
session->onEnterEventLoop();
});
return session;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
const EventLoop::Ptr& getEventLoop() const
{
return mEventLoop;
}
//TODO::如果所属EventLoop已经没有工作则可能导致内存无限大因为所投递的请求都没有得到处理
void send(const SendableMsg::Ptr& msg,
PacketSendedCallback&& callback = nullptr)
{
if (mEventLoop->isInLoopThread())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
sendInLoop(msg, std::move(callback));
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
else
2021-01-29 17:26:17 +01:00
{
auto sharedThis = shared_from_this();
2021-04-30 16:10:14 +02:00
mEventLoop->runAsyncFunctor([sharedThis, msg, callback, this]() mutable {
sendInLoop(msg, std::move(callback));
2021-01-29 17:26:17 +01:00
});
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void send(const char* buffer,
size_t len,
PacketSendedCallback&& callback = nullptr)
{
send(MakeStringMsg(buffer, len), std::move(callback));
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void send(const std::string& buffer,
PacketSendedCallback&& callback = nullptr)
{
send(MakeStringMsg(buffer), std::move(callback));
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void send(std::string&& buffer,
PacketSendedCallback&& callback = nullptr)
{
send(MakeStringMsg(std::move(buffer)), std::move(callback));
}
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
// setDataCallback(std::function<void(brynet::base::BasePacketReader&)>
template<typename Callback>
void setDataCallback(Callback&& cb)
{
verifyArgType(cb, &Callback::operator());
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, cb, this]() mutable {
mDataCallback = cb;
processRecvMessage();
});
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void setDisConnectCallback(DisconnectedCallback&& cb)
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, cb, this]() mutable {
mDisConnectCallback = std::move(cb);
});
}
/* if checkTime is zero, will cancel check heartbeat */
void setHeartBeat(std::chrono::nanoseconds checkTime)
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, checkTime, this]() {
if (mTimer.lock() != nullptr)
2021-01-29 17:26:17 +01:00
{
2021-04-30 16:10:14 +02:00
mTimer.lock()->cancel();
mTimer.reset();
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mCheckTime = checkTime;
startPingCheckTimer();
});
}
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
void setHighWaterCallback(HighWaterCallback cb, size_t size)
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([=]() mutable {
mHighWaterCallback = std::move(cb);
mHighWaterSize = size;
});
}
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
void postShrinkReceiveBuffer()
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, this]() {
mEventLoop->runFunctorAfterLoop([sharedThis, this]() {
shrinkReceiveBuffer();
2021-01-29 17:26:17 +01:00
});
2021-04-30 16:10:14 +02:00
});
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void postDisConnect()
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, this]() {
procCloseInLoop();
});
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void postShutdown()
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis, this]() {
mEventLoop->runFunctorAfterLoop([sharedThis, this]() {
procShutdownInLoop();
2021-01-29 17:26:17 +01:00
});
2021-04-30 16:10:14 +02:00
});
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
const std::string& getIP() const
{
return mIP;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
protected:
TcpConnection(TcpSocket::Ptr socket,
size_t maxRecvBufferSize,
EnterCallback&& enterCallback,
EventLoop::Ptr eventLoop) BRYNET_NOEXCEPT
:
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
mOvlRecv(port::Win::OverlappedType::OverlappedRecv),
mOvlSend(port::Win::OverlappedType::OverlappedSend),
mPostClose(false),
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
mIP(socket->getRemoteIP()),
mSocket(std::move(socket)),
mEventLoop(std::move(eventLoop)),
mAlreadyClose(false),
mMaxRecvBufferSize(maxRecvBufferSize),
mSendingMsgSize(0),
mEnterCallback(std::move(enterCallback)),
mHighWaterSize(0)
{
mRecvData = false;
mCheckTime = std::chrono::steady_clock::duration::zero();
mIsPostFlush = false;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mCanWrite = true;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
mPostRecvCheck = false;
mPostWriteCheck = false;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
growRecvBuffer();
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
mSSLCtx = nullptr;
mSSL = nullptr;
mIsHandsharked = false;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
~TcpConnection() BRYNET_NOEXCEPT override
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if (mSSL != nullptr)
{
SSL_free(mSSL);
mSSL = nullptr;
}
if (mSSLCtx != nullptr)
{
SSL_CTX_free(mSSLCtx);
mSSLCtx = nullptr;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
if (mTimer.lock())
{
mTimer.lock()->cancel();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
void sendInLoop(const SendableMsg::Ptr& msg,
PacketSendedCallback&& callback = nullptr)
{
if (mAlreadyClose)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
return;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
const auto len = msg->size();
mSendingMsgSize += len;
mSendList.emplace_back(PendingPacket{
msg,
len,
std::move(callback)});
runAfterFlush();
if (mSendingMsgSize > mHighWaterSize &&
mHighWaterCallback != nullptr)
{
mHighWaterCallback();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void growRecvBuffer()
{
if (mRecvBuffer == nullptr)
2021-01-29 17:26:17 +01:00
{
2021-04-30 16:10:14 +02:00
mRecvBuffer.reset(brynet::base::buffer_new(std::min<size_t>(16 * 1024, mMaxRecvBufferSize)));
mRecvBuffOriginSize = buffer_getsize(mRecvBuffer.get());
}
else
{
if (buffer_getsize(mRecvBuffer.get()) >= mMaxRecvBufferSize)
2021-01-29 17:26:17 +01:00
{
return;
}
2021-04-30 16:10:14 +02:00
mCurrentTanhXDiff += 0.2;
const auto newTanh = std::tanh(mCurrentTanhXDiff);
const auto maxSizeDiff = mMaxRecvBufferSize - mRecvBuffOriginSize;
const auto NewSize = mRecvBuffOriginSize + (maxSizeDiff * newTanh);
assert(NewSize <= mMaxRecvBufferSize);
std::unique_ptr<struct brynet::base::buffer_s, BufferDeleter> newBuffer(brynet::base::buffer_new(NewSize));
2021-01-29 17:26:17 +01:00
buffer_write(newBuffer.get(),
buffer_getreadptr(mRecvBuffer.get()),
buffer_getreadvalidcount(mRecvBuffer.get()));
mRecvBuffer = std::move(newBuffer);
}
2021-04-30 16:10:14 +02:00
}
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
void shrinkReceiveBuffer()
{
auto newSize = buffer_getreadvalidcount(mRecvBuffer.get());
if (newSize == 0)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
newSize = std::min<size_t>(16 * 1024, mMaxRecvBufferSize);
}
if (newSize == buffer_getsize(mRecvBuffer.get()))
{
return;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
std::unique_ptr<struct brynet::base::buffer_s, BufferDeleter>
newBuffer(brynet::base::buffer_new(newSize));
buffer_write(newBuffer.get(),
buffer_getreadptr(mRecvBuffer.get()),
buffer_getreadvalidcount(mRecvBuffer.get()));
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mRecvBuffer = std::move(newBuffer);
mCurrentTanhXDiff = 0;
mRecvBuffOriginSize = newSize;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
/* must called in network thread */
bool onEnterEventLoop()
{
assert(mEventLoop->isInLoopThread());
if (!mEventLoop->isInLoopThread())
{
return false;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (!brynet::net::base::SocketNonblock(mSocket->getFD()) ||
!mEventLoop->linkChannel(mSocket->getFD(), this))
{
return false;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
const auto findRet = mEventLoop->getTcpConnection(mSocket->getFD());
(void) findRet;
assert(findRet == nullptr);
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
#ifdef BRYNET_USE_OPENSSL
if (mSSL != nullptr)
{
mEventLoop->addTcpConnection(mSocket->getFD(), shared_from_this());
processSSLHandshake();
2020-11-24 15:41:18 +01:00
return true;
}
2021-04-30 16:10:14 +02:00
#endif
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (!checkRead())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
return false;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mEventLoop->addTcpConnection(mSocket->getFD(), shared_from_this());
causeEnterCallback();
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
return true;
}
#ifdef BRYNET_USE_OPENSSL
bool initAcceptSSL(SSL_CTX* ctx)
{
if (mSSL != nullptr)
{
return false;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
mSSL = SSL_new(ctx);
if (SSL_set_fd(mSSL, mSocket->getFD()) != 1)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
ERR_print_errors_fp(stdout);
::fflush(stdout);
return false;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
return true;
}
bool initConnectSSL()
{
if (mSSLCtx != nullptr)
{
return false;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mSSLCtx = SSL_CTX_new(SSLv23_client_method());
mSSL = SSL_new(mSSLCtx);
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (SSL_set_fd(mSSL, mSocket->getFD()) != 1)
{
ERR_print_errors_fp(stdout);
::fflush(stdout);
return false;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
return true;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
void pingCheck()
{
mTimer.reset();
if (mRecvData)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mRecvData = false;
startPingCheckTimer();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
else
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
procCloseInLoop();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void startPingCheckTimer()
{
if (!mTimer.lock() &&
mCheckTime != std::chrono::steady_clock::duration::zero())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
std::weak_ptr<TcpConnection> weakThis = shared_from_this();
mTimer = mEventLoop->runAfter(mCheckTime, [weakThis]() {
auto sharedThis = weakThis.lock();
if (sharedThis != nullptr)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
sharedThis->pingCheck();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
});
}
}
void canRecv(const bool willClose) override
{
#ifdef BRYNET_PLATFORM_WINDOWS
mPostRecvCheck = false;
if (mPostClose)
{
if (!mPostWriteCheck)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
onClose();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
return;
}
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
if (mAlreadyClose)
{
return;
}
2020-11-24 15:41:18 +01:00
#endif
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if (mSSL != nullptr && !mIsHandsharked && (!processSSLHandshake() || !mIsHandsharked))
{
return;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
do
{
2020-11-24 15:41:18 +01:00
recv();
2021-04-30 16:10:14 +02:00
adjustReceiveBuffer();
} while (willClose && !mAlreadyClose && mRecvBuffer != nullptr && buffer_getwritevalidcount(mRecvBuffer.get()) > 0);
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void canSend() override
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
mPostWriteCheck = false;
if (mPostClose)
{
if (!mPostRecvCheck)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
onClose();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
return;
}
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
if (mAlreadyClose)
{
return;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
mCanWrite = true;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if (mSSL != nullptr && !mIsHandsharked && (!processSSLHandshake() || !mIsHandsharked))
{
return;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
runAfterFlush();
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
bool checkRead()
{
bool check_ret = true;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
static CHAR temp[] = {0};
static WSABUF in_buf = {0, temp};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (mPostRecvCheck)
{
return check_ret;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
DWORD dwBytes = 0;
DWORD flag = 0;
const int ret = WSARecv(mSocket->getFD(),
&in_buf,
1,
&dwBytes,
&flag,
&(mOvlRecv.base),
0);
if (ret == BRYNET_SOCKET_ERROR)
{
check_ret = (BRYNET_ERRNO == WSA_IO_PENDING);
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (check_ret)
{
mPostRecvCheck = true;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
return check_ret;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
bool checkWrite()
{
bool check_ret = true;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
static WSABUF wsendbuf[1] = {{ NULL,
0 }};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (mPostWriteCheck)
{
return check_ret;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
DWORD send_len = 0;
const int ret = WSASend(mSocket->getFD(),
wsendbuf,
1,
&send_len,
0,
&(mOvlSend.base),
0);
if (ret == BRYNET_SOCKET_ERROR)
{
check_ret = (BRYNET_ERRNO == WSA_IO_PENDING);
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (check_ret)
{
mPostWriteCheck = true;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
return check_ret;
}
void adjustReceiveBuffer()
{
if (!mRecvBuffer)
{
return;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
if (buffer_getwritevalidcount(mRecvBuffer.get()) == 0 || buffer_getreadvalidcount(mRecvBuffer.get()) == 0)
{
buffer_adjustto_head(mRecvBuffer.get());
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (buffer_getreadvalidcount(mRecvBuffer.get()) == buffer_getsize(mRecvBuffer.get()))
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
growRecvBuffer();
}
}
void recv()
{
bool must_close = false;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
const bool notInSSL = (mSSL == nullptr);
2020-11-24 15:41:18 +01:00
#else
2021-04-30 16:10:14 +02:00
const bool notInSSL = false;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
while (true)
{
adjustReceiveBuffer();
2021-01-29 17:26:17 +01:00
2021-04-30 16:10:14 +02:00
const auto tryRecvLen = buffer_getwritevalidcount(mRecvBuffer.get());
if (tryRecvLen == 0)
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
checkRead();
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
//force recheck IN-OUT Event
recheckEvent();
#endif
break;
}
int retlen = 0;
#ifdef BRYNET_USE_OPENSSL
if (mSSL != nullptr)
{
retlen = SSL_read(mSSL,
buffer_getwriteptr(mRecvBuffer.get()), tryRecvLen);
}
else
{
retlen = ::recv(mSocket->getFD(),
buffer_getwriteptr(mRecvBuffer.get()), tryRecvLen, 0);
}
#else
retlen = ::recv(mSocket->getFD(),
buffer_getwriteptr(mRecvBuffer.get()),
static_cast<int>(tryRecvLen),
0);
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
if (retlen == 0)
{
must_close = true;
break;
}
else if (retlen < 0)
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if ((mSSL != nullptr &&
SSL_get_error(mSSL, retlen) == SSL_ERROR_WANT_READ) ||
(BRYNET_ERRNO == BRYNET_EWOULDBLOCK))
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
must_close = !checkRead();
2020-11-24 15:41:18 +01:00
}
else
{
2021-04-30 16:10:14 +02:00
must_close = true;
2020-11-24 15:41:18 +01:00
}
#else
2021-04-30 16:10:14 +02:00
if (BRYNET_ERRNO != BRYNET_EWOULDBLOCK)
2020-11-24 15:41:18 +01:00
{
must_close = true;
}
2021-04-30 16:10:14 +02:00
else
2020-11-24 15:41:18 +01:00
{
must_close = !checkRead();
}
2021-04-30 16:10:14 +02:00
#endif
break;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
mRecvData = true;
buffer_addwritepos(mRecvBuffer.get(), static_cast<size_t>(retlen));
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (notInSSL && retlen < static_cast<int>(tryRecvLen))
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
must_close = !checkRead();
break;
2020-11-24 15:41:18 +01:00
}
}
2021-04-30 16:10:14 +02:00
processRecvMessage();
if (must_close)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
procCloseInLoop();
}
}
void flush()
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
normalFlush();
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if (mSSL != nullptr)
{
normalFlush();
}
else
{
2020-11-24 15:41:18 +01:00
quickFlush();
2021-04-30 16:10:14 +02:00
}
#else
quickFlush();
2020-11-24 15:41:18 +01:00
#endif
#endif
2021-04-30 16:10:14 +02:00
}
void normalFlush()
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
static __declspec(thread) char* threadLocalSendBuf = nullptr;
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
static __thread char* threadLocalSendBuf = nullptr;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
static const int SENDBUF_SIZE = 1024 * 32;
if (threadLocalSendBuf == nullptr)
{
threadLocalSendBuf = static_cast<char*>(malloc(SENDBUF_SIZE));
}
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
const bool notInSSL = (mSSL == nullptr);
2020-11-24 15:41:18 +01:00
#else
2021-04-30 16:10:14 +02:00
const bool notInSSL = false;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
bool must_close = false;
while (!mSendList.empty() && mCanWrite)
{
auto sendptr = threadLocalSendBuf;
size_t wait_send_size = 0;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
for (auto it = mSendList.begin(); it != mSendList.end(); ++it)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
auto& packet = *it;
auto packetLeftBuf = (char*) (packet.data->data()) + packet.data->size() - packet.left;
const auto packetLeftLen = packet.left;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if ((wait_send_size + packetLeftLen) > SENDBUF_SIZE)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
if (it == mSendList.begin())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
sendptr = packetLeftBuf;
wait_send_size = packetLeftLen;
2020-11-24 15:41:18 +01:00
}
break;
}
2021-04-30 16:10:14 +02:00
memcpy(static_cast<void*>(sendptr + wait_send_size), static_cast<void*>(packetLeftBuf), packetLeftLen);
wait_send_size += packetLeftLen;
}
if (wait_send_size == 0)
{
break;
}
int send_retlen = 0;
#ifdef BRYNET_USE_OPENSSL
if (mSSL != nullptr)
{
send_retlen = SSL_write(mSSL, sendptr, wait_send_size);
}
else
{
send_retlen = ::send(mSocket->getFD(), sendptr, wait_send_size, 0);
}
#else
send_retlen = ::send(mSocket->getFD(), sendptr, static_cast<int>(wait_send_size), 0);
#endif
if (send_retlen <= 0)
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
if ((mSSL != nullptr && SSL_get_error(mSSL, send_retlen) == SSL_ERROR_WANT_WRITE) ||
(BRYNET_ERRNO == BRYNET_EWOULDBLOCK))
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mCanWrite = false;
must_close = !checkWrite();
2020-11-24 15:41:18 +01:00
}
else
{
2021-04-30 16:10:14 +02:00
must_close = true;
2020-11-24 15:41:18 +01:00
}
#else
2021-04-30 16:10:14 +02:00
if (BRYNET_ERRNO == BRYNET_EWOULDBLOCK)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mCanWrite = false;
must_close = !checkWrite();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
else
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
must_close = true;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
#endif
break;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
auto tmp_len = static_cast<size_t>(send_retlen);
for (auto it = mSendList.begin(); it != mSendList.end();)
{
auto& packet = *it;
if (packet.left > tmp_len)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
packet.left -= tmp_len;
2020-11-24 15:41:18 +01:00
break;
}
2021-04-30 16:10:14 +02:00
tmp_len -= packet.left;
if (packet.mCompleteCallback != nullptr)
{
(packet.mCompleteCallback)();
}
mSendingMsgSize -= packet.data->size();
it = mSendList.erase(it);
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
if (notInSSL && static_cast<size_t>(send_retlen) != wait_send_size)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mCanWrite = false;
must_close = !checkWrite();
break;
2020-11-24 15:41:18 +01:00
}
}
2021-04-30 16:10:14 +02:00
if (must_close)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
procCloseInLoop();
}
}
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
void quickFlush()
{
2020-11-24 15:41:18 +01:00
#ifndef MAX_IOVEC
2021-04-30 16:10:14 +02:00
constexpr size_t MAX_IOVEC = 1024;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
struct iovec iov[MAX_IOVEC];
bool must_close = false;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
while (!mSendList.empty() && mCanWrite)
{
size_t num = 0;
size_t ready_send_len = 0;
for (const auto& p : mSendList)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
iov[num].iov_base = (void*) (static_cast<const char*>(p.data->data()) + p.data->size() - p.left);
iov[num].iov_len = p.left;
ready_send_len += p.left;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
num++;
if (num >= MAX_IOVEC)
2020-11-24 15:41:18 +01:00
{
break;
}
2021-04-30 16:10:14 +02:00
}
if (num == 0)
{
break;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
const int send_len = writev(mSocket->getFD(), iov, static_cast<int>(num));
if (send_len <= 0)
{
if (BRYNET_ERRNO == BRYNET_EWOULDBLOCK)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mCanWrite = false;
must_close = !checkWrite();
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
else
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
must_close = true;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
break;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
auto tmp_len = static_cast<size_t>(send_len);
for (auto it = mSendList.begin(); it != mSendList.end();)
{
PendingPacket& b = *it;
if (b.left > tmp_len)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
b.left -= tmp_len;
2020-11-24 15:41:18 +01:00
break;
}
2021-04-30 16:10:14 +02:00
tmp_len -= b.left;
if (b.mCompleteCallback != nullptr)
{
b.mCompleteCallback();
}
mSendingMsgSize -= b.data->size();
it = mSendList.erase(it);
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
if (static_cast<size_t>(send_len) != ready_send_len)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mCanWrite = false;
must_close = !checkWrite();
break;
2020-11-24 15:41:18 +01:00
}
}
2021-04-30 16:10:14 +02:00
if (must_close)
{
procCloseInLoop();
}
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
void onClose() override
{
if (mAlreadyClose)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
return;
}
mAlreadyClose = true;
2020-11-24 15:41:18 +01:00
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
unregisterPollerEvent();
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
assert(mEnterCallback == nullptr);
auto callBack = mDisConnectCallback;
auto sharedThis = shared_from_this();
auto eventLoop = mEventLoop;
std::shared_ptr<TcpSocket> socket = std::move(const_cast<TcpSocket::Ptr&&>(mSocket));
mEventLoop->runFunctorAfterLoop([callBack,
sharedThis,
eventLoop,
socket]() {
if (callBack != nullptr)
{
callBack(sharedThis);
}
auto tmp = eventLoop->getTcpConnection(socket->getFD());
assert(tmp == sharedThis);
if (tmp == sharedThis)
{
eventLoop->removeTcpConnection(socket->getFD());
}
});
mCanWrite = false;
mDataCallback = nullptr;
mDisConnectCallback = nullptr;
mHighWaterCallback = nullptr;
mRecvBuffer = nullptr;
mSendList.clear();
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void procCloseInLoop()
{
mCanWrite = false;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
if (mPostWriteCheck || mPostRecvCheck)
{
if (mPostClose)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
return;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
mPostClose = true;
//windows下立即关闭socket可能导致fd被另外的TcpConnection重用,而导致此对象在IOCP返回相关完成结果时内存已经释放
if (mPostRecvCheck)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
CancelIoEx(HANDLE(mSocket->getFD()), &mOvlRecv.base);
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
if (mPostWriteCheck)
{
CancelIoEx(HANDLE(mSocket->getFD()), &mOvlSend.base);
}
}
else
{
2020-11-24 15:41:18 +01:00
onClose();
2021-04-30 16:10:14 +02:00
}
#elif defined BRYNET_PLATFORM_LINUX
onClose();
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
onClose();
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void procShutdownInLoop()
{
mCanWrite = false;
if (mSocket != nullptr)
2020-11-24 15:41:18 +01:00
{
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
shutdown(mSocket->getFD(), SD_SEND);
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
shutdown(mSocket->getFD(), SHUT_WR);
2020-11-24 15:41:18 +01:00
#endif
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void runAfterFlush()
{
if (!mIsPostFlush && !mSendList.empty() && mCanWrite)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
auto sharedThis = shared_from_this();
mEventLoop->runFunctorAfterLoop([sharedThis, this]() {
mIsPostFlush = false;
flush();
});
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
mIsPostFlush = true;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
void recheckEvent()
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_LINUX
2021-04-30 16:10:14 +02:00
struct epoll_event ev = {0,
{
nullptr
}};
ev.events = EPOLLET | EPOLLIN | EPOLLOUT | EPOLLRDHUP;
ev.data.ptr = (Channel*) (this);
epoll_ctl(mEventLoop->getEpollHandle(), EPOLL_CTL_MOD, mSocket->getFD(), &ev);
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
struct kevent ev[2];
memset(&ev, 0, sizeof(ev));
int n = 0;
EV_SET(&ev[n++], mSocket->getFD(), EVFILT_READ, EV_ENABLE, 0, 0, (Channel*) (this));
EV_SET(&ev[n++], mSocket->getFD(), EVFILT_WRITE, EV_ENABLE, 0, 0, (Channel*) (this));
struct timespec now = {0, 0};
kevent(mEventLoop->getKqueueHandle(), ev, n, NULL, 0, &now);
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
}
void unregisterPollerEvent()
{
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_LINUX
2021-04-30 16:10:14 +02:00
struct epoll_event ev = {0,
{
nullptr
}};
epoll_ctl(mEventLoop->getEpollHandle(), EPOLL_CTL_DEL, mSocket->getFD(), &ev);
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_DARWIN
2021-04-30 16:10:14 +02:00
struct kevent ev[2];
memset(&ev, 0, sizeof(ev));
int n = 0;
EV_SET(&ev[n++], mSocket->getFD(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
EV_SET(&ev[n++], mSocket->getFD(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
struct timespec now = {0, 0};
kevent(mEventLoop->getKqueueHandle(), ev, n, NULL, 0, &now);
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#endif
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
bool processSSLHandshake()
{
if (mIsHandsharked)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
return true;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
bool mustClose = false;
int ret = 0;
if (mSSLCtx != nullptr)
{
ret = SSL_connect(mSSL);
}
else
{
ret = SSL_accept(mSSL);
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (ret == 1)
{
mIsHandsharked = true;
if (checkRead())
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
causeEnterCallback();
2020-11-24 15:41:18 +01:00
}
else
{
2021-04-30 16:10:14 +02:00
mustClose = true;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
else if (ret == 0)
{
mustClose = true;
}
else if (ret < 0)
{
int err = SSL_get_error(mSSL, ret);
if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
if (!checkRead())
2020-11-24 15:41:18 +01:00
{
mustClose = true;
}
}
2021-04-30 16:10:14 +02:00
else
2020-11-24 15:41:18 +01:00
{
mustClose = true;
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
if (mustClose)
{
causeEnterCallback();
procCloseInLoop();
return false;
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
return true;
}
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
void causeEnterCallback()
{
assert(mEventLoop->isInLoopThread());
if (mEventLoop->isInLoopThread() && mEnterCallback != nullptr)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
auto tmp = mEnterCallback;
mEnterCallback = nullptr;
tmp(shared_from_this());
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
void processRecvMessage()
{
if (mDataCallback != nullptr && buffer_getreadvalidcount(mRecvBuffer.get()) > 0)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
auto reader = brynet::base::BasePacketReader(buffer_getreadptr(mRecvBuffer.get()),
buffer_getreadvalidcount(mRecvBuffer.get()), false);
mDataCallback(reader);
const auto consumedLen = reader.savedPos();
assert(consumedLen <= reader.size());
if (consumedLen <= reader.size())
{
buffer_addreadpos(mRecvBuffer.get(), consumedLen);
2020-11-24 15:41:18 +01:00
}
}
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
template<typename CallbackType, typename Arg>
static void verifyArgType(const CallbackType&, void (CallbackType::*)(Arg&) const)
{
static_assert(std::is_reference<Arg&>::value, "arg must be reference type");
static_assert(!std::is_const<typename std::remove_const<Arg>::type>::value, "arg can't be const type");
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-04-30 16:10:14 +02:00
struct port::Win::OverlappedExt mOvlRecv;
struct port::Win::OverlappedExt mOvlSend;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
bool mPostRecvCheck;
bool mPostWriteCheck;
bool mPostClose;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
const std::string mIP;
const TcpSocket::Ptr mSocket;
const EventLoop::Ptr mEventLoop;
bool mCanWrite;
bool mAlreadyClose;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
class BufferDeleter
{
public:
void operator()(struct brynet::base::buffer_s* ptr) const
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
brynet::base::buffer_delete(ptr);
}
};
std::unique_ptr<struct brynet::base::buffer_s, BufferDeleter> mRecvBuffer;
double mCurrentTanhXDiff = 0;
size_t mRecvBuffOriginSize = 0;
const size_t mMaxRecvBufferSize;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
struct PendingPacket
{
SendableMsg::Ptr data;
size_t left;
PacketSendedCallback mCompleteCallback;
};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
using PacketListType = std::deque<PendingPacket>;
PacketListType mSendList;
size_t mSendingMsgSize;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
EnterCallback mEnterCallback;
DataCallback mDataCallback;
DisconnectedCallback mDisConnectCallback;
HighWaterCallback mHighWaterCallback;
size_t mHighWaterSize;
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
bool mIsPostFlush;
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_USE_OPENSSL
2021-04-30 16:10:14 +02:00
SSL_CTX* mSSLCtx;
SSL* mSSL;
bool mIsHandsharked;
2020-11-24 15:41:18 +01:00
#endif
2021-04-30 16:10:14 +02:00
bool mRecvData;
std::chrono::nanoseconds mCheckTime{};
brynet::base::Timer::WeakPtr mTimer;
};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
}}// namespace brynet::net