2007-12-19 11:45:08 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/sockbase/client.cpp
|
|
|
|
// Purpose: Sockets sample for wxBase
|
|
|
|
// Author: Lukasz Michalski
|
|
|
|
// Modified by:
|
|
|
|
// Created: 27.06.2005
|
|
|
|
// Copyright: (c) 2005 Lukasz Michalski <lmichalski@sf.net>
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2007-12-19 11:45:08 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#include "wx/socket.h"
|
|
|
|
#include "wx/event.h"
|
|
|
|
#include "wx/list.h"
|
|
|
|
#include "wx/cmdline.h"
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/datetime.h"
|
|
|
|
#include "wx/timer.h"
|
|
|
|
#include "wx/thread.h"
|
|
|
|
|
|
|
|
const wxEventType wxEVT_WORKER = wxNewEventType();
|
2019-04-05 12:21:29 -04:00
|
|
|
#define EVT_WORKER(func) wxDECLARE_EVENT_TABLE_ENTRY( wxEVT_WORKER, -1, -1, (wxObjectEventFunction) (wxEventFunction) (WorkerEventFunction) & func, (wxObject *) NULL ),
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
const int timeout_val = 1000;
|
|
|
|
|
|
|
|
class WorkerEvent : public wxEvent {
|
|
|
|
public:
|
|
|
|
typedef enum {
|
|
|
|
CONNECTING,
|
|
|
|
SENDING,
|
|
|
|
RECEIVING,
|
|
|
|
DISCONNECTING,
|
|
|
|
DONE
|
|
|
|
} evt_type;
|
|
|
|
WorkerEvent(void* pSender, evt_type type)
|
|
|
|
{
|
|
|
|
SetId(-1);
|
|
|
|
SetEventType(wxEVT_WORKER);
|
|
|
|
m_sender = pSender;
|
|
|
|
m_eventType = type;
|
|
|
|
m_isFailed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFailed() { m_isFailed = true; }
|
|
|
|
bool isFailed() const { return m_isFailed; }
|
|
|
|
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual wxEvent* Clone() const wxOVERRIDE
|
2007-12-19 11:45:08 -05:00
|
|
|
{
|
|
|
|
return new WorkerEvent(*this);
|
|
|
|
}
|
|
|
|
void* m_sender;
|
|
|
|
bool m_isFailed;
|
|
|
|
wxString m_workerIdent;
|
|
|
|
evt_type m_eventType;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (wxEvtHandler::*WorkerEventFunction)(WorkerEvent&);
|
|
|
|
|
|
|
|
class ThreadWorker;
|
|
|
|
class EventWorker;
|
|
|
|
|
|
|
|
WX_DECLARE_LIST(ThreadWorker, TList);
|
|
|
|
WX_DECLARE_LIST(EventWorker, EList);
|
|
|
|
|
|
|
|
class Client : public wxApp {
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2007-12-19 11:45:08 -05:00
|
|
|
public:
|
|
|
|
void RemoveEventWorker(EventWorker* p_worker);
|
|
|
|
private:
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
THREADS,
|
|
|
|
EVENTS
|
|
|
|
} workMode;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SEND_RANDOM,
|
|
|
|
SEND_MESSAGE,
|
|
|
|
STRESS_TEST
|
|
|
|
} sendType;
|
|
|
|
|
|
|
|
workMode m_workMode;
|
|
|
|
sendType m_sendType;
|
|
|
|
wxString m_message;
|
|
|
|
wxString m_host;
|
|
|
|
long m_stressWorkers;
|
|
|
|
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual bool OnInit() wxOVERRIDE;
|
|
|
|
virtual int OnRun() wxOVERRIDE;
|
|
|
|
virtual int OnExit() wxOVERRIDE;
|
|
|
|
void OnInitCmdLine(wxCmdLineParser& pParser) wxOVERRIDE;
|
|
|
|
bool OnCmdLineParsed(wxCmdLineParser& pParser) wxOVERRIDE;
|
2007-12-19 11:45:08 -05:00
|
|
|
void OnWorkerEvent(WorkerEvent& pEvent);
|
|
|
|
void OnTimerEvent(wxTimerEvent& pEvent);
|
|
|
|
|
|
|
|
void StartWorker(workMode pMode, const wxString& pMessage);
|
|
|
|
void StartWorker(workMode pMode);
|
|
|
|
char* CreateBuffer(int *msgsize);
|
|
|
|
|
|
|
|
void dumpStatistics();
|
|
|
|
|
|
|
|
TList m_threadWorkers;
|
|
|
|
EList m_eventWorkers;
|
|
|
|
|
|
|
|
unsigned m_statConnecting;
|
|
|
|
unsigned m_statSending;
|
|
|
|
unsigned m_statReceiving;
|
|
|
|
unsigned m_statDisconnecting;
|
|
|
|
unsigned m_statDone;
|
|
|
|
unsigned m_statFailed;
|
|
|
|
|
|
|
|
wxTimer mTimer;
|
|
|
|
};
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_APP(Client);
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
class ThreadWorker : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ThreadWorker(const wxString& p_host, char* p_buf, int p_size);
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual ExitCode Entry() wxOVERRIDE;
|
2007-12-19 11:45:08 -05:00
|
|
|
private:
|
|
|
|
wxString m_host;
|
|
|
|
wxSocketClient* m_clientSocket;
|
|
|
|
char* m_inbuf;
|
|
|
|
char* m_outbuf;
|
|
|
|
int m_outsize;
|
|
|
|
int m_insize;
|
|
|
|
wxString m_workerIdent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventWorker : public wxEvtHandler
|
|
|
|
{
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2007-12-19 11:45:08 -05:00
|
|
|
public:
|
|
|
|
EventWorker(const wxString& p_host, char* p_buf, int p_size);
|
|
|
|
void Run();
|
|
|
|
virtual ~EventWorker();
|
|
|
|
private:
|
|
|
|
wxString m_host;
|
|
|
|
wxSocketClient* m_clientSocket;
|
|
|
|
char* m_inbuf;
|
|
|
|
char* m_outbuf;
|
|
|
|
int m_outsize;
|
|
|
|
int m_written;
|
|
|
|
int m_insize;
|
|
|
|
int m_readed;
|
|
|
|
|
|
|
|
WorkerEvent::evt_type m_currentType;
|
|
|
|
bool m_doneSent;
|
|
|
|
wxIPV4address m_localaddr;
|
|
|
|
|
|
|
|
void OnSocketEvent(wxSocketEvent& pEvent);
|
|
|
|
void SendEvent(bool failed);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************* Implementation ******************/
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP_CONSOLE(Client);
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
#include <wx/listimpl.cpp>
|
|
|
|
WX_DEFINE_LIST(TList);
|
|
|
|
WX_DEFINE_LIST(EList);
|
|
|
|
|
|
|
|
wxString
|
|
|
|
CreateIdent(const wxIPV4address& addr)
|
|
|
|
{
|
2018-11-01 19:32:02 -04:00
|
|
|
return wxString::Format("%s:%d",addr.IPAddress(),addr.Service());
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::OnInitCmdLine(wxCmdLineParser& pParser)
|
|
|
|
{
|
|
|
|
wxApp::OnInitCmdLine(pParser);
|
2018-09-22 19:15:08 -04:00
|
|
|
pParser.AddSwitch("e","event",_("Use event based worker (default)"),wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddSwitch("t","thread",_("Use thread based worker"),wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddSwitch("r","random",_("Send random data (default)"),wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddOption("m","message",_("Send message from <str>"),wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddOption("f","file",_("Send contents of <file>"),wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddOption("H","hostname",_("IP or name of host to connect to"),wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
pParser.AddOption("s","stress",_("stress test with <num> concurrent connections"),wxCMD_LINE_VAL_NUMBER,wxCMD_LINE_PARAM_OPTIONAL);
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Client::OnCmdLineParsed(wxCmdLineParser& pParser)
|
|
|
|
{
|
|
|
|
wxString fname;
|
|
|
|
m_workMode = EVENTS;
|
|
|
|
m_stressWorkers = 50;
|
|
|
|
|
|
|
|
if (pParser.Found(_("verbose")))
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLog::AddTraceMask("wxSocket");
|
|
|
|
wxLog::AddTraceMask("epolldispatcher");
|
|
|
|
wxLog::AddTraceMask("selectdispatcher");
|
|
|
|
wxLog::AddTraceMask("thread");
|
|
|
|
wxLog::AddTraceMask("events");
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:15:08 -04:00
|
|
|
if (pParser.Found("t"))
|
2007-12-19 11:45:08 -05:00
|
|
|
m_workMode = THREADS;
|
|
|
|
m_sendType = SEND_RANDOM;
|
|
|
|
|
2018-09-22 19:15:08 -04:00
|
|
|
if (pParser.Found("m",&m_message))
|
2007-12-19 11:45:08 -05:00
|
|
|
m_sendType = SEND_MESSAGE;
|
2018-09-22 19:15:08 -04:00
|
|
|
else if (pParser.Found("f",&fname))
|
2007-12-19 11:45:08 -05:00
|
|
|
{
|
|
|
|
wxFFile file(fname);
|
|
|
|
if (!file.IsOpened()) {
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("Cannot open file %s",fname);
|
2007-12-19 11:45:08 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (!file.ReadAll(&m_message)) {
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("Cannot read content of file %s",fname);
|
2007-12-19 11:45:08 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
m_sendType = SEND_MESSAGE;
|
|
|
|
};
|
|
|
|
|
2018-09-22 19:15:08 -04:00
|
|
|
if (pParser.Found("s",&m_stressWorkers))
|
2007-12-19 11:45:08 -05:00
|
|
|
m_sendType = STRESS_TEST;
|
|
|
|
|
2018-09-22 19:15:08 -04:00
|
|
|
m_host = "127.0.0.1";
|
|
|
|
pParser.Found("H",&m_host);
|
2007-12-19 11:45:08 -05:00
|
|
|
return wxApp::OnCmdLineParsed(pParser);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Client::OnInit()
|
|
|
|
{
|
|
|
|
if (!wxApp::OnInit())
|
|
|
|
return false;
|
|
|
|
srand(wxDateTime::Now().GetTicks());
|
|
|
|
mTimer.SetOwner(this);
|
|
|
|
m_statConnecting = 0;
|
|
|
|
m_statSending = 0;
|
|
|
|
m_statReceiving = 0;
|
|
|
|
m_statDisconnecting = 0;
|
|
|
|
m_statDone = 0;
|
|
|
|
m_statFailed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Client::OnRun()
|
|
|
|
{
|
2008-10-03 17:52:59 -04:00
|
|
|
int i;
|
2007-12-19 11:45:08 -05:00
|
|
|
switch(m_sendType)
|
|
|
|
{
|
|
|
|
case STRESS_TEST:
|
|
|
|
switch(m_workMode)
|
|
|
|
{
|
|
|
|
case THREADS:
|
2008-10-03 17:52:59 -04:00
|
|
|
for (i = 0; i < m_stressWorkers; i++) {
|
2007-12-19 11:45:08 -05:00
|
|
|
if (m_message.empty())
|
|
|
|
StartWorker(THREADS);
|
|
|
|
else
|
|
|
|
StartWorker(THREADS, m_message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EVENTS:
|
2008-10-03 17:52:59 -04:00
|
|
|
for (i = 0; i < m_stressWorkers; i++) {
|
2007-12-19 11:45:08 -05:00
|
|
|
if (m_message.empty())
|
|
|
|
StartWorker(EVENTS);
|
|
|
|
else
|
|
|
|
StartWorker(EVENTS, m_message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2008-10-03 17:52:59 -04:00
|
|
|
for (i = 0; i < m_stressWorkers; i++) {
|
2007-12-19 11:45:08 -05:00
|
|
|
if (m_message.empty())
|
|
|
|
StartWorker(i % 5 == 0 ? THREADS : EVENTS);
|
|
|
|
else
|
|
|
|
StartWorker(i % 5 == 0 ? THREADS : EVENTS, m_message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEND_MESSAGE:
|
|
|
|
StartWorker(m_workMode,m_message);
|
|
|
|
break;
|
|
|
|
case SEND_RANDOM:
|
|
|
|
StartWorker(m_workMode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mTimer.Start(timeout_val,true);
|
|
|
|
return wxApp::OnRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Client::OnExit()
|
|
|
|
{
|
|
|
|
for(EList::compatibility_iterator it = m_eventWorkers.GetFirst(); it ; it->GetNext()) {
|
|
|
|
delete it->GetData();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-30 07:44:45 -04:00
|
|
|
// Create buffer to be sent by client. Buffer contains test indicator
|
2007-12-19 11:45:08 -05:00
|
|
|
// message size and place for data
|
2010-09-30 07:44:45 -04:00
|
|
|
// msgsize parameter contains size of data in bytes and
|
2007-12-19 11:45:08 -05:00
|
|
|
// if input value does not fit into 250 bytes then
|
|
|
|
// on exit is updated to new value that is multiply of 1024 bytes
|
|
|
|
char*
|
|
|
|
Client::CreateBuffer(int* msgsize)
|
|
|
|
{
|
|
|
|
int bufsize = 0;
|
|
|
|
char* buf;
|
|
|
|
//if message should have more than 256 bytes then set it as
|
|
|
|
//test3 for compatibility with GUI server sample
|
2010-09-30 07:44:45 -04:00
|
|
|
if ((*msgsize) > 250)
|
2007-12-19 11:45:08 -05:00
|
|
|
{
|
2014-12-24 08:57:19 -05:00
|
|
|
//send at least one KB of data
|
2007-12-19 11:45:08 -05:00
|
|
|
int size = (*msgsize)/1024 + 1;
|
2014-12-24 08:57:19 -05:00
|
|
|
//returned buffer will contain test indicator, message size in KB and data
|
2007-12-19 11:45:08 -05:00
|
|
|
bufsize = size*1024+2;
|
|
|
|
buf = new char[bufsize];
|
2009-06-07 08:31:24 -04:00
|
|
|
buf[0] = (unsigned char)0xDE; //second byte contains size in kilobytes
|
2007-12-19 11:45:08 -05:00
|
|
|
buf[1] = (char)(size);
|
|
|
|
*msgsize = size*1024;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-24 08:57:19 -05:00
|
|
|
//returned buffer will contain test indicator, message size in KB and data
|
2007-12-19 11:45:08 -05:00
|
|
|
bufsize = (*msgsize)+2;
|
|
|
|
buf = new char[bufsize];
|
2009-06-07 08:31:24 -04:00
|
|
|
buf[0] = (unsigned char)0xBE; //second byte contains size in bytes
|
2007-12-19 11:45:08 -05:00
|
|
|
buf[1] = (char)(*msgsize);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::StartWorker(workMode pMode) {
|
|
|
|
int msgsize = 1 + (int) (250000.0 * (rand() / (RAND_MAX + 1.0)));
|
|
|
|
char* buf = CreateBuffer(&msgsize);
|
|
|
|
|
|
|
|
//fill data part of buffer with random bytes
|
|
|
|
for (int i = 2; i < (msgsize); i++) {
|
|
|
|
buf[i] = i % 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pMode == THREADS) {
|
|
|
|
ThreadWorker* c = new ThreadWorker(m_host,buf,msgsize+2);
|
|
|
|
if (c->Create() != wxTHREAD_NO_ERROR) {
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLogError("Cannot create more threads");
|
2007-12-19 11:45:08 -05:00
|
|
|
} else {
|
|
|
|
c->Run();
|
|
|
|
m_threadWorkers.Append(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EventWorker* e = new EventWorker(m_host,buf,msgsize+2);
|
|
|
|
e->Run();
|
|
|
|
m_eventWorkers.Append(e);
|
|
|
|
}
|
|
|
|
m_statConnecting++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::StartWorker(workMode pMode, const wxString& pMessage) {
|
2009-06-06 19:14:30 -04:00
|
|
|
char* tmpbuf = wxStrdup(pMessage.mb_str());
|
2007-12-19 11:45:08 -05:00
|
|
|
int msgsize = strlen(tmpbuf);
|
|
|
|
char* buf = CreateBuffer(&msgsize);
|
|
|
|
memset(buf+2,0x0,msgsize);
|
|
|
|
memcpy(buf+2,tmpbuf,msgsize);
|
|
|
|
free(tmpbuf);
|
|
|
|
|
|
|
|
if (pMode == THREADS) {
|
|
|
|
ThreadWorker* c = new ThreadWorker(m_host,buf,msgsize+2);
|
|
|
|
if (c->Create() != wxTHREAD_NO_ERROR) {
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLogError("Cannot create more threads");
|
2007-12-19 11:45:08 -05:00
|
|
|
} else {
|
|
|
|
c->Run();
|
|
|
|
m_threadWorkers.Append(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EventWorker* e = new EventWorker(m_host,buf,msgsize+2);
|
|
|
|
e->Run();
|
|
|
|
m_eventWorkers.Append(e);
|
|
|
|
}
|
|
|
|
m_statConnecting++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::OnWorkerEvent(WorkerEvent& pEvent) {
|
|
|
|
switch (pEvent.m_eventType) {
|
|
|
|
case WorkerEvent::CONNECTING:
|
|
|
|
if (pEvent.isFailed())
|
|
|
|
{
|
|
|
|
m_statConnecting--;
|
|
|
|
m_statFailed++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WorkerEvent::SENDING:
|
|
|
|
if (pEvent.isFailed())
|
|
|
|
{
|
|
|
|
m_statFailed++;
|
|
|
|
m_statSending--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_statConnecting--;
|
|
|
|
m_statSending++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WorkerEvent::RECEIVING:
|
|
|
|
if (pEvent.isFailed())
|
|
|
|
{
|
|
|
|
m_statReceiving--;
|
|
|
|
m_statFailed++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_statSending--;
|
|
|
|
m_statReceiving++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WorkerEvent::DISCONNECTING:
|
|
|
|
if (pEvent.isFailed())
|
|
|
|
{
|
|
|
|
m_statDisconnecting--;
|
|
|
|
m_statFailed++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_statReceiving--;
|
|
|
|
m_statDisconnecting++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WorkerEvent::DONE:
|
|
|
|
m_statDone++;
|
|
|
|
m_statDisconnecting--;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pEvent.isFailed() || pEvent.m_eventType == WorkerEvent::DONE)
|
|
|
|
{
|
|
|
|
for(TList::compatibility_iterator it = m_threadWorkers.GetFirst(); it ; it = it->GetNext()) {
|
|
|
|
if (it->GetData() == pEvent.m_sender) {
|
|
|
|
m_threadWorkers.DeleteNode(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 17:52:59 -04:00
|
|
|
for(EList::compatibility_iterator it2 = m_eventWorkers.GetFirst(); it2 ; it2 = it2->GetNext())
|
2007-12-19 11:45:08 -05:00
|
|
|
{
|
2008-10-03 17:52:59 -04:00
|
|
|
if (it2->GetData() == pEvent.m_sender) {
|
|
|
|
delete it2->GetData();
|
|
|
|
m_eventWorkers.DeleteNode(it2);
|
2007-12-19 11:45:08 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((m_threadWorkers.GetCount() == 0) && (m_eventWorkers.GetCount() == 0))
|
|
|
|
{
|
|
|
|
mTimer.Stop();
|
|
|
|
dumpStatistics();
|
|
|
|
wxSleep(2);
|
|
|
|
ExitMainLoop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mTimer.Start(timeout_val,true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::RemoveEventWorker(EventWorker* p_worker) {
|
|
|
|
for(EList::compatibility_iterator it = m_eventWorkers.GetFirst(); it ; it = it->GetNext()) {
|
|
|
|
if (it->GetData() == p_worker) {
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("Deleting event worker");
|
2007-12-19 11:45:08 -05:00
|
|
|
delete it->GetData();
|
|
|
|
m_eventWorkers.DeleteNode(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::dumpStatistics() {
|
|
|
|
wxString msg(
|
|
|
|
wxString::Format(_("Connecting:\t%d\nSending\t\t%d\nReceiving\t%d\nDisconnecting:\t%d\nDone:\t\t%d\nFailed:\t\t%d\n"),
|
|
|
|
m_statConnecting,
|
|
|
|
m_statSending,
|
|
|
|
m_statReceiving,
|
|
|
|
m_statDisconnecting,
|
|
|
|
m_statDone,
|
|
|
|
m_statFailed
|
|
|
|
));
|
|
|
|
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogMessage("Current status:\n%s\n",msg);
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Client::OnTimerEvent(wxTimerEvent&) {
|
|
|
|
dumpStatistics();
|
|
|
|
}
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(Client,wxEvtHandler)
|
2007-12-19 11:45:08 -05:00
|
|
|
EVT_WORKER(Client::OnWorkerEvent)
|
|
|
|
EVT_TIMER(wxID_ANY,Client::OnTimerEvent)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventWorker::EventWorker(const wxString& p_host, char* p_buf, int p_size)
|
|
|
|
: m_host(p_host),
|
|
|
|
m_outbuf(p_buf),
|
|
|
|
m_outsize(p_size),
|
|
|
|
m_written(0),
|
|
|
|
m_readed(0)
|
|
|
|
{
|
|
|
|
m_clientSocket = new wxSocketClient(wxSOCKET_NOWAIT);
|
|
|
|
m_clientSocket->SetEventHandler(*this);
|
|
|
|
m_insize = m_outsize - 2;
|
|
|
|
m_inbuf = new char[m_insize];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventWorker::Run() {
|
|
|
|
wxIPV4address ca;
|
|
|
|
ca.Hostname(m_host);
|
|
|
|
ca.Service(3000);
|
|
|
|
m_clientSocket->SetNotify(wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG|wxSOCKET_OUTPUT_FLAG|wxSOCKET_INPUT_FLAG);
|
|
|
|
m_clientSocket->Notify(true);
|
|
|
|
m_currentType = WorkerEvent::CONNECTING;
|
|
|
|
m_doneSent = false;
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogMessage("EventWorker: Connecting.....");
|
2007-12-19 11:45:08 -05:00
|
|
|
m_clientSocket->Connect(ca,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventWorker::OnSocketEvent(wxSocketEvent& pEvent) {
|
|
|
|
switch(pEvent.GetSocketEvent()) {
|
|
|
|
case wxSOCKET_INPUT:
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: INPUT");
|
2007-12-19 11:45:08 -05:00
|
|
|
do {
|
|
|
|
if (m_readed == m_insize)
|
|
|
|
return; //event already posted
|
|
|
|
m_clientSocket->Read(m_inbuf + m_readed, m_insize - m_readed);
|
|
|
|
if (m_clientSocket->Error())
|
|
|
|
{
|
|
|
|
if (m_clientSocket->LastError() != wxSOCKET_WOULDBLOCK)
|
|
|
|
{
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("%s: read error",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_readed += m_clientSocket->LastCount();
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: readed %d bytes, %d bytes to do",m_clientSocket->LastCount(), m_insize - m_readed);
|
2007-12-19 11:45:08 -05:00
|
|
|
if (m_readed == m_insize)
|
|
|
|
{
|
|
|
|
if (!memcmp(m_inbuf,m_outbuf,m_insize)) {
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("%s: data mismatch",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(true);
|
|
|
|
}
|
|
|
|
m_currentType = WorkerEvent::DISCONNECTING;
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogDebug("%s: DISCONNECTING",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(false);
|
|
|
|
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker %p closing",this);
|
2007-12-19 11:45:08 -05:00
|
|
|
m_clientSocket->Close();
|
|
|
|
|
|
|
|
m_currentType = WorkerEvent::DONE;
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogDebug("%s: DONE",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(false);
|
|
|
|
}
|
|
|
|
} while (!m_clientSocket->Error());
|
|
|
|
break;
|
|
|
|
case wxSOCKET_OUTPUT:
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: OUTPUT");
|
2007-12-19 11:45:08 -05:00
|
|
|
do {
|
|
|
|
if (m_written == m_outsize)
|
|
|
|
return;
|
|
|
|
if (m_written == 0)
|
|
|
|
{
|
|
|
|
m_currentType = WorkerEvent::SENDING;
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogDebug("%s: SENDING",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
m_clientSocket->Write(m_outbuf + m_written, m_outsize - m_written);
|
|
|
|
if (m_clientSocket->Error())
|
|
|
|
{
|
|
|
|
if (m_clientSocket->LastError() != wxSOCKET_WOULDBLOCK) {
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("%s: Write error",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_written += m_clientSocket->LastCount();
|
|
|
|
if (m_written != m_outsize)
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: written %d bytes, %d bytes to do",m_clientSocket->LastCount(),m_outsize - m_written);
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker %p SENDING->RECEIVING",this);
|
2007-12-19 11:45:08 -05:00
|
|
|
m_currentType = WorkerEvent::RECEIVING;
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogDebug("%s: RECEIVING",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(false);
|
|
|
|
}
|
|
|
|
} while(!m_clientSocket->Error());
|
|
|
|
break;
|
|
|
|
case wxSOCKET_CONNECTION:
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogMessage("EventWorker: got connection");
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogMessage("%s: starting writing message (2 bytes for signature and %d bytes of data to write)",CreateIdent(m_localaddr),m_outsize-2);
|
2007-12-19 11:45:08 -05:00
|
|
|
if (!m_clientSocket->GetLocal(m_localaddr))
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2007-12-19 11:45:08 -05:00
|
|
|
wxLogError(_("Cannot get peer data for socket %p"),m_clientSocket);
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2007-12-19 11:45:08 -05:00
|
|
|
m_currentType = WorkerEvent::SENDING;
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogDebug("%s: CONNECTING",CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case wxSOCKET_LOST:
|
|
|
|
{
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError(_("%s: connection lost"),CreateIdent(m_localaddr));
|
2007-12-19 11:45:08 -05:00
|
|
|
SendEvent(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventWorker::SendEvent(bool failed) {
|
|
|
|
if (m_doneSent)
|
|
|
|
return;
|
|
|
|
WorkerEvent e(this,m_currentType);
|
|
|
|
if (failed) e.setFailed();
|
|
|
|
wxGetApp().AddPendingEvent(e);
|
|
|
|
m_doneSent = failed || m_currentType == WorkerEvent::DONE;
|
|
|
|
};
|
|
|
|
|
|
|
|
EventWorker::~EventWorker() {
|
|
|
|
m_clientSocket->Destroy();
|
|
|
|
delete [] m_outbuf;
|
|
|
|
delete [] m_inbuf;
|
|
|
|
}
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(EventWorker,wxEvtHandler)
|
2007-12-19 11:45:08 -05:00
|
|
|
EVT_SOCKET(wxID_ANY,EventWorker::OnSocketEvent)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
ThreadWorker::ThreadWorker(const wxString& p_host, char* p_buf, int p_size)
|
|
|
|
: wxThread(wxTHREAD_DETACHED),
|
|
|
|
m_host(p_host),
|
|
|
|
m_outbuf(p_buf),
|
|
|
|
m_outsize(p_size)
|
|
|
|
{
|
|
|
|
m_clientSocket = new wxSocketClient(wxSOCKET_BLOCK|wxSOCKET_WAITALL);
|
|
|
|
m_insize = m_outsize - 2;
|
|
|
|
m_inbuf = new char[m_insize];
|
|
|
|
}
|
|
|
|
|
|
|
|
wxThread::ExitCode ThreadWorker::Entry()
|
|
|
|
{
|
|
|
|
wxIPV4address ca;
|
|
|
|
ca.Hostname(m_host);
|
|
|
|
ca.Service(5678);
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("ThreadWorker: Connecting.....");
|
2007-12-19 11:45:08 -05:00
|
|
|
m_clientSocket->SetTimeout(60);
|
|
|
|
bool failed = false;
|
|
|
|
WorkerEvent::evt_type etype = WorkerEvent::CONNECTING;
|
|
|
|
if (!m_clientSocket->Connect(ca)) {
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogError("Cannot connect to %s:%d",ca.IPAddress(), ca.Service());
|
2007-12-19 11:45:08 -05:00
|
|
|
failed = true;
|
|
|
|
} else {
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogMessage("ThreadWorker: Connected. Sending %d bytes of data",m_outsize);
|
2007-12-19 11:45:08 -05:00
|
|
|
etype = WorkerEvent::SENDING;
|
|
|
|
WorkerEvent e(this,etype);
|
|
|
|
wxGetApp().AddPendingEvent(e);
|
|
|
|
int to_process = m_outsize;
|
|
|
|
do {
|
|
|
|
m_clientSocket->Write(m_outbuf,m_outsize);
|
|
|
|
if (m_clientSocket->Error()) {
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLogError("ThreadWorker: Write error");
|
2007-12-19 11:45:08 -05:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
to_process -= m_clientSocket->LastCount();
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: written %d bytes, %d bytes to do",m_clientSocket->LastCount(),to_process);
|
2007-12-19 11:45:08 -05:00
|
|
|
} while(!m_clientSocket->Error() && to_process != 0);
|
|
|
|
|
|
|
|
if (!failed) {
|
|
|
|
etype = WorkerEvent::RECEIVING;
|
|
|
|
WorkerEvent e(this,etype);
|
|
|
|
wxGetApp().AddPendingEvent(e);
|
|
|
|
to_process = m_insize;
|
|
|
|
do {
|
|
|
|
m_clientSocket->Read(m_inbuf,m_insize);
|
|
|
|
if (m_clientSocket->Error()) {
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLogError("ThreadWorker: Read error");
|
2007-12-19 11:45:08 -05:00
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
to_process -= m_clientSocket->LastCount();
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("EventWorker: readed %d bytes, %d bytes to do",m_clientSocket->LastCount(),to_process);
|
2007-12-19 11:45:08 -05:00
|
|
|
} while(!m_clientSocket->Error() && to_process != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* outdat = (char*)m_outbuf+2;
|
|
|
|
if (!failed && (memcmp(m_inbuf,outdat,m_insize) != 0))
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
wxLogError("Data mismatch");
|
2007-12-19 11:45:08 -05:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 19:15:08 -04:00
|
|
|
//wxLogDebug("ThreadWorker: Finished");
|
2007-12-19 11:45:08 -05:00
|
|
|
if (!failed) {
|
|
|
|
etype = WorkerEvent::DISCONNECTING;
|
|
|
|
WorkerEvent e(this,etype);
|
|
|
|
wxGetApp().AddPendingEvent(e);
|
|
|
|
};
|
|
|
|
m_clientSocket->Close();
|
|
|
|
m_clientSocket->Destroy();
|
|
|
|
m_clientSocket = NULL;
|
|
|
|
delete [] m_outbuf;
|
|
|
|
delete [] m_inbuf;
|
|
|
|
if (!failed)
|
|
|
|
etype = WorkerEvent::DONE;
|
|
|
|
WorkerEvent e(this,etype);
|
|
|
|
if (failed) e.setFailed();
|
|
|
|
wxGetApp().AddPendingEvent(e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|