| 1 |
#ifndef CLWEBSOCKETCLIENT_H |
| 2 |
#define CLWEBSOCKETCLIENT_H |
| 3 |
|
| 4 |
#include "clJoinableThread.h" |
| 5 |
#include "cl_command_event.h" |
| 6 |
#include "codelite_exports.h" |
| 7 |
#include <memory> |
| 8 |
#include <wx/event.h> |
| 9 |
|
| 10 |
class WXDLLIMPEXP_CL clWebSocketClient : public wxEvtHandler |
| 11 |
{ |
| 12 |
protected: |
| 13 |
void* m_client = 0; |
| 14 |
std::weak_ptr<void> m_connection_handle; |
| 15 |
wxEvtHandler* m_owner = nullptr; |
| 16 |
clJoinableThread* m_helperThread = nullptr; |
| 17 |
|
| 18 |
protected: |
| 19 |
template <typename T> T* GetClient() |
| 20 |
{ |
| 21 |
if(!m_client) { return nullptr; } |
| 22 |
return reinterpret_cast<T*>(m_client); |
| 23 |
} |
| 24 |
void DoCleanup(); |
| 25 |
void DoInit(); |
| 26 |
|
| 27 |
public: |
| 28 |
void OnHelperThreadExit(); |
| 29 |
|
| 30 |
public: |
| 31 |
clWebSocketClient(wxEvtHandler* owner); |
| 32 |
virtual ~clWebSocketClient(); |
| 33 |
|
| 34 |
/** |
| 35 |
* @brief prepare the socket before starting the main loop |
| 36 |
*/ |
| 37 |
void Initialise(); |
| 38 |
|
| 39 |
/** |
| 40 |
* @brief connect to a remote URL. This function start a background thread to executes the main loop |
| 41 |
*/ |
| 42 |
void StartLoop(const wxString& url); |
| 43 |
|
| 44 |
/** |
| 45 |
* @brief send data over the websocket |
| 46 |
*/ |
| 47 |
void Send(const wxString& data); |
| 48 |
|
| 49 |
/** |
| 50 |
* @brief return the owner of this socket. The owner will receive events about new data etc in the form of events |
| 51 |
*/ |
| 52 |
wxEvtHandler* GetOwner() { return m_owner; } |
| 53 |
void SetConnectionHandle(std::weak_ptr<void> connection_handle) { this->m_connection_handle = connection_handle; } |
| 54 |
std::weak_ptr<void> GetConnectionHandle() const { return m_connection_handle; } |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief close the connection |
| 58 |
*/ |
| 59 |
void Close(); |
| 60 |
|
| 61 |
/** |
| 62 |
* @brief are we connected? |
| 63 |
*/ |
| 64 |
bool IsConnected() const { return m_helperThread != nullptr; } |
| 65 |
}; |
| 66 |
|
| 67 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_WEBSOCKET_CONNECTED, clCommandEvent); |
| 68 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_WEBSOCKET_DISCONNECTED, clCommandEvent); |
| 69 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_WEBSOCKET_ONMESSAGE, clCommandEvent); |
| 70 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_WEBSOCKET_ERROR, clCommandEvent); |
| 71 |
|
| 72 |
#endif // CLWEBSOCKETCLIENT_H |