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-04-30 16:10:14 +02:00
|
|
|
class TcpConnection;
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-05-14 17:49:39 +02:00
|
|
|
class UniqueFd final : public 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() {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketClose(mFD);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
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:49:39 +02:00
|
|
|
class TcpSocket : public 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 {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketNodelay(mFD);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool setNonblock() const {
|
2021-05-14 17:49:39 +02:00
|
|
|
return SocketNonblock(mFD);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setSendSize(int sdSize) const {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketSetSendSize(mFD, sdSize);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRecvSize(int rdSize) const {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketSetRecvSize(mFD, rdSize);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string getRemoteIP() const {
|
2021-05-14 17:49:39 +02:00
|
|
|
return GetIPOfSocket(mFD);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketClose(mFD);
|
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
|
|
|
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:49:39 +02:00
|
|
|
class ListenSocket : public 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() {
|
2021-05-14 17:49:39 +02:00
|
|
|
const auto clientFD = Accept(mFD, nullptr, nullptr);
|
2021-05-14 17:16:45 +02:00
|
|
|
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();
|
2021-05-14 17:49:39 +02:00
|
|
|
TcpSocket::Create(Accept(mFD, nullptr, nullptr), true);
|
|
|
|
mIdle = TcpSocket::Create(::open("/dev/null", O_RDONLY | O_CLOEXEC), true);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
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:49:39 +02:00
|
|
|
mIdle = 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() {
|
2021-05-14 17:49:39 +02:00
|
|
|
SocketClose(mFD);
|
2021-05-14 17:16:45 +02:00
|
|
|
}
|
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:49:39 +02:00
|
|
|
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
|
|
|
|