blob: 67812c23735e482180f218f383f2d32b90312729 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "ip_utils.h"
20
21#include <functional>
22#include <vector>
23#include <chrono>
24#include <system_error>
25#include <cstdint>
26
27#if defined(_MSC_VER)
28#include <BaseTsd.h>
29using ssize_t = SSIZE_T;
30#endif
31
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040032namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040033
34template<typename T>
35class GenericSocket
36{
37public:
38 using ValueType = T;
39
40 virtual ~GenericSocket() { shutdown(); }
41
42 using RecvCb = std::function<ssize_t(const ValueType* buf, std::size_t len)>;
43
44 /// Close established connection
45 /// \note Terminate outstanding blocking read operations with an empty error code, but a 0 read size.
46 virtual void shutdown() {}
47
48 /// Set Rx callback
49 /// \warning This method is here for backward compatibility
50 /// and because async IO are not implemented yet.
51 virtual void setOnRecv(RecvCb&& cb) = 0;
52
53 virtual bool isReliable() const = 0;
54
55 virtual bool isInitiator() const = 0;
56
57 /// Return maximum application payload size.
58 /// This value is negative if the session is not ready to give a valid answer.
59 /// The value is 0 if such information is irrelevant for the session.
60 /// If stricly positive, the user must use send() with an input buffer size below or equals
61 /// to this value if it want to be sure that the transport sent it in an atomic way.
62 /// Example: in case of non-reliable transport using packet oriented IO,
63 /// this value gives the maximal size used to send one packet.
64 virtual int maxPayload() const = 0;
65
66 /// Wait until data to read available, timeout or io error
67 /// \param ec error code set in case of error (if return value is < 0)
68 /// \return positive number if data ready for read, 0 in case of timeout or error.
69 /// \note error code is not set in case of timeout, but set only in case of io error
70 /// (i.e. socket deconnection).
71 /// \todo make a std::chrono version for the timeout
72 virtual int waitForData(std::chrono::milliseconds timeout, std::error_code& ec) const = 0;
73
74 /// Write a given amount of data.
75 /// \param buf data to write.
76 /// \param len number of bytes to write.
77 /// \param ec error code set in case of error.
78 /// \return number of bytes written, 0 is valid.
79 /// \warning error checking consists in checking if \a !ec is true, not if returned size is 0
80 /// as a write of 0 could be considered a valid operation.
81 virtual std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) = 0;
82
83 /// Read a given amount of data.
84 /// \param buf data to read.
85 /// \param len number of bytes to read.
86 /// \param ec error code set in case of error.
87 /// \return number of bytes read, 0 is valid.
88 /// \warning error checking consists in checking if \a !ec is true, not if returned size is 0
89 /// as a read of 0 could be considered a valid operation (i.e. non-blocking IO).
90 virtual std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) = 0;
91
92 /// write() adaptor for STL containers
93 template<typename U>
94 std::size_t write(const U& obj, std::error_code& ec)
95 {
96 return write(obj.data(), obj.size() * sizeof(typename U::value_type), ec);
97 }
98
99 /// read() adaptor for STL containers
100 template<typename U>
101 std::size_t read(U& storage, std::error_code& ec)
102 {
103 auto res = read(storage.data(), storage.size() * sizeof(typename U::value_type), ec);
104 if (!ec)
105 storage.resize(res);
106 return res;
107 }
108
109 /// Return the local IP address if known.
110 /// \note The address is not valid (addr.isUnspecified() returns true) if it's not known
111 /// or not available.
112 virtual IpAddr localAddr() const { return {}; }
113
114 /// Return the remote IP address if known.
115 /// \note The address is not valid (addr.isUnspecified() returns true) if it's not known
116 /// or not available.
117 virtual IpAddr remoteAddr() const { return {}; }
118
119protected:
120 GenericSocket() = default;
121};
122
Sébastien Blin464bdff2023-07-19 08:02:53 -0400123} // namespace dhtnet