rcpp_framework/libs/brynet/net/Socket.hpp

192 lines
3.9 KiB
C++
Raw Normal View History

2020-11-24 15:41:18 +01:00
#pragma once
#include <brynet/base/NonCopyable.hpp>
#include <brynet/net/SocketLibFunction.hpp>
2021-04-30 16:10:14 +02:00
#include <exception>
#include <memory>
#include <stdexcept>
#include <string>
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
namespace brynet {
namespace net {
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
class TcpConnection;
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class UniqueFd final : public brynet::base::NonCopyable {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
explicit UniqueFd(BrynetSocketFD fd) :
mFD(fd) {}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
~UniqueFd() {
brynet::net::base::SocketClose(mFD);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
UniqueFd(const UniqueFd &other) = delete;
UniqueFd &operator=(const UniqueFd &other) = delete;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
BrynetSocketFD getFD() const {
return mFD;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
BrynetSocketFD mFD;
2021-04-30 16:10:14 +02:00
};
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class TcpSocket : public brynet::base::NonCopyable {
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
class TcpSocketDeleter {
public:
void operator()(TcpSocket *ptr) const {
delete ptr;
}
};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
using Ptr = std::unique_ptr<TcpSocket, TcpSocketDeleter>;
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
static Ptr Create(BrynetSocketFD fd, bool serverSide) {
class make_unique_enabler : public TcpSocket {
public:
make_unique_enabler(BrynetSocketFD fd, bool serverSide) :
TcpSocket(fd, serverSide) {}
};
return Ptr(new make_unique_enabler(fd, serverSide));
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
void setNodelay() const {
brynet::net::base::SocketNodelay(mFD);
}
bool setNonblock() const {
return brynet::net::base::SocketNonblock(mFD);
}
void setSendSize(int sdSize) const {
brynet::net::base::SocketSetSendSize(mFD, sdSize);
}
void setRecvSize(int rdSize) const {
brynet::net::base::SocketSetRecvSize(mFD, rdSize);
}
std::string getRemoteIP() const {
return brynet::net::base::GetIPOfSocket(mFD);
}
bool isServerSide() const {
return mServerSide;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
protected:
2021-05-14 17:16:45 +02:00
TcpSocket(BrynetSocketFD fd, bool serverSide) :
mFD(fd),
mServerSide(serverSide) {
}
virtual ~TcpSocket() {
brynet::net::base::SocketClose(mFD);
}
BrynetSocketFD getFD() const {
return mFD;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
const BrynetSocketFD mFD;
const bool mServerSide;
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
friend class TcpConnection;
2021-04-30 16:10:14 +02:00
};
2021-05-14 17:16:45 +02:00
class EintrError : public std::exception {
2021-04-30 16:10:14 +02:00
};
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class AcceptError : public std::runtime_error {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
explicit AcceptError(int errorCode) :
std::runtime_error(std::to_string(errorCode)),
mErrorCode(errorCode) {}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
int getErrorCode() const {
return mErrorCode;
}
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
int mErrorCode;
2021-04-30 16:10:14 +02:00
};
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class ListenSocket : public brynet::base::NonCopyable {
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
class ListenSocketDeleter {
public:
void operator()(ListenSocket *ptr) const {
delete ptr;
}
};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
using Ptr = std::unique_ptr<ListenSocket, ListenSocketDeleter>;
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
TcpSocket::Ptr accept() {
const auto clientFD = brynet::net::base::Accept(mFD, nullptr, nullptr);
if (clientFD == BRYNET_INVALID_SOCKET) {
2021-04-30 16:10:14 +02:00
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-05-14 17:16:45 +02:00
if (BRYNET_ERRNO == EMFILE) {
// Thanks libev and muduo.
// Read the section named "The special problem of
// accept()ing when you can't" in libev's doc.
// By Marc Lehmann, author of libev.
mIdle.reset();
TcpSocket::Create(brynet::net::base::Accept(mFD, nullptr, nullptr), true);
mIdle = brynet::net::TcpSocket::Create(::open("/dev/null", O_RDONLY | O_CLOEXEC), true);
}
2020-11-24 15:41:18 +01:00
#endif
2021-05-14 17:16:45 +02:00
if (BRYNET_ERRNO == EINTR) {
throw EintrError();
} else {
throw AcceptError(BRYNET_ERRNO);
}
}
return TcpSocket::Create(clientFD, true);
}
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
static Ptr Create(BrynetSocketFD fd) {
class make_unique_enabler : public ListenSocket {
public:
explicit make_unique_enabler(BrynetSocketFD fd) :
ListenSocket(fd) {}
};
return Ptr(new make_unique_enabler(fd));
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
protected:
2021-05-14 17:16:45 +02:00
explicit ListenSocket(BrynetSocketFD fd) :
mFD(fd) {
2020-11-24 15:41:18 +01:00
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-05-14 17:16:45 +02:00
mIdle = brynet::net::TcpSocket::Create(::open("/dev/null", O_RDONLY | O_CLOEXEC), true);
2020-11-24 15:41:18 +01:00
#endif
2021-05-14 17:16:45 +02:00
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
virtual ~ListenSocket() {
brynet::net::base::SocketClose(mFD);
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
const BrynetSocketFD mFD;
2020-11-24 15:41:18 +01:00
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
2021-05-14 17:16:45 +02:00
brynet::net::TcpSocket::Ptr mIdle;
2020-11-24 15:41:18 +01:00
#endif
2021-05-14 17:16:45 +02:00
friend class TcpConnection;
2021-04-30 16:10:14 +02:00
};
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
} // namespace net
} // namespace brynet