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 <platform/estdint.h>
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 :
43 : // [AbstractServerSocket]
44 : /**
45 : * constructor taking the AbstractServerSockets port
46 : * \param port port of socket
47 : * \param providingListener ISocketProvidingConnectionListener which
48 : * provides the AbstractServerSocket with AbstractSocket objects
49 : * and gets notified when a connection is successfully established
50 : *
51 : * As long as the ISocketProvidingConnectionListener returns valid pointers
52 : * to AbstractSocket objects connections are accpeted and delegated to the
53 : * ISocketProvidingConnectionListener. When NULL is returned, the connection
54 : * is refused.
55 : */
56 : AbstractServerSocket(uint16_t port, ISocketProvidingConnectionListener& providingListener);
57 :
58 : /**
59 : * \return port the AbstractServerSocket is listening for
60 : */
61 3 : uint16_t getLocalPort() const { return _port; }
62 :
63 1 : void setPort(uint16_t const port) { _port = port; }
64 :
65 : /**
66 : * accepts a connection to the AbstractServerSockets port
67 : */
68 : virtual bool accept() = 0;
69 :
70 : /**
71 : * Binds this server socket to a given netif.
72 : */
73 : virtual bool bind(ip::IPAddress const& localIpAddress, uint16_t port) = 0;
74 :
75 : /**
76 : * closes the accepted port
77 : */
78 : virtual void close() = 0;
79 :
80 : virtual bool isClosed() const = 0;
81 :
82 : // [AbstractServerSocket]
83 :
84 5 : void setSocketProvidingConnectionListener(ISocketProvidingConnectionListener& listener)
85 : {
86 5 : _socketProvidingConnectionListener = &listener;
87 5 : }
88 :
89 8 : ISocketProvidingConnectionListener& getSocketProvidingConnectionListener() const
90 : {
91 8 : return *_socketProvidingConnectionListener;
92 : }
93 :
94 : protected:
95 : /** AbstractServerSockets port */
96 : uint16_t _port;
97 : /** reference to ISocketProvidingConnectionListener */
98 : ISocketProvidingConnectionListener* _socketProvidingConnectionListener;
99 : };
100 :
101 : } // namespace tcp
|