]> git.saurik.com Git - wxWidgets.git/blob - samples/sockets/server.cpp
Shorten lang names in wxTranslations, not wxFileTranslationsLoader.
[wxWidgets.git] / samples / sockets / server.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: server.cpp
3 // Purpose: Server for wxSocket demo
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 // Created: 1999/09/19
6 // RCS-ID: $Id$
7 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
8 // (c) 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ==========================================================================
13 // declarations
14 // ==========================================================================
15
16 // --------------------------------------------------------------------------
17 // headers
18 // --------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 # pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 # include "wx/wx.h"
30 #endif
31
32 #include "wx/busyinfo.h"
33 #include "wx/socket.h"
34
35 // this example is currently written to use only IP or only IPv6 sockets, it
36 // should be extended to allow using either in the future
37 #if wxUSE_IPV6
38 typedef wxIPV6address IPaddress;
39 #else
40 typedef wxIPV4address IPaddress;
41 #endif
42
43 // --------------------------------------------------------------------------
44 // resources
45 // --------------------------------------------------------------------------
46
47 // the application icon
48 #include "mondrian.xpm"
49
50 // --------------------------------------------------------------------------
51 // classes
52 // --------------------------------------------------------------------------
53
54 // Define a new application type
55 class MyApp : public wxApp
56 {
57 public:
58 virtual bool OnInit();
59 };
60
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame : public wxFrame
63 {
64 public:
65 MyFrame();
66 ~MyFrame();
67
68 // event handlers (these functions should _not_ be virtual)
69 void OnUDPTest(wxCommandEvent& event);
70 void OnWaitForAccept(wxCommandEvent& event);
71 void OnQuit(wxCommandEvent& event);
72 void OnAbout(wxCommandEvent& event);
73 void OnServerEvent(wxSocketEvent& event);
74 void OnSocketEvent(wxSocketEvent& event);
75
76 void Test1(wxSocketBase *sock);
77 void Test2(wxSocketBase *sock);
78 void Test3(wxSocketBase *sock);
79
80 // convenience functions
81 void UpdateStatusBar();
82
83 private:
84 wxSocketServer *m_server;
85 wxTextCtrl *m_text;
86 wxMenu *m_menuFile;
87 wxMenuBar *m_menuBar;
88 bool m_busy;
89 int m_numClients;
90
91 // any class wishing to process wxWidgets events must use this macro
92 DECLARE_EVENT_TABLE()
93 };
94
95 // simple helper class to log start and end of each test
96 class TestLogger
97 {
98 public:
99 TestLogger(const wxString& name) : m_name(name)
100 {
101 wxLogMessage("=== %s begins ===", m_name);
102 }
103
104 ~TestLogger()
105 {
106 wxLogMessage("=== %s ends ===", m_name);
107 }
108
109 private:
110 const wxString m_name;
111 };
112
113 // --------------------------------------------------------------------------
114 // constants
115 // --------------------------------------------------------------------------
116
117 // IDs for the controls and the menu commands
118 enum
119 {
120 // menu items
121 SERVER_UDPTEST = 10,
122 SERVER_WAITFORACCEPT,
123 SERVER_QUIT = wxID_EXIT,
124 SERVER_ABOUT = wxID_ABOUT,
125
126 // id for sockets
127 SERVER_ID = 100,
128 SOCKET_ID
129 };
130
131 // --------------------------------------------------------------------------
132 // event tables and other macros for wxWidgets
133 // --------------------------------------------------------------------------
134
135 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
136 EVT_MENU(SERVER_QUIT, MyFrame::OnQuit)
137 EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout)
138 EVT_MENU(SERVER_UDPTEST, MyFrame::OnUDPTest)
139 EVT_MENU(SERVER_WAITFORACCEPT, MyFrame::OnWaitForAccept)
140 EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent)
141 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
142 END_EVENT_TABLE()
143
144 IMPLEMENT_APP(MyApp)
145
146
147 // ==========================================================================
148 // implementation
149 // ==========================================================================
150
151 // --------------------------------------------------------------------------
152 // the application class
153 // --------------------------------------------------------------------------
154
155 bool MyApp::OnInit()
156 {
157 if ( !wxApp::OnInit() )
158 return false;
159
160 // Create the main application window
161 MyFrame *frame = new MyFrame();
162
163 // Show it and tell the application that it's our main window
164 frame->Show(true);
165 SetTopWindow(frame);
166
167 // Success
168 return true;
169 }
170
171 // --------------------------------------------------------------------------
172 // main frame
173 // --------------------------------------------------------------------------
174
175 // frame constructor
176
177 MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
178 _("wxSocket demo: Server"),
179 wxDefaultPosition, wxSize(300, 200))
180 {
181 // Give the frame an icon
182 SetIcon(wxICON(mondrian));
183
184 // Make menus
185 m_menuFile = new wxMenu();
186 m_menuFile->Append(SERVER_WAITFORACCEPT, "&Wait for connection\tCtrl-W");
187 m_menuFile->Append(SERVER_UDPTEST, "&UDP test\tCtrl-U");
188 m_menuFile->AppendSeparator();
189 m_menuFile->Append(SERVER_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
190 m_menuFile->AppendSeparator();
191 m_menuFile->Append(SERVER_QUIT, _("E&xit\tAlt-X"), _("Quit server"));
192
193 // Append menus to the menubar
194 m_menuBar = new wxMenuBar();
195 m_menuBar->Append(m_menuFile, _("&File"));
196 SetMenuBar(m_menuBar);
197
198 #if wxUSE_STATUSBAR
199 // Status bar
200 CreateStatusBar(2);
201 #endif // wxUSE_STATUSBAR
202
203 // Make a textctrl for logging
204 m_text = new wxTextCtrl(this, wxID_ANY,
205 _("Welcome to wxSocket demo: Server\n"),
206 wxDefaultPosition, wxDefaultSize,
207 wxTE_MULTILINE | wxTE_READONLY);
208 delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text));
209
210 // Create the address - defaults to localhost:0 initially
211 IPaddress addr;
212 addr.Service(3000);
213
214 wxLogMessage("Creating server at %s:%u", addr.IPAddress(), addr.Service());
215
216 // Create the socket
217 m_server = new wxSocketServer(addr);
218
219 // We use Ok() here to see if the server is really listening
220 if (! m_server->Ok())
221 {
222 wxLogMessage("Could not listen at the specified port !");
223 return;
224 }
225
226 IPaddress addrReal;
227 if ( !m_server->GetLocal(addrReal) )
228 {
229 wxLogMessage("ERROR: couldn't get the address we bound to");
230 }
231 else
232 {
233 wxLogMessage("Server listening at %s:%u",
234 addrReal.IPAddress(), addrReal.Service());
235 }
236
237 // Setup the event handler and subscribe to connection events
238 m_server->SetEventHandler(*this, SERVER_ID);
239 m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
240 m_server->Notify(true);
241
242 m_busy = false;
243 m_numClients = 0;
244 UpdateStatusBar();
245 }
246
247 MyFrame::~MyFrame()
248 {
249 // No delayed deletion here, as the frame is dying anyway
250 delete m_server;
251 }
252
253 // event handlers
254
255 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
256 {
257 // true is to force the frame to close
258 Close(true);
259 }
260
261 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
262 {
263 wxMessageBox(_("wxSocket demo: Server\n(c) 1999 Guillermo Rodriguez Garcia\n"),
264 _("About Server"),
265 wxOK | wxICON_INFORMATION, this);
266 }
267
268 void MyFrame::OnUDPTest(wxCommandEvent& WXUNUSED(event))
269 {
270 TestLogger logtest("UDP test");
271
272 IPaddress addr;
273 addr.Service(3000);
274 wxDatagramSocket sock(addr);
275
276 char buf[1024];
277 size_t n = sock.RecvFrom(addr, buf, sizeof(buf)).LastCount();
278 if ( !n )
279 {
280 wxLogMessage("ERROR: failed to receive data");
281 return;
282 }
283
284 wxLogMessage("Received \"%s\" from %s:%u.",
285 wxString::From8BitData(buf, n),
286 addr.IPAddress(), addr.Service());
287
288 for ( size_t i = 0; i < n; i++ )
289 {
290 char& c = buf[i];
291 if ( (c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'm') )
292 c += 13;
293 else if ( (c >= 'N' && c <= 'Z') || (c >= 'n' && c <= 'z') )
294 c -= 13;
295 }
296
297 if ( sock.SendTo(addr, buf, n).LastCount() != n )
298 {
299 wxLogMessage("ERROR: failed to send data");
300 return;
301 }
302 }
303
304 void MyFrame::OnWaitForAccept(wxCommandEvent& WXUNUSED(event))
305 {
306 TestLogger logtest("WaitForAccept() test");
307
308 wxBusyInfo("Waiting for connection for 10 seconds...", this);
309 if ( m_server->WaitForAccept(10) )
310 wxLogMessage("Accepted client connection.");
311 else
312 wxLogMessage("Connection error or timeout expired.");
313 }
314
315 void MyFrame::Test1(wxSocketBase *sock)
316 {
317 TestLogger logtest("Test 1");
318
319 // Receive data from socket and send it back. We will first
320 // get a byte with the buffer size, so we can specify the
321 // exact size and use the wxSOCKET_WAITALL flag. Also, we
322 // disabled input events so we won't have unwanted reentrance.
323 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
324
325 sock->SetFlags(wxSOCKET_WAITALL);
326
327 // Read the size
328 unsigned char len;
329 sock->Read(&len, 1);
330 wxCharBuffer buf(len);
331
332 // Read the data
333 sock->Read(buf.data(), len);
334 wxLogMessage("Got the data, sending it back");
335
336 // Write it back
337 sock->Write(buf, len);
338 }
339
340 void MyFrame::Test2(wxSocketBase *sock)
341 {
342 char buf[4096];
343
344 TestLogger logtest("Test 2");
345
346 // We don't need to set flags because ReadMsg and WriteMsg
347 // are not affected by them anyway.
348
349 // Read the message
350 wxUint32 len = sock->ReadMsg(buf, sizeof(buf)).LastCount();
351 if ( !len )
352 {
353 wxLogError("Failed to read message.");
354 return;
355 }
356
357 wxLogMessage("Got \"%s\" from client.", wxString::FromUTF8(buf, len));
358 wxLogMessage("Sending the data back");
359
360 // Write it back
361 sock->WriteMsg(buf, len);
362 }
363
364 void MyFrame::Test3(wxSocketBase *sock)
365 {
366 TestLogger logtest("Test 3");
367
368 // This test is similar to the first one, but the len is
369 // expressed in kbytes - this tests large data transfers.
370
371 sock->SetFlags(wxSOCKET_WAITALL);
372
373 // Read the size
374 unsigned char len;
375 sock->Read(&len, 1);
376 wxCharBuffer buf(len*1024);
377
378 // Read the data
379 sock->Read(buf.data(), len * 1024);
380 wxLogMessage("Got the data, sending it back");
381
382 // Write it back
383 sock->Write(buf, len * 1024);
384 }
385
386 void MyFrame::OnServerEvent(wxSocketEvent& event)
387 {
388 wxString s = _("OnServerEvent: ");
389 wxSocketBase *sock;
390
391 switch(event.GetSocketEvent())
392 {
393 case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break;
394 default : s.Append(_("Unexpected event !\n")); break;
395 }
396
397 m_text->AppendText(s);
398
399 // Accept new connection if there is one in the pending
400 // connections queue, else exit. We use Accept(false) for
401 // non-blocking accept (although if we got here, there
402 // should ALWAYS be a pending connection).
403
404 sock = m_server->Accept(false);
405
406 if (sock)
407 {
408 IPaddress addr;
409 if ( !sock->GetPeer(addr) )
410 {
411 wxLogMessage("New connection from unknown client accepted.");
412 }
413 else
414 {
415 wxLogMessage("New client connection from %s:%u accepted",
416 addr.IPAddress(), addr.Service());
417 }
418 }
419 else
420 {
421 wxLogMessage("Error: couldn't accept a new connection");
422 return;
423 }
424
425 sock->SetEventHandler(*this, SOCKET_ID);
426 sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
427 sock->Notify(true);
428
429 m_numClients++;
430 UpdateStatusBar();
431 }
432
433 void MyFrame::OnSocketEvent(wxSocketEvent& event)
434 {
435 wxString s = _("OnSocketEvent: ");
436 wxSocketBase *sock = event.GetSocket();
437
438 // First, print a message
439 switch(event.GetSocketEvent())
440 {
441 case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break;
442 case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break;
443 default : s.Append(_("Unexpected event !\n")); break;
444 }
445
446 m_text->AppendText(s);
447
448 // Now we process the event
449 switch(event.GetSocketEvent())
450 {
451 case wxSOCKET_INPUT:
452 {
453 // We disable input events, so that the test doesn't trigger
454 // wxSocketEvent again.
455 sock->SetNotify(wxSOCKET_LOST_FLAG);
456
457 // Which test are we going to run?
458 unsigned char c;
459 sock->Read(&c, 1);
460
461 switch (c)
462 {
463 case 0xBE: Test1(sock); break;
464 case 0xCE: Test2(sock); break;
465 case 0xDE: Test3(sock); break;
466 default:
467 wxLogMessage("Unknown test id received from client");
468 }
469
470 // Enable input events again.
471 sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
472 break;
473 }
474 case wxSOCKET_LOST:
475 {
476 m_numClients--;
477
478 // Destroy() should be used instead of delete wherever possible,
479 // due to the fact that wxSocket uses 'delayed events' (see the
480 // documentation for wxPostEvent) and we don't want an event to
481 // arrive to the event handler (the frame, here) after the socket
482 // has been deleted. Also, we might be doing some other thing with
483 // the socket at the same time; for example, we might be in the
484 // middle of a test or something. Destroy() takes care of all
485 // this for us.
486
487 wxLogMessage("Deleting socket.");
488 sock->Destroy();
489 break;
490 }
491 default: ;
492 }
493
494 UpdateStatusBar();
495 }
496
497 // convenience functions
498
499 void MyFrame::UpdateStatusBar()
500 {
501 #if wxUSE_STATUSBAR
502 wxString s;
503 s.Printf(_("%d clients connected"), m_numClients);
504 SetStatusText(s, 1);
505 #endif // wxUSE_STATUSBAR
506 }