| 1 |
#include "SocketAPI/clSocketClient.h" |
| 2 |
#include "clSocketClientAsync.h" |
| 3 |
#include <wx/utils.h> |
| 4 |
#include "fileutils.h" |
| 5 |
#include "SocketAPI/clConnectionString.h" |
| 6 |
#include "SocketAPI/clSocketServer.h" |
| 7 |
#include "fileutils.h" |
| 8 |
|
| 9 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECTED, clCommandEvent); |
| 10 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECT_ERROR, clCommandEvent); |
| 11 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_CONNECTION_LOST, clCommandEvent); |
| 12 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_INPUT, clCommandEvent); |
| 13 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_ERROR, clCommandEvent); |
| 14 |
wxDEFINE_EVENT(wxEVT_ASYNC_SOCKET_SERVER_READY, clCommandEvent); |
| 15 |
|
| 16 |
clAsyncSocket::clAsyncSocket(const wxString& connectionString, size_t mode) |
| 17 |
: m_thread(NULL) |
| 18 |
, m_mode(mode) |
| 19 |
, m_connectionString(connectionString) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
clAsyncSocket::~clAsyncSocket() { Stop(); } |
| 24 |
|
| 25 |
void clAsyncSocket::Start() |
| 26 |
{ |
| 27 |
Stop(); |
| 28 |
m_thread = new clSocketAsyncThread(this, m_connectionString, m_mode, wxEmptyString); |
| 29 |
m_thread->Start(); |
| 30 |
} |
| 31 |
|
| 32 |
void clAsyncSocket::Stop() { wxDELETE(m_thread); } |
| 33 |
|
| 34 |
void clAsyncSocket::Send(const std::string& buffer) |
| 35 |
{ |
| 36 |
if(m_thread) { |
| 37 |
clSocketAsyncThread::MyRequest req; |
| 38 |
req.m_command = clSocketAsyncThread::kSend; |
| 39 |
req.m_buffer = buffer; |
| 40 |
m_thread->AddRequest(req); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
void clAsyncSocket::Send(const wxString& buffer) { Send(FileUtils::ToStdString(buffer)); } |
| 45 |
|
| 46 |
//----------------------------------------------------------------------------------------------- |
| 47 |
// The helper thread |
| 48 |
//----------------------------------------------------------------------------------------------- |
| 49 |
clSocketAsyncThread::clSocketAsyncThread(wxEvtHandler* sink, const wxString& connectionString, size_t mode, |
| 50 |
const wxString& keepAliveMessage) |
| 51 |
: wxThread(wxTHREAD_JOINABLE) |
| 52 |
, m_sink(sink) |
| 53 |
, m_keepAliveMessage(keepAliveMessage) |
| 54 |
, m_connectionString(connectionString) |
| 55 |
, m_mode(mode) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
clSocketAsyncThread::~clSocketAsyncThread() { Stop(); } |
| 60 |
|
| 61 |
void* clSocketAsyncThread::Entry() |
| 62 |
{ |
| 63 |
if(m_mode & kAsyncSocketClient) { |
| 64 |
return ClientMain(); |
| 65 |
} else { |
| 66 |
return ServerMain(); |
| 67 |
} |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
|
| 71 |
void* clSocketAsyncThread::ServerMain() |
| 72 |
{ |
| 73 |
try { |
| 74 |
clSocketServer server; |
| 75 |
int port = server.Start(m_connectionString); |
| 76 |
|
| 77 |
// Notify that we are ready to accept connections |
| 78 |
clCommandEvent eventReady(wxEVT_ASYNC_SOCKET_SERVER_READY); |
| 79 |
eventReady.SetInt(port); |
| 80 |
m_sink->AddPendingEvent(eventReady); |
| 81 |
|
| 82 |
clSocketBase::Ptr_t conn; |
| 83 |
while(!TestDestroy()) { |
| 84 |
conn = server.WaitForNewConnection(1); |
| 85 |
if(conn) { |
| 86 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECTED); |
| 87 |
m_sink->AddPendingEvent(event); |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if(m_mode & kAsyncSocketMessage) { |
| 93 |
MessageLoop(conn); |
| 94 |
} else { |
| 95 |
BufferLoop(conn); |
| 96 |
} |
| 97 |
} catch(clSocketException& e) { |
| 98 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECT_ERROR); |
| 99 |
event.SetString(e.what()); |
| 100 |
m_sink->AddPendingEvent(event); |
| 101 |
} |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
void* clSocketAsyncThread::ClientMain() |
| 106 |
{ |
| 107 |
// Connect |
| 108 |
clSocketClient* client = new clSocketClient(); |
| 109 |
clSocketBase::Ptr_t socket(client); |
| 110 |
|
| 111 |
bool connected = false; |
| 112 |
|
| 113 |
// Try to connect and wait up to 5 seconds |
| 114 |
if(m_mode & kAsyncSocketNonBlocking) { |
| 115 |
bool wouldBlock = false; |
| 116 |
connected = client->ConnectNonBlocking(m_connectionString, wouldBlock); |
| 117 |
if(!connected && wouldBlock) { |
| 118 |
// We should select here (wait for the socket to be writable) |
| 119 |
for(size_t i = 0; i < 5; ++i) { |
| 120 |
int rc = client->SelectWrite(1); |
| 121 |
if(rc == clSocketBase::kSuccess) { |
| 122 |
// we are connected |
| 123 |
connected = true; |
| 124 |
break; |
| 125 |
} else if(rc == clSocketBase::kError) { |
| 126 |
// an error occured |
| 127 |
break; |
| 128 |
} else { |
| 129 |
// Timeout |
| 130 |
// Loop again |
| 131 |
} |
| 132 |
|
| 133 |
// Test for thread shutdown before we continue |
| 134 |
if(TestDestroy()) { break; } |
| 135 |
} |
| 136 |
} |
| 137 |
} else { |
| 138 |
for(size_t i = 0; i < 10; ++i) { |
| 139 |
connected = client->Connect(m_connectionString, false); |
| 140 |
if(connected) { break; } |
| 141 |
if(TestDestroy()) { |
| 142 |
// We were requested to go down during connect phase |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
::wxMilliSleep(500); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Connected? |
| 150 |
if(!connected) { |
| 151 |
// report error and go out |
| 152 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECT_ERROR); |
| 153 |
event.SetString(socket->error()); |
| 154 |
m_sink->AddPendingEvent(event); |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
// Notify about connection successful |
| 159 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECTED); |
| 160 |
m_sink->AddPendingEvent(event); |
| 161 |
|
| 162 |
if(m_mode & kAsyncSocketMessage) { |
| 163 |
MessageLoop(socket); |
| 164 |
} else { |
| 165 |
BufferLoop(socket); |
| 166 |
} |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
void clSocketAsyncThread::MessageLoop(clSocketBase::Ptr_t socket) |
| 171 |
{ |
| 172 |
try { |
| 173 |
int counter = 0; |
| 174 |
while(!TestDestroy()) { |
| 175 |
MyRequest req; |
| 176 |
if(m_queue.ReceiveTimeout(100, req) == wxMSGQUEUE_NO_ERROR) { |
| 177 |
// got something |
| 178 |
if(req.m_command == kDisconnect) { |
| 179 |
socket.reset(NULL); |
| 180 |
return; |
| 181 |
|
| 182 |
} else if(req.m_command == kSend) { |
| 183 |
socket->WriteMessage(req.m_buffer); |
| 184 |
wxString replyMessage; |
| 185 |
while(socket->ReadMessage(replyMessage, 1) == clSocketBase::kTimeout) { |
| 186 |
if(TestDestroy()) { |
| 187 |
// Going down |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
// we got data |
| 192 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_INPUT); |
| 193 |
event.SetString(replyMessage); |
| 194 |
m_sink->AddPendingEvent(event); |
| 195 |
} |
| 196 |
} else { |
| 197 |
// Make sure that the socket is still alive |
| 198 |
if((counter % 10) == 0) { |
| 199 |
socket->WriteMessage(""); |
| 200 |
counter = 0; |
| 201 |
} |
| 202 |
} |
| 203 |
++counter; |
| 204 |
} |
| 205 |
} catch(clSocketException& e) { |
| 206 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_ERROR); |
| 207 |
event.SetString(e.what()); |
| 208 |
m_sink->AddPendingEvent(event); |
| 209 |
return; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void clSocketAsyncThread::BufferLoop(clSocketBase::Ptr_t socket) |
| 214 |
{ |
| 215 |
try { |
| 216 |
int counter = 0; |
| 217 |
while(!TestDestroy()) { |
| 218 |
// Wait for request from the user |
| 219 |
++counter; |
| 220 |
if(!m_keepAliveMessage.IsEmpty() && (counter % 10) == 0) { |
| 221 |
// Fire the keep alive message |
| 222 |
// if we lost the socket, it will raise an exception |
| 223 |
socket->Send(m_keepAliveMessage); |
| 224 |
} |
| 225 |
|
| 226 |
MyRequest req; |
| 227 |
if(m_queue.ReceiveTimeout(1, req) == wxMSGQUEUE_NO_ERROR) { |
| 228 |
// got something |
| 229 |
if(req.m_command == kDisconnect) { |
| 230 |
socket.reset(NULL); |
| 231 |
return; |
| 232 |
|
| 233 |
} else if(req.m_command == kSend) { |
| 234 |
socket->Send(req.m_buffer); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// timeout, test to see if we got something on the socket |
| 239 |
wxString buffer; |
| 240 |
if(socket->SelectReadMS(5) == clSocketBase::kSuccess) { |
| 241 |
int rc = socket->Read(buffer); |
| 242 |
if(rc == clSocketBase::kSuccess) { |
| 243 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_INPUT); |
| 244 |
event.SetString(buffer); |
| 245 |
m_sink->AddPendingEvent(event); |
| 246 |
|
| 247 |
} else if(rc == clSocketBase::kError) { |
| 248 |
// Connection lost |
| 249 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_CONNECTION_LOST); |
| 250 |
m_sink->AddPendingEvent(event); |
| 251 |
return; |
| 252 |
} else { |
| 253 |
// Timeout |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} catch(clSocketException& e) { |
| 258 |
clCommandEvent event(wxEVT_ASYNC_SOCKET_ERROR); |
| 259 |
event.SetString(e.what()); |
| 260 |
m_sink->AddPendingEvent(event); |
| 261 |
} |
| 262 |
} |