30#include <sptk5/Buffer.h>
31#include <sptk5/DateTime.h>
32#include <sptk5/Exception.h>
33#include <sptk5/Strings.h>
34#include <sptk5/net/Host.h>
41#include <netinet/in.h>
43#include <sys/socket.h>
53using SOCKET_ADDRESS_FAMILY = sa_family_t;
62#define INVALID_SOCKET (-1)
70using SOCKET_ADDRESS_FAMILY =
unsigned short;
114 std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
122 explicit BaseSocket(SOCKET_ADDRESS_FAMILY domain = AF_INET, int32_t type = SOCK_STREAM, int32_t protocol = 0);
168 virtual void attach(SOCKET socketHandle,
bool accept);
199 std::chrono::milliseconds timeoutMS = std::chrono::milliseconds(0))
201 _open(host, openMode, blockingMode, timeoutMS);
211 void open(
const struct sockaddr_in& address,
OpenMode openMode = OpenMode::CONNECT,
212 bool blockingMode =
true, std::chrono::milliseconds timeoutMS = std::chrono::milliseconds(0))
214 _open(address, openMode, blockingMode, timeoutMS);
222 void bind(
const char* address, uint32_t portNumber);
239 [[nodiscard]]
bool active()
const
241 return m_sockfd != INVALID_SOCKET;
247 int32_t
control(
int flag,
const uint32_t* check)
const;
260 void getOption(
int level,
int option,
int& value)
const;
268 [[nodiscard]]
virtual size_t recv(uint8_t* buffer,
size_t len);
276 [[nodiscard]]
virtual size_t send(
const uint8_t* buffer,
size_t len);
285 [[nodiscard]]
virtual size_t read(uint8_t* buffer,
size_t size, sockaddr_in* from);
293 [[nodiscard]]
virtual size_t read(uint8_t* buffer,
size_t size)
295 return read(buffer, size,
nullptr);
307 [[nodiscard]]
virtual size_t read(
Buffer& buffer,
size_t size, sockaddr_in* from);
319 return read(buffer, size,
nullptr);
331 [[nodiscard]]
virtual size_t read(
String& buffer,
size_t size, sockaddr_in* from);
343 return read(buffer, size,
nullptr);
355 virtual size_t write(
const uint8_t* buffer,
size_t size,
const sockaddr_in* peer);
365 size_t write(
const uint8_t* buffer,
size_t size)
367 return write(buffer, size,
nullptr);
376 virtual size_t write(
const Buffer& buffer,
const sockaddr_in* peer);
385 return write(buffer,
nullptr);
394 virtual size_t write(
const String& buffer,
const sockaddr_in* peer);
403 return write(buffer,
nullptr);
410 [[nodiscard]]
virtual bool readyToRead(std::chrono::milliseconds timeout);
416 [[nodiscard]]
virtual bool readyToWrite(std::chrono::milliseconds timeout);
424 return m_blockingMode;
447 [[nodiscard]] int32_t
type()
const
464 static void init() noexcept;
469 static
void cleanup() noexcept;
479 virtual void _open(
const Host& host,
OpenMode openMode,
bool blockingMode, std::chrono::milliseconds timeoutMS);
488 virtual void _open(
const struct sockaddr_in& address,
OpenMode openMode,
bool blockingMode,
489 std::chrono::milliseconds timeoutMS)
495 SOCKET m_sockfd {INVALID_SOCKET};
500 bool m_blockingMode {
false};
511#define THROW_SOCKET_ERROR(msg) sptk::throwSocketError(msg, __FILE__, __LINE__)
Definition: BaseSocket.h:87
virtual ~BaseSocket()
Destructor.
void open_addr(OpenMode openMode=OpenMode::CREATE, const sockaddr_in *addr=nullptr, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
bool blockingMode() const
Return current blocking mode state.
Definition: BaseSocket.h:422
void listen(uint16_t portNumber=0)
void getOption(int level, int option, int &value) const
virtual size_t read(uint8_t *buffer, size_t size, sockaddr_in *from)
size_t write(const String &buffer)
Definition: BaseSocket.h:401
size_t read(String &buffer, size_t size)
Definition: BaseSocket.h:341
size_t write(const uint8_t *buffer, size_t size)
Definition: BaseSocket.h:365
virtual size_t recv(uint8_t *buffer, size_t len)
virtual size_t read(String &buffer, size_t size, sockaddr_in *from)
int32_t domain() const
Definition: BaseSocket.h:439
void bind(const char *address, uint32_t portNumber)
BaseSocket(SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
const Host & host() const
Definition: BaseSocket.h:186
virtual void _open(const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
int32_t control(int flag, const uint32_t *check) const
virtual size_t write(const String &buffer, const sockaddr_in *peer)
BaseSocket(const BaseSocket &other)=delete
virtual size_t write(const Buffer &buffer, const sockaddr_in *peer)
BaseSocket & operator=(const BaseSocket &other)=delete
virtual void _open(const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
Definition: BaseSocket.h:488
virtual bool readyToWrite(std::chrono::milliseconds timeout)
void blockingMode(bool blocking)
OpenMode
Definition: BaseSocket.h:93
size_t read(Buffer &buffer, size_t size)
Definition: BaseSocket.h:317
BaseSocket(BaseSocket &&other) noexcept=default
virtual size_t socketBytes()
virtual bool readyToRead(std::chrono::milliseconds timeout)
void setSocketFD(SOCKET socket)
Definition: BaseSocket.h:431
void open(const struct sockaddr_in &address, OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
Definition: BaseSocket.h:211
virtual size_t write(const uint8_t *buffer, size_t size, const sockaddr_in *peer)
virtual void attach(SOCKET socketHandle, bool accept)
void host(const Host &host)
size_t write(const Buffer &buffer)
Definition: BaseSocket.h:383
virtual size_t read(Buffer &buffer, size_t size, sockaddr_in *from)
virtual size_t read(uint8_t *buffer, size_t size)
Definition: BaseSocket.h:293
void setOption(int level, int option, int value) const
SOCKET fd() const
Definition: BaseSocket.h:102
virtual size_t send(const uint8_t *buffer, size_t len)
int32_t protocol() const
Definition: BaseSocket.h:455
BaseSocket & operator=(BaseSocket &&other) noexcept=default
int32_t type() const
Definition: BaseSocket.h:447
void open(const Host &host=Host(), OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
Definition: BaseSocket.h:198
virtual void close() noexcept
SP_EXPORT void throwSocketError(const String &message, const char *file, int line)