[DEV] add the capability to the HTTP to reconnect on an other client socket (not availlable for server mode)

This commit is contained in:
Edouard DUPIN 2017-10-14 10:48:31 +02:00
parent e6f2d8041b
commit 6ec43d60ed
2 changed files with 31 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include <etk/Map.hpp>
#include <etk/Stream.hpp>
#include <etk/stdTools.hpp>
#include <enet/TcpClient.hpp>
extern "C" {
#include <string.h>
}
@ -126,7 +127,9 @@ void enet::Http::threadCallback() {
// READ section data:
if (m_headerIsSend == false) {
getHeader();
m_headerIsSend = true;
if (m_headerIsSend == false) {
continue;
}
}
if (m_observerRaw != nullptr) {
m_observerRaw(m_connection);
@ -145,6 +148,15 @@ void enet::Http::threadCallback() {
ENET_DEBUG("End of thread HTTP");
}
void enet::Http::redirectTo(const etk::String& _addressRedirect, bool _inThreadStop) {
if (m_isServer == true) {
ENET_ERROR("Request a redirect in Server mode ==> not authorised");
return;
}
stop(_inThreadStop);
m_headerIsSend = false;
m_connection = etk::move(connectTcpClient(_addressRedirect));
}
void enet::Http::start() {
ENET_DEBUG("connect [START]");
@ -508,6 +520,7 @@ void enet::Http::getHeader() {
m_connection.unlink();
}
}
m_headerIsSend = true;
if (m_isServer == false) {
if (m_observerAnswer != nullptr) {
m_observerAnswer(m_answerHeader);

View File

@ -219,6 +219,14 @@ namespace enet {
ethread::Thread* m_thread;
bool m_threadRunning;
etk::Vector<uint8_t> m_temporaryBuffer;
public:
/**
* @brief Get the adress of the connection source IP:port
* @return string with the remote address name.
*/
const etk::String& getRemoteAddress() const {
return m_connection.getRemoteName();
}
private:
void threadCallback();
private:
@ -229,6 +237,13 @@ namespace enet {
bool isAlive() const {
return m_connection.getConnectionStatus() == enet::Tcp::status::link;
}
public:
/**
* @brief Redirect the current HTTP request to an other network address.
* @param[in] _addressRedirect new redirection address:port
* @param[in] _inThreadStop the http thread request an auto-stop.
*/
void redirectTo(const etk::String& _addressRedirect, bool _inThreadStop=false);
public:
using Observer = etk::Function<void(etk::Vector<uint8_t>&)>; //!< Define an Observer: function pointer
Observer m_observer;
@ -323,6 +338,7 @@ namespace enet {
HttpClient(enet::Tcp _connection);
public:
void setHeader(const enet::HttpRequest& _header) {
_header.display();
setRequestHeader(_header);
}
public:
@ -352,6 +368,7 @@ namespace enet {
HttpServer(enet::Tcp _connection);
public:
void setHeader(const enet::HttpAnswer& _header) {
_header.display();
setAnswerHeader(_header);
}
public: