1998-09-06 14:28:00 -04:00
|
|
|
/*
|
|
|
|
* File: server.cpp
|
|
|
|
* Purpose: wxSocket: server demo
|
1999-01-25 13:33:08 -05:00
|
|
|
* Author: LAVAUX Guilhem
|
1998-09-06 14:28:00 -04:00
|
|
|
* Created: June 1997
|
1999-05-25 13:14:56 -04:00
|
|
|
* CVS Id: $Id$
|
1999-01-25 13:33:08 -05:00
|
|
|
* Copyright: (C) 1997, LAVAUX Guilhem
|
1998-09-06 14:28:00 -04:00
|
|
|
*/
|
1999-01-25 13:33:08 -05:00
|
|
|
|
1998-09-06 14:28:00 -04:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
1999-01-25 13:33:08 -05:00
|
|
|
|
1998-09-06 14:28:00 -04:00
|
|
|
#include "wx/socket.h"
|
1999-05-22 06:11:03 -04:00
|
|
|
#include "wx/thread.h"
|
1998-09-06 14:28:00 -04:00
|
|
|
|
1999-01-25 13:33:08 -05:00
|
|
|
#if defined(__WXMOTIF__) || defined(__WXGTK__)
|
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
1998-09-06 14:28:00 -04:00
|
|
|
// Define a new application type
|
|
|
|
class MyApp: public wxApp
|
|
|
|
{ public:
|
|
|
|
bool OnInit(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyServer;
|
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
public:
|
1999-04-26 14:16:56 -04:00
|
|
|
wxSocketServer *sock;
|
1998-09-06 14:28:00 -04:00
|
|
|
int nb_clients;
|
|
|
|
|
|
|
|
MyFrame(wxFrame *frame);
|
|
|
|
virtual ~MyFrame();
|
|
|
|
void Menu_Exit(wxCommandEvent& evt);
|
1999-04-26 14:16:56 -04:00
|
|
|
void OnSockRequest(wxSocketEvent& evt);
|
|
|
|
void OnSockRequestServer(wxSocketEvent& evt);
|
1998-09-06 14:28:00 -04:00
|
|
|
void ExecTest1(wxSocketBase *sock_o);
|
|
|
|
void UpdateStatus(int incr);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SKDEMO_QUIT 101
|
1999-04-26 14:16:56 -04:00
|
|
|
#define SKDEMO_SOCKET_SERV 102
|
|
|
|
#define SKDEMO_SOCKET 103
|
1998-09-06 14:28:00 -04:00
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
|
1999-04-26 14:16:56 -04:00
|
|
|
EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer)
|
|
|
|
EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest)
|
1998-09-06 14:28:00 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
bool MyApp::OnInit(void)
|
|
|
|
{
|
|
|
|
// Create the main frame window
|
|
|
|
MyFrame *frame = new MyFrame(NULL);
|
|
|
|
|
|
|
|
// Give it an icon
|
1999-01-25 13:33:08 -05:00
|
|
|
frame->SetIcon(wxICON(mondrian));
|
1998-09-06 14:28:00 -04:00
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
|
|
file_menu->Append(SKDEMO_QUIT, "E&xit");
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "File");
|
|
|
|
frame->SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
// Make a panel with a message
|
|
|
|
(void)new wxPanel(frame, 0, 0, 300, 100);
|
|
|
|
|
|
|
|
// Show the frame
|
|
|
|
frame->Show(TRUE);
|
|
|
|
|
|
|
|
// Return the main frame window
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
extern wxList wxPendingDelete;
|
|
|
|
|
|
|
|
void MyFrame::OnSockRequest(wxSocketEvent& evt)
|
1998-09-06 14:28:00 -04:00
|
|
|
{
|
1999-05-22 06:11:03 -04:00
|
|
|
/* this routine gets called from within the
|
|
|
|
waiting socket thread, i.e. here we are
|
|
|
|
not in the main GUI thread and thus we
|
|
|
|
must not call any GUI function here. */
|
1999-05-25 13:14:56 -04:00
|
|
|
/* Wrong ! This routine is called by the main GUI thread
|
|
|
|
because the main GUI thread received a signal from the other
|
|
|
|
thread using wxEvent::ProcessThreadEvent */
|
1999-05-22 06:11:03 -04:00
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
wxSocketBase *sock = evt.Socket();
|
1998-09-06 14:28:00 -04:00
|
|
|
|
1999-07-28 13:29:59 -04:00
|
|
|
wxPrintf(_T("OnSockRequest OK\n"));
|
|
|
|
wxPrintf(_T("OnSockRequest (event = %d)\n"),evt.SocketEvent());
|
1999-04-26 14:16:56 -04:00
|
|
|
switch (evt.SocketEvent()) {
|
1999-07-22 13:51:54 -04:00
|
|
|
case GSOCK_INPUT:
|
1998-09-06 14:28:00 -04:00
|
|
|
unsigned char c;
|
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
sock->Read((char *)&c, 1);
|
1998-09-06 14:28:00 -04:00
|
|
|
if (c == 0xbe)
|
1999-04-26 14:16:56 -04:00
|
|
|
ExecTest1(sock);
|
1998-09-06 14:28:00 -04:00
|
|
|
|
|
|
|
break;
|
1999-07-22 13:51:54 -04:00
|
|
|
case GSOCK_LOST:
|
1999-07-28 13:29:59 -04:00
|
|
|
wxPrintf(_T("Destroying socket\n"));
|
1999-04-26 14:16:56 -04:00
|
|
|
wxPendingDelete.Append(sock);
|
1999-05-25 13:14:56 -04:00
|
|
|
UpdateStatus(-1);
|
1999-04-26 14:16:56 -04:00
|
|
|
return;
|
1998-09-06 14:28:00 -04:00
|
|
|
break;
|
1999-07-28 13:29:59 -04:00
|
|
|
default:
|
|
|
|
wxPrintf(_T("Invalid event !\n"));
|
1998-09-06 14:28:00 -04:00
|
|
|
}
|
1999-07-28 13:29:59 -04:00
|
|
|
wxPrintf(_T("OnSockRequest Exiting\n"));
|
1998-09-06 14:28:00 -04:00
|
|
|
}
|
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
|
1998-09-06 14:28:00 -04:00
|
|
|
{
|
1999-05-22 06:11:03 -04:00
|
|
|
/* this routine gets called from within the
|
|
|
|
waiting socket thread, i.e. here we are
|
|
|
|
not in the main GUI thread and thus we
|
|
|
|
must not call any GUI function here. */
|
1999-05-25 13:14:56 -04:00
|
|
|
/* Wrong ! This routine is called by the main GUI thread
|
|
|
|
because the main GUI thread received a signal from the other
|
|
|
|
thread using wxEvent::ProcessThreadEvent */
|
1999-05-22 06:11:03 -04:00
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
wxSocketBase *sock2;
|
|
|
|
wxSocketServer *server = (wxSocketServer *) evt.Socket();
|
1998-09-06 14:28:00 -04:00
|
|
|
|
1999-07-28 13:29:59 -04:00
|
|
|
wxPrintf(_T("OnSockRequestServer OK\n"));
|
|
|
|
wxPrintf(_T("OnSockRequest (Main = %d) (event = %d)\n"),wxThread::IsMain(), evt.SocketEvent());
|
1998-09-06 14:28:00 -04:00
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
sock2 = server->Accept();
|
|
|
|
if (sock2 == NULL)
|
|
|
|
return;
|
1998-09-06 14:28:00 -04:00
|
|
|
|
1999-05-25 13:14:56 -04:00
|
|
|
UpdateStatus(1);
|
1999-07-22 13:51:54 -04:00
|
|
|
sock2->SetFlags(wxSocketBase::NONE);
|
1998-09-06 14:28:00 -04:00
|
|
|
sock2->Notify(TRUE);
|
1999-04-26 14:16:56 -04:00
|
|
|
sock2->SetEventHandler(*this, SKDEMO_SOCKET);
|
1999-07-22 13:51:54 -04:00
|
|
|
sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
|
1998-09-06 14:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// My frame Constructor
|
|
|
|
MyFrame::MyFrame(wxFrame *frame):
|
|
|
|
wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
|
|
|
|
wxSize(300, 200))
|
|
|
|
{
|
|
|
|
wxIPV4address addr;
|
|
|
|
addr.Service(3000);
|
|
|
|
|
|
|
|
// Init all
|
|
|
|
|
1999-04-26 14:16:56 -04:00
|
|
|
sock = new wxSocketServer(addr);
|
1999-07-22 13:51:54 -04:00
|
|
|
sock->SetNotify(GSOCK_CONNECTION_FLAG);
|
1999-04-26 14:16:56 -04:00
|
|
|
sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
|
1999-05-17 14:10:57 -04:00
|
|
|
sock->SetFlags(wxSocketBase::SPEED);
|
1998-09-06 14:28:00 -04:00
|
|
|
sock->Notify(TRUE);
|
|
|
|
nb_clients = 0;
|
|
|
|
|
|
|
|
CreateStatusBar(1);
|
|
|
|
UpdateStatus(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
delete sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept menu commands
|
1998-11-08 11:42:20 -05:00
|
|
|
void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 14:28:00 -04:00
|
|
|
{
|
|
|
|
Close(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::ExecTest1(wxSocketBase *sock_o)
|
|
|
|
{
|
|
|
|
char *buf = new char[50];
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = sock_o->Read(buf, 50).LastCount();
|
|
|
|
sock_o->Write(buf, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::UpdateStatus(int incr)
|
|
|
|
{
|
1999-07-28 13:29:59 -04:00
|
|
|
wxChar s[30];
|
1998-09-06 14:28:00 -04:00
|
|
|
nb_clients += incr;
|
1999-07-28 13:29:59 -04:00
|
|
|
wxSprintf(s, _T("%d clients connected"), nb_clients);
|
1998-09-06 14:28:00 -04:00
|
|
|
SetStatusText(s);
|
|
|
|
}
|