]> git.saurik.com Git - wxWidgets.git/blame - samples/sockets/baseserver.cpp
Include <fcntl.h> from a header using fcntl().
[wxWidgets.git] / samples / sockets / baseserver.cpp
CommitLineData
2804f77d
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/sockbase/client.cpp
3// Purpose: Sockets sample for wxBase
4// Author: Lukasz Michalski
5// Modified by:
6// Created: 27.06.2005
7// RCS-ID: $Id$
8// Copyright: (c) 2005 Lukasz Michalski <lmichalski@user.sourceforge.net>
526954c5 9// Licence: wxWindows licence
2804f77d
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wx.h"
21#include "wx/socket.h"
22#include "wx/event.h"
23#include "wx/list.h"
24#include "wx/cmdline.h"
25#include "wx/datetime.h"
26#include "wx/timer.h"
27#include "wx/thread.h"
28
e2bcc781 29const char *GetSocketErrorMsg(int pSockError)
2804f77d
VZ
30{
31 switch(pSockError)
32 {
33 case wxSOCKET_NOERROR:
e2bcc781
VZ
34 return "wxSOCKET_NOERROR";
35
2804f77d 36 case wxSOCKET_INVOP:
e2bcc781
VZ
37 return "wxSOCKET_INVOP";
38
2804f77d 39 case wxSOCKET_IOERR:
e2bcc781
VZ
40 return "wxSOCKET_IOERR";
41
2804f77d 42 case wxSOCKET_INVADDR:
e2bcc781
VZ
43 return "wxSOCKET_INVADDR";
44
2804f77d 45 case wxSOCKET_NOHOST:
e2bcc781
VZ
46 return "wxSOCKET_NOHOST";
47
2804f77d 48 case wxSOCKET_INVPORT:
e2bcc781
VZ
49 return "wxSOCKET_INVPORT";
50
2804f77d 51 case wxSOCKET_WOULDBLOCK:
e2bcc781
VZ
52 return "wxSOCKET_WOULDBLOCK";
53
2804f77d 54 case wxSOCKET_TIMEDOUT:
e2bcc781
VZ
55 return "wxSOCKET_TIMEDOUT";
56
2804f77d 57 case wxSOCKET_MEMERR:
e2bcc781
VZ
58 return "wxSOCKET_MEMERR";
59
2804f77d 60 default:
e2bcc781 61 return "Unknown";
2804f77d
VZ
62 }
63}
64
2804f77d
VZ
65//event sent by workers to server class
66//after client is served
67const wxEventType wxEVT_WORKER = wxNewEventType();
68#define EVT_WORKER(func) DECLARE_EVENT_TABLE_ENTRY( wxEVT_WORKER, -1, -1, (wxObjectEventFunction) (wxEventFunction) (WorkerEventFunction) & func, (wxObject *) NULL ),
69
e2bcc781
VZ
70class WorkerEvent : public wxEvent
71{
2804f77d
VZ
72public:
73 WorkerEvent(void* pSender)
74 {
75 SetId(-1);
76 SetEventType(wxEVT_WORKER);
77 m_sender = pSender;
78 m_exit = false;
79 m_workerFailed = false;
80 }
81
82 virtual wxEvent* Clone() const
83 {
84 return new WorkerEvent(*this);
85 }
86
87 void* m_sender;
88 bool m_exit;
89 bool m_workerFailed;
90};
91
92typedef void (wxEvtHandler::*WorkerEventFunction)(WorkerEvent&);
93
94class ThreadWorker;
95class EventWorker;
96
97WX_DECLARE_LIST(ThreadWorker, TList);
98WX_DECLARE_LIST(EventWorker, EList);
99
100//main server class contains listening socket
101//and list of two type worker classes that serve clients
102class Server : public wxApp
103{
66034aba 104 DECLARE_EVENT_TABLE()
2804f77d
VZ
105public:
106 Server() : m_maxConnections(-1) {}
107 ~Server() {}
108private:
109 enum WorkMode
110 {
111 MIXED,
112 THREADS,
113 EVENTS
114 };
115
116 virtual bool OnInit();
117 virtual int OnExit();
118
119 void OnInitCmdLine(wxCmdLineParser& pParser);
120 bool OnCmdLineParsed(wxCmdLineParser& pParser);
121
122 void OnSocketEvent(wxSocketEvent& pEvent);
123 void OnWorkerEvent(WorkerEvent& pEvent);
124 void OnTimerEvent(wxTimerEvent& pEvent);
125 void DumpStatistics();
126
127 TList m_threadWorkers;
128 EList m_eventWorkers;
129 WorkMode m_workMode;
130 wxSocketServer* m_listeningSocket;
131
132 // statistics
133 unsigned m_threadWorkersCreated;
134 unsigned m_threadWorkersDone;
135 unsigned m_threadWorkersFailed;
136 unsigned m_maxThreadWorkers;
137
138 unsigned m_eventWorkersCreated;
139 unsigned m_eventWorkersDone;
140 unsigned m_eventWorkersFailed;
141 unsigned m_maxEventWorkers;
142
143 long int m_maxConnections;
144
f919ae34 145 unsigned short m_port;
2804f77d
VZ
146
147 wxTimer mTimer;
148};
149
150DECLARE_APP(Server);
151
c3bcb48f
VZ
152// just some common things shared between ThreadWorker and EventWorker
153class WorkerBase
154{
155protected:
156 // outputs log message with IP and TCP port number prepended
157 void LogWorker(const wxString& msg, wxLogLevel level = wxLOG_Info)
158 {
159 wxLogGeneric(level,
160 "%s:%d %s", m_peer.IPAddress(), m_peer.Service(), msg);
161 }
162
163 wxIPV4address m_peer;
164};
165
2804f77d
VZ
166//thread based worker reads signature and all data first from connected client
167//and resends data to client after reading
c3bcb48f 168class ThreadWorker : public wxThread, private WorkerBase
2804f77d
VZ
169{
170public:
171 ThreadWorker(wxSocketBase* pSocket);
172 virtual ExitCode Entry();
c3bcb48f 173
2804f77d
VZ
174private:
175 wxSocketBase* m_socket;
2804f77d
VZ
176};
177
178//event based worker reads signature and creates buffer for incoming data.
179//When part of data arrives this worker resends it as soon as possible.
c3bcb48f 180class EventWorker : public wxEvtHandler, private WorkerBase
2804f77d 181{
2804f77d
VZ
182public:
183 EventWorker(wxSocketBase* pSock);
c3bcb48f
VZ
184 virtual ~EventWorker();
185
2804f77d
VZ
186private:
187 wxSocketBase* m_socket;
2804f77d
VZ
188
189 unsigned char m_signature[2];
190 char* m_inbuf;
191 int m_infill;
192 int m_size;
193 char* m_outbuf;
194 int m_outfill;
195 int m_written;
196
197 void OnSocketEvent(wxSocketEvent& pEvent);
198 void DoWrite();
199 void DoRead();
c3bcb48f 200
66034aba 201 DECLARE_EVENT_TABLE()
2804f77d
VZ
202};
203
204/******************* Implementation ******************/
205IMPLEMENT_APP_CONSOLE(Server)
206
207#include <wx/listimpl.cpp>
208WX_DEFINE_LIST(TList);
209WX_DEFINE_LIST(EList);
210
211
212void
213Server::OnInitCmdLine(wxCmdLineParser& pParser)
214{
215 wxApp::OnInitCmdLine(pParser);
c3bcb48f
VZ
216 pParser.AddSwitch("t","threads","Use thread based workers only");
217 pParser.AddSwitch("e","events","Use event based workers only");
218 pParser.AddOption("m","max","Exit after <n> connections",
219 wxCMD_LINE_VAL_NUMBER);
220 pParser.AddOption("p","port","listen on given port (default 3000)",
221 wxCMD_LINE_VAL_NUMBER);
2804f77d
VZ
222}
223
224void
225Server::DumpStatistics()
226{
227 wxString mode;
228 switch(m_workMode)
229 {
230 case EVENTS:
c3bcb48f 231 mode = "Event based workers";
2804f77d
VZ
232 break;
233 case THREADS:
c3bcb48f 234 mode = "Thread based workers";
2804f77d
VZ
235 break;
236 case MIXED:
c3bcb48f 237 mode = "Event and thread based workers";
2804f77d
VZ
238 break;
239 }
e2bcc781
VZ
240 wxLogMessage("Server mode: %s",mode);
241 wxLogMessage("\t\t\t\tThreads\tEvents\tTotal");
242 wxLogMessage("Workers created:\t\t%d\t%d\t%d",
243 m_threadWorkersCreated,
244 m_eventWorkersCreated,
245 m_threadWorkersCreated + m_eventWorkersCreated);
246 wxLogMessage("Max concurrent workers:\t%d\t%d\t%d",
247 m_maxThreadWorkers,
248 m_maxEventWorkers,
249 m_maxThreadWorkers + m_maxEventWorkers);
250 wxLogMessage("Workers failed:\t\t%d\t%d\t%d",
251 m_threadWorkersFailed,
252 m_eventWorkersFailed,
253 m_threadWorkersFailed + m_eventWorkersFailed);
254 wxLogMessage("Workers done:\t\t%d\t%d\t%d",
255 m_threadWorkersDone,
256 m_eventWorkersDone,
257 m_threadWorkersDone + m_eventWorkersDone);
2804f77d
VZ
258
259 if ((int)(m_threadWorkersDone+m_eventWorkersDone) == m_maxConnections)
260 {
3ecb4c01 261 wxLogMessage("%ld connection(s) served, exiting",m_maxConnections);
2804f77d
VZ
262 ExitMainLoop();
263 }
264}
265
266
267bool
268Server::OnCmdLineParsed(wxCmdLineParser& pParser)
269{
c3bcb48f 270 if (pParser.Found("verbose"))
2804f77d 271 {
e2bcc781
VZ
272 wxLog::AddTraceMask("wxSocket");
273 wxLog::AddTraceMask("epolldispatcher");
274 wxLog::AddTraceMask("selectdispatcher");
275 wxLog::AddTraceMask("thread");
276 wxLog::AddTraceMask("events");
277 wxLog::AddTraceMask("timer");
2804f77d
VZ
278 }
279
e2bcc781 280 if (pParser.Found("m",&m_maxConnections))
2804f77d 281 {
3ecb4c01 282 wxLogMessage("%ld connection(s) to exit",m_maxConnections);
2804f77d
VZ
283 }
284
f919ae34
VZ
285 long port;
286 if (pParser.Found("p", &port))
2804f77d 287 {
f919ae34
VZ
288 if ( port <= 0 || port > USHRT_MAX )
289 {
290 wxLogError("Invalid port number %ld, must be in 0..%u range.",
291 port, USHRT_MAX);
292 return false;
293 }
294
295 m_port = static_cast<unsigned short>(port);
296 wxLogMessage("Will listen on port %u", m_port);
2804f77d
VZ
297 }
298
e2bcc781 299 if (pParser.Found("t"))
2804f77d 300 m_workMode = THREADS;
e2bcc781 301 else if (pParser.Found("e"))
2804f77d
VZ
302 m_workMode = EVENTS;
303 else
304 m_workMode = MIXED;
305
306 return wxApp::OnCmdLineParsed(pParser);
307}
308
309bool Server::OnInit()
310{
311 wxLog* logger = new wxLogStderr();
312 wxLog::SetActiveTarget(logger);
313
314 m_port = 3000;
315
316 //send interesting things to console
317 if (!wxApp::OnInit())
318 return false;
319
320 //setup listening socket
321 wxIPV4address la;
322 la.Service(m_port);
323 m_listeningSocket = new wxSocketServer(la,wxSOCKET_NOWAIT|wxSOCKET_REUSEADDR);
324 m_listeningSocket->SetEventHandler(*this);
325 m_listeningSocket->SetNotify(wxSOCKET_CONNECTION_FLAG);
326 m_listeningSocket->Notify(true);
a1b806b9 327 if (!m_listeningSocket->IsOk())
2804f77d 328 {
e2bcc781 329 wxLogError("Cannot bind listening socket");
2804f77d
VZ
330 return false;
331 }
332
333 m_threadWorkersCreated = 0;
334 m_threadWorkersDone = 0;
335 m_threadWorkersFailed = 0;
336 m_maxThreadWorkers = 0;
337
338 m_eventWorkersCreated = 0;
339 m_eventWorkersDone = 0;
340 m_eventWorkersFailed = 0;
341 m_maxEventWorkers = 0;
342
f919ae34 343 wxLogMessage("Server listening at port %u, waiting for connections", m_port);
2804f77d
VZ
344 return true;
345}
346
347
348int Server::OnExit()
349{
e2bcc781
VZ
350 for ( TList::compatibility_iterator it = m_threadWorkers.GetFirst();
351 it;
352 it = it->GetNext() )
353 {
2804f77d
VZ
354 it->GetData()->Wait();
355 delete it->GetData();
356 }
357
e2bcc781
VZ
358 for ( EList::compatibility_iterator it2 = m_eventWorkers.GetFirst();
359 it2;
360 it2->GetNext() )
361 {
45a50a2e 362 delete it2->GetData();
2804f77d
VZ
363 }
364
365 m_threadWorkers.Clear();
366 m_eventWorkers.Clear();
367 m_listeningSocket->Destroy();
368 return 0;
369}
370
371void Server::OnSocketEvent(wxSocketEvent& pEvent)
372{
373 switch(pEvent.GetSocketEvent())
374 {
375 case wxSOCKET_INPUT:
e2bcc781
VZ
376 wxLogError("Unexpected wxSOCKET_INPUT in wxSocketServer");
377 break;
2804f77d 378 case wxSOCKET_OUTPUT:
e2bcc781 379 wxLogError("Unexpected wxSOCKET_OUTPUT in wxSocketServer");
2804f77d
VZ
380 break;
381 case wxSOCKET_CONNECTION:
382 {
383 wxSocketBase* sock = m_listeningSocket->Accept();
384 wxIPV4address addr;
385 if (!sock->GetPeer(addr))
386 {
e2bcc781 387 wxLogError("Server: cannot get peer info");
2804f77d 388 } else {
e2bcc781 389 wxLogMessage("Got connection from %s:%d",addr.IPAddress().c_str(), addr.Service());
2804f77d
VZ
390 }
391 bool createThread;
392
393 if (m_workMode != MIXED)
394 createThread = m_workMode == THREADS;
395 else
396 createThread = (wxDateTime::Now().GetSecond())%2 == 0;
397
398 if (createThread)
399 {
400 ThreadWorker* c = new ThreadWorker(sock);
401 if (c->Create() == wxTHREAD_NO_ERROR)
402 {
403 m_threadWorkers.Append(c);
404 if (m_threadWorkers.GetCount() > m_maxThreadWorkers)
405 m_maxThreadWorkers++;
406 m_threadWorkersCreated++;
407 c->Run();
408 }
409 else
410 {
e2bcc781 411 wxLogError("Server: cannot create next thread (current threads: %d", m_threadWorkers.size());
2804f77d
VZ
412 };
413 }
414 else
415 {
416 EventWorker* w = new EventWorker(sock);
417 m_eventWorkers.Append(w);
418 if (m_eventWorkers.GetCount() > m_maxEventWorkers)
419 m_maxEventWorkers++;
420 m_eventWorkersCreated++;
421 }
422 }
423 break;
424 case wxSOCKET_LOST:
e2bcc781 425 wxLogError("Unexpected wxSOCKET_LOST in wxSocketServer");
2804f77d
VZ
426 break;
427 }
428}
429
430void Server::OnWorkerEvent(WorkerEvent& pEvent)
431{
e2bcc781 432 //wxLogMessage("Got worker event");
2804f77d
VZ
433 for(TList::compatibility_iterator it = m_threadWorkers.GetFirst(); it ; it = it->GetNext())
434 {
435 if (it->GetData() == pEvent.m_sender)
436 {
3ecb4c01
VZ
437 wxLogVerbose("Deleting thread worker (%lu left)",
438 static_cast<unsigned long>( m_threadWorkers.GetCount() ));
2804f77d
VZ
439 it->GetData()->Wait();
440 delete it->GetData();
441 m_threadWorkers.DeleteNode(it);
442 if (!pEvent.m_workerFailed)
443 m_threadWorkersDone++;
444 else
445 m_threadWorkersFailed++;
446 break;
447 }
448 }
45a50a2e 449 for(EList::compatibility_iterator it2 = m_eventWorkers.GetFirst(); it2 ; it2 = it2->GetNext())
2804f77d 450 {
45a50a2e 451 if (it2->GetData() == pEvent.m_sender)
2804f77d 452 {
3ecb4c01
VZ
453 wxLogVerbose("Deleting event worker (%lu left)",
454 static_cast<unsigned long>( m_eventWorkers.GetCount() ));
45a50a2e
VZ
455 delete it2->GetData();
456 m_eventWorkers.DeleteNode(it2);
2804f77d
VZ
457 if (!pEvent.m_workerFailed)
458 m_eventWorkersDone++;
459 else
460 m_eventWorkersFailed++;
461 break;
462 }
463 }
464
465 if (m_eventWorkers.GetCount() == 0 && m_threadWorkers.GetCount() == 0)
466 {
467 mTimer.Start(1000,true);
468 }
469}
470
471void Server::OnTimerEvent(wxTimerEvent&)
472{
473 DumpStatistics();
474}
475
476
477BEGIN_EVENT_TABLE(Server,wxEvtHandler)
478 EVT_SOCKET(wxID_ANY,Server::OnSocketEvent)
479 EVT_WORKER(Server::OnWorkerEvent)
480 EVT_TIMER(wxID_ANY,Server::OnTimerEvent)
481END_EVENT_TABLE()
482
483
484ThreadWorker::ThreadWorker(wxSocketBase* pSocket) : wxThread(wxTHREAD_JOINABLE)
485{
486 m_socket = pSocket;
487 //Notify() cannot be called in thread context. We have to detach from main loop
488 //before switching thread contexts.
489 m_socket->Notify(false);
490 m_socket->SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
491 pSocket->GetPeer(m_peer);
492}
493
494wxThread::ExitCode ThreadWorker::Entry()
495{
496 WorkerEvent e(this);
497 if (!m_socket->IsConnected())
498 {
c3bcb48f 499 LogWorker("ThreadWorker: not connected",wxLOG_Error);
2804f77d
VZ
500 return 0;
501 }
502 int to_process = -1;
503 if (m_socket->IsConnected())
504 {
505 unsigned char signature[2];
c3bcb48f 506 LogWorker("ThreadWorker: reading for data");
2804f77d
VZ
507 to_process = 2;
508 do
509 {
510 m_socket->Read(&signature,to_process);
511 if (m_socket->Error())
512 {
c3bcb48f 513 LogWorker("ThreadWorker: Read error",wxLOG_Error);
2804f77d
VZ
514 wxGetApp().AddPendingEvent(e);
515 return 0;
516 }
517 to_process -= m_socket->LastCount();
c3bcb48f 518 LogWorker(wxString::Format("to_process: %d",to_process));
2804f77d
VZ
519
520 }
521 while (!m_socket->Error() && to_process != 0);
522
523 if (signature[0] == 0)
524 {
525 e.m_exit = true;
526 return 0;
527 }
528
529 if (signature[0] == 0xCE)
530 {
c3bcb48f 531 LogWorker("This server does not support test2 from GUI client",wxLOG_Error);
2804f77d
VZ
532 e.m_workerFailed = true;
533 e.m_exit = true;
534 return 0;
535 }
536 int size = signature[1] * (signature[0] == 0xBE ? 1 : 1024);
537 char* buf = new char[size];
c3bcb48f 538 LogWorker(wxString::Format("Message signature: chunks: %d, kilobytes: %d, size: %d (bytes)",signature[0],signature[1],size));
2804f77d
VZ
539
540 to_process = size;
c3bcb48f 541 LogWorker(wxString::Format("ThreadWorker: reading %d bytes of data",to_process));
2804f77d
VZ
542
543 do
544 {
545 m_socket->Read(buf,to_process);
546 if (m_socket->Error())
547 {
c3bcb48f 548 LogWorker("ThreadWorker: Read error",wxLOG_Error);
2804f77d
VZ
549 wxGetApp().AddPendingEvent(e);
550 return 0;
551 }
552 to_process -= m_socket->LastCount();
c3bcb48f 553 LogWorker(wxString::Format("ThreadWorker: %d bytes readed, %d todo",m_socket->LastCount(),to_process));
2804f77d
VZ
554
555 }
556 while(!m_socket->Error() && to_process != 0);
557
558 to_process = size;
559
560 do
561 {
562 m_socket->Write(buf,to_process);
e2bcc781
VZ
563 if (m_socket->Error())
564 {
c3bcb48f 565 LogWorker("ThreadWorker: Write error",wxLOG_Error);
2804f77d 566 break;
e2bcc781 567 }
2804f77d 568 to_process -= m_socket->LastCount();
c3bcb48f 569 LogWorker(wxString::Format("ThreadWorker: %d bytes written, %d todo",m_socket->LastCount(),to_process));
2804f77d
VZ
570 }
571 while(!m_socket->Error() && to_process != 0);
572 }
573
c3bcb48f 574 LogWorker("ThreadWorker: done");
2804f77d
VZ
575 e.m_workerFailed = to_process != 0;
576 m_socket->Destroy();
577 wxGetApp().AddPendingEvent(e);
578 return 0;
579}
580
581EventWorker::EventWorker(wxSocketBase* pSock)
582 : m_socket(pSock),
583 m_inbuf(NULL),
584 m_infill(0),
585 m_outbuf(NULL),
586 m_outfill(0)
587{
588 m_socket->SetNotify(wxSOCKET_LOST_FLAG|wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG);
589 m_socket->Notify(true);
590 m_socket->SetEventHandler(*this);
591 m_socket->SetFlags(wxSOCKET_NOWAIT);
592 m_socket->GetPeer(m_peer);
593}
594
e2bcc781
VZ
595EventWorker::~EventWorker()
596{
2804f77d
VZ
597 m_socket->Destroy();
598 delete [] m_inbuf;
599 delete [] m_outbuf;
600}
601
602void
e2bcc781 603EventWorker::DoRead()
2804f77d
VZ
604{
605 if (m_inbuf == NULL)
606 {
607 //read message header
608 do
609 {
0f359e5c 610 m_socket->Read(m_signature + m_infill, 2 - m_infill);
e2bcc781
VZ
611 if (m_socket->Error())
612 {
2804f77d
VZ
613 if (m_socket->LastError() != wxSOCKET_WOULDBLOCK)
614 {
c3bcb48f 615 LogWorker(wxString::Format("Read error (%d): %s",m_socket->LastError(),GetSocketErrorMsg(m_socket->LastError())),wxLOG_Error);
2804f77d
VZ
616 m_socket->Close();
617 }
618 }
619 else
620 {
621 m_infill += m_socket->LastCount();
e2bcc781
VZ
622 if (m_infill == 2)
623 {
2804f77d
VZ
624 unsigned char chunks = m_signature[1];
625 unsigned char type = m_signature[0];
626 if (type == 0xCE)
627 {
c3bcb48f 628 LogWorker("This server does not support test2 from GUI client",wxLOG_Error);
2804f77d
VZ
629 m_written = -1; //wxSOCKET_LOST will interpret this as failure
630 m_socket->Close();
631 }
632 else if (type == 0xBE || type == 0xDE)
633 {
634 m_size = chunks * (type == 0xBE ? 1 : 1024);
635 m_inbuf = new char[m_size];
636 m_outbuf = new char[m_size];
637 m_infill = 0;
638 m_outfill = 0;
639 m_written = 0;
c3bcb48f 640 LogWorker(wxString::Format("Message signature: len: %d, type: %s, size: %d (bytes)",chunks,type == 0xBE ? "b" : "kB",m_size));
2804f77d 641 break;
0f359e5c
VZ
642 }
643 else
2804f77d 644 {
c3bcb48f 645 LogWorker(wxString::Format("Unknown test type %x",type));
2804f77d
VZ
646 m_socket->Close();
647 }
648 }
649 }
650 }
651 while(!m_socket->Error() && (2 - m_infill != 0));
652 }
653
654 if (m_inbuf == NULL)
655 return;
656 //read message data
e2bcc781
VZ
657 do
658 {
659 if (m_size == m_infill)
660 {
2804f77d 661 m_signature[0] = m_signature[1] = 0x0;
5276b0a5 662 wxDELETEA(m_inbuf);
2804f77d
VZ
663 m_infill = 0;
664 return;
665 }
666 m_socket->Read(m_inbuf + m_infill,m_size - m_infill);
e2bcc781
VZ
667 if (m_socket->Error())
668 {
2804f77d
VZ
669 if (m_socket->LastError() != wxSOCKET_WOULDBLOCK)
670 {
c3bcb48f
VZ
671 LogWorker(wxString::Format("Read error (%d): %s",
672 m_socket->LastError(),
673 GetSocketErrorMsg(m_socket->LastError())),
674 wxLOG_Error);
2804f77d
VZ
675
676 m_socket->Close();
677 }
678 }
679 else
680 {
681 memcpy(m_outbuf+m_outfill,m_inbuf+m_infill,m_socket->LastCount());
682 m_infill += m_socket->LastCount();
683 m_outfill += m_socket->LastCount();
684 DoWrite();
685 }
686 }
687 while(!m_socket->Error());
688};
689
690void EventWorker::OnSocketEvent(wxSocketEvent& pEvent)
691{
692 switch(pEvent.GetSocketEvent())
693 {
694 case wxSOCKET_INPUT:
695 DoRead();
e2bcc781
VZ
696 break;
697
2804f77d 698 case wxSOCKET_OUTPUT:
7ea70b6f 699 if ( m_outbuf )
2804f77d 700 DoWrite();
e2bcc781
VZ
701 break;
702
2804f77d 703 case wxSOCKET_CONNECTION:
7ea70b6f 704 LogWorker("Unexpected wxSOCKET_CONNECTION in EventWorker", wxLOG_Error);
e2bcc781
VZ
705 break;
706
2804f77d 707 case wxSOCKET_LOST:
e2bcc781 708 {
7ea70b6f 709 LogWorker("Connection lost");
e2bcc781
VZ
710 WorkerEvent e(this);
711 e.m_workerFailed = m_written != m_size;
712 wxGetApp().AddPendingEvent(e);
713 }
714 break;
2804f77d
VZ
715 }
716}
717
e2bcc781
VZ
718void EventWorker::DoWrite()
719{
720 do
721 {
2804f77d
VZ
722 if (m_written == m_size)
723 {
5276b0a5 724 wxDELETEA(m_outbuf);
e2bcc781 725 m_outfill = 0;
c3bcb48f 726 LogWorker( "All data written");
e2bcc781 727 return;
2804f77d
VZ
728 }
729 if (m_outfill - m_written == 0)
730 {
731 return;
732 }
733 m_socket->Write(m_outbuf + m_written,m_outfill - m_written);
734 if (m_socket->Error())
735 {
736 if (m_socket->LastError() != wxSOCKET_WOULDBLOCK)
737 {
c3bcb48f 738 LogWorker(
e2bcc781 739 wxString::Format("Write error (%d): %s",
2804f77d
VZ
740 m_socket->LastError(),
741 GetSocketErrorMsg(m_socket->LastError())
742 )
e2bcc781 743 ,wxLOG_Error
2804f77d
VZ
744 );
745 m_socket->Close();
746 }
747 else
748 {
c3bcb48f 749 LogWorker("Write would block, waiting for OUTPUT event");
2804f77d
VZ
750 }
751 }
752 else
753 {
754 memmove(m_outbuf,m_outbuf+m_socket->LastCount(),m_outfill-m_socket->LastCount());
755 m_written += m_socket->LastCount();
756 }
c3bcb48f
VZ
757 LogWorker(wxString::Format("Written %d of %d bytes, todo %d",
758 m_socket->LastCount(),m_size,m_size - m_written));
2804f77d
VZ
759 }
760 while (!m_socket->Error());
761}
762
763BEGIN_EVENT_TABLE(EventWorker,wxEvtHandler)
e2bcc781 764 EVT_SOCKET(wxID_ANY,EventWorker::OnSocketEvent)
2804f77d 765END_EVENT_TABLE()