Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2025 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include <etl/delegate.h>
14 : #include <etl/span.h>
15 : #include <ip/IPAddress.h>
16 :
17 : #include <cstdint>
18 :
19 : namespace tcp
20 : {
21 : class IDataListener;
22 : class IDataSendNotificationListener;
23 :
24 : /**
25 : * Abstract base class for all sockets.
26 : *
27 : * \section Overview
28 : * The AbstractSocket is designed to be a socket abstraction to
29 : * a TCP stack.
30 : * The socket class can be used to connect to a remote system. It is also
31 : * returned to an ISocketConnectionListener by AbstractServerSocket
32 : * (using its ISocketProvider) once a connection from a remote system
33 : * is accepted. An active socket is used to receive and transmit data
34 : * from and to a remote system.
35 : *
36 : * \section Receive
37 : * The application that wants to receive data from a socket has to register
38 : * through the setDataListener method as an IDataListener.
39 : * After that the application is notified through its dataReceived
40 : * method.
41 : *
42 : * \section Send
43 : * Sending data through a socket is usually asynchronous. Because of that
44 : * it is possible to register an IDataSendNotificationListener who is
45 : * notified when the data passed to the send method is written to the
46 : * TCP-stack.
47 : * <b>This is not the time when the ACK package from the remote
48 : * system, is received!</b>
49 : *
50 : * \see AbstractServerSocket
51 : * \see ISocketProvidingConnectionListener
52 : * \see IDataListener
53 : * \see IDataSendNotificationListener
54 : */
55 : class AbstractSocket
56 : {
57 : public:
58 : // [AbstractSocket]
59 : /**
60 : * all ErrorCodes used by socket
61 : * \enum ErrorCode
62 : */
63 : enum class ErrorCode : uint8_t
64 : {
65 : /** everything ok */
66 : SOCKET_ERR_OK,
67 : /** an error occurred */
68 : SOCKET_ERR_NOT_OK,
69 : /** socket not open to send */
70 : SOCKET_ERR_NOT_OPEN,
71 : /** no more outgoing buffer available */
72 : SOCKET_ERR_NO_MORE_BUFFER,
73 : /** socket is full, flush before continuing */
74 : SOCKET_FLUSH
75 : };
76 :
77 : using ConnectedDelegate = ::etl::delegate<void(ErrorCode)>;
78 :
79 : /**
80 : * constructor
81 : * \post fpDataListener == 0L
82 : * \post fpSendNotificationListener == 0L
83 : */
84 208 : AbstractSocket() = default;
85 :
86 : AbstractSocket(AbstractSocket const&) = delete;
87 : AbstractSocket& operator=(AbstractSocket const&) = delete;
88 : AbstractSocket(AbstractSocket&&) = delete;
89 : AbstractSocket& operator=(AbstractSocket&&) = delete;
90 :
91 : /**
92 : * binds to a local address and port
93 : * \param ipAddr local ip address
94 : * \param port local port
95 : */
96 : virtual ErrorCode bind(ip::IPAddress const& ipAddr, uint16_t port) = 0;
97 :
98 : /**
99 : * connects to a remote host
100 : * \param ipAddr remote ip address
101 : * \param port remote port
102 : */
103 : virtual ErrorCode
104 : connect(ip::IPAddress const& ipAddr, uint16_t port, ConnectedDelegate delegate)
105 : = 0;
106 :
107 : /**
108 : * closes the socket by sending a FIN packet
109 : */
110 : virtual ErrorCode close() = 0;
111 :
112 : /**
113 : * aborts the socket by sending a RST packet
114 : */
115 : virtual void abort() = 0;
116 :
117 : /**
118 : * flushes enqueued data
119 : */
120 : virtual ErrorCode flush() = 0;
121 :
122 : /**
123 : * discards any received data without ack
124 : */
125 : virtual void discardData() = 0;
126 :
127 : /**
128 : * \return number of bytes available in outgoing buffer
129 : */
130 : virtual size_t available() = 0;
131 :
132 : /**
133 : * reads a single byte
134 : * \param byte byte to read
135 : * \return number of bytes read
136 : * - 0: an error occurred reading from the socket
137 : * - 1: byte read
138 : */
139 : virtual uint8_t read(uint8_t& byte) = 0;
140 :
141 : /**
142 : * reads a given number of bytes from the socket
143 : * \param buffer buffer to receive data to. If 0L is passed the n
144 : * bytes should be skipped, i.e. the input stream has to advance by
145 : * n bytes.
146 : * \param n number of bytes to receive
147 : * \return number of bytes really read
148 : * - 0: an error occurred reading from the socket
149 : * - else: bytes read
150 : */
151 : virtual size_t read(uint8_t* buffer, size_t n) = 0;
152 :
153 : /**
154 : * sends an amount of data
155 : * \param data data to send
156 : * \return status of transmission
157 : * - SOCKET_OK when data was sent to TCP stack
158 : * - SOCKET_CLOSED when socket has not been opened
159 : * \note
160 : * Sending the data may be asynchronous. For this reason the
161 : * IDataSendNotificationListener has an appropriate callback.
162 : */
163 : virtual ErrorCode send(::etl::span<uint8_t const> const& data) = 0;
164 :
165 : /**
166 : * sets the listener to this socket instance
167 : * \param pListener IDataListener to attach
168 : */
169 : void setDataListener(IDataListener* pListener);
170 :
171 : IDataListener* getDataListener() const;
172 :
173 : /**
174 : * sets the IDataSendNotificationListener of this socket
175 : * \param pListener listener that gets notified when data has been
176 : * copied completely to TCP stack
177 : */
178 : void setSendNotificationListener(IDataSendNotificationListener* pListener);
179 :
180 : IDataSendNotificationListener* getSendNotificationListener() const;
181 :
182 : virtual ip::IPAddress getRemoteIPAddress() const = 0;
183 :
184 : virtual ip::IPAddress getLocalIPAddress() const = 0;
185 :
186 : virtual uint16_t getRemotePort() const = 0;
187 :
188 : virtual uint16_t getLocalPort() const = 0;
189 :
190 : virtual bool isClosed() const = 0;
191 :
192 : virtual bool isEstablished() const = 0;
193 :
194 : /**
195 : * Permanently disables NAGLE algorithm for this socket.
196 : */
197 : virtual void disableNagleAlgorithm() = 0;
198 :
199 : /**
200 : * Enables TCP keepalive for this socket.
201 : * \param idle the length of time to keep an idle TCP connection active
202 : * \param interval the interval between packets sent to validate the TCP connection
203 : * \param probes the number of keepalive probes to be sent before terminating the connection
204 : */
205 : virtual void enableKeepAlive(uint32_t idle, uint32_t interval, uint32_t probes) = 0;
206 :
207 : /**
208 : * Disables TCP keepalive for this socket.
209 : */
210 : virtual void disableKeepAlive() = 0;
211 : // [AbstractSocket]
212 :
213 : protected:
214 : ~AbstractSocket() = default;
215 :
216 : IDataListener* _dataListener{nullptr};
217 : IDataSendNotificationListener* _sendNotificationListener{nullptr};
218 : };
219 :
220 : /*
221 : * inline methods
222 : */
223 155 : inline void AbstractSocket::setDataListener(IDataListener* const pListener)
224 : {
225 155 : _dataListener = pListener;
226 155 : }
227 :
228 : inline void
229 47 : AbstractSocket::setSendNotificationListener(IDataSendNotificationListener* const pListener)
230 : {
231 47 : _sendNotificationListener = pListener;
232 47 : }
233 :
234 57 : inline IDataListener* AbstractSocket::getDataListener() const { return _dataListener; }
235 :
236 44 : inline IDataSendNotificationListener* AbstractSocket::getSendNotificationListener() const
237 : {
238 44 : return _sendNotificationListener;
239 : }
240 :
241 : } // namespace tcp
|