1 #ifndef DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H
2 #define DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H
5 * Copyright (c) 2019 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.
33 * @brief Abstract socket interface.
34 * The typical usage is:
38 * SocketFactory::NewSocket( SocketInterface::TCP );
39 * socket->ReuseAddress( true );
40 * socket->Bind( port );
43 * ret = socket->Select(); // call socket->ExitSelect to break from select from another thread
44 * if ( ret == DATA_AVAILABLE )
58 * @brief Protocol type
62 TCP, ///< Reliable, connection oriented
63 UDP, ///< Connection less, no guarantees of packet delivery, ordering
67 * @brief check is a socket is open
68 * @return true if the socket is currently open
70 virtual bool SocketIsOpen() const = 0;
74 * @return true on success, false on failure
76 virtual bool CloseSocket() = 0;
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
83 virtual bool Bind( uint16_t port ) = 0;
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
90 virtual bool Listen( int blacklog) = 0;
93 * @brief Wait for connection request and make connection
94 * @return new client socket
96 virtual SocketInterface* Accept() const = 0;
99 * @brief Select return values
103 DATA_AVAILABLE, ///< Data is available to read
104 QUIT, ///< ExitSelect() has been called on the socket
105 ERROR ///< Socket error
109 * @brief Waits for an event to occur (data available / error)
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
117 virtual SelectReturn Select() = 0;
120 * @brief To be called from a separate thread to break out of select
122 virtual void ExitSelect() = 0;
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
131 virtual bool Read( void* buffer, unsigned int bufferSizeInBytes, unsigned int& bytesRead ) = 0;
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
139 virtual bool Write( const void* buffer, unsigned int bufferSizeInBytes ) = 0;
142 // Common socket options. Please add more as required.
143 // These should be wrappers around the setsockopt API
146 * @brief Whether the SO_REUSEADDR is enabled or not.
147 * @param[in] reuse flag.
148 * @return true on success, false on failure
150 virtual bool ReuseAddress( bool reUse ) = 0;
153 * @brief Socket buffer type
157 SEND_BUFFER, ///< (SO_SNDBUF) Send buffer size
158 RECIEVE_BUFFER ///< (SO_RCVBUF) Size of buffer allocated to hold data arriving to the socket
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
167 virtual bool SetBufferSize( BufferType type, unsigned int size ) = 0;
179 * @brief virtual destructor
181 virtual ~SocketInterface()
187 // Undefined copy constructor.
188 SocketInterface( const SocketInterface& );
190 // Undefined assignment operator.
191 SocketInterface& operator=( const SocketInterface& );
200 #endif // DALI_INTERNAL_ADAPTOR_BASE_SOCKET_INTERFACE_H