29#include <sptk5/Buffer.h>
30#include <sptk5/Exception.h>
31#include <sptk5/Strings.h>
32#include <sptk5/net/BaseSocket.h>
33#include <sptk5/net/Proxy.h>
40#include <netinet/in.h>
41#include <sys/socket.h>
50using SOCKET_ADDRESS_FAMILY = sa_family_t;
55#define INVALID_SOCKET (-1)
63using SOCKET_ADDRESS_FAMILY =
unsigned short;
106 size_t read(uint8_t* destination,
size_t sz,
char delimiter,
bool read_line, struct sockaddr_in* from =
nullptr);
114 size_t readLine(
Buffer& dest,
char delimiter);
119 [[nodiscard]]
size_t availableBytes() const;
125 void readMoreFromSocket(
int availableBytes);
136 uint32_t m_readOffset {0};
138 [[nodiscard]] int32_t readFromSocket(sockaddr_in* from);
151 [[nodiscard]] int32_t bufferedRead(uint8_t* destination,
size_t sz,
char delimiter,
bool read_line,
152 struct sockaddr_in* from =
nullptr);
154 void handleReadFromSocketError(
int error);
173 explicit TCPSocket(SOCKET_ADDRESS_FAMILY domain = AF_INET, int32_t type = SOCK_STREAM, int32_t protocol = 0);
200 [[nodiscard]] virtual
bool accept(SOCKET& clientSocketFD, struct sockaddr_in& clientInfo, std::chrono::milliseconds timeout);
205 size_t socketBytes() override;
211 bool readyToRead(std::chrono::milliseconds timeout) override;
222 size_t readLine(
char* buffer,
size_t size,
char delimiter = '\n');
232 size_t readLine(
Buffer& buffer,
char delimiter = '\n');
240 size_t readLine(
String& s,
char delimiter = '\n');
249 size_t read(uint8_t* buffer,
size_t size, sockaddr_in* from =
nullptr) override;
260 size_t read(
Buffer& buffer,
size_t size, sockaddr_in* from =
nullptr) override;
271 size_t read(
String& buffer,
size_t size, sockaddr_in* from =
nullptr) override;
274 size_t read(T& value, sockaddr_in* from =
nullptr)
276 return read((uint8_t*) &value,
sizeof(T), from);
296 void _open(
const Host& host,
OpenMode openMode,
bool blockingMode, std::chrono::milliseconds timeout)
override;
305 void _open(
const struct sockaddr_in& address,
OpenMode openMode,
bool blockingMode,
306 std::chrono::milliseconds timeout)
override;
314 return m_proxy.get();
319 std::shared_ptr<Proxy> m_proxy;
323using STCPSocket = std::shared_ptr<TCPSocket>;
Definition: BaseSocket.h:87
OpenMode
Definition: BaseSocket.h:93
Definition: TCPSocket.h:78
TCPSocketReader(BaseSocket &socket, size_t bufferSize=16384)
Definition: TCPSocket.h:165
void _open(const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
Proxy * proxy()
Definition: TCPSocket.h:312
void _open(const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
TCPSocket(SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
void close() noexcept override
TCPSocketReader & reader()
Definition: TCPSocket.h:284
void setProxy(std::shared_ptr< Proxy > proxy)