1#ifndef TENDUKE_OSA_SOCKETTEMPLATE_H
2#define TENDUKE_OSA_SOCKETTEMPLATE_H
4#include "net/NetworkingException.h"
11#if defined(__APPLE__) || defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION) || defined(__ANDROID__)
14#include <sys/socket.h>
17#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined (_WIN64)
33 AbstractBaseSocket(
const AbstractBaseSocket&) =
delete;
34 AbstractBaseSocket& operator=(
const AbstractBaseSocket&) =
delete;
47 std::unique_ptr<::tenduke::osa::Socket>
accept(
const uint32_t timeoutMs)
override
64 throw ::tenduke::net::NetworkingException(
mkErrorMessage(
"Error trying to listen() the socket: "));
71 int read(
void * buffer,
const std::size_t bufferSize)
override
73 auto bytesRead = ::recv(
75 static_cast<char *
>(buffer),
88 throw ::tenduke::net::NetworkingException(
mkErrorMessage(
"::recv() from socket failed: ", errorCode));
90 return static_cast<int>(bytesRead);
96 int write(
const void * buffer,
const std::size_t len)
override
98 const auto bytesWritten = ::send(
100 static_cast<const char *
>(buffer),
105 if (bytesWritten < 0) {
106 throw ::tenduke::net::NetworkingException(
mkErrorMessage(
"::send() to socket failed: "));
109 return static_cast<int>(bytesWritten);
156 virtual std::string
mkErrorMessage(
const std::string &body,
int errorCode)
const = 0;
173 static_cast<std::int32_t
>(milliseconds) / 1000,
174 (
static_cast<std::int32_t
>(milliseconds) % 1000) * 1000 };
void listen() override
Configures the socket to listen for incoming connections.
Definition AbstractBaseSocket.h:60
AbstractBaseSocket(const DESCRIPTOR_TYPE descriptor)
Constructs a new socket.
Definition AbstractBaseSocket.h:39
int write(const void *buffer, const std::size_t len) override
Write bytes to the socket.number of bytes written
Definition AbstractBaseSocket.h:96
virtual std::string mkErrorMessage(const std::string &body) const
Builds error message.
Definition AbstractBaseSocket.h:133
virtual int getLastError() const =0
Returns last error code.
int read(void *buffer, const std::size_t bufferSize) override
Reads bytes from the socket.number of bytes read. Returns -1, if socket is non-blocking and nothing t...
Definition AbstractBaseSocket.h:71
virtual std::string mkErrorMessage(const char *body) const
Builds error message.
Definition AbstractBaseSocket.h:144
virtual std::string mkErrorMessage(const std::string &body, int errorCode) const =0
Builds error message.
virtual std::unique_ptr< Socket > accept()=0
Accepts next connection from listening socket.
virtual bool wouldBlock(int errorCode) const =0
Checks if error code means that call would have blocked.
static struct::timeval timeFromMilliseconds(const std::uint32_t milliseconds)
Returns struct timeval populated from milliseconds.
Definition AbstractBaseSocket.h:170
std::unique_ptr<::tenduke::osa::Socket > accept(const uint32_t timeoutMs) override
Accepts next connection from listening socket, with a timeout.The socket must be set to listening mod...
Definition AbstractBaseSocket.h:47
virtual bool hasIncomingConnections(std::uint32_t timeoutMs) const =0
Checks if the socket has incoming connections.
DESCRIPTOR_TYPE descriptor
The socket descriptor.
Definition AbstractBaseSocket.h:178
OS-independent abstraction of a socket.
Definition Socket.h:14
virtual std::unique_ptr< Socket > accept(uint32_t timeoutMs)=0
Accepts next connection from listening socket, with a timeout.
Operating system abstraction.
Definition win32_utils.h:7
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4