]>
Commit | Line | Data |
---|---|---|
f85d901f GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: client.cpp | |
3 | // Purpose: Client for wxSocket demo | |
4 | // Author: Guillermo Rodriguez Garcia <guille@iies.es> | |
5 | // Modified by: | |
6 | // Created: 1999/09/19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Guillermo Rodriguez Garcia | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ========================================================================== | |
13 | // declarations | |
14 | // ========================================================================== | |
15 | ||
16 | // -------------------------------------------------------------------------- | |
17 | // headers | |
18 | // -------------------------------------------------------------------------- | |
e2a6f233 | 19 | |
f85d901f | 20 | // For compilers that support precompilation, includes "wx/wx.h". |
f4ada568 GL |
21 | #include "wx/wxprec.h" |
22 | ||
23 | #ifdef __BORLANDC__ | |
f85d901f | 24 | # pragma hdrstop |
f4ada568 GL |
25 | #endif |
26 | ||
925e9792 | 27 | // for all others, include the necessary headers |
f4ada568 | 28 | #ifndef WX_PRECOMP |
f85d901f | 29 | # include "wx/wx.h" |
09fb22cf JS |
30 | #endif |
31 | ||
6097c3a2 GRG |
32 | #include "wx/socket.h" |
33 | #include "wx/url.h" | |
1e4080f0 | 34 | #include "wx/sstream.h" |
1e4080f0 | 35 | #include <memory> |
e2a6f233 | 36 | |
f85d901f GRG |
37 | // -------------------------------------------------------------------------- |
38 | // resources | |
39 | // -------------------------------------------------------------------------- | |
f4ada568 | 40 | |
f85d901f | 41 | // the application icon |
3cb332c1 VZ |
42 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
43 | #include "../sample.xpm" | |
44 | #endif | |
e2a6f233 | 45 | |
f85d901f GRG |
46 | // -------------------------------------------------------------------------- |
47 | // classes | |
48 | // -------------------------------------------------------------------------- | |
f4ada568 | 49 | |
f85d901f GRG |
50 | // Define a new application type |
51 | class MyApp : public wxApp | |
52 | { | |
f4ada568 | 53 | public: |
f85d901f | 54 | virtual bool OnInit(); |
f4ada568 GL |
55 | }; |
56 | ||
f85d901f GRG |
57 | // Define a new frame type: this is going to be our main frame |
58 | class MyFrame : public wxFrame | |
f4ada568 GL |
59 | { |
60 | public: | |
f85d901f GRG |
61 | MyFrame(); |
62 | ~MyFrame(); | |
63 | ||
25ba2b89 | 64 | // event handlers for File menu |
f85d901f GRG |
65 | void OnQuit(wxCommandEvent& event); |
66 | void OnAbout(wxCommandEvent& event); | |
25ba2b89 GRG |
67 | |
68 | // event handlers for Socket menu | |
f85d901f GRG |
69 | void OnOpenConnection(wxCommandEvent& event); |
70 | void OnTest1(wxCommandEvent& event); | |
71 | void OnTest2(wxCommandEvent& event); | |
72 | void OnTest3(wxCommandEvent& event); | |
73 | void OnCloseConnection(wxCommandEvent& event); | |
25ba2b89 | 74 | |
16cafacf | 75 | #if wxUSE_URL |
25ba2b89 GRG |
76 | // event handlers for Protocols menu |
77 | void OnTestURL(wxCommandEvent& event); | |
16cafacf | 78 | #endif |
8575ff50 VZ |
79 | #if wxUSE_IPV6 |
80 | void OnOpenConnectionIPv6(wxCommandEvent& event); | |
81 | #endif | |
82 | ||
778f682e | 83 | void OpenConnection(wxSockAddress::Family family); |
25ba2b89 | 84 | |
e51b0130 | 85 | // event handlers for DatagramSocket menu (stub) |
6097c3a2 | 86 | void OnDatagram(wxCommandEvent& event); |
f85d901f | 87 | |
25ba2b89 GRG |
88 | // socket event handler |
89 | void OnSocketEvent(wxSocketEvent& event); | |
90 | ||
f85d901f GRG |
91 | // convenience functions |
92 | void UpdateStatusBar(); | |
93 | ||
94 | private: | |
95 | wxSocketClient *m_sock; | |
f85d901f GRG |
96 | wxTextCtrl *m_text; |
97 | wxMenu *m_menuFile; | |
98 | wxMenu *m_menuSocket; | |
6097c3a2 | 99 | wxMenu *m_menuDatagramSocket; |
25ba2b89 | 100 | wxMenu *m_menuProtocols; |
f85d901f GRG |
101 | wxMenuBar *m_menuBar; |
102 | bool m_busy; | |
103 | ||
be5a51fb | 104 | // any class wishing to process wxWidgets events must use this macro |
f85d901f | 105 | DECLARE_EVENT_TABLE() |
f4ada568 GL |
106 | }; |
107 | ||
f98f168e VZ |
108 | // simple helper class to log start and end of each test |
109 | class TestLogger | |
110 | { | |
111 | public: | |
112 | TestLogger(const wxString& name) : m_name(name) | |
113 | { | |
114 | wxLogMessage("=== %s test begins ===", m_name); | |
115 | } | |
116 | ||
117 | ~TestLogger() | |
118 | { | |
119 | wxLogMessage("=== %s test ends ===", m_name); | |
120 | } | |
121 | ||
122 | private: | |
123 | const wxString m_name; | |
124 | }; | |
125 | ||
f85d901f GRG |
126 | // -------------------------------------------------------------------------- |
127 | // constants | |
128 | // -------------------------------------------------------------------------- | |
f4ada568 | 129 | |
f85d901f GRG |
130 | // IDs for the controls and the menu commands |
131 | enum | |
f4ada568 | 132 | { |
f85d901f | 133 | // menu items |
91b07357 JS |
134 | CLIENT_QUIT = wxID_EXIT, |
135 | CLIENT_ABOUT = wxID_ABOUT, | |
136 | CLIENT_OPEN = 100, | |
8575ff50 VZ |
137 | #if wxUSE_IPV6 |
138 | CLIENT_OPENIPV6, | |
139 | #endif | |
f85d901f GRG |
140 | CLIENT_TEST1, |
141 | CLIENT_TEST2, | |
142 | CLIENT_TEST3, | |
143 | CLIENT_CLOSE, | |
16cafacf | 144 | #if wxUSE_URL |
25ba2b89 | 145 | CLIENT_TESTURL, |
16cafacf | 146 | #endif |
6097c3a2 | 147 | CLIENT_DGRAM, |
f85d901f GRG |
148 | |
149 | // id for socket | |
150 | SOCKET_ID | |
151 | }; | |
f4ada568 | 152 | |
f85d901f | 153 | // -------------------------------------------------------------------------- |
be5a51fb | 154 | // event tables and other macros for wxWidgets |
f85d901f | 155 | // -------------------------------------------------------------------------- |
f4ada568 | 156 | |
f85d901f | 157 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
25ba2b89 GRG |
158 | EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit) |
159 | EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout) | |
160 | EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection) | |
8575ff50 VZ |
161 | #if wxUSE_IPV6 |
162 | EVT_MENU(CLIENT_OPENIPV6, MyFrame::OnOpenConnectionIPv6) | |
163 | #endif | |
25ba2b89 GRG |
164 | EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1) |
165 | EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2) | |
166 | EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3) | |
167 | EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection) | |
168 | EVT_MENU(CLIENT_DGRAM, MyFrame::OnDatagram) | |
16cafacf | 169 | #if wxUSE_URL |
25ba2b89 | 170 | EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL) |
16cafacf | 171 | #endif |
25ba2b89 | 172 | EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) |
f85d901f | 173 | END_EVENT_TABLE() |
f4ada568 | 174 | |
f85d901f | 175 | IMPLEMENT_APP(MyApp) |
f4ada568 | 176 | |
f85d901f GRG |
177 | // ========================================================================== |
178 | // implementation | |
179 | // ========================================================================== | |
f4ada568 | 180 | |
f85d901f GRG |
181 | // -------------------------------------------------------------------------- |
182 | // the application class | |
183 | // -------------------------------------------------------------------------- | |
f4ada568 | 184 | |
f85d901f GRG |
185 | bool MyApp::OnInit() |
186 | { | |
45e6e6f8 VZ |
187 | if ( !wxApp::OnInit() ) |
188 | return false; | |
189 | ||
f85d901f GRG |
190 | // Create the main application window |
191 | MyFrame *frame = new MyFrame(); | |
f4ada568 | 192 | |
f85d901f | 193 | // Show it and tell the application that it's our main window |
b62ca03d | 194 | frame->Show(true); |
f85d901f GRG |
195 | SetTopWindow(frame); |
196 | ||
197 | // success | |
b62ca03d | 198 | return true; |
f4ada568 GL |
199 | } |
200 | ||
f85d901f GRG |
201 | // -------------------------------------------------------------------------- |
202 | // main frame | |
203 | // -------------------------------------------------------------------------- | |
204 | ||
205 | // frame constructor | |
b62ca03d | 206 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, |
25ba2b89 | 207 | _("wxSocket demo: Client"), |
f85d901f | 208 | wxDefaultPosition, wxSize(300, 200)) |
f4ada568 | 209 | { |
f85d901f | 210 | // Give the frame an icon |
3cb332c1 | 211 | SetIcon(wxICON(sample)); |
f85d901f GRG |
212 | |
213 | // Make menus | |
214 | m_menuFile = new wxMenu(); | |
25ba2b89 | 215 | m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); |
f85d901f | 216 | m_menuFile->AppendSeparator(); |
25ba2b89 | 217 | m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client")); |
f85d901f GRG |
218 | |
219 | m_menuSocket = new wxMenu(); | |
9c22ef28 | 220 | m_menuSocket->Append(CLIENT_OPEN, _("&Open session\tCtrl-O"), _("Connect to server")); |
8575ff50 | 221 | #if wxUSE_IPV6 |
9c22ef28 | 222 | m_menuSocket->Append(CLIENT_OPENIPV6, _("&Open session(IPv6)\tShift-Ctrl-O"), _("Connect to server(IPv6)")); |
8575ff50 | 223 | #endif |
f85d901f | 224 | m_menuSocket->AppendSeparator(); |
9c22ef28 VZ |
225 | m_menuSocket->Append(CLIENT_TEST1, _("Test &1\tCtrl-F1"), _("Test basic functionality")); |
226 | m_menuSocket->Append(CLIENT_TEST2, _("Test &2\tCtrl-F2"), _("Test ReadMsg and WriteMsg")); | |
227 | m_menuSocket->Append(CLIENT_TEST3, _("Test &3\tCtrl-F3"), _("Test large data transfer")); | |
f85d901f | 228 | m_menuSocket->AppendSeparator(); |
9c22ef28 | 229 | m_menuSocket->Append(CLIENT_CLOSE, _("&Close session\tCtrl-Q"), _("Close connection")); |
f85d901f | 230 | |
6097c3a2 | 231 | m_menuDatagramSocket = new wxMenu(); |
46b11427 | 232 | m_menuDatagramSocket->Append(CLIENT_DGRAM, _("&Datagram test\tCtrl-D"), _("Test UDP sockets")); |
25ba2b89 | 233 | |
16cafacf | 234 | #if wxUSE_URL |
25ba2b89 | 235 | m_menuProtocols = new wxMenu(); |
1e4080f0 VZ |
236 | m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL\tCtrl-U"), |
237 | _("Get data from the specified URL")); | |
16cafacf | 238 | #endif |
6097c3a2 | 239 | |
f85d901f GRG |
240 | // Append menus to the menubar |
241 | m_menuBar = new wxMenuBar(); | |
25ba2b89 | 242 | m_menuBar->Append(m_menuFile, _("&File")); |
46b11427 VZ |
243 | m_menuBar->Append(m_menuSocket, _("&TCP")); |
244 | m_menuBar->Append(m_menuDatagramSocket, _("&UDP")); | |
16cafacf | 245 | #if wxUSE_URL |
25ba2b89 | 246 | m_menuBar->Append(m_menuProtocols, _("&Protocols")); |
16cafacf | 247 | #endif |
f85d901f GRG |
248 | SetMenuBar(m_menuBar); |
249 | ||
8520f137 | 250 | #if wxUSE_STATUSBAR |
f85d901f | 251 | // Status bar |
f4ada568 | 252 | CreateStatusBar(2); |
8520f137 | 253 | #endif // wxUSE_STATUSBAR |
f85d901f | 254 | |
bd4d918f | 255 | // Make a textctrl for logging |
b62ca03d | 256 | m_text = new wxTextCtrl(this, wxID_ANY, |
4693b20c | 257 | _("Welcome to wxSocket demo: Client\nClient ready\n"), |
bd4d918f | 258 | wxDefaultPosition, wxDefaultSize, |
f85d901f | 259 | wxTE_MULTILINE | wxTE_READONLY); |
1e4080f0 | 260 | delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text)); |
f85d901f GRG |
261 | |
262 | // Create the socket | |
263 | m_sock = new wxSocketClient(); | |
bd4d918f GRG |
264 | |
265 | // Setup the event handler and subscribe to most events | |
f85d901f GRG |
266 | m_sock->SetEventHandler(*this, SOCKET_ID); |
267 | m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG | | |
268 | wxSOCKET_INPUT_FLAG | | |
269 | wxSOCKET_LOST_FLAG); | |
b62ca03d | 270 | m_sock->Notify(true); |
f85d901f | 271 | |
b62ca03d | 272 | m_busy = false; |
f85d901f | 273 | UpdateStatusBar(); |
f4ada568 GL |
274 | } |
275 | ||
276 | MyFrame::~MyFrame() | |
277 | { | |
bd4d918f | 278 | // No delayed deletion here, as the frame is dying anyway |
f85d901f | 279 | delete m_sock; |
f4ada568 GL |
280 | } |
281 | ||
f85d901f GRG |
282 | // event handlers |
283 | ||
284 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 285 | { |
b62ca03d WS |
286 | // true is to force the frame to close |
287 | Close(true); | |
f4ada568 GL |
288 | } |
289 | ||
f85d901f GRG |
290 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
291 | { | |
4693b20c | 292 | wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"), |
25ba2b89 | 293 | _("About Client"), |
f85d901f GRG |
294 | wxOK | wxICON_INFORMATION, this); |
295 | } | |
296 | ||
297 | void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 298 | { |
778f682e | 299 | OpenConnection(wxSockAddress::IPV4); |
8575ff50 VZ |
300 | } |
301 | #if wxUSE_IPV6 | |
302 | void MyFrame::OnOpenConnectionIPv6(wxCommandEvent& WXUNUSED(event)) | |
303 | { | |
778f682e | 304 | OpenConnection(wxSockAddress::IPV6); |
8575ff50 VZ |
305 | } |
306 | #endif // wxUSE_IPV6 | |
307 | ||
778f682e | 308 | void MyFrame::OpenConnection(wxSockAddress::Family family) |
8575ff50 | 309 | { |
778f682e VZ |
310 | wxUnusedVar(family); // unused in !wxUSE_IPV6 case |
311 | ||
8575ff50 | 312 | wxIPaddress * addr; |
25248cb6 | 313 | wxIPV4address addr4; |
8575ff50 VZ |
314 | #if wxUSE_IPV6 |
315 | wxIPV6address addr6; | |
778f682e | 316 | if ( family == wxSockAddress::IPV6 ) |
25248cb6 VZ |
317 | addr = &addr6; |
318 | else | |
8575ff50 | 319 | #endif |
778f682e | 320 | addr = &addr4; |
f4ada568 | 321 | |
b62ca03d | 322 | m_menuSocket->Enable(CLIENT_OPEN, false); |
8575ff50 VZ |
323 | #if wxUSE_IPV6 |
324 | m_menuSocket->Enable(CLIENT_OPENIPV6, false); | |
325 | #endif | |
b62ca03d | 326 | m_menuSocket->Enable(CLIENT_CLOSE, false); |
f85d901f | 327 | |
25ba2b89 | 328 | // Ask user for server address |
f85d901f | 329 | wxString hostname = wxGetTextFromUser( |
25ba2b89 GRG |
330 | _("Enter the address of the wxSocket demo server:"), |
331 | _("Connect ..."), | |
332 | _("localhost")); | |
46b11427 VZ |
333 | if ( hostname.empty() ) |
334 | return; | |
f85d901f | 335 | |
8575ff50 VZ |
336 | addr->Hostname(hostname); |
337 | addr->Service(3000); | |
f4ada568 | 338 | |
b2f747d8 VZ |
339 | // we connect asynchronously and will get a wxSOCKET_CONNECTION event when |
340 | // the connection is really established | |
25ba2b89 | 341 | // |
b2f747d8 VZ |
342 | // if you want to make sure that connection is established right here you |
343 | // could call WaitOnConnect(timeout) instead | |
344 | wxLogMessage("Trying to connect to %s:%d", hostname, addr->Service()); | |
e51b0130 | 345 | |
8575ff50 | 346 | m_sock->Connect(*addr, false); |
f4ada568 GL |
347 | } |
348 | ||
f85d901f | 349 | void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 350 | { |
f85d901f | 351 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 352 | m_busy = true; |
f85d901f GRG |
353 | UpdateStatusBar(); |
354 | ||
25ba2b89 | 355 | m_text->AppendText(_("\n=== Test 1 begins ===\n")); |
f85d901f GRG |
356 | |
357 | // Tell the server which test we are running | |
f4d5e009 | 358 | unsigned char c = 0xBE; |
f85d901f GRG |
359 | m_sock->Write(&c, 1); |
360 | ||
361 | // Send some data and read it back. We know the size of the | |
362 | // buffer, so we can specify the exact number of bytes to be | |
6097c3a2 GRG |
363 | // sent or received and use the wxSOCKET_WAITALL flag. Also, |
364 | // we have disabled menu entries which could interfere with | |
365 | // the test, so we can safely avoid the wxSOCKET_BLOCK flag. | |
f85d901f GRG |
366 | // |
367 | // First we send a byte with the length of the string, then | |
368 | // we send the string itself (do NOT try to send any integral | |
6097c3a2 | 369 | // value larger than a byte "as is" across the network, or |
f85d901f GRG |
370 | // you might be in trouble! Ever heard about big and little |
371 | // endian computers?) | |
e51b0130 | 372 | |
f85d901f GRG |
373 | m_sock->SetFlags(wxSOCKET_WAITALL); |
374 | ||
9c22ef28 VZ |
375 | const char *buf1 = "Test string (less than 256 chars!)"; |
376 | unsigned char len = (unsigned char)(wxStrlen(buf1) + 1); | |
377 | wxCharBuffer buf2(wxStrlen(buf1)); | |
f85d901f | 378 | |
25ba2b89 | 379 | m_text->AppendText(_("Sending a test buffer to the server ...")); |
f187448d | 380 | m_sock->Write(&len, 1); |
f85d901f | 381 | m_sock->Write(buf1, len); |
25ba2b89 | 382 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 383 | |
25ba2b89 | 384 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
9c22ef28 | 385 | m_sock->Read(buf2.data(), len); |
25ba2b89 | 386 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 387 | |
25ba2b89 | 388 | m_text->AppendText(_("Comparing the two buffers ...")); |
f85d901f GRG |
389 | if (memcmp(buf1, buf2, len) != 0) |
390 | { | |
25ba2b89 GRG |
391 | m_text->AppendText(_("failed!\n")); |
392 | m_text->AppendText(_("Test 1 failed !\n")); | |
f85d901f GRG |
393 | } |
394 | else | |
395 | { | |
25ba2b89 GRG |
396 | m_text->AppendText(_("done\n")); |
397 | m_text->AppendText(_("Test 1 passed !\n")); | |
f85d901f | 398 | } |
25ba2b89 | 399 | m_text->AppendText(_("=== Test 1 ends ===\n")); |
f4ada568 | 400 | |
b62ca03d | 401 | m_busy = false; |
f85d901f GRG |
402 | UpdateStatusBar(); |
403 | } | |
f4ada568 | 404 | |
f85d901f GRG |
405 | void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) |
406 | { | |
f85d901f | 407 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 408 | m_busy = true; |
f85d901f GRG |
409 | UpdateStatusBar(); |
410 | ||
25ba2b89 | 411 | m_text->AppendText(_("\n=== Test 2 begins ===\n")); |
f85d901f GRG |
412 | |
413 | // Tell the server which test we are running | |
f4d5e009 | 414 | unsigned char c = 0xCE; |
f85d901f GRG |
415 | m_sock->Write(&c, 1); |
416 | ||
417 | // Here we use ReadMsg and WriteMsg to send messages with | |
418 | // a header with size information. Also, the reception is | |
419 | // event triggered, so we test input events as well. | |
420 | // | |
421 | // We need to set no flags here (ReadMsg and WriteMsg are | |
422 | // not affected by flags) | |
e51b0130 | 423 | |
f85d901f GRG |
424 | m_sock->SetFlags(wxSOCKET_WAITALL); |
425 | ||
426 | wxString s = wxGetTextFromUser( | |
25ba2b89 GRG |
427 | _("Enter an arbitrary string to send to the server:"), |
428 | _("Test 2 ..."), | |
be5a51fb | 429 | _("Yes I like wxWidgets!")); |
f85d901f | 430 | |
197380a0 | 431 | const wxScopedCharBuffer msg1(s.utf8_str()); |
9c22ef28 VZ |
432 | size_t len = wxStrlen(msg1) + 1; |
433 | wxCharBuffer msg2(wxStrlen(msg1)); | |
f85d901f | 434 | |
25ba2b89 | 435 | m_text->AppendText(_("Sending the string with WriteMsg ...")); |
f85d901f | 436 | m_sock->WriteMsg(msg1, len); |
25ba2b89 GRG |
437 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
438 | m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n")); | |
f85d901f GRG |
439 | |
440 | // Wait until data available (will also return if the connection is lost) | |
441 | m_sock->WaitForRead(2); | |
442 | ||
443 | if (m_sock->IsData()) | |
444 | { | |
25ba2b89 | 445 | m_text->AppendText(_("Reading the string back with ReadMsg ...")); |
9c22ef28 | 446 | m_sock->ReadMsg(msg2.data(), len); |
25ba2b89 GRG |
447 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
448 | m_text->AppendText(_("Comparing the two buffers ...")); | |
f85d901f GRG |
449 | if (memcmp(msg1, msg2, len) != 0) |
450 | { | |
925e9792 | 451 | m_text->AppendText(_("failed!\n")); |
25ba2b89 | 452 | m_text->AppendText(_("Test 2 failed !\n")); |
f85d901f GRG |
453 | } |
454 | else | |
455 | { | |
25ba2b89 GRG |
456 | m_text->AppendText(_("done\n")); |
457 | m_text->AppendText(_("Test 2 passed !\n")); | |
f85d901f | 458 | } |
765e386b | 459 | } |
f85d901f | 460 | else |
25ba2b89 | 461 | m_text->AppendText(_("Timeout ! Test 2 failed.\n")); |
765e386b | 462 | |
25ba2b89 | 463 | m_text->AppendText(_("=== Test 2 ends ===\n")); |
765e386b | 464 | |
b62ca03d | 465 | m_busy = false; |
f85d901f | 466 | UpdateStatusBar(); |
765e386b GL |
467 | } |
468 | ||
f85d901f | 469 | void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 470 | { |
6097c3a2 | 471 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 472 | m_busy = true; |
6097c3a2 GRG |
473 | UpdateStatusBar(); |
474 | ||
25ba2b89 | 475 | m_text->AppendText(_("\n=== Test 3 begins ===\n")); |
6097c3a2 GRG |
476 | |
477 | // Tell the server which test we are running | |
f4d5e009 | 478 | unsigned char c = 0xDE; |
6097c3a2 GRG |
479 | m_sock->Write(&c, 1); |
480 | ||
481 | // This test also is similar to the first one but it sends a | |
482 | // large buffer so that wxSocket is actually forced to split | |
483 | // it into pieces and take care of sending everything before | |
484 | // returning. | |
e51b0130 | 485 | |
6097c3a2 GRG |
486 | m_sock->SetFlags(wxSOCKET_WAITALL); |
487 | ||
488 | // Note that len is in kbytes here! | |
9c22ef28 VZ |
489 | const unsigned char len = 32; |
490 | wxCharBuffer buf1(len * 1024), | |
491 | buf2(len * 1024); | |
6097c3a2 | 492 | |
9c22ef28 VZ |
493 | for (size_t i = 0; i < len * 1024; i ++) |
494 | buf1.data()[i] = (char)(i % 256); | |
6097c3a2 | 495 | |
25ba2b89 | 496 | m_text->AppendText(_("Sending a large buffer (32K) to the server ...")); |
f187448d | 497 | m_sock->Write(&len, 1); |
6097c3a2 | 498 | m_sock->Write(buf1, len * 1024); |
25ba2b89 | 499 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 500 | |
25ba2b89 | 501 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
9c22ef28 | 502 | m_sock->Read(buf2.data(), len * 1024); |
25ba2b89 | 503 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 504 | |
25ba2b89 | 505 | m_text->AppendText(_("Comparing the two buffers ...")); |
6097c3a2 GRG |
506 | if (memcmp(buf1, buf2, len) != 0) |
507 | { | |
25ba2b89 GRG |
508 | m_text->AppendText(_("failed!\n")); |
509 | m_text->AppendText(_("Test 3 failed !\n")); | |
6097c3a2 GRG |
510 | } |
511 | else | |
512 | { | |
25ba2b89 GRG |
513 | m_text->AppendText(_("done\n")); |
514 | m_text->AppendText(_("Test 3 passed !\n")); | |
6097c3a2 | 515 | } |
25ba2b89 | 516 | m_text->AppendText(_("=== Test 3 ends ===\n")); |
6097c3a2 | 517 | |
b62ca03d | 518 | m_busy = false; |
6097c3a2 | 519 | UpdateStatusBar(); |
f4ada568 GL |
520 | } |
521 | ||
f85d901f | 522 | void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 523 | { |
f85d901f GRG |
524 | m_sock->Close(); |
525 | UpdateStatusBar(); | |
f4ada568 GL |
526 | } |
527 | ||
6097c3a2 GRG |
528 | void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) |
529 | { | |
46b11427 VZ |
530 | wxString hostname = wxGetTextFromUser |
531 | ( | |
532 | "Enter the address of the wxSocket demo server:", | |
533 | "UDP peer", | |
534 | "localhost" | |
535 | ); | |
536 | if ( hostname.empty() ) | |
537 | return; | |
538 | ||
539 | TestLogger logtest("UDP"); | |
540 | ||
541 | wxIPV4address addrLocal; | |
542 | addrLocal.Hostname(); | |
543 | wxDatagramSocket sock(addrLocal); | |
544 | if ( !sock.IsOk() ) | |
545 | { | |
546 | wxLogMessage("ERROR: failed to create UDP socket"); | |
547 | return; | |
548 | } | |
549 | ||
550 | wxLogMessage("Created UDP socket at %s:%u", | |
551 | addrLocal.IPAddress(), addrLocal.Service()); | |
552 | ||
553 | wxIPV4address addrPeer; | |
554 | addrPeer.Hostname(hostname); | |
555 | addrPeer.Service(3000); | |
556 | ||
557 | wxLogMessage("Testing UDP with peer at %s:%u", | |
558 | addrPeer.IPAddress(), addrPeer.Service()); | |
559 | ||
560 | char buf[] = "Uryyb sebz pyvrag!"; | |
561 | if ( sock.SendTo(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) ) | |
562 | { | |
563 | wxLogMessage("ERROR: failed to send data"); | |
564 | return; | |
565 | } | |
566 | ||
567 | if ( sock.RecvFrom(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) ) | |
568 | { | |
569 | wxLogMessage("ERROR: failed to receive data"); | |
570 | return; | |
571 | } | |
572 | ||
573 | wxLogMessage("Received \"%s\" from %s:%u.", | |
574 | wxString::From8BitData(buf, sock.LastCount()), | |
575 | addrPeer.IPAddress(), addrPeer.Service()); | |
6097c3a2 GRG |
576 | } |
577 | ||
16cafacf RN |
578 | #if wxUSE_URL |
579 | ||
25ba2b89 GRG |
580 | void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event)) |
581 | { | |
1e4080f0 VZ |
582 | // Ask for the URL |
583 | static wxString s_urlname("http://www.google.com/"); | |
584 | wxString urlname = wxGetTextFromUser | |
585 | ( | |
586 | _("Enter an URL to get"), | |
587 | _("URL:"), | |
588 | s_urlname | |
589 | ); | |
590 | if ( urlname.empty() ) | |
591 | return; // cancelled by user | |
592 | ||
593 | s_urlname = urlname; | |
594 | ||
595 | ||
f98f168e | 596 | TestLogger logtest("URL"); |
1e4080f0 VZ |
597 | |
598 | // Parse the URL | |
599 | wxURL url(urlname); | |
600 | if ( url.GetError() != wxURL_NOERR ) | |
601 | { | |
602 | wxLogError("Failed to parse URL \"%s\"", urlname); | |
603 | return; | |
604 | } | |
25ba2b89 | 605 | |
1e4080f0 VZ |
606 | // Try to get the input stream (connects to the given URL) |
607 | wxLogMessage("Establishing connection to \"%s\"...", urlname); | |
608 | const std::auto_ptr<wxInputStream> data(url.GetInputStream()); | |
609 | if ( !data.get() ) | |
610 | { | |
611 | wxLogError("Failed to retrieve URL \"%s\"", urlname); | |
612 | return; | |
613 | } | |
25ba2b89 | 614 | |
1e4080f0 VZ |
615 | // Print the contents type and file size |
616 | wxLogMessage("Contents type: %s\nFile size: %i\nStarting to download...", | |
617 | url.GetProtocol().GetContentType(), | |
618 | data->GetSize()); | |
6f505cae | 619 | |
1e4080f0 VZ |
620 | // Get the data |
621 | wxStringOutputStream sout; | |
622 | if ( data->Read(sout).GetLastError() != wxSTREAM_EOF ) | |
43b2d5e7 | 623 | { |
1e4080f0 | 624 | wxLogError("Error reading the input stream."); |
43b2d5e7 | 625 | } |
25ba2b89 | 626 | |
1e4080f0 VZ |
627 | wxLogMessage("Text retrieved from URL \"%s\" follows:\n%s", |
628 | urlname, sout.GetString()); | |
25ba2b89 GRG |
629 | } |
630 | ||
1e4080f0 | 631 | #endif // wxUSE_URL |
16cafacf | 632 | |
f85d901f | 633 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
f4ada568 | 634 | { |
b2f747d8 VZ |
635 | switch ( event.GetSocketEvent() ) |
636 | { | |
637 | case wxSOCKET_INPUT: | |
638 | wxLogMessage("Input available on the socket"); | |
639 | break; | |
640 | ||
641 | case wxSOCKET_LOST: | |
642 | wxLogMessage("Socket connection was unexpectedly lost."); | |
643 | UpdateStatusBar(); | |
644 | break; | |
645 | ||
646 | case wxSOCKET_CONNECTION: | |
647 | wxLogMessage("... socket is now connected."); | |
648 | UpdateStatusBar(); | |
649 | break; | |
650 | ||
651 | default: | |
652 | wxLogMessage("Unknown socket event!!!"); | |
653 | break; | |
654 | } | |
f4ada568 GL |
655 | } |
656 | ||
f85d901f | 657 | // convenience functions |
a324a7bc | 658 | |
f85d901f | 659 | void MyFrame::UpdateStatusBar() |
a324a7bc | 660 | { |
b2f747d8 | 661 | #if wxUSE_STATUSBAR |
f85d901f | 662 | wxString s; |
a324a7bc | 663 | |
f85d901f GRG |
664 | if (!m_sock->IsConnected()) |
665 | { | |
b2f747d8 | 666 | s = "Not connected"; |
a324a7bc | 667 | } |
f85d901f GRG |
668 | else |
669 | { | |
8575ff50 VZ |
670 | #if wxUSE_IPV6 |
671 | wxIPV6address addr; | |
672 | #else | |
f85d901f | 673 | wxIPV4address addr; |
8575ff50 | 674 | #endif |
a324a7bc | 675 | |
f85d901f | 676 | m_sock->GetPeer(addr); |
b2f747d8 | 677 | s.Printf("%s : %d", addr.Hostname(), addr.Service()); |
f85d901f | 678 | } |
f4ada568 | 679 | |
f85d901f | 680 | SetStatusText(s, 1); |
8520f137 | 681 | #endif // wxUSE_STATUSBAR |
a737331d | 682 | |
f85d901f | 683 | m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy); |
8575ff50 VZ |
684 | #if wxUSE_IPV6 |
685 | m_menuSocket->Enable(CLIENT_OPENIPV6, !m_sock->IsConnected() && !m_busy); | |
686 | #endif | |
f85d901f GRG |
687 | m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy); |
688 | m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy); | |
689 | m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy); | |
690 | m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected()); | |
f4ada568 | 691 | } |