summaryrefslogtreecommitdiff
path: root/comm/Channel.h
diff options
context:
space:
mode:
authorchatty1993-04-07 11:50:31 +0000
committerchatty1993-04-07 11:50:31 +0000
commitba066c34dde204aa192d03a23a81356374d93731 (patch)
tree39391f6235d2cf8a59a0634ac5ea430cdd21f5d4 /comm/Channel.h
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/Channel.h')
-rw-r--r--comm/Channel.h114
1 files changed, 114 insertions, 0 deletions
diff --git a/comm/Channel.h b/comm/Channel.h
new file mode 100644
index 0000000..302d05d
--- /dev/null
+++ b/comm/Channel.h
@@ -0,0 +1,114 @@
+/*
+ * The Unix Channel
+ *
+ * by Michel Beaudouin-Lafon
+ *
+ * Copyright 1990-1993
+ * Laboratoire de Recherche en Informatique (LRI)
+ *
+ * File descriptors, channels
+ *
+ * $Id$
+ * $CurLog$
+ */
+
+#ifndef Channel_H_
+#define Channel_H_
+
+#include "cplus_bugs.h"
+#include "global.h"
+#include "ccu/SmartPointer.h"
+
+// we need read and write system calls
+#include <unistd.h>
+
+class UchMsgBuffer;
+
+#ifndef NFILE
+#define NFILE 20
+#endif
+
+enum IOMODE { IONone = 0, IORead = 1, IOWrite = 2,
+ IOReadWrite = 3, IOSelect = 4, IOReadSelect = 5,
+ IOWriteSelect = 6, IOAll = 7 };
+
+// Declarations for inlines
+//
+extern void _AddRef (int);
+extern void _DelRef (int);
+
+extern char* StrReprBuf;
+
+class UchFilDes {
+protected:
+ int Fd;
+public:
+inline UchFilDes () : Fd (-1) { }
+inline UchFilDes (const UchFilDes& fd) { _AddRef (Fd = fd.Fd); }
+inline UchFilDes (int fd) { _AddRef (Fd = fd); }
+inline ~UchFilDes () { _DelRef (Fd); }
+
+inline operator int () const { return Fd; }
+inline UchFilDes& operator = (const UchFilDes& fd) { _DelRef (Fd); Fd = fd.Fd; _AddRef (Fd); return *this; }
+inline int FilDes () { return Fd; }
+inline void Open (int fd) { _DelRef (Fd); _AddRef (Fd = fd); }
+inline void Close () { _DelRef (Fd); Fd = -1; }
+inline bool Opened () { return bool (Fd >= 0); }
+inline int Read (byte* b, int n) { return read (Fd, (char*) b, n); }
+inline int Write (byte* b, int n) { return write (Fd, (const char*) b, n); }
+ char* StrRepr (char* = StrReprBuf);
+};
+
+// This class defines channels, ie file descriptors with handlers for read/write.
+// The file descriptor of a channel is immutable: once created, it cannot change;
+// this is crucial for channel sets, for instance.
+// However, because Copy and operator= are defined,immutability can be
+// potentially bypassed...
+// UchChannel also derives from CcuSmartData, for building smart pointers
+//
+class UchChannel;
+typedef void (* CH_HANDLER) (UchChannel *);
+class UchMultiplexer;
+
+class UchChannel : public CcuSmartData {
+friend class UchMultiplexer;
+protected:
+ UchFilDes Fd;
+ IOMODE Mode;
+
+ void AddNotify (UchMultiplexer&);
+ void RemoveNotify (UchMultiplexer&);
+
+public:
+ UchChannel ();
+ UchChannel (int, IOMODE = IOReadWrite);
+ UchChannel (const UchChannel& ch);
+ ~UchChannel ();
+
+inline void Open (int fd) { if (Fd < 0) Fd.Open (fd); }
+inline IOMODE IOMode () const { return Mode; }
+inline void SetMode (IOMODE io) { Mode = io; }
+ int Read (UchMsgBuffer&);
+ int Write (UchMsgBuffer&);
+ bool ReadBuffer (UchMsgBuffer&);
+ bool WriteBuffer (UchMsgBuffer&);
+ UchChannel* Accept ();
+virtual UchChannel* Copy () const;
+virtual void HandleWrite ();
+virtual void HandleRead ();
+virtual bool HandleSelect ();
+
+inline int FilDes () const { return Fd; }
+inline int Read (byte* b, int n) { return read (Fd, (char*) b, n); }
+inline int Write (byte* b, int n) { return write (Fd, (const char*) b, n); }
+inline operator int () { return Fd; }
+inline char* StrRepr (char* s = StrReprBuf) { return Fd.StrRepr (s); }
+
+static CH_HANDLER ReadHandler;
+static CH_HANDLER WriteHandler;
+};
+
+PointerClass (pUchChannel, UchChannel)
+
+#endif /* Channel_H_ */
+