]>
Commit | Line | Data |
---|---|---|
f85d901f GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: server.cpp | |
3 | // Purpose: Server 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 | ||
677a9f0c | 27 | // for all others, include the necessary headers |
f4ada568 | 28 | #ifndef WX_PRECOMP |
f85d901f | 29 | # include "wx/wx.h" |
f4ada568 | 30 | #endif |
e2a6f233 | 31 | |
09fb22cf JS |
32 | #include "wx/socket.h" |
33 | ||
f85d901f GRG |
34 | // -------------------------------------------------------------------------- |
35 | // resources | |
36 | // -------------------------------------------------------------------------- | |
f4ada568 | 37 | |
f85d901f | 38 | // the application icon |
618f2efa | 39 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) |
f85d901f | 40 | # include "mondrian.xpm" |
e2a6f233 JS |
41 | #endif |
42 | ||
f85d901f GRG |
43 | // -------------------------------------------------------------------------- |
44 | // classes | |
45 | // -------------------------------------------------------------------------- | |
46 | ||
f4ada568 | 47 | // Define a new application type |
f85d901f GRG |
48 | class MyApp : public wxApp |
49 | { | |
50 | public: | |
51 | virtual bool OnInit(); | |
f4ada568 GL |
52 | }; |
53 | ||
f85d901f GRG |
54 | // Define a new frame type: this is going to be our main frame |
55 | class MyFrame : public wxFrame | |
56 | { | |
f4ada568 | 57 | public: |
f85d901f GRG |
58 | MyFrame(); |
59 | ~MyFrame(); | |
60 | ||
61 | // event handlers (these functions should _not_ be virtual) | |
62 | void OnQuit(wxCommandEvent& event); | |
63 | void OnAbout(wxCommandEvent& event); | |
64 | void OnServerEvent(wxSocketEvent& event); | |
65 | void OnSocketEvent(wxSocketEvent& event); | |
66 | ||
67 | void Test1(wxSocketBase *sock); | |
68 | void Test2(wxSocketBase *sock); | |
69 | void Test3(wxSocketBase *sock); | |
70 | ||
71 | // convenience functions | |
72 | void UpdateStatusBar(); | |
73 | ||
74 | private: | |
75 | wxSocketServer *m_server; | |
f85d901f GRG |
76 | wxTextCtrl *m_text; |
77 | wxMenu *m_menuFile; | |
78 | wxMenuBar *m_menuBar; | |
79 | bool m_busy; | |
80 | int m_numClients; | |
81 | ||
be5a51fb | 82 | // any class wishing to process wxWidgets events must use this macro |
f85d901f GRG |
83 | DECLARE_EVENT_TABLE() |
84 | }; | |
85 | ||
86 | // -------------------------------------------------------------------------- | |
87 | // constants | |
88 | // -------------------------------------------------------------------------- | |
89 | ||
90 | // IDs for the controls and the menu commands | |
91 | enum | |
92 | { | |
93 | // menu items | |
91b07357 JS |
94 | SERVER_QUIT = wxID_EXIT, |
95 | SERVER_ABOUT = wxID_ABOUT, | |
f85d901f GRG |
96 | |
97 | // id for sockets | |
91b07357 | 98 | SERVER_ID = 100, |
f85d901f | 99 | SOCKET_ID |
f4ada568 GL |
100 | }; |
101 | ||
f85d901f | 102 | // -------------------------------------------------------------------------- |
be5a51fb | 103 | // event tables and other macros for wxWidgets |
f85d901f | 104 | // -------------------------------------------------------------------------- |
f4ada568 GL |
105 | |
106 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
f85d901f GRG |
107 | EVT_MENU(SERVER_QUIT, MyFrame::OnQuit) |
108 | EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout) | |
109 | EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent) | |
110 | EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) | |
f4ada568 GL |
111 | END_EVENT_TABLE() |
112 | ||
f4ada568 GL |
113 | IMPLEMENT_APP(MyApp) |
114 | ||
f4ada568 | 115 | |
f85d901f GRG |
116 | // ========================================================================== |
117 | // implementation | |
118 | // ========================================================================== | |
f4ada568 | 119 | |
f85d901f GRG |
120 | // -------------------------------------------------------------------------- |
121 | // the application class | |
122 | // -------------------------------------------------------------------------- | |
f4ada568 | 123 | |
f85d901f GRG |
124 | bool MyApp::OnInit() |
125 | { | |
45e6e6f8 VZ |
126 | if ( !wxApp::OnInit() ) |
127 | return false; | |
128 | ||
f85d901f GRG |
129 | // Create the main application window |
130 | MyFrame *frame = new MyFrame(); | |
f4ada568 | 131 | |
f85d901f | 132 | // Show it and tell the application that it's our main window |
b62ca03d | 133 | frame->Show(true); |
f85d901f | 134 | SetTopWindow(frame); |
2b98cb1f | 135 | |
bd4d918f | 136 | // Success |
b62ca03d | 137 | return true; |
f4ada568 GL |
138 | } |
139 | ||
f85d901f GRG |
140 | // -------------------------------------------------------------------------- |
141 | // main frame | |
142 | // -------------------------------------------------------------------------- | |
a737331d | 143 | |
f85d901f | 144 | // frame constructor |
bd4d918f | 145 | |
b62ca03d | 146 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, |
bd4d918f | 147 | _("wxSocket demo: Server"), |
f85d901f | 148 | wxDefaultPosition, wxSize(300, 200)) |
f4ada568 | 149 | { |
f85d901f GRG |
150 | // Give the frame an icon |
151 | SetIcon(wxICON(mondrian)); | |
152 | ||
153 | // Make menus | |
154 | m_menuFile = new wxMenu(); | |
bd4d918f | 155 | m_menuFile->Append(SERVER_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); |
f85d901f | 156 | m_menuFile->AppendSeparator(); |
bd4d918f | 157 | m_menuFile->Append(SERVER_QUIT, _("E&xit\tAlt-X"), _("Quit server")); |
f85d901f GRG |
158 | |
159 | // Append menus to the menubar | |
160 | m_menuBar = new wxMenuBar(); | |
bd4d918f | 161 | m_menuBar->Append(m_menuFile, _("&File")); |
f85d901f GRG |
162 | SetMenuBar(m_menuBar); |
163 | ||
8520f137 | 164 | #if wxUSE_STATUSBAR |
f85d901f GRG |
165 | // Status bar |
166 | CreateStatusBar(2); | |
8520f137 | 167 | #endif // wxUSE_STATUSBAR |
f85d901f | 168 | |
bd4d918f | 169 | // Make a textctrl for logging |
b62ca03d | 170 | m_text = new wxTextCtrl(this, wxID_ANY, |
bd4d918f GRG |
171 | _("Welcome to wxSocket demo: Server\n"), |
172 | wxDefaultPosition, wxDefaultSize, | |
f85d901f GRG |
173 | wxTE_MULTILINE | wxTE_READONLY); |
174 | ||
bd4d918f | 175 | // Create the address - defaults to localhost:0 initially |
8575ff50 VZ |
176 | #if wxUSE_IPV6 |
177 | wxIPV6address addr; | |
178 | #else | |
f85d901f | 179 | wxIPV4address addr; |
8575ff50 | 180 | #endif |
f85d901f GRG |
181 | addr.Service(3000); |
182 | ||
bd4d918f | 183 | // Create the socket |
f85d901f | 184 | m_server = new wxSocketServer(addr); |
f85d901f GRG |
185 | |
186 | // We use Ok() here to see if the server is really listening | |
bd4d918f GRG |
187 | if (! m_server->Ok()) |
188 | { | |
189 | m_text->AppendText(_("Could not listen at the specified port !\n\n")); | |
190 | return; | |
191 | } | |
f85d901f | 192 | else |
bd4d918f GRG |
193 | { |
194 | m_text->AppendText(_("Server listening.\n\n")); | |
195 | } | |
196 | ||
197 | // Setup the event handler and subscribe to connection events | |
198 | m_server->SetEventHandler(*this, SERVER_ID); | |
199 | m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); | |
b62ca03d | 200 | m_server->Notify(true); |
f85d901f | 201 | |
b62ca03d | 202 | m_busy = false; |
f85d901f GRG |
203 | m_numClients = 0; |
204 | UpdateStatusBar(); | |
f4ada568 GL |
205 | } |
206 | ||
f85d901f | 207 | MyFrame::~MyFrame() |
f4ada568 | 208 | { |
bd4d918f | 209 | // No delayed deletion here, as the frame is dying anyway |
f85d901f GRG |
210 | delete m_server; |
211 | } | |
f4ada568 | 212 | |
f85d901f | 213 | // event handlers |
f4ada568 | 214 | |
f85d901f GRG |
215 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
216 | { | |
b62ca03d WS |
217 | // true is to force the frame to close |
218 | Close(true); | |
f85d901f | 219 | } |
f4ada568 | 220 | |
f85d901f GRG |
221 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
222 | { | |
4693b20c | 223 | wxMessageBox(_("wxSocket demo: Server\n(c) 1999 Guillermo Rodriguez Garcia\n"), |
bd4d918f | 224 | _("About Server"), |
f85d901f | 225 | wxOK | wxICON_INFORMATION, this); |
f4ada568 GL |
226 | } |
227 | ||
f85d901f | 228 | void MyFrame::Test1(wxSocketBase *sock) |
f4ada568 | 229 | { |
f85d901f GRG |
230 | unsigned char len; |
231 | char *buf; | |
f4ada568 | 232 | |
bd4d918f | 233 | m_text->AppendText(_("Test 1 begins\n")); |
f4ada568 | 234 | |
f85d901f GRG |
235 | // Receive data from socket and send it back. We will first |
236 | // get a byte with the buffer size, so we can specify the | |
6097c3a2 GRG |
237 | // exact size and use the wxSOCKET_WAITALL flag. Also, we |
238 | // disabled input events so we won't have unwanted reentrance. | |
239 | // This way we can avoid the infamous wxSOCKET_BLOCK flag. | |
e51b0130 | 240 | |
f85d901f GRG |
241 | sock->SetFlags(wxSOCKET_WAITALL); |
242 | ||
bc7e88ae | 243 | // Read the size |
f187448d | 244 | sock->Read(&len, 1); |
6097c3a2 | 245 | buf = new char[len]; |
bc7e88ae GRG |
246 | |
247 | // Read the data | |
f85d901f | 248 | sock->Read(buf, len); |
bc7e88ae GRG |
249 | m_text->AppendText(_("Got the data, sending it back\n")); |
250 | ||
251 | // Write it back | |
f85d901f | 252 | sock->Write(buf, len); |
6097c3a2 | 253 | delete[] buf; |
f85d901f | 254 | |
bc7e88ae | 255 | m_text->AppendText(_("Test 1 ends\n\n")); |
f4ada568 GL |
256 | } |
257 | ||
f85d901f | 258 | void MyFrame::Test2(wxSocketBase *sock) |
f4ada568 | 259 | { |
f85d901f GRG |
260 | #define MAX_MSG_SIZE 10000 |
261 | ||
262 | wxString s; | |
4693b20c | 263 | wxChar *buf = new wxChar[MAX_MSG_SIZE]; |
f85d901f GRG |
264 | wxUint32 len; |
265 | ||
bd4d918f | 266 | m_text->AppendText(_("Test 2 begins\n")); |
f85d901f GRG |
267 | |
268 | // We don't need to set flags because ReadMsg and WriteMsg | |
269 | // are not affected by them anyway. | |
e51b0130 | 270 | |
bc7e88ae | 271 | // Read the message |
4693b20c | 272 | len = sock->ReadMsg(buf, MAX_MSG_SIZE * sizeof(wxChar)).LastCount(); |
bd4d918f | 273 | s.Printf(_("Client says: %s\n"), buf); |
f85d901f | 274 | m_text->AppendText(s); |
36bec0ac | 275 | m_text->AppendText(_("Sending the data back\n")); |
f85d901f | 276 | |
bc7e88ae | 277 | // Write it back |
f85d901f | 278 | sock->WriteMsg(buf, len); |
6097c3a2 | 279 | delete[] buf; |
f85d901f | 280 | |
bc7e88ae | 281 | m_text->AppendText(_("Test 2 ends\n\n")); |
f85d901f GRG |
282 | |
283 | #undef MAX_MSG_SIZE | |
f4ada568 GL |
284 | } |
285 | ||
f85d901f | 286 | void MyFrame::Test3(wxSocketBase *sock) |
f4ada568 | 287 | { |
6097c3a2 GRG |
288 | unsigned char len; |
289 | char *buf; | |
290 | ||
bd4d918f | 291 | m_text->AppendText(_("Test 3 begins\n")); |
6097c3a2 | 292 | |
677a9f0c | 293 | // This test is similar to the first one, but the len is |
6097c3a2 | 294 | // expressed in kbytes - this tests large data transfers. |
e51b0130 | 295 | |
6097c3a2 GRG |
296 | sock->SetFlags(wxSOCKET_WAITALL); |
297 | ||
bc7e88ae | 298 | // Read the size |
f187448d | 299 | sock->Read(&len, 1); |
6097c3a2 | 300 | buf = new char[len * 1024]; |
bc7e88ae GRG |
301 | |
302 | // Read the data | |
6097c3a2 | 303 | sock->Read(buf, len * 1024); |
bc7e88ae GRG |
304 | m_text->AppendText(_("Got the data, sending it back\n")); |
305 | ||
306 | // Write it back | |
6097c3a2 GRG |
307 | sock->Write(buf, len * 1024); |
308 | delete[] buf; | |
309 | ||
bc7e88ae | 310 | m_text->AppendText(_("Test 3 ends\n\n")); |
f4ada568 GL |
311 | } |
312 | ||
f85d901f | 313 | void MyFrame::OnServerEvent(wxSocketEvent& event) |
f4ada568 | 314 | { |
bd4d918f | 315 | wxString s = _("OnServerEvent: "); |
f85d901f GRG |
316 | wxSocketBase *sock; |
317 | ||
bc7e88ae | 318 | switch(event.GetSocketEvent()) |
f85d901f | 319 | { |
bd4d918f GRG |
320 | case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break; |
321 | default : s.Append(_("Unexpected event !\n")); break; | |
f85d901f | 322 | } |
f4ada568 | 323 | |
f85d901f | 324 | m_text->AppendText(s); |
765e386b | 325 | |
f85d901f | 326 | // Accept new connection if there is one in the pending |
b62ca03d | 327 | // connections queue, else exit. We use Accept(false) for |
f85d901f GRG |
328 | // non-blocking accept (although if we got here, there |
329 | // should ALWAYS be a pending connection). | |
e51b0130 | 330 | |
b62ca03d | 331 | sock = m_server->Accept(false); |
f85d901f GRG |
332 | |
333 | if (sock) | |
334 | { | |
bc7e88ae | 335 | m_text->AppendText(_("New client connection accepted\n\n")); |
f85d901f GRG |
336 | } |
337 | else | |
338 | { | |
bc7e88ae | 339 | m_text->AppendText(_("Error: couldn't accept a new connection\n\n")); |
f85d901f GRG |
340 | return; |
341 | } | |
342 | ||
343 | sock->SetEventHandler(*this, SOCKET_ID); | |
344 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
b62ca03d | 345 | sock->Notify(true); |
f85d901f GRG |
346 | |
347 | m_numClients++; | |
348 | UpdateStatusBar(); | |
f4ada568 GL |
349 | } |
350 | ||
f85d901f GRG |
351 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
352 | { | |
bd4d918f | 353 | wxString s = _("OnSocketEvent: "); |
f4d5e009 | 354 | wxSocketBase *sock = event.GetSocket(); |
f85d901f | 355 | |
f4d5e009 | 356 | // First, print a message |
bc7e88ae | 357 | switch(event.GetSocketEvent()) |
f85d901f | 358 | { |
f4d5e009 GRG |
359 | case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break; |
360 | case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break; | |
361 | default : s.Append(_("Unexpected event !\n")); break; | |
f85d901f GRG |
362 | } |
363 | ||
364 | m_text->AppendText(s); | |
365 | ||
366 | // Now we process the event | |
bc7e88ae | 367 | switch(event.GetSocketEvent()) |
f85d901f GRG |
368 | { |
369 | case wxSOCKET_INPUT: | |
370 | { | |
371 | // We disable input events, so that the test doesn't trigger | |
372 | // wxSocketEvent again. | |
373 | sock->SetNotify(wxSOCKET_LOST_FLAG); | |
374 | ||
375 | // Which test are we going to run? | |
376 | unsigned char c; | |
f4d5e009 | 377 | sock->Read(&c, 1); |
f85d901f GRG |
378 | |
379 | switch (c) | |
380 | { | |
381 | case 0xBE: Test1(sock); break; | |
382 | case 0xCE: Test2(sock); break; | |
383 | case 0xDE: Test3(sock); break; | |
f4d5e009 GRG |
384 | default: |
385 | m_text->AppendText(_("Unknown test id received from client\n\n")); | |
f85d901f GRG |
386 | } |
387 | ||
388 | // Enable input events again. | |
389 | sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG); | |
390 | break; | |
391 | } | |
392 | case wxSOCKET_LOST: | |
393 | { | |
394 | m_numClients--; | |
395 | ||
bd4d918f GRG |
396 | // Destroy() should be used instead of delete wherever possible, |
397 | // due to the fact that wxSocket uses 'delayed events' (see the | |
398 | // documentation for wxPostEvent) and we don't want an event to | |
399 | // arrive to the event handler (the frame, here) after the socket | |
400 | // has been deleted. Also, we might be doing some other thing with | |
401 | // the socket at the same time; for example, we might be in the | |
402 | // middle of a test or something. Destroy() takes care of all | |
bc7e88ae | 403 | // this for us. |
bd4d918f | 404 | |
bc7e88ae | 405 | m_text->AppendText(_("Deleting socket.\n\n")); |
bd4d918f | 406 | sock->Destroy(); |
f85d901f GRG |
407 | break; |
408 | } | |
409 | default: ; | |
410 | } | |
411 | ||
412 | UpdateStatusBar(); | |
413 | } | |
414 | ||
415 | // convenience functions | |
416 | ||
417 | void MyFrame::UpdateStatusBar() | |
f4ada568 | 418 | { |
8520f137 | 419 | #if wxUSE_STATUSBAR |
f85d901f | 420 | wxString s; |
bd4d918f | 421 | s.Printf(_("%d clients connected"), m_numClients); |
f85d901f | 422 | SetStatusText(s, 1); |
8520f137 | 423 | #endif // wxUSE_STATUSBAR |
f4ada568 | 424 | } |