1 #ifndef __DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H__
2 #define __DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 * @brief Abstract socket interface.
32 * The typical usage is:
36 * SocketFactory::NewSocket( SocketInterface::TCP );
37 * socket->ReuseAddress( true );
38 * socket->Bind( port );
41 * ret = socket->Select(); // call socket->ExitSelect to break from select from another thread
42 * if ( ret == DATA_AVAILABLE )
56 * @brief Protocol type
60 TCP, ///< Reliable, connection oriented
61 UDP, ///< Connection less, no guarantees of packet delivery, ordering
65 * @brief check is a socket is open
66 * @return true if the socket is currently open
68 virtual bool SocketIsOpen() const = 0;
72 * @return true on success, false on failure
74 virtual bool CloseSocket() = 0;
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
81 virtual bool Bind( uint16_t port ) = 0;
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
88 virtual bool Listen( int blacklog) = 0;
91 * @brief Wait for connection request and make connection
92 * @return new client socket
94 virtual SocketInterface* Accept() const = 0;
97 * @brief Select return values
101 DATA_AVAILABLE, ///< Data is available to read
102 QUIT, ///< ExitSelect() has been called on the socket
103 ERROR ///< Socket error
107 * @brief Waits for an event to occur (data available / error)
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
115 virtual SelectReturn Select() = 0;
118 * @brief To be called from a separate thread to break out of select
120 virtual void ExitSelect() = 0;
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
129 virtual bool Read( void* buffer, unsigned int bufferSizeInBytes, unsigned int& bytesRead ) = 0;
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
137 virtual bool Write( const void* buffer, unsigned int bufferSizeInBytes ) = 0;
140 // Common socket options. Please add more as required.
141 // These should be wrappers around the setsockopt API
144 * @brief Whether the SO_REUSEADDR is enabled or not.
145 * @param[in] reuse flag.
146 * @return true on success, false on failure
148 virtual bool ReuseAddress( bool reUse ) = 0;
151 * @brief Socket buffer type
155 SEND_BUFFER, ///< (SO_SNDBUF) Send buffer size
156 RECIEVE_BUFFER ///< (SO_RCVBUF) Size of buffer allocated to hold data arriving to the socket
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
165 virtual bool SetBufferSize( BufferType type, unsigned int size ) = 0;
177 * @brief virtual destructor
179 virtual ~SocketInterface()
185 // Undefined copy constructor.
186 SocketInterface( const SocketInterface& );
188 // Undefined assignment operator.
189 SocketInterface& operator=( const SocketInterface& );
198 #endif // __DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H__