2013-05-31 23:13:03 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2013-06-06 14:16:57 +08:00
|
|
|
|
|
|
|
"[WebSocket module] is based in part on the work of the libwebsockets project
|
|
|
|
(http://libwebsockets.org)"
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-03-07 13:35:59 +08:00
|
|
|
#pragma once
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-20 15:03:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-01-22 18:10:24 +08:00
|
|
|
#include <mutex>
|
|
|
|
#include <memory> // for std::shared_ptr
|
2016-04-07 15:06:19 +08:00
|
|
|
#include <atomic>
|
2017-01-19 13:55:14 +08:00
|
|
|
#include <condition_variable>
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCStdC.h"
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
struct lws;
|
|
|
|
struct lws_protocols;
|
2017-02-13 15:15:23 +08:00
|
|
|
struct lws_vhost;
|
2015-03-26 22:07:44 +08:00
|
|
|
/**
|
2015-05-28 15:46:36 +08:00
|
|
|
* @addtogroup network
|
2015-03-26 22:07:44 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2016-02-29 18:55:51 +08:00
|
|
|
class EventListenerCustom;
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
namespace network {
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
class WsThreadHelper;
|
|
|
|
|
2015-03-27 15:27:08 +08:00
|
|
|
/**
|
|
|
|
* WebSocket is wrapper of the libwebsockets-protocol, let the develop could call the websocket easily.
|
2016-01-22 15:20:43 +08:00
|
|
|
* Please note that all public methods of WebSocket have to be invoked on Cocos Thread.
|
2015-03-27 15:27:08 +08:00
|
|
|
*/
|
2014-08-22 13:42:46 +08:00
|
|
|
class CC_DLL WebSocket
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-01-09 18:07:48 +08:00
|
|
|
/**
|
|
|
|
* Close all connections and wait for all websocket threads to exit
|
|
|
|
* @note This method has to be invoked on Cocos Thread
|
|
|
|
*/
|
|
|
|
static void closeAllConnections();
|
|
|
|
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
2016-01-11 18:15:34 +08:00
|
|
|
* Constructor of WebSocket.
|
2015-03-19 16:45:53 +08:00
|
|
|
*
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
WebSocket();
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Destructor of WebSocket.
|
|
|
|
*
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual ~WebSocket();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Data structure for message
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
struct Data
|
|
|
|
{
|
2016-01-07 23:15:11 +08:00
|
|
|
Data():bytes(nullptr), len(0), issued(0), isBinary(false), ext(nullptr){}
|
2013-05-31 23:13:03 +08:00
|
|
|
char* bytes;
|
2014-01-09 13:29:56 +08:00
|
|
|
ssize_t len, issued;
|
2013-05-31 23:13:03 +08:00
|
|
|
bool isBinary;
|
2016-01-07 23:15:11 +08:00
|
|
|
void* ext;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* ErrorCode enum used to represent the error in the websocket.
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
2013-07-26 12:08:18 +08:00
|
|
|
enum class ErrorCode
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2015-03-19 16:45:53 +08:00
|
|
|
TIME_OUT, /** < value 0 */
|
|
|
|
CONNECTION_FAILURE, /** < value 1 */
|
|
|
|
UNKNOWN, /** < value 2 */
|
2013-07-26 12:08:18 +08:00
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* State enum used to represent the Websocket state.
|
2013-07-26 12:08:18 +08:00
|
|
|
*/
|
|
|
|
enum class State
|
|
|
|
{
|
2015-03-19 16:45:53 +08:00
|
|
|
CONNECTING, /** < value 0 */
|
|
|
|
OPEN, /** < value 1 */
|
|
|
|
CLOSING, /** < value 2 */
|
|
|
|
CLOSED, /** < value 3 */
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* The delegate class is used to process websocket events.
|
|
|
|
*
|
|
|
|
* The most member function are pure virtual functions,they should be implemented the in subclass.
|
|
|
|
* @lua NA
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
class Delegate
|
|
|
|
{
|
|
|
|
public:
|
2015-03-19 16:45:53 +08:00
|
|
|
/** Destructor of Delegate. */
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual ~Delegate() {}
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* This function to be called after the client connection complete a handshake with the remote server.
|
|
|
|
* This means that the WebSocket connection is ready to send and receive data.
|
|
|
|
*
|
|
|
|
* @param ws The WebSocket object connected
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual void onOpen(WebSocket* ws) = 0;
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* This function to be called when data has appeared from the server for the client connection.
|
|
|
|
*
|
|
|
|
* @param ws The WebSocket object connected.
|
|
|
|
* @param data Data object for message.
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual void onMessage(WebSocket* ws, const Data& data) = 0;
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* When the WebSocket object connected wants to close or the protocol won't get used at all and current _readyState is State::CLOSING,this function is to be called.
|
|
|
|
*
|
|
|
|
* @param ws The WebSocket object connected.
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual void onClose(WebSocket* ws) = 0;
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* This function is to be called in the following cases:
|
|
|
|
* 1. client connection is failed.
|
|
|
|
* 2. the request client connection has been unable to complete a handshake with the remote server.
|
|
|
|
* 3. the protocol won't get used at all after this callback and current _readyState is State::CONNECTING.
|
|
|
|
* 4. when a socket descriptor needs to be removed from an external polling array. in is again the struct libwebsocket_pollargs containing the fd member to be removed. If you are using the internal polling loop, you can just ignore it and current _readyState is State::CONNECTING.
|
|
|
|
*
|
|
|
|
* @param ws The WebSocket object connected.
|
|
|
|
* @param error WebSocket::ErrorCode enum,would be ErrorCode::TIME_OUT or ErrorCode::CONNECTION_FAILURE.
|
|
|
|
*/
|
2013-06-03 09:55:43 +08:00
|
|
|
virtual void onError(WebSocket* ws, const ErrorCode& error) = 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
2017-01-19 13:55:14 +08:00
|
|
|
* @brief The initialized method for websocket.
|
|
|
|
* It needs to be invoked right after websocket instance is allocated.
|
|
|
|
* @param delegate The delegate which want to receive event from websocket.
|
|
|
|
* @param url The URL of websocket server.
|
|
|
|
* @param protocols The websocket protocols that agree with websocket server
|
|
|
|
* @param caFilePath The ca file path for wss connection
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return true: Success, false: Failure.
|
|
|
|
* @lua NA
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
bool init(const Delegate& delegate,
|
|
|
|
const std::string& url,
|
2017-01-19 13:55:14 +08:00
|
|
|
const std::vector<std::string>* protocols = nullptr,
|
|
|
|
const std::string& caFilePath = "");
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends string data to websocket server.
|
2015-03-19 16:45:53 +08:00
|
|
|
*
|
|
|
|
* @param message string data.
|
|
|
|
* @lua sendstring
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
void send(const std::string& message);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends binary data to websocket server.
|
2015-03-19 16:45:53 +08:00
|
|
|
*
|
|
|
|
* @param binaryMsg binary string data.
|
|
|
|
* @param len the size of binary string data.
|
|
|
|
* @lua sendstring
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
void send(const unsigned char* binaryMsg, unsigned int len);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
2016-01-22 14:22:33 +08:00
|
|
|
* @brief Closes the connection to server synchronously.
|
|
|
|
* @note It's a synchronous method, it will not return until websocket thread exits.
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
|
|
|
void close();
|
2016-01-22 14:22:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Closes the connection to server asynchronously.
|
|
|
|
* @note It's an asynchronous method, it just notifies websocket thread to exit and returns directly,
|
|
|
|
* If using 'closeAsync' to close websocket connection,
|
2016-05-05 07:05:20 +08:00
|
|
|
* be careful of not using destructed variables in the callback of 'onClose'.
|
2016-01-22 14:22:33 +08:00
|
|
|
*/
|
|
|
|
void closeAsync();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Gets current state of connection.
|
2015-10-23 15:37:33 +08:00
|
|
|
* @return State the state value could be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED
|
2013-05-31 23:13:03 +08:00
|
|
|
*/
|
2013-06-03 09:55:43 +08:00
|
|
|
State getReadyState();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
/**
|
|
|
|
* @brief Gets the URL of websocket connection.
|
|
|
|
*/
|
|
|
|
inline const std::string& getUrl() const { return _url; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets the protocol selected by websocket server.
|
|
|
|
*/
|
|
|
|
inline const std::string& getProtocol() const { return _selectedProtocol; }
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
private:
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
// The following callback functions are invoked in websocket thread
|
2017-01-19 13:55:14 +08:00
|
|
|
void onClientOpenConnectionRequest();
|
|
|
|
int onSocketCallback(struct lws *wsi, int reason, void *in, ssize_t len);
|
|
|
|
|
|
|
|
int onClientWritable();
|
|
|
|
int onClientReceivedData(void* in, ssize_t len);
|
|
|
|
int onConnectionOpened();
|
|
|
|
int onConnectionError();
|
|
|
|
int onConnectionClosed();
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
struct lws_vhost* createVhost(struct lws_protocols* protocols, int& sslConnection);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
private:
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
std::mutex _readyStateMutex;
|
2014-03-24 17:58:35 +08:00
|
|
|
State _readyState;
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
std::string _url;
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
std::vector<char> _receivedData;
|
2014-01-09 13:29:56 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
struct lws* _wsInstance;
|
2017-02-20 09:44:20 +08:00
|
|
|
struct lws_protocols* _lwsProtocols;
|
|
|
|
std::string _clientSupportedProtocols;
|
|
|
|
std::string _selectedProtocol;
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> _isDestroyed;
|
2013-05-31 23:13:03 +08:00
|
|
|
Delegate* _delegate;
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
std::mutex _closeMutex;
|
|
|
|
std::condition_variable _closeCondition;
|
|
|
|
|
|
|
|
enum class CloseState
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
SYNC_CLOSING,
|
|
|
|
SYNC_CLOSED,
|
|
|
|
ASYNC_CLOSING
|
|
|
|
};
|
|
|
|
CloseState _closeState;
|
|
|
|
|
|
|
|
std::string _caFilePath;
|
|
|
|
|
2016-02-29 18:55:51 +08:00
|
|
|
EventListenerCustom* _resetDirectorListener;
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
friend class WsThreadHelper;
|
|
|
|
friend class WebSocketCallbackWrapper;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
|
|
|
|
2017-03-07 13:35:59 +08:00
|
|
|
} // namespace network {
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|