/////////////////////////////////////////////////////////////////////////////
// Name: thread.cpp
// Purpose: wxWidgets thread sample
// Author: Guilhem Lavaux, Vadim Zeitlin
// Modified by:
// Created: 06/16/98
// Copyright: (c) 1998-2009 wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ---------------------------------------------------------------------------
// headers
// ---------------------------------------------------------------------------
/*
技术支持 联系方式
QQ 75039960@qq.com, 2219992847@qq.com ,
tel : 18665802636
*/
#include "public.hpp"
bool InitLog()
{
try
{
g_Logger = spd::daily_logger_mt("daily_logger", "log/wyproxy", 2, 30);
g_Logger->flush_on(spd::level::debug);
return true;
}
catch (const spd::spdlog_ex& ex)
{
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
wxLogError(wxString("初始化日志系统失败:" + std::string(ex.what())));
return false;
}
}
void MyCleanUp()
{
if(g_pContext)
g_pContext->close();
spdlog::drop_all();
}
// ============================================================================
// implementation
// ============================================================================
// ============================================================================
// declarations
// ============================================================================
class MyThread;
WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
// ----------------------------------------------------------------------------
// the application object
// ----------------------------------------------------------------------------
class MyApp : public wxApp
{
public:
MyApp();
virtual ~MyApp(){};
virtual bool OnInit() wxOVERRIDE;
//void startProxy(int iType);
//void pauseProxy();
//void resumeProxy();
//void stopProxy();
// critical section protects access to all of the fields below
wxCriticalSection m_critsect;
// all the threads currently alive - as soon as the thread terminates, it's
// removed from the array
wxArrayThread m_threads;
// semaphore used to wait for the threads to exit, see MyFrame::OnQuit()
wxSemaphore m_semAllDone;
// indicates that we're shutting down and all threads should exit
bool m_shuttingDown;
};
// ----------------------------------------------------------------------------
// the main application frame
// ----------------------------------------------------------------------------
class MyFrame : public wxFrame,
private wxLog
{
public:
// ctor
MyFrame(const wxString& title);
virtual ~MyFrame();
// accessors for MyWorkerThread (called in its context!)
bool Cancelled();
protected:
virtual void DoLogRecord(wxLogLevel level,
const wxString& msg,
const wxLogRecordInfo& info) wxOVERRIDE;
private:
// event handlers
// --------------
void OnQuit(wxCommandEvent& event);
void OnClear(wxCommandEvent& event);
void OnStartThread(wxCommandEvent& event);
void OnStopThread(wxCommandEvent& event);
void OnPauseThread(wxCommandEvent& event);
void OnResumeThread(wxCommandEvent& event);
void OnStartWorker(wxCommandEvent& event);
void OnExecMain(wxCommandEvent& event);
void OnStartGUIThread(wxCommandEvent& event);
void OnShowCPUs(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnIdle(wxIdleEvent &event);
void OnWorkerEvent(wxThreadEvent& event);
void OnUpdateWorker(wxUpdateUIEvent& event);
// logging helper
void DoLogLine(wxTextCtrl *text,
const wxString& timestr,
const wxString& threadstr,
const wxString& msg);
// thread helper functions
// -----------------------
// helper function - creates a new thread (but doesn't run it)
MyThread *CreateThread(unsigned iType, std::string strPortSub, std::string strPortPub, std::string strIpAddr);
// update display in our status bar: called during idle handling
void UpdateThreadStatus();
// internal variables
// ------------------
// just some place to put our messages in
wxTextCtrl *m_txtctrl;
// old log target, we replace it with one using m_txtctrl during this
// frame life time
wxLog *m_oldLogger;
// the array of pending messages to be displayed and the critical section
// protecting it
wxArrayString m_messages;
wxCriticalSection m_csMessages;
// remember the number of running threads and total number of threads
size_t m_nRunning,
m_nCount;
// the progress dialog which we show while worker thread is running
wxProgressDialog *m_dlgProgress;
// was the worker thread cancelled by user?
bool m_cancelled;
wxCriticalSection m_csCancelled; // protects m_cancelled
wxDECLARE_EVENT_TABLE();
};
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// ID for the menu commands
enum
{
THREAD_QUIT = wxID_EXIT,
THREAD_ABOUT = wxID_ABOUT,
THREAD_TEXT = 101,
THREAD_CLEAR,
THREAD_START_THREAD = 201,
THREAD_STOP_THREAD,
THREAD_PAUSE_THREAD,
THREAD_RESUME_THREAD,
THREAD_START_WORKER,
THREAD_EXEC_MAIN,
THREAD_START_GUI_THREAD,
THREAD_SHOWCPUS,
WORKER_EVENT = wxID_HIGHEST+1, // this one gets sent from MyWorkerThread
GUITHREAD_EVENT // this one gets sent from MyGUIThread
};
// ----------------------------------------------------------------------------
// a simple thread
// ----------------------------------------------------------------------------
class MyThread : public wxThread
{
public:
MyThread(unsigned iType, std::string strPortSub, std::string strPortPub, std::string strIpAddr);
virtual ~MyThread();
// thread execution starts here
virtual void *Entry() wxOVERRIDE;
public:
std::shared_ptr<zmq::socket_t> m_pSock_outerCtl;
private:
unsigned m_iProxyType;
std::string m_strPortSub;
std::string m_strPortPub;
//std::string m_strPortCtl;
std::string m_strIPAddr;
std::shared_ptr<zmq::socket_t> m_pSock_Xsub;
std::shared_ptr<zmq::socket_t> m_pSock_Xpub;
std::shared_ptr<zmq::socket_t> m_pSock_Ctl;
};
// ----------------------------------------------------------------------------
// a worker thread
// ----------------------------------------------------------------------------
class MyWorkerThread : public wxThread
{
public:
MyWorkerThread(MyFrame *frame);
// thread execution starts here
virtual void *Entry() wxOVERRIDE;
// called when the thread exits - whether it terminates normally or is
// stopped with Delete() (but not when it is Kill()ed!)
virtual void OnExit() wxOVERRIDE;
public:
MyFrame *m_frame;
unsigned m_count;
};
// ----------------------------------------------------------------------------
// a thread which executes GUI calls using wxMutexGuiEnter/Leave
// ----------------------------------------------------------------------------
#define GUITHREAD_BMP_SIZE 300
#define GUITHREAD_NUM_UPDATES 50
class MyImageDialog;
class MyGUIThread : public wxThread
{
public:
MyGUIThread(MyImageDialog *dlg) : wxThread(wxTHREAD_JOINABLE)
{
m_dlg = dlg;
}
virtual ExitCode Entry() wxOVERRIDE;
private:
MyImageDialog *m_dlg;
};
// ----------------------------------------------------------------------------
// an helper dialog used by MyFrame::OnStartGUIThread
// ----------------------------------------------------------------------------
class MyImageDialog: public wxDialog
{
public:
// ctor
MyImageDialog(wxFrame *fram
评论1
最新资源