| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// Copyright : (C) 2015 Eran Ifrah |
| 5 |
// File name : clAsyncSocket.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef CLSOCKETCLIENTASYNC_H |
| 27 |
#define CLSOCKETCLIENTASYNC_H |
| 28 |
|
| 29 |
#include "SocketAPI/clSocketBase.h" |
| 30 |
#include "SocketAPI/clSocketClient.h" |
| 31 |
#include "cl_command_event.h" |
| 32 |
#include "codelite_exports.h" |
| 33 |
#include "worker_thread.h" |
| 34 |
#include <wx/event.h> |
| 35 |
#include <wx/msgqueue.h> |
| 36 |
#include <wx/sharedptr.h> |
| 37 |
|
| 38 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_CONNECTED, clCommandEvent); |
| 39 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_CONNECTION_LOST, clCommandEvent); |
| 40 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_INPUT, clCommandEvent); |
| 41 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_ERROR, clCommandEvent); |
| 42 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_CONNECT_ERROR, clCommandEvent); |
| 43 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_ASYNC_SOCKET_SERVER_READY, clCommandEvent); |
| 44 |
|
| 45 |
enum eAsyncSocketMode { |
| 46 |
kAsyncSocketClient = (1 << 0), |
| 47 |
kAsyncSocketServer = (1 << 1), |
| 48 |
kAsyncSocketMessage = (1 << 2), |
| 49 |
kAsyncSocketBuffer = (1 << 3), |
| 50 |
kAsyncSocketNonBlocking = (1 << 4), |
| 51 |
}; |
| 52 |
|
| 53 |
class WXDLLIMPEXP_CL clSocketAsyncThread : public wxThread |
| 54 |
{ |
| 55 |
public: |
| 56 |
enum eCommand { |
| 57 |
kSend, |
| 58 |
kDisconnect, |
| 59 |
}; |
| 60 |
|
| 61 |
struct MyRequest { |
| 62 |
eCommand m_command; |
| 63 |
std::string m_buffer; |
| 64 |
}; |
| 65 |
|
| 66 |
protected: |
| 67 |
wxEvtHandler* m_sink; |
| 68 |
wxString m_keepAliveMessage; |
| 69 |
wxString m_connectionString; |
| 70 |
wxMessageQueue<MyRequest> m_queue; |
| 71 |
size_t m_mode = (kAsyncSocketClient | kAsyncSocketBuffer); |
| 72 |
|
| 73 |
protected: |
| 74 |
void MessageLoop(clSocketBase::Ptr_t socket); |
| 75 |
void BufferLoop(clSocketBase::Ptr_t socket); |
| 76 |
|
| 77 |
void* ClientMain(); |
| 78 |
void* ServerMain(); |
| 79 |
|
| 80 |
public: |
| 81 |
virtual void AddRequest(const MyRequest& req) { m_queue.Post(req); } |
| 82 |
virtual void* Entry(); |
| 83 |
|
| 84 |
/** |
| 85 |
* @brief start the worker thread |
| 86 |
*/ |
| 87 |
void Start() |
| 88 |
{ |
| 89 |
Create(); |
| 90 |
Run(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @brief stop and wait for the thread to terminate |
| 95 |
*/ |
| 96 |
void Stop() |
| 97 |
{ |
| 98 |
if(IsAlive()) { |
| 99 |
Delete(NULL, wxTHREAD_WAIT_BLOCK); |
| 100 |
|
| 101 |
} else { |
| 102 |
Wait(wxTHREAD_WAIT_BLOCK); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
clSocketAsyncThread(wxEvtHandler* sink, const wxString& connectionString, size_t mode, |
| 107 |
const wxString& keepAliveMessage = ""); |
| 108 |
virtual ~clSocketAsyncThread(); |
| 109 |
}; |
| 110 |
|
| 111 |
class WXDLLIMPEXP_CL clAsyncSocket : public wxEvtHandler |
| 112 |
{ |
| 113 |
clSocketAsyncThread* m_thread; |
| 114 |
size_t m_mode = kAsyncSocketClient | kAsyncSocketBuffer; |
| 115 |
wxString m_connectionString; |
| 116 |
|
| 117 |
public: |
| 118 |
typedef wxSharedPtr<clAsyncSocket> Ptr_t; |
| 119 |
|
| 120 |
public: |
| 121 |
clAsyncSocket(const wxString& connectionString, |
| 122 |
size_t mode = (kAsyncSocketClient | kAsyncSocketBuffer)); |
| 123 |
virtual ~clAsyncSocket(); |
| 124 |
|
| 125 |
/** |
| 126 |
* @brief connect using connection string |
| 127 |
*/ |
| 128 |
void Start(); |
| 129 |
void Stop(); |
| 130 |
void Send(const wxString& buffer); |
| 131 |
void Send(const std::string& buffer); |
| 132 |
}; |
| 133 |
|
| 134 |
#endif // CLSOCKETCLIENTASYNC_H |