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