diff options
Diffstat (limited to 'comm/OLD')
-rw-r--r-- | comm/OLD/Server.cc | 141 |
1 files changed, 24 insertions, 117 deletions
diff --git a/comm/OLD/Server.cc b/comm/OLD/Server.cc index 82e256a..9bc6cec 100644 --- a/comm/OLD/Server.cc +++ b/comm/OLD/Server.cc @@ -17,7 +17,7 @@ /*?class UchServer The class \typ{UchServer} derives from \typ{UchStream}. It is associated to a channel set to monitor the connected clients in parallel. -Whenever a new client connects to the server, the virtual function \fun{HandleNew} is called. +Whenever a new client connects to the server, the virtual function \fun{CreateClient} is called. This function must return a pointer to an object of class \typ{UchClient} (or a derived class of \typ{UchClient}). Because the server and the clients may be on different machines with different architectures, @@ -38,21 +38,10 @@ Thus the virtual function \fun{Delete} is redefined to achieve a clean removal f // copy // -/*? -Construct an empty server port. -?*/ -UchServer :: UchServer () -: UchStream (), - Clients (), - Mpx (0) -{ -} - /*?nodoc?*/ UchServer :: UchServer (const UchServer& sp) : UchStream (sp), - Clients (), - Mpx (0) + Clients () { } @@ -62,11 +51,16 @@ When using Internet domain addresses, \var{a} should refer to the wildcard address so that clients can connect from any machine. This can be done with the instruction \com{new UchInetAddress(ANYADDR)}. ?*/ -UchServer :: UchServer (UchAddress* a) +UchServer :: UchServer (UchBaseMultiplexer& m, UchAddress* a) : UchStream (a, 0), - Clients (), - Mpx (0) + Clients () { + if (Listen () < 0) { + SysError (ErrWarn, "UchServer::UchServer"); + } else { + SetMode (IORead); + m.Add (this); + } } /*?nodoc?*/ @@ -76,7 +70,6 @@ UchServer :: ~UchServer () while (cl = Clients.First ()) cl->Delete (); - Mpx = 0; // will delete it if necessary } /*?nodoc?*/ @@ -101,36 +94,6 @@ UchServer :: RemoveClient (UchClient* cl) Error (ErrWarn, "UchServer::RemoveClient", "not owned by this server"); } -/*? -Bind the socket and listen to it. -Initialize the channel set of the server to \var{cs}, or create a new one if \var{cs} is 0. -The channel set is used by the server to read and process messages from its clients: -whenever a client connects to the server, an instance of \typ{UchClient} is added to the channel set. -Whenever data is readable from a client, it is read in an input buffer and processed as soon -as a full message is received. -Whenever a client closes the connection, it is removed from the channel set of the server. -This function returns TRUE if all went well, else it calls \fun{SysError} and returns FALSE. -You may want to pass your own channel set if you are already multiplexing -i/o on that channel set. For instance, you may have several \typ{UchServer} objects -in your application. -A smart pointer to the channel set is actually kept in the server, so that it can be shared -securely. -?*/ -bool -UchServer :: Setup (UchBaseMultiplexer* cs) -{ - if (Listen () < 0) { - SysError (ErrWarn, "UchServer::Setup"); - return FALSE; - } - SetMode (IORead); - if (! cs) - cs = new UchMultiplexer; - Mpx = cs; - Mpx->Add (this); - - return TRUE; -} /*? This function removes the server from its channel set. @@ -155,21 +118,13 @@ UchServer :: Unlisten () void UchServer :: HandleRead () { -// cannot redefine Accept to return UchClient*, so this does not work : -// UchClient* cl = Accept (); -// need to delete the channel created by Accept, so this does not work either : -// UchClient* cl = new UchClient (Accept ()); -// so we do this (less elegant ...) : - UchChannel* ch = Accept (); - if (! ch) { + int fd = Accept (); + if (fd < 0) { SysError (ErrWarn, "UchServer::HandleRead: Accept"); return; } - UchClient* cl = HandleNew (ch); - delete ch; + UchClient* cl = CreateClient (fd); - cl->SetMode (IOReadSelect); - cl->SetOwner (this); Clients.Append (cl); if (Mpx) Mpx->Add (cl); @@ -178,8 +133,8 @@ UchServer :: HandleRead () // Default virtual function for handling new connections // If a class MY_CLIENT is derived from UchClient, // a class MY_SERVER must be derived from UchServer, -// with MY_SERVER::HandleNew (UchChannel* ch) -// doing at least a return new MY_CLIENT (ch) +// with MY_SERVER::CreateClient +// doing at least a return new MY_CLIENT (fd) // /*? This virtual function is called whenever a new client connects to the server. @@ -189,9 +144,9 @@ say \typ{MY\_CLIENT}, in which case it should be redefined in a derived class of \typ{UchServer} to return \com{new MY\_CLIENT(ch)}. ?*/ UchClient* -UchServer :: HandleNew (UchChannel* ch) +UchServer :: CreateClient (int fd) { - return new UchClient (ch); + return new UchClient (*this, fd); } /*? @@ -231,29 +186,10 @@ UchServer :: Error (errtype how, const char* who, const char* what) ::Error (how, who, what); } -/*? -Setup the server if necessary, then scan and process its channel set -by calling \fun{LoopScan} for it. -To have a server active, you need at least to initialize it (and thus know its address), -and then run it. -If you have added your own channels to the channel set of the server, -their \fun{HandleRead} and \fun{HandleWrite} functions will be called normally by \fun{Run}. -?*/ -MPX_RES -UchServer :: Run () -{ - if (! Mpx) - if (! Setup ()) - Error (ErrFatal, "UchServer::Run", "could not setup"); - if (Mpx) - return Mpx->Run (); - else - return isMpxError; -} /*? Send a message to all clients currently connected to this server. -If \var{flush} is TRUE, the output buffer of each client is flushed. +If \var{flush} is true, the output buffer of each client is flushed. ?*/ void UchServer :: Broadcast (UchMessage& msg, bool flush) @@ -271,7 +207,7 @@ UchServer :: Broadcast (UchMessage& msg, bool flush) /*? Send a message to all clients currently connected to this server, except \var{exclude}. -If \var{flush} is TRUE, the output buffer of each client is flushed. +If \var{flush} is true, the output buffer of each client is flushed. ?*/ void UchServer :: Broadcast (UchMessage& msg, UchClient* excl, bool flush) @@ -330,16 +266,6 @@ UchServer :: Broadcast (UchMsgBuffer& buf, UchClient* excl, bool flush) #endif } -// clients: -// - -/*?nodoc?*/ -UchClient :: UchClient () -: UchMsgStream (), - MyServer (0) -{ -} - /*?nodoc?*/ UchClient :: UchClient (const UchClient& cl) : UchMsgStream (cl), @@ -348,13 +274,14 @@ UchClient :: UchClient (const UchClient& cl) } /*? -Construct a new client connected to the server on channel \var{ch}. +Construct a new client connected to the server on file descriptor fd. ?*/ -UchClient :: UchClient (UchChannel* ch) +UchClient :: UchClient (UchServer& s, int fd) : UchMsgStream (), - MyServer (0) + MyServer (&s) { - UchChannel::Open (ch->FilDes ()); + SetMode (IOReadSelect); + UchChannel::Open (fd); } /*?nodoc?*/ @@ -393,27 +320,7 @@ UchClient :: Delete () } } -/*?nodoc?*/ -void -UchClient :: SetOwner (UchServer* serv) -{ - if (MyServer) - serv->Error (ErrFatal, "UchClient::SetOwner", "already owned"); - else - MyServer = serv; -} - #ifdef DOC -//fake entries for inline functions - -/*? -Return the channel set used by the server. -This is useful if you want to add your own channels to the channel set. -?*/ -UchBaseMultiplexer* -UchServer :: GetMultiplexer () -{ -} /*? Return the server corresponding to a given client. |