sptk2 logo
SPTK Home Page
TCPSocket.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
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>
34
35#ifndef _WIN32
36
37#include <arpa/inet.h>
38#include <fcntl.h>
39#include <netdb.h>
40#include <netinet/in.h>
41#include <sys/socket.h>
42#include <sys/types.h>
43#include <sys/un.h>
44#include <unistd.h>
45
49using SOCKET = int;
50using SOCKET_ADDRESS_FAMILY = sa_family_t;
51
55#define INVALID_SOCKET (-1)
56
57#else
58#include <winsock2.h>
59
60#include <windows.h>
61
62using socklen_t = int;
63using SOCKET_ADDRESS_FAMILY = unsigned short;
64#endif
65
66namespace sptk {
67
76class SP_EXPORT TCPSocketReader
77 : public Buffer
78{
79public:
85 explicit TCPSocketReader(BaseSocket& socket, size_t bufferSize = 16384);
86
90 void open();
91
95 void close() noexcept;
96
106 size_t read(uint8_t* destination, size_t sz, char delimiter, bool read_line, struct sockaddr_in* from = nullptr);
107
114 size_t readLine(Buffer& dest, char delimiter);
115
119 [[nodiscard]] size_t availableBytes() const;
120
125 void readMoreFromSocket(int availableBytes);
126
127private:
131 BaseSocket& m_socket;
132
136 uint32_t m_readOffset {0};
137
138 [[nodiscard]] int32_t readFromSocket(sockaddr_in* from);
139
151 [[nodiscard]] int32_t bufferedRead(uint8_t* destination, size_t sz, char delimiter, bool read_line,
152 struct sockaddr_in* from = nullptr);
153
154 void handleReadFromSocketError(int error);
155};
156
163class SP_EXPORT TCPSocket
164 : public BaseSocket
165{
166public:
173 explicit TCPSocket(SOCKET_ADDRESS_FAMILY domain = AF_INET, int32_t type = SOCK_STREAM, int32_t protocol = 0);
174
178 ~TCPSocket() override;
179
184 void setProxy(std::shared_ptr<Proxy> proxy);
185
189 void close() noexcept override;
190
200 [[nodiscard]] virtual bool accept(SOCKET& clientSocketFD, struct sockaddr_in& clientInfo, std::chrono::milliseconds timeout);
201
205 size_t socketBytes() override;
206
211 bool readyToRead(std::chrono::milliseconds timeout) override;
212
222 size_t readLine(char* buffer, size_t size, char delimiter = '\n');
223
232 size_t readLine(Buffer& buffer, char delimiter = '\n');
233
240 size_t readLine(String& s, char delimiter = '\n');
241
249 size_t read(uint8_t* buffer, size_t size, sockaddr_in* from = nullptr) override;
250
260 size_t read(Buffer& buffer, size_t size, sockaddr_in* from = nullptr) override;
261
271 size_t read(String& buffer, size_t size, sockaddr_in* from = nullptr) override;
272
273 template<typename T>
274 size_t read(T& value, sockaddr_in* from = nullptr)
275 {
276 return read((uint8_t*) &value, sizeof(T), from);
277 }
278
279protected:
285 {
286 return m_reader;
287 }
288
296 void _open(const Host& host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override;
297
305 void _open(const struct sockaddr_in& address, OpenMode openMode, bool blockingMode,
306 std::chrono::milliseconds timeout) override;
307
313 {
314 return m_proxy.get();
315 }
316
317private:
318 TCPSocketReader m_reader;
319 std::shared_ptr<Proxy> m_proxy;
320 Buffer m_stringBuffer;
321};
322
323using STCPSocket = std::shared_ptr<TCPSocket>;
324
328} // namespace sptk
Definition: BaseSocket.h:87
OpenMode
Definition: BaseSocket.h:93
Definition: Buffer.h:51
Definition: Host.h:55
Definition: Proxy.h:37
Definition: String.h:49
Definition: TCPSocket.h:78
TCPSocketReader(BaseSocket &socket, size_t bufferSize=16384)
void close() noexcept
Definition: TCPSocket.h:165
void _open(const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
~TCPSocket() 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)

Fri Oct 14 2022 09:58:32: SPTK 5.4.1