]>
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; | |
81 | wxPanel *m_panel; | |
82 | wxTextCtrl *m_text; | |
83 | wxMenu *m_menuFile; | |
84 | wxMenuBar *m_menuBar; | |
85 | bool m_busy; | |
86 | int m_numClients; | |
87 | ||
88 | // any class wishing to process wxWindows events must use this macro | |
89 | DECLARE_EVENT_TABLE() | |
90 | }; | |
91 | ||
92 | // -------------------------------------------------------------------------- | |
93 | // constants | |
94 | // -------------------------------------------------------------------------- | |
95 | ||
96 | // IDs for the controls and the menu commands | |
97 | enum | |
98 | { | |
99 | // menu items | |
100 | SERVER_QUIT = 1000, | |
101 | SERVER_ABOUT, | |
102 | ||
103 | // id for sockets | |
104 | SERVER_ID, | |
105 | SOCKET_ID | |
f4ada568 GL |
106 | }; |
107 | ||
f85d901f GRG |
108 | // -------------------------------------------------------------------------- |
109 | // event tables and other macros for wxWindows | |
110 | // -------------------------------------------------------------------------- | |
f4ada568 GL |
111 | |
112 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
f85d901f GRG |
113 | EVT_MENU(SERVER_QUIT, MyFrame::OnQuit) |
114 | EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout) | |
115 | EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent) | |
116 | EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) | |
f4ada568 GL |
117 | END_EVENT_TABLE() |
118 | ||
f4ada568 GL |
119 | IMPLEMENT_APP(MyApp) |
120 | ||
f4ada568 | 121 | |
f85d901f GRG |
122 | // To append sockets for delayed deletion |
123 | extern wxList wxPendingDelete; | |
124 | ||
f4ada568 | 125 | |
f85d901f GRG |
126 | // ========================================================================== |
127 | // implementation | |
128 | // ========================================================================== | |
f4ada568 | 129 | |
f85d901f GRG |
130 | // -------------------------------------------------------------------------- |
131 | // the application class | |
132 | // -------------------------------------------------------------------------- | |
f4ada568 | 133 | |
f85d901f GRG |
134 | bool MyApp::OnInit() |
135 | { | |
136 | // Create the main application window | |
137 | MyFrame *frame = new MyFrame(); | |
f4ada568 | 138 | |
f85d901f | 139 | // Show it and tell the application that it's our main window |
f4ada568 | 140 | frame->Show(TRUE); |
f85d901f | 141 | SetTopWindow(frame); |
2b98cb1f | 142 | |
f85d901f | 143 | // success |
f4ada568 GL |
144 | return TRUE; |
145 | } | |
146 | ||
f85d901f GRG |
147 | // -------------------------------------------------------------------------- |
148 | // main frame | |
149 | // -------------------------------------------------------------------------- | |
a737331d | 150 | |
f85d901f GRG |
151 | // frame constructor |
152 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, | |
153 | _T("wxSocket demo: Server"), | |
154 | wxDefaultPosition, wxSize(300, 200)) | |
f4ada568 | 155 | { |
f85d901f GRG |
156 | // Give the frame an icon |
157 | SetIcon(wxICON(mondrian)); | |
158 | ||
159 | // Make menus | |
160 | m_menuFile = new wxMenu(); | |
161 | m_menuFile->Append(SERVER_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog")); | |
162 | m_menuFile->AppendSeparator(); | |
163 | m_menuFile->Append(SERVER_QUIT, _T("E&xit\tAlt-X"), _T("Quit server")); | |
164 | ||
165 | // Append menus to the menubar | |
166 | m_menuBar = new wxMenuBar(); | |
167 | m_menuBar->Append(m_menuFile, _T("&File")); | |
168 | SetMenuBar(m_menuBar); | |
169 | ||
170 | // Status bar | |
171 | CreateStatusBar(2); | |
172 | ||
173 | // Make a panel with a textctrl in it | |
174 | m_panel = new wxPanel(this, -1, wxPoint(0, 0), GetClientSize()); | |
175 | m_text = new wxTextCtrl(m_panel, -1, | |
176 | _T("Welcome to wxSocket demo: Server\n"), | |
177 | wxPoint(0, 0), m_panel->GetClientSize(), | |
178 | wxTE_MULTILINE | wxTE_READONLY); | |
179 | ||
180 | // Create the socket | |
181 | wxIPV4address addr; | |
182 | addr.Service(3000); | |
55e84507 | 183 | addr.LocalHost(); |
f85d901f GRG |
184 | |
185 | m_server = new wxSocketServer(addr); | |
186 | m_server->SetEventHandler(*this, SERVER_ID); | |
187 | m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); | |
188 | m_server->Notify(TRUE); | |
189 | ||
190 | // We use Ok() here to see if the server is really listening | |
191 | if (m_server->Ok()) | |
192 | m_text->AppendText(_T("Server listening.\n\n")); | |
193 | else | |
194 | m_text->AppendText(_T("Could not listen at the specified port !\n\n")); | |
195 | ||
196 | m_busy = FALSE; | |
197 | m_numClients = 0; | |
198 | UpdateStatusBar(); | |
f4ada568 GL |
199 | } |
200 | ||
f85d901f | 201 | MyFrame::~MyFrame() |
f4ada568 | 202 | { |
f85d901f GRG |
203 | delete m_server; |
204 | } | |
f4ada568 | 205 | |
f85d901f | 206 | // event handlers |
f4ada568 | 207 | |
f85d901f GRG |
208 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
209 | { | |
210 | // TRUE is to force the frame to close | |
211 | Close(TRUE); | |
212 | } | |
f4ada568 | 213 | |
f85d901f GRG |
214 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
215 | { | |
216 | wxMessageBox(_T("wxSocket demo: Server\n") | |
217 | _T("(c) 1999 Guillermo Rodriguez Garcia\n"), | |
218 | _T("About Server"), | |
219 | wxOK | wxICON_INFORMATION, this); | |
f4ada568 GL |
220 | } |
221 | ||
f85d901f | 222 | void MyFrame::Test1(wxSocketBase *sock) |
f4ada568 | 223 | { |
f85d901f GRG |
224 | unsigned char len; |
225 | char *buf; | |
f4ada568 | 226 | |
f85d901f | 227 | m_text->AppendText(_T("Test 1 begins\n")); |
f4ada568 | 228 | |
f85d901f GRG |
229 | // Receive data from socket and send it back. We will first |
230 | // get a byte with the buffer size, so we can specify the | |
231 | // exact size and use the WAITALL flag. Also, we disabled | |
232 | // input events so we won't have unwanted reentrance. This | |
233 | // way we can avoid the infamous BLOCK (formerly SPEED) flag. | |
234 | // | |
235 | sock->SetFlags(wxSOCKET_WAITALL); | |
236 | ||
237 | sock->Read((char *)&len, 1); | |
238 | ||
239 | buf = (char *)malloc(len); | |
240 | sock->Read(buf, len); | |
241 | sock->Write(buf, len); | |
242 | free(buf); | |
243 | ||
244 | m_text->AppendText(_T("Test 1 ends\n")); | |
f4ada568 GL |
245 | } |
246 | ||
f85d901f | 247 | void MyFrame::Test2(wxSocketBase *sock) |
f4ada568 | 248 | { |
f85d901f GRG |
249 | #define MAX_MSG_SIZE 10000 |
250 | ||
251 | wxString s; | |
252 | char *buf = (char *)malloc(MAX_MSG_SIZE); | |
253 | wxUint32 len; | |
254 | ||
255 | m_text->AppendText(_T("Test 2 begins\n")); | |
256 | ||
257 | // We don't need to set flags because ReadMsg and WriteMsg | |
258 | // are not affected by them anyway. | |
259 | // | |
260 | len = sock->ReadMsg(buf, MAX_MSG_SIZE).LastCount(); | |
261 | ||
262 | s.Printf(_T("Client says: %s\n"), buf); | |
263 | m_text->AppendText(s); | |
264 | ||
265 | sock->WriteMsg(buf, len); | |
266 | free(buf); | |
267 | ||
268 | m_text->AppendText(_T("Test 2 ends\n")); | |
269 | ||
270 | #undef MAX_MSG_SIZE | |
f4ada568 GL |
271 | } |
272 | ||
f85d901f | 273 | void MyFrame::Test3(wxSocketBase *sock) |
f4ada568 | 274 | { |
f85d901f GRG |
275 | m_text->AppendText(_T("Test 3 begins\n")); |
276 | m_text->AppendText(_T("(not implemented)\n")); | |
277 | m_text->AppendText(_T("Test 3 ends\n")); | |
f4ada568 GL |
278 | } |
279 | ||
f85d901f | 280 | void MyFrame::OnServerEvent(wxSocketEvent& event) |
f4ada568 | 281 | { |
f85d901f GRG |
282 | wxString s = _T("OnServerEvent: "); |
283 | wxSocketBase *sock; | |
284 | ||
285 | switch(event.SocketEvent()) | |
286 | { | |
287 | case wxSOCKET_CONNECTION : s.Append(_T("wxSOCKET_CONNECTION\n")); break; | |
288 | default : s.Append(_T("Unexpected event !\n")); break; | |
289 | } | |
f4ada568 | 290 | |
f85d901f | 291 | m_text->AppendText(s); |
765e386b | 292 | |
f85d901f GRG |
293 | // Accept new connection if there is one in the pending |
294 | // connections queue, else exit. We use Accept(FALSE) for | |
295 | // non-blocking accept (although if we got here, there | |
296 | // should ALWAYS be a pending connection). | |
297 | // | |
298 | sock = m_server->Accept(FALSE); | |
299 | ||
300 | if (sock) | |
301 | { | |
302 | m_text->AppendText(_T("New client connection accepted\n")); | |
303 | } | |
304 | else | |
305 | { | |
306 | m_text->AppendText(_T("Error: couldn't accept a new connection")); | |
307 | return; | |
308 | } | |
309 | ||
310 | sock->SetEventHandler(*this, SOCKET_ID); | |
311 | sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); | |
312 | sock->Notify(TRUE); | |
313 | ||
314 | m_numClients++; | |
315 | UpdateStatusBar(); | |
f4ada568 GL |
316 | } |
317 | ||
f85d901f GRG |
318 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
319 | { | |
320 | wxSocketBase *sock = event.Socket(); | |
321 | wxString s = _T("OnSocketEvent: "); | |
322 | ||
323 | // We first print a msg | |
324 | switch(event.SocketEvent()) | |
325 | { | |
326 | case wxSOCKET_INPUT: s.Append(_T("wxSOCKET_INPUT\n")); break; | |
327 | case wxSOCKET_LOST: s.Append(_T("wxSOCKET_LOST\n")); break; | |
328 | default: s.Append(_T("unexpected event !\n")); | |
329 | } | |
330 | ||
331 | m_text->AppendText(s); | |
332 | ||
333 | // Now we process the event | |
334 | switch(event.SocketEvent()) | |
335 | { | |
336 | case wxSOCKET_INPUT: | |
337 | { | |
338 | // We disable input events, so that the test doesn't trigger | |
339 | // wxSocketEvent again. | |
340 | sock->SetNotify(wxSOCKET_LOST_FLAG); | |
341 | ||
342 | // Which test are we going to run? | |
343 | unsigned char c; | |
344 | sock->Read((char *)&c ,1); | |
345 | ||
346 | switch (c) | |
347 | { | |
348 | case 0xBE: Test1(sock); break; | |
349 | case 0xCE: Test2(sock); break; | |
350 | case 0xDE: Test3(sock); break; | |
351 | default: s.Append(_T("Unknown test id received from client\n")); | |
352 | } | |
353 | ||
354 | // Enable input events again. | |
355 | sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG); | |
356 | break; | |
357 | } | |
358 | case wxSOCKET_LOST: | |
359 | { | |
360 | m_numClients--; | |
361 | ||
362 | // We cannot delete the socket right now because we can | |
363 | // be in the middle of a test or something. So we append | |
364 | // it to the list of objects to be deleted. | |
365 | m_text->AppendText(_T("Deleting socket.\n")); | |
366 | wxPendingDelete.Append(sock); | |
367 | break; | |
368 | } | |
369 | default: ; | |
370 | } | |
371 | ||
372 | UpdateStatusBar(); | |
373 | } | |
374 | ||
375 | // convenience functions | |
376 | ||
377 | void MyFrame::UpdateStatusBar() | |
f4ada568 | 378 | { |
f85d901f GRG |
379 | wxString s; |
380 | s.Printf(_T("%d clients connected"), m_numClients); | |
381 | SetStatusText(s, 1); | |
f4ada568 | 382 | } |