Skip to content
Snippets Groups Projects
Commit 2da02cc7 authored by Sergey Linev's avatar Sergey Linev Committed by Philippe Canal
Browse files

http: improve cleanup of websocket connection

parent 446f51ec
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ private: ...@@ -41,7 +41,7 @@ private:
Int_t PerformSend(std::shared_ptr<THttpWSEngine> engine); Int_t PerformSend(std::shared_ptr<THttpWSEngine> engine);
void RemoveEngine(std::shared_ptr<THttpWSEngine> &engine); void RemoveEngine(std::shared_ptr<THttpWSEngine> &engine, Bool_t terminate = kFALSE);
Int_t CompleteSend(std::shared_ptr<THttpWSEngine> &engine); Int_t CompleteSend(std::shared_ptr<THttpWSEngine> &engine);
......
...@@ -38,9 +38,19 @@ protected: ...@@ -38,9 +38,19 @@ protected:
public: public:
TCivetwebWSEngine(struct mg_connection *conn) : THttpWSEngine(), fWSconn(conn) {} TCivetwebWSEngine(struct mg_connection *conn) : THttpWSEngine(), fWSconn(conn) {}
virtual ~TCivetwebWSEngine()
{
TCivetwebWSEngine::ClearHandle(kTRUE);
}
virtual UInt_t GetId() const { return TString::Hash((void *)&fWSconn, sizeof(void *)); } virtual UInt_t GetId() const { return TString::Hash((void *)&fWSconn, sizeof(void *)); }
virtual void ClearHandle() { fWSconn = nullptr; } virtual void ClearHandle(Bool_t terminate) override
{
if (fWSconn && terminate)
mg_websocket_write(fWSconn, MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE, nullptr, 0);
fWSconn = nullptr;
}
virtual void Send(const void *buf, int len) virtual void Send(const void *buf, int len)
{ {
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
const std::string THttpLongPollEngine::gLongPollNope = "<<nope>>"; const std::string THttpLongPollEngine::gLongPollNope = "<<nope>>";
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/// constructor /// constructor
...@@ -36,6 +35,14 @@ THttpLongPollEngine::THttpLongPollEngine(bool raw) : THttpWSEngine(), fRaw(raw) ...@@ -36,6 +35,14 @@ THttpLongPollEngine::THttpLongPollEngine(bool raw) : THttpWSEngine(), fRaw(raw)
{ {
} }
//////////////////////////////////////////////////////////////////////////
/// destructor
THttpLongPollEngine::~THttpLongPollEngine()
{
// THttpLongPollEngine::ClearHandle(kTRUE);
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/// returns ID of the engine, created from this pointer /// returns ID of the engine, created from this pointer
...@@ -46,9 +53,9 @@ UInt_t THttpLongPollEngine::GetId() const ...@@ -46,9 +53,9 @@ UInt_t THttpLongPollEngine::GetId() const
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/// clear request, waiting for next portion of data /// clear request, normally called shortly before destructor
void THttpLongPollEngine::ClearHandle() void THttpLongPollEngine::ClearHandle(Bool_t)
{ {
std::shared_ptr<THttpCallArg> poll; std::shared_ptr<THttpCallArg> poll;
......
...@@ -36,10 +36,11 @@ protected: ...@@ -36,10 +36,11 @@ protected:
public: public:
THttpLongPollEngine(bool raw = false); THttpLongPollEngine(bool raw = false);
virtual ~THttpLongPollEngine();
virtual UInt_t GetId() const override; virtual UInt_t GetId() const override;
virtual void ClearHandle() override; virtual void ClearHandle(Bool_t) override;
virtual void Send(const void *buf, int len) override; virtual void Send(const void *buf, int len) override;
......
...@@ -27,7 +27,7 @@ private: ...@@ -27,7 +27,7 @@ private:
friend class THttpWSHandler; friend class THttpWSHandler;
bool fMTSend{false}; ///<! true when send operation runs, set under locked fMutex from WSHandler bool fMTSend{false}; ///<! true when send operation runs, set under locked fMutex from WSHandler
bool fDisabled{false}; ///<! set shortly before cleanup bool fDisabled{false}; ///<! true shortly before cleanup, set under locked fMutex from WSHandler
std::mutex fDataMutex; ///<! protects data submited for send operation std::mutex fDataMutex; ///<! protects data submited for send operation
enum { kNone, kData, kHeader, kText } fKind{kNone}; ///<! kind of operation enum { kNone, kData, kHeader, kText } fKind{kNone}; ///<! kind of operation
...@@ -47,12 +47,9 @@ protected: ...@@ -47,12 +47,9 @@ protected:
public: public:
virtual ~THttpWSEngine() {} virtual ~THttpWSEngine() {}
/// Returns kTRUE when handle is deactivated and need to be destroyed
Bool_t IsDisabled() const { return fDisabled; }
virtual UInt_t GetId() const = 0; virtual UInt_t GetId() const = 0;
virtual void ClearHandle() = 0; virtual void ClearHandle(Bool_t) = 0;
virtual void Send(const void *buf, int len) = 0; virtual void Send(const void *buf, int len) = 0;
......
...@@ -133,8 +133,10 @@ std::shared_ptr<THttpWSEngine> THttpWSHandler::FindEngine(UInt_t wsid, Bool_t bo ...@@ -133,8 +133,10 @@ std::shared_ptr<THttpWSEngine> THttpWSHandler::FindEngine(UInt_t wsid, Bool_t bo
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// Remove and destroy WS connection /// Remove and destroy WS connection
void THttpWSHandler::RemoveEngine(std::shared_ptr<THttpWSEngine> &engine) void THttpWSHandler::RemoveEngine(std::shared_ptr<THttpWSEngine> &engine, Bool_t terminate)
{ {
if (!engine) return;
{ {
std::lock_guard<std::mutex> grd(fMutex); std::lock_guard<std::mutex> grd(fMutex);
...@@ -149,7 +151,7 @@ void THttpWSHandler::RemoveEngine(std::shared_ptr<THttpWSEngine> &engine) ...@@ -149,7 +151,7 @@ void THttpWSHandler::RemoveEngine(std::shared_ptr<THttpWSEngine> &engine)
} }
} }
engine->ClearHandle(); engine->ClearHandle(terminate);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -178,7 +180,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg) ...@@ -178,7 +180,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg)
if (engine) { if (engine) {
Error("HandleWS", "WS engine with similar id exists %u", arg->GetWSId()); Error("HandleWS", "WS engine with similar id exists %u", arg->GetWSId());
RemoveEngine(engine); RemoveEngine(engine, kTRUE);
} }
engine = arg->TakeWSEngine(); engine = arg->TakeWSEngine();
...@@ -189,7 +191,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg) ...@@ -189,7 +191,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg)
if (!ProcessWS(arg.get())) { if (!ProcessWS(arg.get())) {
// if connection refused, remove engine again // if connection refused, remove engine again
RemoveEngine(engine); RemoveEngine(engine, kTRUE);
return kFALSE; return kFALSE;
} }
...@@ -199,10 +201,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg) ...@@ -199,10 +201,7 @@ Bool_t THttpWSHandler::HandleWS(std::shared_ptr<THttpCallArg> &arg)
if (arg->IsMethod("WS_CLOSE")) { if (arg->IsMethod("WS_CLOSE")) {
// connection is closed, one can remove handle // connection is closed, one can remove handle
if (engine) { RemoveEngine(engine);
engine->ClearHandle();
RemoveEngine(engine);
}
return ProcessWS(arg.get()); return ProcessWS(arg.get());
} }
...@@ -227,8 +226,7 @@ void THttpWSHandler::CloseWS(UInt_t wsid) ...@@ -227,8 +226,7 @@ void THttpWSHandler::CloseWS(UInt_t wsid)
{ {
auto engine = FindEngine(wsid); auto engine = FindEngine(wsid);
if (engine) RemoveEngine(engine, kTRUE);
RemoveEngine(engine);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -253,7 +251,7 @@ Int_t THttpWSHandler::RunSendingThrd(std::shared_ptr<THttpWSEngine> engine) ...@@ -253,7 +251,7 @@ Int_t THttpWSHandler::RunSendingThrd(std::shared_ptr<THttpWSEngine> engine)
Int_t sendcnt = fSendCnt, loopcnt(0); Int_t sendcnt = fSendCnt, loopcnt(0);
while (!IsDisabled() && !engine->IsDisabled()) { while (!IsDisabled() && !engine->fDisabled) {
gSystem->ProcessEvents(); gSystem->ProcessEvents();
// if send counter changed - current send operation is completed // if send counter changed - current send operation is completed
if (sendcnt != fSendCnt) if (sendcnt != fSendCnt)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment