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