]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
Added the forgotten file txtstrm.tex
[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 * CVS Id: $Id$
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 #include "wx/thread.h"
28
29 #if defined(__WXMOTIF__) || defined(__WXGTK__)
30 #include "mondrian.xpm"
31 #endif
32
33 // Define a new application type
34 class MyApp: public wxApp
35 { public:
36 bool OnInit(void);
37 };
38
39 class MyServer;
40
41 // Define a new frame type
42 class MyFrame: public wxFrame
43 {
44 DECLARE_EVENT_TABLE()
45 public:
46 wxSocketServer *sock;
47 int nb_clients;
48
49 MyFrame(wxFrame *frame);
50 virtual ~MyFrame();
51 void Menu_Exit(wxCommandEvent& evt);
52 void OnSockRequest(wxSocketEvent& evt);
53 void OnSockRequestServer(wxSocketEvent& evt);
54 void ExecTest1(wxSocketBase *sock_o);
55 void UpdateStatus(int incr);
56 };
57
58 #define SKDEMO_QUIT 101
59 #define SKDEMO_SOCKET_SERV 102
60 #define SKDEMO_SOCKET 103
61
62 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
63 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
64 EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer)
65 EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest)
66 END_EVENT_TABLE()
67
68 IMPLEMENT_APP(MyApp)
69
70 // `Main program' equivalent, creating windows and returning main app frame
71 bool MyApp::OnInit(void)
72 {
73 // Create the main frame window
74 MyFrame *frame = new MyFrame(NULL);
75
76 // Give it an icon
77 frame->SetIcon(wxICON(mondrian));
78
79 // Make a menubar
80 wxMenu *file_menu = new wxMenu;
81
82 file_menu->Append(SKDEMO_QUIT, "E&xit");
83 wxMenuBar *menu_bar = new wxMenuBar;
84 menu_bar->Append(file_menu, "File");
85 frame->SetMenuBar(menu_bar);
86
87 // Make a panel with a message
88 (void)new wxPanel(frame, 0, 0, 300, 100);
89
90 // Show the frame
91 frame->Show(TRUE);
92
93 // Return the main frame window
94 return TRUE;
95 }
96
97 extern wxList wxPendingDelete;
98
99 void MyFrame::OnSockRequest(wxSocketEvent& evt)
100 {
101 /* this routine gets called from within the
102 waiting socket thread, i.e. here we are
103 not in the main GUI thread and thus we
104 must not call any GUI function here. */
105 /* Wrong ! This routine is called by the main GUI thread
106 because the main GUI thread received a signal from the other
107 thread using wxEvent::ProcessThreadEvent */
108
109 wxSocketBase *sock = evt.Socket();
110
111 printf("OnSockRequest OK\n");
112 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
113 switch (evt.SocketEvent()) {
114 case GSOCK_INPUT:
115 unsigned char c;
116
117 sock->Read((char *)&c, 1);
118 if (c == 0xbe)
119 ExecTest1(sock);
120
121 break;
122 case GSOCK_LOST:
123 printf("Destroying socket\n");
124 wxPendingDelete.Append(sock);
125 UpdateStatus(-1);
126 return;
127 break;
128 }
129 printf("OnSockRequest Exiting\n");
130 }
131
132 void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
133 {
134 /* this routine gets called from within the
135 waiting socket thread, i.e. here we are
136 not in the main GUI thread and thus we
137 must not call any GUI function here. */
138 /* Wrong ! This routine is called by the main GUI thread
139 because the main GUI thread received a signal from the other
140 thread using wxEvent::ProcessThreadEvent */
141
142 wxSocketBase *sock2;
143 wxSocketServer *server = (wxSocketServer *) evt.Socket();
144
145 printf("OnSockRequestServer OK\n");
146 printf("OnSockRequest (Main = %d) (event = %d)\n",wxThread::IsMain(), evt.SocketEvent());
147
148 sock2 = server->Accept();
149 if (sock2 == NULL)
150 return;
151
152 UpdateStatus(1);
153 sock2->SetFlags(wxSocketBase::NONE);
154 sock2->Notify(TRUE);
155 sock2->SetEventHandler(*this, SKDEMO_SOCKET);
156 sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
157 }
158
159 // My frame Constructor
160 MyFrame::MyFrame(wxFrame *frame):
161 wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
162 wxSize(300, 200))
163 {
164 wxIPV4address addr;
165 addr.Service(3000);
166
167 // Init all
168
169 sock = new wxSocketServer(addr);
170 sock->SetNotify(GSOCK_CONNECTION_FLAG);
171 sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
172 sock->SetFlags(wxSocketBase::SPEED);
173 sock->Notify(TRUE);
174 nb_clients = 0;
175
176 CreateStatusBar(1);
177 UpdateStatus(0);
178 }
179
180 MyFrame::~MyFrame()
181 {
182 delete sock;
183 }
184
185 // Intercept menu commands
186 void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
187 {
188 Close(TRUE);
189 }
190
191 void MyFrame::ExecTest1(wxSocketBase *sock_o)
192 {
193 char *buf = new char[50];
194 size_t l;
195
196 l = sock_o->Read(buf, 50).LastCount();
197 sock_o->Write(buf, l);
198 }
199
200 void MyFrame::UpdateStatus(int incr)
201 {
202 char s[30];
203 nb_clients += incr;
204 sprintf(s, "%d clients connected", nb_clients);
205 SetStatusText(s);
206 }