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