]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | /* |
2 | * File: server.cpp | |
3 | * Purpose: wxSocket: server demo | |
4 | * Author: LAVAUX Guilhem (from minimal.cc) | |
5 | * Created: June 1997 | |
6 | * Updated: | |
7 | * Copyright: (c) 1993, AIAI, University of Edinburgh | |
8 | * (C) 1997, LAVAUX Guilhem | |
9 | */ | |
10 | #ifdef __GNUG__ | |
11 | #pragma implementation | |
12 | #pragma interface | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/wx.h" | |
24 | #endif | |
25 | #include "wx/socket.h" | |
26 | ||
27 | // Define a new application type | |
28 | class MyApp: public wxApp | |
29 | { public: | |
30 | bool OnInit(void); | |
31 | }; | |
32 | ||
33 | class MyServer; | |
34 | ||
35 | // Define a new frame type | |
36 | class MyFrame: public wxFrame | |
37 | { | |
38 | DECLARE_EVENT_TABLE() | |
39 | public: | |
40 | MyServer *sock; | |
41 | int nb_clients; | |
42 | ||
43 | MyFrame(wxFrame *frame); | |
44 | virtual ~MyFrame(); | |
45 | void Menu_Exit(wxCommandEvent& evt); | |
46 | void ExecTest1(wxSocketBase *sock_o); | |
47 | void UpdateStatus(int incr); | |
48 | }; | |
49 | ||
50 | #define SKDEMO_QUIT 101 | |
51 | ||
52 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
53 | EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit) | |
54 | END_EVENT_TABLE() | |
55 | ||
56 | class MySock: public wxSocketBase { | |
57 | public: | |
58 | MyFrame *frame; | |
59 | ||
60 | void OldOnNotify(wxRequestEvent flags); | |
61 | }; | |
62 | ||
63 | class MyServer: public wxSocketServer { | |
64 | public: | |
65 | MyFrame *frame; | |
66 | ||
67 | MyServer(wxSockAddress& addr) : wxSocketServer(addr) { } | |
68 | void OldOnNotify(wxRequestEvent flags); | |
69 | }; | |
70 | ||
71 | IMPLEMENT_APP(MyApp) | |
72 | ||
73 | // `Main program' equivalent, creating windows and returning main app frame | |
74 | bool MyApp::OnInit(void) | |
75 | { | |
76 | // Create the main frame window | |
77 | MyFrame *frame = new MyFrame(NULL); | |
78 | ||
79 | // Give it an icon | |
80 | #ifdef wx_msw | |
81 | frame->SetIcon(new wxIcon("mondrian")); | |
82 | #endif | |
83 | #ifdef wx_x | |
84 | frame->SetIcon(new wxIcon("aiai.xbm")); | |
85 | #endif | |
86 | ||
87 | // Make a menubar | |
88 | wxMenu *file_menu = new wxMenu; | |
89 | ||
90 | file_menu->Append(SKDEMO_QUIT, "E&xit"); | |
91 | wxMenuBar *menu_bar = new wxMenuBar; | |
92 | menu_bar->Append(file_menu, "File"); | |
93 | frame->SetMenuBar(menu_bar); | |
94 | ||
95 | // Make a panel with a message | |
96 | (void)new wxPanel(frame, 0, 0, 300, 100); | |
97 | ||
98 | // Show the frame | |
99 | frame->Show(TRUE); | |
100 | ||
101 | // Return the main frame window | |
102 | return TRUE; | |
103 | } | |
104 | ||
105 | void MySock::OldOnNotify(wxRequestEvent flags) | |
106 | { | |
107 | extern wxList wxPendingDelete; | |
108 | ||
109 | switch (flags) { | |
110 | case EVT_READ: | |
111 | unsigned char c; | |
112 | ||
113 | ReadMsg((char *)&c, 1); | |
114 | if (c == 0xbe) | |
115 | frame->ExecTest1(this); | |
116 | ||
117 | break; | |
118 | case EVT_LOST: | |
119 | frame->UpdateStatus(-1); | |
120 | wxPendingDelete.Append(this); | |
121 | break; | |
122 | } | |
123 | } | |
124 | ||
125 | void MyServer::OldOnNotify(wxRequestEvent WXUNUSED(flags)) | |
126 | { | |
127 | MySock *sock2 = new MySock(); | |
128 | ||
129 | if (!AcceptWith(*sock2)) | |
130 | return; | |
131 | ||
132 | m_handler->Register(sock2); | |
133 | ||
134 | sock2->SetFlags(NONE); | |
135 | sock2->frame = frame; | |
136 | sock2->SetNotify(REQ_READ | REQ_LOST); | |
137 | sock2->Notify(TRUE); | |
138 | frame->UpdateStatus(1); | |
139 | } | |
140 | ||
141 | // My frame Constructor | |
142 | MyFrame::MyFrame(wxFrame *frame): | |
143 | wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition, | |
144 | wxSize(300, 200)) | |
145 | { | |
146 | wxIPV4address addr; | |
147 | addr.Service(3000); | |
148 | ||
149 | // Init all | |
150 | wxSocketHandler::Master(); | |
151 | ||
152 | sock = new MyServer(addr); | |
153 | wxSocketHandler::Master().Register(sock); | |
154 | sock->frame = this; | |
155 | sock->SetNotify(wxSocketBase::REQ_ACCEPT); | |
156 | sock->Notify(TRUE); | |
157 | nb_clients = 0; | |
158 | ||
159 | CreateStatusBar(1); | |
160 | UpdateStatus(0); | |
161 | } | |
162 | ||
163 | MyFrame::~MyFrame() | |
164 | { | |
165 | delete sock; | |
166 | } | |
167 | ||
168 | // Intercept menu commands | |
d18ed59a | 169 | void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event)) |
f4ada568 GL |
170 | { |
171 | Close(TRUE); | |
172 | } | |
173 | ||
174 | void MyFrame::ExecTest1(wxSocketBase *sock_o) | |
175 | { | |
176 | char *buf = new char[50]; | |
177 | size_t l; | |
178 | ||
179 | l = sock_o->Read(buf, 50).LastCount(); | |
180 | sock_o->Write(buf, l); | |
181 | } | |
182 | ||
183 | void MyFrame::UpdateStatus(int incr) | |
184 | { | |
185 | char s[30]; | |
186 | nb_clients += incr; | |
187 | sprintf(s, "%d clients connected", nb_clients); | |
188 | SetStatusText(s); | |
189 | } |