Enable atspi
[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) 2019 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 /**
33  * @brief Abstract socket interface.
34  * The typical usage is:
35  *
36  * @code
37  *
38  * SocketFactory::NewSocket( SocketInterface::TCP );
39  * socket->ReuseAddress( true );
40  * socket->Bind( port );
41  * socket->Listen();
42  *
43  * ret = socket->Select();     // call socket->ExitSelect to break from select from another thread
44  * if ( ret == DATA_AVAILABLE )
45  * {
46  *   socket->Read( ...);
47  * }
48  * socket->Close();
49  *
50  * @endcode
51  *
52  */
53 class SocketInterface
54 {
55 public:
56
57   /**
58    * @brief Protocol type
59    */
60   enum Protocol
61   {
62     TCP,          ///< Reliable, connection oriented
63     UDP,          ///< Connection less, no guarantees of packet delivery, ordering
64   };
65
66   /**
67    * @brief check is a socket is open
68    * @return true if the socket is currently open
69    */
70   virtual bool SocketIsOpen() const = 0;
71
72   /**
73    * @brief CloseSocket
74    * @return true on success, false on failure
75    */
76   virtual bool CloseSocket() = 0;
77
78   /**
79    * @brief Socket bind,  associate a local address with a socket ( normally uses AF_INET + INADDR_ANY)
80    * @param[in] port network port
81    * @return true on success, false on failure
82    */
83   virtual bool Bind( uint16_t port ) = 0;
84
85   /**
86    * @brief Indicate a willingness to accept incoming connection requests
87    * @param[in] backlog maximum length of the queue of pending connections.
88    * @return true on success, false on failure
89    */
90   virtual bool Listen( int blacklog) = 0;
91
92   /**
93    * @brief Wait for connection request and make connection
94    * @return new client socket
95    */
96   virtual SocketInterface* Accept() const = 0;
97
98   /**
99    * @brief Select return values
100    */
101   enum SelectReturn
102   {
103     DATA_AVAILABLE,   ///< Data is available to read
104     QUIT,             ///< ExitSelect() has been called on the socket
105     ERROR             ///< Socket error
106   };
107
108   /**
109    * @brief Waits for an event to occur (data available / error)
110    * Returns when
111    * - data has been sent to the socket
112    * - client has closed the connection ( Read will return 0 bytes)
113    * - ExitSelect has been called (returns QUIT)
114    * - There is an error (returns ERROR)
115    * @return DATA_AVAILABLE if data is available
116    */
117   virtual SelectReturn Select() = 0;
118
119   /**
120    * @brief To be called from a separate thread to break out of select
121    */
122   virtual void ExitSelect() = 0;
123
124   /**
125    * @brief Read data from the socket
126    * @param[out] buffer data
127    * @param[in] bufferSizeInBytes buffer size in bytes
128    * @param[out] bytesRead number of bytes read
129    * @return true on success, false on failure
130    */
131   virtual bool Read( void* buffer, unsigned int bufferSizeInBytes, unsigned int& bytesRead  ) = 0;
132
133   /**
134    * @brief Send data to the socket
135    * @param[in] buffer data to write
136    * @param[in] bufferSizeInBytes buffer size in write
137    * @return true on success, false on failure
138    */
139   virtual bool Write( const void* buffer, unsigned int bufferSizeInBytes ) = 0;
140
141   //
142   // Common socket options. Please add more as required.
143   // These should be wrappers around the setsockopt API
144
145   /**
146    * @brief Whether the SO_REUSEADDR is enabled or not.
147    * @param[in] reuse flag.
148    * @return true on success, false on failure
149    */
150   virtual bool ReuseAddress( bool reUse ) = 0;
151
152   /**
153    * @brief Socket buffer type
154    */
155   enum BufferType
156   {
157     SEND_BUFFER,        ///< (SO_SNDBUF) Send buffer size
158     RECIEVE_BUFFER      ///< (SO_RCVBUF) Size of buffer allocated to hold data arriving to the socket
159   };
160
161   /**
162    * @brief Set the send and recieve buffer sizes ( SO_SNDBUF, SO_RCVBUF )
163    * @param[in] type buffer type
164    * @param[in] size buffer size
165    * @return true on success, false on failure
166    */
167   virtual bool SetBufferSize( BufferType type, unsigned int size ) = 0;
168
169 protected:
170
171   /**
172    * @brief Constructor
173    */
174   SocketInterface( )
175   {
176   }
177
178   /**
179    * @brief virtual destructor
180    */
181   virtual ~SocketInterface()
182   {
183   }
184
185 private:
186
187   // Undefined copy constructor.
188   SocketInterface( const SocketInterface& );
189
190   // Undefined assignment operator.
191   SocketInterface& operator=( const SocketInterface& );
192
193 };
194
195
196 } // Adaptor
197 } // Internal
198 } // Dali
199
200 #endif // DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H