PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Socket.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_NETWORK_SOCKET_H__ 00024 #define __PLCORE_NETWORK_SOCKET_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/Network/SocketAddress.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Socket class 00046 * 00047 * @remarks 00048 * A socket is an object used to send and receive data. 00049 */ 00050 class Socket { 00051 00052 00053 //[-------------------------------------------------------] 00054 //[ Public functions ] 00055 //[-------------------------------------------------------] 00056 public: 00057 //[-------------------------------------------------------] 00058 //[ Construction/Destruction ] 00059 //[-------------------------------------------------------] 00060 /** 00061 * @brief 00062 * Constructor 00063 */ 00064 PLCORE_API Socket(); 00065 00066 /** 00067 * @brief 00068 * Copy constructor 00069 * 00070 * @param[in] cSocket 00071 * Socket to copy from 00072 */ 00073 inline Socket(const Socket &cSocket); 00074 00075 /** 00076 * @brief 00077 * Constructor 00078 * 00079 * @param[in] nSocket 00080 * System socket handle 00081 * 00082 * @note 00083 * - Should be used mainly to create an invalid socket ('INVALID_SOCKET') 00084 */ 00085 inline Socket(handle nSocket); 00086 00087 /** 00088 * @brief 00089 * Destructor 00090 */ 00091 inline ~Socket(); 00092 00093 //[-------------------------------------------------------] 00094 //[ Connect ] 00095 //[-------------------------------------------------------] 00096 /** 00097 * @brief 00098 * Returns whether the socket is currently valid 00099 * 00100 * @return 00101 * 'true' if the socket is currently valid, else 'false' 00102 */ 00103 PLCORE_API bool IsValid() const; 00104 00105 /** 00106 * @brief 00107 * Closes the socket 00108 * 00109 * @return 00110 * 'true' if all went fine, else 'false' (maybe it's already closed?) 00111 */ 00112 PLCORE_API bool Close(); 00113 00114 /** 00115 * @brief 00116 * Returns the socket address object 00117 * 00118 * @return 00119 * The socket address object 00120 */ 00121 inline const SocketAddress &GetSocketAddress() const; 00122 00123 /** 00124 * @brief 00125 * Establishes a connection to a host by using an address and port 00126 * 00127 * @param[in] sAddress 00128 * Address of the host to connect to 00129 * @param[in] nPort 00130 * Number of the port to use 00131 * 00132 * @return 00133 * 'true' if all went fine, else 'false' 00134 */ 00135 inline bool Connect(const String &sAddress, uint32 nPort); 00136 00137 /** 00138 * @brief 00139 * Establishes a connection to a host by using a given socked address 00140 * 00141 * @param[in] cSocketAddress 00142 * Socket address of the host to connect to 00143 * 00144 * @return 00145 * 'true' if all went fine, else 'false' 00146 */ 00147 PLCORE_API bool Connect(const SocketAddress &cSocketAddress); 00148 00149 //[-------------------------------------------------------] 00150 //[ Bind address ] 00151 //[-------------------------------------------------------] 00152 /** 00153 * @brief 00154 * Associate local address with socket 00155 * 00156 * @param[in] nPort 00157 * Number of the port to use 00158 * 00159 * @return 00160 * 'true' if all went fine, else 'false' 00161 */ 00162 inline bool Bind(uint32 nPort); 00163 00164 /** 00165 * @brief 00166 * Associate local address with socket 00167 * 00168 * @param[in] sAddress 00169 * Address of the host to bind to 00170 * @param[in] nPort 00171 * Number of the port to use 00172 * 00173 * @return 00174 * 'true' if all went fine, else 'false' 00175 */ 00176 inline bool Bind(const String &sAddress, uint32 nPort); 00177 00178 /** 00179 * @brief 00180 * Associate local address with socket 00181 * 00182 * @param[in] cSocketAddress 00183 * Socket address of the host to bind to 00184 * 00185 * @return 00186 * 'true' if all went fine, else 'false' 00187 */ 00188 PLCORE_API bool Bind(const SocketAddress &cSocketAddress); 00189 00190 //[-------------------------------------------------------] 00191 //[ Listen for new connections ] 00192 //[-------------------------------------------------------] 00193 /** 00194 * @brief 00195 * Mark a socket as accepting connections 00196 * 00197 * @param[in] nMaxQueue 00198 * Maximum length of the queue of pending connections 00199 * 00200 * @return 00201 * 'true' if all went fine, else 'false' 00202 */ 00203 PLCORE_API bool Listen(int nMaxQueue = 0) const; 00204 00205 /** 00206 * @brief 00207 * Accept a connection on a socket (blocking) 00208 * 00209 * @return 00210 * Socket with the accepted connection or an invalid socket on error 00211 */ 00212 PLCORE_API Socket Accept() const; 00213 00214 //[-------------------------------------------------------] 00215 //[ Data transfer ] 00216 //[-------------------------------------------------------] 00217 /** 00218 * @brief 00219 * Sends data 00220 * 00221 * @param[in] pBuffer 00222 * Data to send, if a null pointer, nothing is send 00223 * @param[in] nSize 00224 * Size in bytes of the buffer to send, MUST be valid! 00225 * 00226 * @return 00227 * Total number of bytes sent which can be less than the number requested 00228 * to be sent, negative value on error 00229 */ 00230 PLCORE_API int Send(const char *pBuffer, uint32 nSize) const; 00231 00232 /** 00233 * @brief 00234 * Returns whether or not data is waiting to be received (non-blocking request) 00235 * 00236 * @return 00237 * 'true' if data is waiting to be received, else 'false' 00238 */ 00239 PLCORE_API bool IsDataWaiting() const; 00240 00241 /** 00242 * @brief 00243 * Receives data (blocking request) 00244 * 00245 * @param[out] pBuffer 00246 * Buffer that receives the data, if a null pointer, nothing can be received 00247 * @param[in] nSize 00248 * Size in bytes of the buffer that receives the data, MUST be valid! 00249 * 00250 * @return 00251 * Total number of received bytes, negative value on error 00252 * 00253 * @note 00254 * - If there is currently not enough data available, this function will read as much 00255 * as possible, meaning that less data can be read than requested 00256 * - If more data is waiting to be received as the given buffer is able to store, 00257 * you have to call this method multiple times in order to gather all waiting data 00258 */ 00259 PLCORE_API int Receive(char *pBuffer, uint32 nSize) const; 00260 00261 00262 //[-------------------------------------------------------] 00263 //[ Private data ] 00264 //[-------------------------------------------------------] 00265 private: 00266 SocketAddress m_cSocketAddress; /**< Socket address */ 00267 handle m_nSocket; /**< Socket handle */ 00268 00269 00270 }; 00271 00272 00273 //[-------------------------------------------------------] 00274 //[ Namespace ] 00275 //[-------------------------------------------------------] 00276 } // PLCore 00277 00278 00279 //[-------------------------------------------------------] 00280 //[ Implementation ] 00281 //[-------------------------------------------------------] 00282 #include "PLCore/Network/Socket.inl" 00283 00284 00285 #endif // __PLCORE_NETWORK_SOCKET_H__
|