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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include "SocketAPI/clSocketClient.h"
#include "clSocketClientAsync.h"
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECTED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECT_ERROR, clCommandEvent);
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECTION_LOST, clCommandEvent);
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_INPUT, clCommandEvent);
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_ERROR, clCommandEvent);
clSocketClientAsync::clSocketClientAsync(wxEvtHandler* owner)
: m_owner(owner)
, m_thread(NULL)
{
}
clSocketClientAsync::~clSocketClientAsync() { Disconnect(); }
void clSocketClientAsync::Connect(const wxString& connectionString, const wxString& keepAliveMessage)
{
Disconnect();
m_thread = new clSocketClientAsyncHelperThread(m_owner, connectionString, false, keepAliveMessage);
m_thread->Start();
}
void clSocketClientAsync::ConnectNonBlocking(const wxString& connectionString, const wxString& keepAliveMessage)
{
Disconnect();
m_thread = new clSocketClientAsyncHelperThread(m_owner, connectionString, true, keepAliveMessage);
m_thread->Start();
}
void clSocketClientAsync::Disconnect()
{
if(m_thread) {
m_thread->Stop();
wxDELETE(m_thread);
}
}
void clSocketClientAsync::Send(const wxString& buffer)
{
if(m_thread) {
clSocketClientAsyncHelperThread::MyRequest req;
req.m_command = clSocketClientAsyncHelperThread::kSend;
req.m_buffer = buffer;
m_thread->AddRequest(req);
}
}
//-----------------------------------------------------------------------------------------------
// The helper thread
//-----------------------------------------------------------------------------------------------
clSocketClientAsyncHelperThread::clSocketClientAsyncHelperThread(wxEvtHandler* sink, const wxString& connectionString,
bool nonBlockingMode, const wxString& keepAliveMessage)
: wxThread(wxTHREAD_JOINABLE)
, m_sink(sink)
, m_keepAliveMessage(keepAliveMessage)
, m_connectionString(connectionString)
, m_nonBlockingMode(nonBlockingMode)
{
}
clSocketClientAsyncHelperThread::~clSocketClientAsyncHelperThread() {}
void* clSocketClientAsyncHelperThread::Entry()
{
// Connect
clSocketClient* client = new clSocketClient();
clSocketBase::Ptr_t socket(client);
bool connected = false;
// Try to connect and wait up to 5 seconds
if(!m_nonBlockingMode) {
for(size_t i = 0; i < 10; ++i) {
connected = client->Connect(m_connectionString, false);
if(connected) { break; }
if(TestDestroy()) {
// We were requested to go down during connect phase
return NULL;
}
::wxMilliSleep(500);
}
} else {
bool wouldBlock = false;
connected = client->ConnectNonBlocking(m_connectionString, wouldBlock);
if(!connected && wouldBlock) {
// We should select here (wait for the socket to be writable)
for(size_t i = 0; i < 5; ++i) {
int rc = client->SelectWrite(1);
if(rc == clSocketBase::kSuccess) {
// we are connected
connected = true;
break;
} else if(rc == clSocketBase::kError) {
// an error occured
break;
} else {
// Timeout
// Loop again
}
// Test for thread shutdown before we continue
if(TestDestroy()) { break; }
}
}
}
// Connected?
if(!connected) {
// report error and go out
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECT_ERROR);
event.SetString(socket->error());
m_sink->AddPendingEvent(event);
return NULL;
}
// Notify about connection successful
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECTED);
m_sink->AddPendingEvent(event);
try {
int counter = 0;
while(!TestDestroy()) {
// Wait for request from the user
++counter;
if(!m_keepAliveMessage.IsEmpty() && (counter % 10) == 0) {
// Fire the keep alive message
// if we lost the socket, it will raise an exception
socket->Send(m_keepAliveMessage);
}
MyRequest req;
if(m_queue.ReceiveTimeout(1, req) == wxMSGQUEUE_NO_ERROR) {
// got something
if(req.m_command == kDisconnect) {
socket.reset(NULL);
return NULL;
} else if(req.m_command == kSend) {
socket->Send(req.m_buffer);
}
}
// timeout, test to see if we got something on the socket
wxString buffer;
if(socket->SelectReadMS(5) == clSocketBase::kSuccess) {
int rc = socket->Read(buffer);
if(rc == clSocketBase::kSuccess) {
clCommandEvent event(wxEVT_ASYNC_SOCKET_INPUT);
event.SetString(buffer);
m_sink->AddPendingEvent(event);
} else if(rc == clSocketBase::kError) {
// Connection lost
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECTION_LOST);
m_sink->AddPendingEvent(event);
return NULL;
} else {
// Timeout
}
}
}
} catch(clSocketException& e) {
clCommandEvent event(wxEVT_ASYNC_SOCKET_ERROR);
event.SetString(e.what());
m_sink->AddPendingEvent(event);
return NULL;
}
return NULL;
}
|