summaryrefslogtreecommitdiff
path: root/comm/Stream.cc
diff options
context:
space:
mode:
authorchatty1993-04-07 11:50:31 +0000
committerchatty1993-04-07 11:50:31 +0000
commitba066c34dde204aa192d03a23a81356374d93731 (patch)
tree39391f6235d2cf8a59a0634ac5ea430cdd21f5d4 /comm/Stream.cc
parent05ab076e1c2a9ca16472f9a6b47b8d22914b3783 (diff)
downloadivy-league-ba066c34dde204aa192d03a23a81356374d93731.zip
ivy-league-ba066c34dde204aa192d03a23a81356374d93731.tar.gz
ivy-league-ba066c34dde204aa192d03a23a81356374d93731.tar.bz2
ivy-league-ba066c34dde204aa192d03a23a81356374d93731.tar.xz
Initial revision
Diffstat (limited to 'comm/Stream.cc')
-rw-r--r--comm/Stream.cc124
1 files changed, 124 insertions, 0 deletions
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 <stdlib.h>
+#include <sys/socket.h>
+
+/*?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;
+}
+
+