]>
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 | |
18f42b94 | 193 | // Show it |
b62ca03d | 194 | frame->Show(true); |
f85d901f GRG |
195 | |
196 | // success | |
b62ca03d | 197 | return true; |
f4ada568 GL |
198 | } |
199 | ||
f85d901f GRG |
200 | // -------------------------------------------------------------------------- |
201 | // main frame | |
202 | // -------------------------------------------------------------------------- | |
203 | ||
204 | // frame constructor | |
b62ca03d | 205 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, |
25ba2b89 | 206 | _("wxSocket demo: Client"), |
f85d901f | 207 | wxDefaultPosition, wxSize(300, 200)) |
f4ada568 | 208 | { |
f85d901f | 209 | // Give the frame an icon |
3cb332c1 | 210 | SetIcon(wxICON(sample)); |
f85d901f GRG |
211 | |
212 | // Make menus | |
213 | m_menuFile = new wxMenu(); | |
25ba2b89 | 214 | m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); |
f85d901f | 215 | m_menuFile->AppendSeparator(); |
25ba2b89 | 216 | m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client")); |
f85d901f GRG |
217 | |
218 | m_menuSocket = new wxMenu(); | |
9c22ef28 | 219 | m_menuSocket->Append(CLIENT_OPEN, _("&Open session\tCtrl-O"), _("Connect to server")); |
8575ff50 | 220 | #if wxUSE_IPV6 |
9c22ef28 | 221 | m_menuSocket->Append(CLIENT_OPENIPV6, _("&Open session(IPv6)\tShift-Ctrl-O"), _("Connect to server(IPv6)")); |
8575ff50 | 222 | #endif |
f85d901f | 223 | m_menuSocket->AppendSeparator(); |
9c22ef28 VZ |
224 | m_menuSocket->Append(CLIENT_TEST1, _("Test &1\tCtrl-F1"), _("Test basic functionality")); |
225 | m_menuSocket->Append(CLIENT_TEST2, _("Test &2\tCtrl-F2"), _("Test ReadMsg and WriteMsg")); | |
226 | m_menuSocket->Append(CLIENT_TEST3, _("Test &3\tCtrl-F3"), _("Test large data transfer")); | |
f85d901f | 227 | m_menuSocket->AppendSeparator(); |
9c22ef28 | 228 | m_menuSocket->Append(CLIENT_CLOSE, _("&Close session\tCtrl-Q"), _("Close connection")); |
f85d901f | 229 | |
6097c3a2 | 230 | m_menuDatagramSocket = new wxMenu(); |
46b11427 | 231 | m_menuDatagramSocket->Append(CLIENT_DGRAM, _("&Datagram test\tCtrl-D"), _("Test UDP sockets")); |
25ba2b89 | 232 | |
16cafacf | 233 | #if wxUSE_URL |
25ba2b89 | 234 | m_menuProtocols = new wxMenu(); |
1e4080f0 VZ |
235 | m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL\tCtrl-U"), |
236 | _("Get data from the specified URL")); | |
16cafacf | 237 | #endif |
6097c3a2 | 238 | |
f85d901f GRG |
239 | // Append menus to the menubar |
240 | m_menuBar = new wxMenuBar(); | |
25ba2b89 | 241 | m_menuBar->Append(m_menuFile, _("&File")); |
46b11427 VZ |
242 | m_menuBar->Append(m_menuSocket, _("&TCP")); |
243 | m_menuBar->Append(m_menuDatagramSocket, _("&UDP")); | |
16cafacf | 244 | #if wxUSE_URL |
25ba2b89 | 245 | m_menuBar->Append(m_menuProtocols, _("&Protocols")); |
16cafacf | 246 | #endif |
f85d901f GRG |
247 | SetMenuBar(m_menuBar); |
248 | ||
8520f137 | 249 | #if wxUSE_STATUSBAR |
f85d901f | 250 | // Status bar |
f4ada568 | 251 | CreateStatusBar(2); |
8520f137 | 252 | #endif // wxUSE_STATUSBAR |
f85d901f | 253 | |
bd4d918f | 254 | // Make a textctrl for logging |
b62ca03d | 255 | m_text = new wxTextCtrl(this, wxID_ANY, |
4693b20c | 256 | _("Welcome to wxSocket demo: Client\nClient ready\n"), |
bd4d918f | 257 | wxDefaultPosition, wxDefaultSize, |
f85d901f | 258 | wxTE_MULTILINE | wxTE_READONLY); |
1e4080f0 | 259 | delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text)); |
f85d901f GRG |
260 | |
261 | // Create the socket | |
262 | m_sock = new wxSocketClient(); | |
bd4d918f GRG |
263 | |
264 | // Setup the event handler and subscribe to most events | |
f85d901f GRG |
265 | m_sock->SetEventHandler(*this, SOCKET_ID); |
266 | m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG | | |
267 | wxSOCKET_INPUT_FLAG | | |
268 | wxSOCKET_LOST_FLAG); | |
b62ca03d | 269 | m_sock->Notify(true); |
f85d901f | 270 | |
b62ca03d | 271 | m_busy = false; |
f85d901f | 272 | UpdateStatusBar(); |
f4ada568 GL |
273 | } |
274 | ||
275 | MyFrame::~MyFrame() | |
276 | { | |
bd4d918f | 277 | // No delayed deletion here, as the frame is dying anyway |
f85d901f | 278 | delete m_sock; |
f4ada568 GL |
279 | } |
280 | ||
f85d901f GRG |
281 | // event handlers |
282 | ||
283 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 284 | { |
b62ca03d WS |
285 | // true is to force the frame to close |
286 | Close(true); | |
f4ada568 GL |
287 | } |
288 | ||
f85d901f GRG |
289 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
290 | { | |
4693b20c | 291 | wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"), |
25ba2b89 | 292 | _("About Client"), |
f85d901f GRG |
293 | wxOK | wxICON_INFORMATION, this); |
294 | } | |
295 | ||
296 | void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 297 | { |
778f682e | 298 | OpenConnection(wxSockAddress::IPV4); |
8575ff50 VZ |
299 | } |
300 | #if wxUSE_IPV6 | |
301 | void MyFrame::OnOpenConnectionIPv6(wxCommandEvent& WXUNUSED(event)) | |
302 | { | |
778f682e | 303 | OpenConnection(wxSockAddress::IPV6); |
8575ff50 VZ |
304 | } |
305 | #endif // wxUSE_IPV6 | |
306 | ||
778f682e | 307 | void MyFrame::OpenConnection(wxSockAddress::Family family) |
8575ff50 | 308 | { |
778f682e VZ |
309 | wxUnusedVar(family); // unused in !wxUSE_IPV6 case |
310 | ||
8575ff50 | 311 | wxIPaddress * addr; |
25248cb6 | 312 | wxIPV4address addr4; |
8575ff50 VZ |
313 | #if wxUSE_IPV6 |
314 | wxIPV6address addr6; | |
778f682e | 315 | if ( family == wxSockAddress::IPV6 ) |
25248cb6 VZ |
316 | addr = &addr6; |
317 | else | |
8575ff50 | 318 | #endif |
778f682e | 319 | addr = &addr4; |
f4ada568 | 320 | |
b62ca03d | 321 | m_menuSocket->Enable(CLIENT_OPEN, false); |
8575ff50 VZ |
322 | #if wxUSE_IPV6 |
323 | m_menuSocket->Enable(CLIENT_OPENIPV6, false); | |
324 | #endif | |
b62ca03d | 325 | m_menuSocket->Enable(CLIENT_CLOSE, false); |
f85d901f | 326 | |
25ba2b89 | 327 | // Ask user for server address |
f85d901f | 328 | wxString hostname = wxGetTextFromUser( |
25ba2b89 GRG |
329 | _("Enter the address of the wxSocket demo server:"), |
330 | _("Connect ..."), | |
331 | _("localhost")); | |
46b11427 VZ |
332 | if ( hostname.empty() ) |
333 | return; | |
f85d901f | 334 | |
8575ff50 VZ |
335 | addr->Hostname(hostname); |
336 | addr->Service(3000); | |
f4ada568 | 337 | |
b2f747d8 VZ |
338 | // we connect asynchronously and will get a wxSOCKET_CONNECTION event when |
339 | // the connection is really established | |
25ba2b89 | 340 | // |
b2f747d8 VZ |
341 | // if you want to make sure that connection is established right here you |
342 | // could call WaitOnConnect(timeout) instead | |
343 | wxLogMessage("Trying to connect to %s:%d", hostname, addr->Service()); | |
e51b0130 | 344 | |
8575ff50 | 345 | m_sock->Connect(*addr, false); |
f4ada568 GL |
346 | } |
347 | ||
f85d901f | 348 | void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 349 | { |
f85d901f | 350 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 351 | m_busy = true; |
f85d901f GRG |
352 | UpdateStatusBar(); |
353 | ||
25ba2b89 | 354 | m_text->AppendText(_("\n=== Test 1 begins ===\n")); |
f85d901f GRG |
355 | |
356 | // Tell the server which test we are running | |
f4d5e009 | 357 | unsigned char c = 0xBE; |
f85d901f GRG |
358 | m_sock->Write(&c, 1); |
359 | ||
360 | // Send some data and read it back. We know the size of the | |
361 | // buffer, so we can specify the exact number of bytes to be | |
6097c3a2 GRG |
362 | // sent or received and use the wxSOCKET_WAITALL flag. Also, |
363 | // we have disabled menu entries which could interfere with | |
364 | // the test, so we can safely avoid the wxSOCKET_BLOCK flag. | |
f85d901f GRG |
365 | // |
366 | // First we send a byte with the length of the string, then | |
367 | // we send the string itself (do NOT try to send any integral | |
6097c3a2 | 368 | // value larger than a byte "as is" across the network, or |
f85d901f GRG |
369 | // you might be in trouble! Ever heard about big and little |
370 | // endian computers?) | |
e51b0130 | 371 | |
f85d901f GRG |
372 | m_sock->SetFlags(wxSOCKET_WAITALL); |
373 | ||
9c22ef28 VZ |
374 | const char *buf1 = "Test string (less than 256 chars!)"; |
375 | unsigned char len = (unsigned char)(wxStrlen(buf1) + 1); | |
376 | wxCharBuffer buf2(wxStrlen(buf1)); | |
f85d901f | 377 | |
25ba2b89 | 378 | m_text->AppendText(_("Sending a test buffer to the server ...")); |
f187448d | 379 | m_sock->Write(&len, 1); |
f85d901f | 380 | m_sock->Write(buf1, len); |
25ba2b89 | 381 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 382 | |
25ba2b89 | 383 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
9c22ef28 | 384 | m_sock->Read(buf2.data(), len); |
25ba2b89 | 385 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 386 | |
25ba2b89 | 387 | m_text->AppendText(_("Comparing the two buffers ...")); |
f85d901f GRG |
388 | if (memcmp(buf1, buf2, len) != 0) |
389 | { | |
25ba2b89 GRG |
390 | m_text->AppendText(_("failed!\n")); |
391 | m_text->AppendText(_("Test 1 failed !\n")); | |
f85d901f GRG |
392 | } |
393 | else | |
394 | { | |
25ba2b89 GRG |
395 | m_text->AppendText(_("done\n")); |
396 | m_text->AppendText(_("Test 1 passed !\n")); | |
f85d901f | 397 | } |
25ba2b89 | 398 | m_text->AppendText(_("=== Test 1 ends ===\n")); |
f4ada568 | 399 | |
b62ca03d | 400 | m_busy = false; |
f85d901f GRG |
401 | UpdateStatusBar(); |
402 | } | |
f4ada568 | 403 | |
f85d901f GRG |
404 | void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) |
405 | { | |
f85d901f | 406 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 407 | m_busy = true; |
f85d901f GRG |
408 | UpdateStatusBar(); |
409 | ||
25ba2b89 | 410 | m_text->AppendText(_("\n=== Test 2 begins ===\n")); |
f85d901f GRG |
411 | |
412 | // Tell the server which test we are running | |
f4d5e009 | 413 | unsigned char c = 0xCE; |
f85d901f GRG |
414 | m_sock->Write(&c, 1); |
415 | ||
416 | // Here we use ReadMsg and WriteMsg to send messages with | |
417 | // a header with size information. Also, the reception is | |
418 | // event triggered, so we test input events as well. | |
419 | // | |
420 | // We need to set no flags here (ReadMsg and WriteMsg are | |
421 | // not affected by flags) | |
e51b0130 | 422 | |
f85d901f GRG |
423 | m_sock->SetFlags(wxSOCKET_WAITALL); |
424 | ||
425 | wxString s = wxGetTextFromUser( | |
25ba2b89 GRG |
426 | _("Enter an arbitrary string to send to the server:"), |
427 | _("Test 2 ..."), | |
be5a51fb | 428 | _("Yes I like wxWidgets!")); |
f85d901f | 429 | |
197380a0 | 430 | const wxScopedCharBuffer msg1(s.utf8_str()); |
9c22ef28 VZ |
431 | size_t len = wxStrlen(msg1) + 1; |
432 | wxCharBuffer msg2(wxStrlen(msg1)); | |
f85d901f | 433 | |
25ba2b89 | 434 | m_text->AppendText(_("Sending the string with WriteMsg ...")); |
f85d901f | 435 | m_sock->WriteMsg(msg1, len); |
25ba2b89 GRG |
436 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
437 | m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n")); | |
f85d901f GRG |
438 | |
439 | // Wait until data available (will also return if the connection is lost) | |
440 | m_sock->WaitForRead(2); | |
441 | ||
442 | if (m_sock->IsData()) | |
443 | { | |
25ba2b89 | 444 | m_text->AppendText(_("Reading the string back with ReadMsg ...")); |
9c22ef28 | 445 | m_sock->ReadMsg(msg2.data(), len); |
25ba2b89 GRG |
446 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
447 | m_text->AppendText(_("Comparing the two buffers ...")); | |
f85d901f GRG |
448 | if (memcmp(msg1, msg2, len) != 0) |
449 | { | |
925e9792 | 450 | m_text->AppendText(_("failed!\n")); |
25ba2b89 | 451 | m_text->AppendText(_("Test 2 failed !\n")); |
f85d901f GRG |
452 | } |
453 | else | |
454 | { | |
25ba2b89 GRG |
455 | m_text->AppendText(_("done\n")); |
456 | m_text->AppendText(_("Test 2 passed !\n")); | |
f85d901f | 457 | } |
765e386b | 458 | } |
f85d901f | 459 | else |
25ba2b89 | 460 | m_text->AppendText(_("Timeout ! Test 2 failed.\n")); |
765e386b | 461 | |
25ba2b89 | 462 | m_text->AppendText(_("=== Test 2 ends ===\n")); |
765e386b | 463 | |
b62ca03d | 464 | m_busy = false; |
f85d901f | 465 | UpdateStatusBar(); |
765e386b GL |
466 | } |
467 | ||
f85d901f | 468 | void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 469 | { |
6097c3a2 | 470 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 471 | m_busy = true; |
6097c3a2 GRG |
472 | UpdateStatusBar(); |
473 | ||
25ba2b89 | 474 | m_text->AppendText(_("\n=== Test 3 begins ===\n")); |
6097c3a2 GRG |
475 | |
476 | // Tell the server which test we are running | |
f4d5e009 | 477 | unsigned char c = 0xDE; |
6097c3a2 GRG |
478 | m_sock->Write(&c, 1); |
479 | ||
480 | // This test also is similar to the first one but it sends a | |
481 | // large buffer so that wxSocket is actually forced to split | |
482 | // it into pieces and take care of sending everything before | |
483 | // returning. | |
e51b0130 | 484 | |
6097c3a2 GRG |
485 | m_sock->SetFlags(wxSOCKET_WAITALL); |
486 | ||
487 | // Note that len is in kbytes here! | |
9c22ef28 VZ |
488 | const unsigned char len = 32; |
489 | wxCharBuffer buf1(len * 1024), | |
490 | buf2(len * 1024); | |
6097c3a2 | 491 | |
9c22ef28 VZ |
492 | for (size_t i = 0; i < len * 1024; i ++) |
493 | buf1.data()[i] = (char)(i % 256); | |
6097c3a2 | 494 | |
25ba2b89 | 495 | m_text->AppendText(_("Sending a large buffer (32K) to the server ...")); |
f187448d | 496 | m_sock->Write(&len, 1); |
6097c3a2 | 497 | m_sock->Write(buf1, len * 1024); |
25ba2b89 | 498 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 499 | |
25ba2b89 | 500 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
9c22ef28 | 501 | m_sock->Read(buf2.data(), len * 1024); |
25ba2b89 | 502 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 503 | |
25ba2b89 | 504 | m_text->AppendText(_("Comparing the two buffers ...")); |
6097c3a2 GRG |
505 | if (memcmp(buf1, buf2, len) != 0) |
506 | { | |
25ba2b89 GRG |
507 | m_text->AppendText(_("failed!\n")); |
508 | m_text->AppendText(_("Test 3 failed !\n")); | |
6097c3a2 GRG |
509 | } |
510 | else | |
511 | { | |
25ba2b89 GRG |
512 | m_text->AppendText(_("done\n")); |
513 | m_text->AppendText(_("Test 3 passed !\n")); | |
6097c3a2 | 514 | } |
25ba2b89 | 515 | m_text->AppendText(_("=== Test 3 ends ===\n")); |
6097c3a2 | 516 | |
b62ca03d | 517 | m_busy = false; |
6097c3a2 | 518 | UpdateStatusBar(); |
f4ada568 GL |
519 | } |
520 | ||
f85d901f | 521 | void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 522 | { |
f85d901f GRG |
523 | m_sock->Close(); |
524 | UpdateStatusBar(); | |
f4ada568 GL |
525 | } |
526 | ||
6097c3a2 GRG |
527 | void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) |
528 | { | |
46b11427 VZ |
529 | wxString hostname = wxGetTextFromUser |
530 | ( | |
531 | "Enter the address of the wxSocket demo server:", | |
532 | "UDP peer", | |
533 | "localhost" | |
534 | ); | |
535 | if ( hostname.empty() ) | |
536 | return; | |
537 | ||
538 | TestLogger logtest("UDP"); | |
539 | ||
540 | wxIPV4address addrLocal; | |
541 | addrLocal.Hostname(); | |
542 | wxDatagramSocket sock(addrLocal); | |
543 | if ( !sock.IsOk() ) | |
544 | { | |
545 | wxLogMessage("ERROR: failed to create UDP socket"); | |
546 | return; | |
547 | } | |
548 | ||
549 | wxLogMessage("Created UDP socket at %s:%u", | |
550 | addrLocal.IPAddress(), addrLocal.Service()); | |
551 | ||
552 | wxIPV4address addrPeer; | |
553 | addrPeer.Hostname(hostname); | |
554 | addrPeer.Service(3000); | |
555 | ||
556 | wxLogMessage("Testing UDP with peer at %s:%u", | |
557 | addrPeer.IPAddress(), addrPeer.Service()); | |
558 | ||
559 | char buf[] = "Uryyb sebz pyvrag!"; | |
560 | if ( sock.SendTo(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) ) | |
561 | { | |
562 | wxLogMessage("ERROR: failed to send data"); | |
563 | return; | |
564 | } | |
565 | ||
566 | if ( sock.RecvFrom(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) ) | |
567 | { | |
568 | wxLogMessage("ERROR: failed to receive data"); | |
569 | return; | |
570 | } | |
571 | ||
572 | wxLogMessage("Received \"%s\" from %s:%u.", | |
573 | wxString::From8BitData(buf, sock.LastCount()), | |
574 | addrPeer.IPAddress(), addrPeer.Service()); | |
6097c3a2 GRG |
575 | } |
576 | ||
16cafacf RN |
577 | #if wxUSE_URL |
578 | ||
25ba2b89 GRG |
579 | void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event)) |
580 | { | |
1e4080f0 VZ |
581 | // Ask for the URL |
582 | static wxString s_urlname("http://www.google.com/"); | |
583 | wxString urlname = wxGetTextFromUser | |
584 | ( | |
585 | _("Enter an URL to get"), | |
586 | _("URL:"), | |
587 | s_urlname | |
588 | ); | |
589 | if ( urlname.empty() ) | |
590 | return; // cancelled by user | |
591 | ||
592 | s_urlname = urlname; | |
593 | ||
594 | ||
f98f168e | 595 | TestLogger logtest("URL"); |
1e4080f0 VZ |
596 | |
597 | // Parse the URL | |
598 | wxURL url(urlname); | |
599 | if ( url.GetError() != wxURL_NOERR ) | |
600 | { | |
601 | wxLogError("Failed to parse URL \"%s\"", urlname); | |
602 | return; | |
603 | } | |
25ba2b89 | 604 | |
1e4080f0 VZ |
605 | // Try to get the input stream (connects to the given URL) |
606 | wxLogMessage("Establishing connection to \"%s\"...", urlname); | |
607 | const std::auto_ptr<wxInputStream> data(url.GetInputStream()); | |
608 | if ( !data.get() ) | |
609 | { | |
610 | wxLogError("Failed to retrieve URL \"%s\"", urlname); | |
611 | return; | |
612 | } | |
25ba2b89 | 613 | |
1e4080f0 VZ |
614 | // Print the contents type and file size |
615 | wxLogMessage("Contents type: %s\nFile size: %i\nStarting to download...", | |
616 | url.GetProtocol().GetContentType(), | |
617 | data->GetSize()); | |
6f505cae | 618 | |
1e4080f0 VZ |
619 | // Get the data |
620 | wxStringOutputStream sout; | |
621 | if ( data->Read(sout).GetLastError() != wxSTREAM_EOF ) | |
43b2d5e7 | 622 | { |
1e4080f0 | 623 | wxLogError("Error reading the input stream."); |
43b2d5e7 | 624 | } |
25ba2b89 | 625 | |
1e4080f0 VZ |
626 | wxLogMessage("Text retrieved from URL \"%s\" follows:\n%s", |
627 | urlname, sout.GetString()); | |
25ba2b89 GRG |
628 | } |
629 | ||
1e4080f0 | 630 | #endif // wxUSE_URL |
16cafacf | 631 | |
f85d901f | 632 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
f4ada568 | 633 | { |
b2f747d8 VZ |
634 | switch ( event.GetSocketEvent() ) |
635 | { | |
636 | case wxSOCKET_INPUT: | |
637 | wxLogMessage("Input available on the socket"); | |
638 | break; | |
639 | ||
640 | case wxSOCKET_LOST: | |
641 | wxLogMessage("Socket connection was unexpectedly lost."); | |
642 | UpdateStatusBar(); | |
643 | break; | |
644 | ||
645 | case wxSOCKET_CONNECTION: | |
646 | wxLogMessage("... socket is now connected."); | |
647 | UpdateStatusBar(); | |
648 | break; | |
649 | ||
650 | default: | |
651 | wxLogMessage("Unknown socket event!!!"); | |
652 | break; | |
653 | } | |
f4ada568 GL |
654 | } |
655 | ||
f85d901f | 656 | // convenience functions |
a324a7bc | 657 | |
f85d901f | 658 | void MyFrame::UpdateStatusBar() |
a324a7bc | 659 | { |
b2f747d8 | 660 | #if wxUSE_STATUSBAR |
f85d901f | 661 | wxString s; |
a324a7bc | 662 | |
f85d901f GRG |
663 | if (!m_sock->IsConnected()) |
664 | { | |
b2f747d8 | 665 | s = "Not connected"; |
a324a7bc | 666 | } |
f85d901f GRG |
667 | else |
668 | { | |
8575ff50 VZ |
669 | #if wxUSE_IPV6 |
670 | wxIPV6address addr; | |
671 | #else | |
f85d901f | 672 | wxIPV4address addr; |
8575ff50 | 673 | #endif |
a324a7bc | 674 | |
f85d901f | 675 | m_sock->GetPeer(addr); |
b2f747d8 | 676 | s.Printf("%s : %d", addr.Hostname(), addr.Service()); |
f85d901f | 677 | } |
f4ada568 | 678 | |
f85d901f | 679 | SetStatusText(s, 1); |
8520f137 | 680 | #endif // wxUSE_STATUSBAR |
a737331d | 681 | |
f85d901f | 682 | m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy); |
8575ff50 VZ |
683 | #if wxUSE_IPV6 |
684 | m_menuSocket->Enable(CLIENT_OPENIPV6, !m_sock->IsConnected() && !m_busy); | |
685 | #endif | |
f85d901f GRG |
686 | m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy); |
687 | m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy); | |
688 | m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy); | |
689 | m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected()); | |
f4ada568 | 690 | } |