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