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