]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
* Stream: update in doc, fix in code.
[wxWidgets.git] / samples / wxsocket / server.cpp
1 /*
2 * File: server.cpp
3 * Purpose: wxSocket: server demo
4 * Author: LAVAUX Guilhem
5 * Created: June 1997
6 * Updated:
7 * Copyright: (C) 1997, LAVAUX Guilhem
8 */
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
26 #include "wx/socket.h"
27
28 #if defined(__WXMOTIF__) || defined(__WXGTK__)
29 #include "mondrian.xpm"
30 #endif
31
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:
45 MyServer *sock;
46 int nb_clients;
47
48 MyFrame(wxFrame *frame);
49 virtual ~MyFrame();
50 void Menu_Exit(wxCommandEvent& evt);
51 void ExecTest1(wxSocketBase *sock_o);
52 void UpdateStatus(int incr);
53 };
54
55 #define SKDEMO_QUIT 101
56
57 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
58 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
59 END_EVENT_TABLE()
60
61 class MySock: public wxSocketBase {
62 public:
63 MyFrame *frame;
64
65 void OldOnNotify(wxRequestEvent flags);
66 };
67
68 class MyServer: public wxSocketServer {
69 public:
70 MyFrame *frame;
71
72 MyServer(wxSockAddress& addr) : wxSocketServer(addr) { }
73 void OldOnNotify(wxRequestEvent flags);
74 };
75
76 IMPLEMENT_APP(MyApp)
77
78 // `Main program' equivalent, creating windows and returning main app frame
79 bool MyApp::OnInit(void)
80 {
81 // Create the main frame window
82 MyFrame *frame = new MyFrame(NULL);
83
84 // Give it an icon
85 frame->SetIcon(wxICON(mondrian));
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 WXDLLEXPORT 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
169 void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
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 }