/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1995 * Laboratoire de Recherche en Informatique (LRI) * * File descriptors, channels * * $Id$ * $CurLog$ */ #ifndef Channel_H_ #define Channel_H_ #ifdef __GNUG__ #pragma interface #endif #include "cplus_bugs.h" #include "global.h" #include "IOS.h" #include "ccu/SmartPointer.h" // we need read and write system calls #include class UchMsgBuffer; class CcuString; #ifndef NFILE #define NFILE 20 #endif enum IOMODE { IONone = 0, IORead = 1, IOWrite = 2, IOReadWrite = 3, IOSelect = 4, IOReadSelect = 5, IOWriteSelect = 6, IOAll = 7 }; extern char* StrReprBuf; class UchFilDes { private: static void Use (int); static void Unuse (int); protected: int Fd; public: inline UchFilDes () : Fd (-1) { } inline UchFilDes (const UchFilDes& fd) { Use (Fd = fd.Fd); } inline UchFilDes (int fd) { Use (Fd = fd); } inline ~UchFilDes () { Unuse (Fd); } inline operator int () const { return Fd; } inline UchFilDes& operator = (const UchFilDes& fd) { Unuse (Fd); Fd = fd.Fd; Use (Fd); return *this; } inline int FilDes () { return Fd; } inline void Open (int fd) { Unuse (Fd); Use (Fd = fd); } inline void Close () { Unuse (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 UchChannel : public CcuSmartData, public UchIOS { friend class UchBaseMultiplexer; protected: UchFilDes Fd; IOMODE Mode; UchBaseMultiplexer* Mpx; virtual void Added (UchBaseMultiplexer&); virtual void Removed (UchBaseMultiplexer&); 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&); 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 (const byte* b, int n) { return write (Fd, (const char*) b, n); } inline operator int () { return Fd; } inline UchBaseMultiplexer* GetMultiplexer () const { return Mpx; } inline char* StrRepr (char* s = StrReprBuf) { return Fd.StrRepr (s); } virtual void WriteLong (lword); virtual void WriteShort (sword); virtual void WriteByte (byte); virtual void WriteChar (char); virtual void WriteString (const char*); virtual void WriteBuf (const byte*, int); virtual bool ReadLong (lword&); virtual bool ReadShort (sword&); virtual bool ReadByte (byte&); virtual bool ReadChar (char&); virtual int ReadString (char*, int); virtual int ReadString (CcuString&); virtual bool ReadBuf (byte*, int); static CH_HANDLER ReadHandler; static CH_HANDLER WriteHandler; }; PointerClass (pUchChannel, UchChannel) #endif /* Channel_H_ */