home *** CD-ROM | disk | FTP | other *** search
- Save Format v1.3
- @begin ClassFile "ClientWorker"
- Exported 0;
-
- @begin-code BaseClassList
-
- public WObject
-
- @end-code;
-
- @begin UserFunction "ClientWorker()"
- GencodeSrcLine 20;
- FunctionName "ClientWorker::ClientWorker()";
- @end;
-
- @begin UserFunction "Prototype for ClientWorker()"
- Private 1;
- GencodeSrcLine 27;
- FunctionName "ClientWorker::Prototype for ClientWorker()";
- @end;
-
- @begin UserFunction "~ClientWorker()"
- GencodeSrcLine 26;
- FunctionName "ClientWorker::~ClientWorker()";
- @end;
-
- @begin UserFunction "Prototype for ~ClientWorker()"
- Private 1;
- GencodeSrcLine 29;
- FunctionName "ClientWorker::Prototype for ~ClientWorker()";
- @end;
-
- @begin UserFunction "Create( Form1 * displayWindow, WInt i )"
- GencodeSrcLine 30;
- FunctionName "ClientWorker::Create( Form1 * displayWindow, WInt i )";
- @end;
-
- @begin UserFunction "Prototype for Create( Form1 * displayWindow, WInt i )"
- Private 1;
- GencodeSrcLine 31;
- FunctionName "ClientWorker::Prototype for Create( Form1 * displayWindow, WInt i )";
- @end;
-
- @begin UserFunction "Client( void * )"
- Private 1;
- GencodeSrcLine 42;
- FunctionName "ClientWorker::Client( void * )";
- @end;
-
- @begin UserFunction "Prototype for Client( void * )"
- Private 1;
- GencodeSrcLine 33;
- FunctionName "ClientWorker::Prototype for Client( void * )";
- @end;
-
- @begin HPPPrefixBlock
- @begin-code HPPPrefix
-
- // Declarations added here will be included at the top of the .HPP file
-
- class Form1;
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin CPPPrefixBlock
- @begin-code CPPPrefix
-
- // Code added here will be included at the top of the .CPP file
- #include "Form1.hpp"
- #include "ServerWorker.hpp"
- #define MAX_BUFFER_LENGTH 500
- extern template WVector< WSocket >;
- extern template WVector< ClientWorker >;
-
- // Include definitions for resources.
- #include "WRes.h"
-
- @end-code;
- GencodeSrcLine 11;
- @end;
-
- @begin ClassContentsBlock
- @begin-code ClassContents
-
- public:
- // add your public instance data here
- private:
- // add your private instance data here
- WThread _thread;
- Form1 * _displayWindow;
- WInt _id;
- protected:
- // add your protected instance data here
-
- @end-code;
- GencodeSrcLine 18;
- @end;
-
- @begin-code GeneratedClassContents
-
-
- @end-code;
-
- @begin-code Code "ClientWorker::ClientWorker()"
-
- @@CLASSNAME@::@CLASSNAME@()
- : _displayWindow( NULL ),
- _id( 0 )
- {
-
- }
-
- @end-code;
-
- @begin-code Code "ClientWorker::Prototype for ClientWorker()"
-
- public:
- @@CLASSNAME@();
-
- @end-code;
-
- @begin-code Code "ClientWorker::~ClientWorker()"
-
- @@CLASSNAME@::~@CLASSNAME@()
- {
-
- }
-
- @end-code;
-
- @begin-code Code "ClientWorker::Prototype for ~ClientWorker()"
-
- public:
- ~@CLASSNAME@();
-
- @end-code;
-
- @begin-code Code "ClientWorker::Create( Form1 * displayWindow, WInt i )"
-
- WBool @CLASSNAME@::Create( Form1 * displayWindow, WInt i )
- {
- WBool success;
-
- _displayWindow = displayWindow;
- _id = i;
- success =
- _thread.Create( this,
- (WThreadRoutine)&@CLASSNAME@::Client,
- NULL );
- return success;
- }
-
- @end-code;
-
- @begin-code Code "ClientWorker::Prototype for Create( Form1 * displayWindow, WInt i )"
-
- public:
- WBool Create( Form1 * displayWindow, WInt i );
-
- @end-code;
-
- @begin-code Code "ClientWorker::Client( void * )"
-
- WDWord @CLASSNAME@::Client( void * )
- {
- WInt bytes_received;
- WBuffer buffer;
- WBuffer buffer_2;
- WString welcome;
- WString msg;
- WSocket * my_sock;
- WInt num_clients;
- WString my_name;
-
- ServerWorker::_client_sockets_cs.Enter();
- my_sock = ServerWorker::_client_sockets[_id];
- ServerWorker::_client_sockets_cs.Exit();
- my_name = my_sock->GetRemoteHostName();
-
- // Send welcome message to all clients.
- welcome.SetText( WTEXT( "Welcome " ) );
- welcome.Concat( my_name );
- buffer.Create( welcome.GetSize(), (void *)welcome.GetText() );
- ServerWorker::_client_sockets_cs.Enter();
- num_clients = ServerWorker::_client_sockets.GetCount();
- for ( WInt i = 0; i < num_clients; i += 1 ) {
- ServerWorker::_client_sockets[i]->
- Send( buffer );
- }
- ServerWorker::_client_sockets_cs.Exit();
-
- buffer.Clear();
- buffer.Create( (WULong)MAX_BUFFER_LENGTH );
- // Read messages from client, and send to all clients.
- while ( TRUE ) {
- bytes_received =
- my_sock->Receive( buffer );
- if ( bytes_received < 0 ) {
- msg.Sprintf( WTEXT( "Client %d receive error!" ),
- _id );
- _displayWindow->AddLog( msg.GetText() );
- break;
- }
- if ( bytes_received == 0 ) {
- msg.Sprintf( WTEXT( "Client %d closed." ),
- _id );
- _displayWindow->AddLog( msg.GetText() );
- break;
- }
- //msg.Sprintf( WTEXT( "Client %d received %d bytes." ),
- // _id, bytes_received );
- //_displayWindow->AddLog( msg.GetText() );
- msg.SetText( my_name.GetText() );
- msg.Concat( WTEXT( ": " ) );
- msg.Concat(
- WString( (WChar *)buffer.GetBuffer(),
- bytes_received ) );
- buffer_2.Create(
- bytes_received + my_name.GetSize() + 2,
- (void *)msg.GetText() );
- // Send buffer received to all clients.
- ServerWorker::_client_sockets_cs.Enter();
- num_clients = ServerWorker::_client_sockets.GetCount();
- for ( WInt i = 0; i < num_clients; i += 1 ) {
- ServerWorker::_client_sockets[i]->
- Send( buffer_2 );
- }
- buffer_2.Clear();
- ServerWorker::_client_sockets_cs.Exit();
- }
- my_sock->Close( TRUE );
- ServerWorker::_client_sockets_cs.Enter();
- ServerWorker::_client_sockets.Remove( my_sock );
- ServerWorker::_client_workers.Remove( this );
- ServerWorker::_client_sockets_cs.Exit();
- return 0;
- }
-
- @end-code;
-
- @begin-code Code "ClientWorker::Prototype for Client( void * )"
-
- private:
- WDWord Client( void * );
-
- @end-code;
- @end;
-