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 "tcp/socket/ISocketProvidingConnectionListener.h"
14 :
15 : #include <cstdint>
16 :
17 : /**
18 : * contains TCP abstraction layer related code
19 : * \namespace tcp
20 : */
21 : namespace tcp
22 : {
23 : /**
24 : * Abstract base class for server sockets.
25 : *
26 : * An AbstractServerSocket is the base class for server sockets that may
27 : * open a specific port. To accept incoming connections the AbstractServerSocket
28 : * needs a ISocketProvidingConnectionListener. On a incoming connection request
29 : * the AbstractServerSocket requests an AbstractSocket object from the
30 : * ISocketProvidingConnectionListener to bind the connection to via its
31 : * getSocket() method. In case of success the listeners connectionAccepted()
32 : * method is called passing the bound AbstractSocket to the application.
33 : *
34 : * \see ISocketProvidingConnectionListener
35 : */
36 : class AbstractServerSocket
37 : {
38 : public:
39 : AbstractServerSocket();
40 : AbstractServerSocket(AbstractServerSocket const&) = delete;
41 : AbstractServerSocket& operator=(AbstractServerSocket const&) = delete;
42 : AbstractServerSocket(AbstractServerSocket&&) = delete;
43 : AbstractServerSocket& operator=(AbstractServerSocket&&) = delete;
44 :
45 : // [AbstractServerSocket]
46 : /**
47 : * constructor taking the AbstractServerSockets port
48 : * \param port port of socket
49 : * \param providingListener ISocketProvidingConnectionListener which
50 : * provides the AbstractServerSocket with AbstractSocket objects
51 : * and gets notified when a connection is successfully established
52 : *
53 : * As long as the ISocketProvidingConnectionListener returns valid pointers
54 : * to AbstractSocket objects connections are accpeted and delegated to the
55 : * ISocketProvidingConnectionListener. When NULL is returned, the connection
56 : * is refused.
57 : */
58 : AbstractServerSocket(uint16_t port, ISocketProvidingConnectionListener& providingListener);
59 :
60 : /**
61 : * \return port the AbstractServerSocket is listening for
62 : */
63 14 : uint16_t getLocalPort() const { return _port; }
64 :
65 2 : void setPort(uint16_t const port) { _port = port; }
66 :
67 : /**
68 : * accepts a connection to the AbstractServerSockets port
69 : */
70 : virtual bool accept() = 0;
71 :
72 : /**
73 : * Binds this server socket to a given netif.
74 : */
75 : virtual bool bind(ip::IPAddress const& localIpAddress, uint16_t port) = 0;
76 :
77 : /**
78 : * closes the accepted port
79 : */
80 : virtual void close() = 0;
81 :
82 : virtual bool isClosed() const = 0;
83 :
84 : // [AbstractServerSocket]
85 :
86 60 : void setSocketProvidingConnectionListener(ISocketProvidingConnectionListener& listener)
87 : {
88 60 : _socketProvidingConnectionListener = &listener;
89 60 : }
90 :
91 8 : ISocketProvidingConnectionListener& getSocketProvidingConnectionListener() const
92 : {
93 8 : return *_socketProvidingConnectionListener;
94 : }
95 :
96 : protected:
97 : ~AbstractServerSocket() = default;
98 :
99 : /** AbstractServerSockets port */
100 : uint16_t _port;
101 : /** reference to ISocketProvidingConnectionListener */
102 : ISocketProvidingConnectionListener* _socketProvidingConnectionListener;
103 : };
104 :
105 : } // namespace tcp
|