From ba066c34dde204aa192d03a23a81356374d93731 Mon Sep 17 00:00:00 2001 From: chatty Date: Wed, 7 Apr 1993 11:50:31 +0000 Subject: Initial revision --- comm/Stream.cc | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 comm/Stream.cc (limited to 'comm/Stream.cc') diff --git a/comm/Stream.cc b/comm/Stream.cc new file mode 100644 index 0000000..f03e298 --- /dev/null +++ b/comm/Stream.cc @@ -0,0 +1,124 @@ +/* + * The Unix Channel + * + * by Michel Beaudouin-Lafon + * + * Copyright 1990-1993 + * Laboratoire de Recherche en Informatique (LRI) + * + * Streams + * + * $Id$ + * $CurLog$ + */ + +#include "Stream.h" +#include +#include + +/*?class UchStream +Streams implement reliable point to point connections. +UchStream connections do not keep the message boundaries: +several messages can be read at a time, and a message can be read in several parts. + +Establishing a stream connection is not a symmetric operation: +one process (the server) creates a bound socket and listens to it (with function \fun{Listen}); +other processes (the clients) create sockets and connect them to the address of the server. +Each such connection is accepted (\fun{SockAccept}) by the server and results in the creation of a new socket. +Communication with each client will then proceed on this channel, +while the server still can accept connections. + +Because a server has to listen to pending connections and communicate +with its connected clients, using a channel set is highly recommended: +when a connection is pending, the channel is ready for reading, +and you can accept the connection with \fun{SockAccept} (instead of reading data). +Note that because \typ{UchStream} derives from \typ{UchSocket} +and thus from \typ{UchChannel}, the virtual functions +\fun{HandleRead} and \fun{HandeWrite} can be used. +?*/ + +/*?nextdoc?*/ +UchStream :: UchStream () +: UchSocket () +{ +} + +/*? +These constructors are similar to those of the class \typ{UchSocket}. +?*/ +UchStream :: UchStream (UchAddress* bound, UchAddress* connected) +: UchSocket (bound, connected) +{ +} + +/*?nodoc?*/ +UchStream :: UchStream (const UchStream& s) +: UchSocket (s) +{ +} + +/*?nodoc?*/ +UchStream :: ~UchStream () +{ +} + +/*?nodoc?*/ +UchChannel* +UchStream :: Copy () const +{ + return new UchStream (*this); +} + +/*?nodoc?*/ +int +UchStream :: SockType () +{ + return SOCK_STREAM; +} + +/*? +This function implements the Unix system call \fun{listen}. +\var{n} is the maximum number of pending connections: +if \var{n} clients are waiting for the server to accept their connection, +the next client will have its connection refused. +You can safely use the default value. +The stream socket is first set up (with \fun{Setup}), and then the system call is performed. +The returned value is that of the system call, unless the set up failed in which case +-1 is returned. +Note that it is useless to listen on a socket if it is not bound to an address. +?*/ +int +UchStream :: Listen (int n) +{ + if (! Setup ()) + return -1; + return listen (FilDes (), n); +} + +/*? +This function implements the Unix system call \fun{accept}. +The stream socket is first set up (with \fun{Setup}), and then the system call is performed. +A new dynamically created socket is returned, connected to the address returned by \fun{accept}. +If the socket could not be set up or a system error occurred, 0 is returned. +Note that in order to accept connections, the socket must be listen to, +and thus an address must be bound to it. +?*/ +UchSocket* +UchStream :: SockAccept () +{ + GEN_ADDR addr; + int alen = sizeof (addr); + int fd; + + if (! Setup ()) + return (UchSocket*) 0; + + if ((fd = accept (FilDes (), &addr.sa, &alen)) < 0) + return (UchSocket*) 0; + + UchSocket* s = new UchSocket (0, UchAddress::Decode (&addr, alen)); + s -> UchChannel :: Open (fd); + return s; +} + + -- cgit v1.1