Skip to content

Commit

Permalink
Documentation for the updated connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed Nov 6, 2018
1 parent 67a289c commit 2f0c52a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Sming/SmingCore/Network/Http/HttpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#include "../Services/DateTime/DateTime.h"
#include "Data/ObjectQueue.h"

/** @defgroup HTTP client connection
* @brief Provides http client connection
* @ingroup http
* @{
*/

typedef ObjectQueue<HttpRequest, HTTP_REQUEST_POOL_SIZE> RequestQueue;

class HttpConnection : public HttpConnectionBase
Expand Down Expand Up @@ -140,4 +146,5 @@ class HttpConnection : public HttpConnectionBase
HttpResponse response;
};

/** @} */
#endif /* _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ */
11 changes: 11 additions & 0 deletions Sming/SmingCore/Network/Http/HttpConnectionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include "HttpResponse.h"
#include "HttpRequest.h"

/** @defgroup HTTP base connection
* @brief Provides http base used for client and server connections
* @ingroup http
* @{
*/

class HttpConnectionBase : public TcpClient
{
public:
Expand All @@ -36,6 +42,10 @@ class HttpConnectionBase : public TcpClient
protected:
void resetHeaders();

/**
* @brief Initializes the http parser for a specific type of HTTP message
* @param http_parser_type
*/
virtual void init(http_parser_type type);

// HTTP parser methods
Expand Down Expand Up @@ -139,4 +149,5 @@ class HttpConnectionBase : public TcpClient
HttpConnectionState state = eHCS_Ready;
};

/** @} */
#endif /* _SMING_CORE_NETWORK_HTTP_HTTPCONNECTIONBASE_H_ */
9 changes: 8 additions & 1 deletion Sming/SmingCore/Network/Http/HttpServerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#include <functional>

/** @defgroup HTTP server connection
* @brief Provides http server connection
* @ingroup http
* @{
*/

#ifndef HTTP_SERVER_EXPOSE_NAME
#define HTTP_SERVER_EXPOSE_NAME 1
#endif
Expand Down Expand Up @@ -46,7 +52,7 @@ class HttpServerConnection : public HttpConnectionBase

using TcpClient::send;

void setUpgdareCallback(HttpServerProtocolUpgradeCallback callback)
void setUpgradeCallback(HttpServerProtocolUpgradeCallback callback)
{
upgradeCallback = callback;
}
Expand Down Expand Up @@ -120,4 +126,5 @@ class HttpServerConnection : public HttpConnectionBase
HttpBodyParserDelegate bodyParser = 0;
};

/** @} */
#endif /* _SMING_CORE_NETWORK_HTTP_HTTPSERVERCONNECTION_H_ */
98 changes: 95 additions & 3 deletions Sming/SmingCore/Network/Http/Websocket/WebsocketConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ extern "C" {
#include "../ws_parser/ws_parser.h"
}

/** @defgroup Websocket connection
* @brief Provides websocket connection (server and client)
* @ingroup websocket http
* @{
*/

#define WEBSOCKET_VERSION 13 // 1.3

#define WSSTR_CONNECTION _F("connection")
Expand Down Expand Up @@ -46,24 +52,72 @@ typedef struct {
class WebsocketConnection
{
public:
/**
* @brief Constructs a websocket connection on top of http client or server connection
* @param HttpConnectionBase* connection the transport connection
* @param bool isClientConnection true when the passed connection is an http client conneciton
*/
WebsocketConnection(HttpConnectionBase* connection, bool isClientConnection = true);

virtual ~WebsocketConnection();

/**
* @brief Binds websocket connection to an http server connection
* @param HttpRequest& request
* @param HttpResponse& response
* @return true on success, false otherwise
*/
bool bind(HttpRequest& request, HttpResponse& response);

/**
* @brief Sends a websocket message
* @param const char* message
* @param int length
* @param ws_frame_type_t type
*/
virtual void send(const char* message, int length, ws_frame_type_t type = WS_FRAME_TEXT);

/**
* @brief Broadcasts a message to all active websocket connections
* @param const char* message
* @param int length
* @param ws_frame_type_t type
*/
static void broadcast(const char* message, int length, ws_frame_type_t type = WS_FRAME_TEXT);

/**
* @brief Sends a string websocket message
* @param const String& message
*/
void sendString(const String& message);
void sendBinary(const uint8_t* data, int size);

/**
* @brief Sends a binary websocket message
* @param const uint8_t* data
* @param int length
*/
void sendBinary(const uint8_t* data, int length);

/**
* @brief Closes a websocket connection (without closing the underlying http connection
*/
void close();

/**
* @brief Resets a websocket connection
*/
void reset();

/**
* @brief Attaches a user data to a websocket connection
* @param void* userData
*/
void setUserData(void* userData);

/**
* @brief Retrieves user data attached
* @return void*
*/
void* getUserData();

// @deprecated
Expand All @@ -72,27 +126,64 @@ class WebsocketConnection
WebsocketList& getActiveWebSockets();
// @end deprecated

/**
* @brief Sets the callback handler to be called after successful websocket connection
* @param WebsocketDelegate handler
*/
void setConnectionHandler(WebsocketDelegate handler);

/**
* @brief Sets the callback handler to be called after a websocket message is received
* @param WebsocketMessageDelegate handler
*/
void setMessageHandler(WebsocketMessageDelegate handler);

/**
* @brief Sets the callback handler to be called after a binary websocket message is received
* @param WebsocketBinaryDelegate handler
*/
void setBinaryHandler(WebsocketBinaryDelegate handler);

/**
* @brief Sets the callback handler to be called before closing a websocket connection
* @param WebsocketDelegate handler
*/
void setDisconnectionHandler(WebsocketDelegate handler);

/**
* @brief Should be called after a websocket connection is established to activate
* the websocket parser and allow sending of websocket data
*/
void activate();

/**
* @brief Call this method when the websocket connection was (re)activated.
* @return bool true on success
*/
bool onConnected();

/**
* @brief Gets the underlying HTTP connection
* @return HttpConnectionBase
*/
HttpConnectionBase* getConnection()
{
return connection;
}

/**
* @brief Sets the underlying (transport ) HTTP connection
* @param HttpConnectionBase* connection the transport connection
* @param bool isClientConnection true when the passed connection is an http client conneciton
*/
void setConnection(HttpConnectionBase* connection, bool isClientConnection = true)
{
this->connection = connection;
this->isClientConnection = isClientConnection;
}

/** @brief Get websocket client mode
* @retval Return websocket client mode
/** @brief Gets the state of the websocket connection
* @retval WsConnectionState
*/
WsConnectionState getState()
{
Expand Down Expand Up @@ -137,4 +228,5 @@ class WebsocketConnection
bool activated = false;
};

/** @} */
#endif /* SMINGCORE_NETWORK_WEBSOCKETCONNECTION_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int WebsocketResource::checkHeaders(HttpServerConnection& connection, HttpReques

connection.setTimeOut(USHRT_MAX); //Disable disconnection on connection idle (no rx/tx)

connection.setUpgdareCallback(std::bind(&WebsocketConnection::onConnected, socket));
connection.setUpgradeCallback(std::bind(&WebsocketConnection::onConnected, socket));

// TODO: Re-Enable Command Executor...

Expand Down

0 comments on commit 2f0c52a

Please sign in to comment.