Merge "Set proper locale to harfbuzz" into 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) 2015 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 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Adaptor
28 {
29
30 /**
31  * @brief Abstract socket interface.
32  * The typical usage is:
33  *
34  * @code
35  *
36  * SocketFactory::NewSocket( SocketInterface::TCP );
37  * socket->ReuseAddress( true );
38  * socket->Bind( port );
39  * socket->Listen();
40  *
41  * ret = socket->Select();     // call socket->ExitSelect to break from select from another thread
42  * if ( ret == DATA_AVAILABLE )
43  * {
44  *   socket->Read( ...);
45  * }
46  * socket->Close();
47  *
48  * @endcode
49  *
50  */
51 class SocketInterface
52 {
53 public:
54
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   /**
170    * @brief Constructor
171    */
172   SocketInterface( )
173   {
174   }
175
176   /**
177    * @brief virtual destructor
178    */
179   virtual ~SocketInterface()
180   {
181   }
182
183 private:
184
185   // Undefined copy constructor.
186   SocketInterface( const SocketInterface& );
187
188   // Undefined assignment operator.
189   SocketInterface& operator=( const SocketInterface& );
190
191 };
192
193
194 } // Adaptor
195 } // Internal
196 } // Dali
197
198 #endif // __DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H__