[dali_2.3.27] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / network / common / socket-interface.h
1 #ifndef DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H
2 #define DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <stdint.h>
22
23 #undef ERROR
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 /**
32  * @brief Abstract socket interface.
33  * The typical usage is:
34  *
35  * @code
36  *
37  * SocketFactory::NewSocket( SocketInterface::TCP );
38  * socket->ReuseAddress( true );
39  * socket->Bind( port );
40  * socket->Listen();
41  *
42  * ret = socket->Select();     // call socket->ExitSelect to break from select from another thread
43  * if ( ret == DATA_AVAILABLE )
44  * {
45  *   socket->Read( ...);
46  * }
47  * socket->Close();
48  *
49  * @endcode
50  *
51  */
52 class SocketInterface
53 {
54 public:
55   /**
56    * @brief Protocol type
57    */
58   enum Protocol
59   {
60     TCP, ///< Reliable, connection oriented
61     UDP, ///< Connection less, no guarantees of packet delivery, ordering
62   };
63
64   /**
65    * @brief check is a socket is open
66    * @return true if the socket is currently open
67    */
68   virtual bool SocketIsOpen() const = 0;
69
70   /**
71    * @brief CloseSocket
72    * @return true on success, false on failure
73    */
74   virtual bool CloseSocket() = 0;
75
76   /**
77    * @brief Socket bind,  associate a local address with a socket ( normally uses AF_INET + INADDR_ANY)
78    * @param[in] port network port
79    * @return true on success, false on failure
80    */
81   virtual bool Bind(uint16_t port) = 0;
82
83   /**
84    * @brief Indicate a willingness to accept incoming connection requests
85    * @param[in] backlog maximum length of the queue of pending connections.
86    * @return true on success, false on failure
87    */
88   virtual bool Listen(int blacklog) = 0;
89
90   /**
91    * @brief Wait for connection request and make connection
92    * @return new client socket
93    */
94   virtual SocketInterface* Accept() const = 0;
95
96   /**
97    * @brief Select return values
98    */
99   enum SelectReturn
100   {
101     DATA_AVAILABLE, ///< Data is available to read
102     QUIT,           ///< ExitSelect() has been called on the socket
103     ERROR           ///< Socket error
104   };
105
106   /**
107    * @brief Waits for an event to occur (data available / error)
108    * Returns when
109    * - data has been sent to the socket
110    * - client has closed the connection ( Read will return 0 bytes)
111    * - ExitSelect has been called (returns QUIT)
112    * - There is an error (returns ERROR)
113    * @return DATA_AVAILABLE if data is available
114    */
115   virtual SelectReturn Select() = 0;
116
117   /**
118    * @brief To be called from a separate thread to break out of select
119    */
120   virtual void ExitSelect() = 0;
121
122   /**
123    * @brief Read data from the socket
124    * @param[out] buffer data
125    * @param[in] bufferSizeInBytes buffer size in bytes
126    * @param[out] bytesRead number of bytes read
127    * @return true on success, false on failure
128    */
129   virtual bool Read(void* buffer, unsigned int bufferSizeInBytes, unsigned int& bytesRead) = 0;
130
131   /**
132    * @brief Send data to the socket
133    * @param[in] buffer data to write
134    * @param[in] bufferSizeInBytes buffer size in write
135    * @return true on success, false on failure
136    */
137   virtual bool Write(const void* buffer, unsigned int bufferSizeInBytes) = 0;
138
139   //
140   // Common socket options. Please add more as required.
141   // These should be wrappers around the setsockopt API
142
143   /**
144    * @brief Whether the SO_REUSEADDR is enabled or not.
145    * @param[in] reuse flag.
146    * @return true on success, false on failure
147    */
148   virtual bool ReuseAddress(bool reUse) = 0;
149
150   /**
151    * @brief Socket buffer type
152    */
153   enum BufferType
154   {
155     SEND_BUFFER,   ///< (SO_SNDBUF) Send buffer size
156     RECIEVE_BUFFER ///< (SO_RCVBUF) Size of buffer allocated to hold data arriving to the socket
157   };
158
159   /**
160    * @brief Set the send and recieve buffer sizes ( SO_SNDBUF, SO_RCVBUF )
161    * @param[in] type buffer type
162    * @param[in] size buffer size
163    * @return true on success, false on failure
164    */
165   virtual bool SetBufferSize(BufferType type, unsigned int size) = 0;
166
167 protected:
168   /**
169    * @brief Constructor
170    */
171   SocketInterface()
172   {
173   }
174
175   /**
176    * @brief virtual destructor
177    */
178   virtual ~SocketInterface()
179   {
180   }
181
182 private:
183   // Undefined copy constructor.
184   SocketInterface(const SocketInterface&);
185
186   // Undefined assignment operator.
187   SocketInterface& operator=(const SocketInterface&);
188 };
189
190 } // namespace Adaptor
191 } // namespace Internal
192 } // namespace Dali
193
194 #endif // DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H