From f293314d175677862ac89b5bce61a1babbe45c26 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 10 May 1994 09:50:52 +0000 Subject: Renamed _AddRef and _DelRef Moved Accept to UchSocket replaced TRUE/FALSE by true/false Now derive from UchIOS --- comm/Channel.cc | 150 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 45 deletions(-) (limited to 'comm/Channel.cc') diff --git a/comm/Channel.cc b/comm/Channel.cc index 57dac54..902eec1 100644 --- a/comm/Channel.cc +++ b/comm/Channel.cc @@ -15,13 +15,15 @@ #include "Channel.h" #include "MsgBuffer.h" #include "error.h" +#include "ccu/String.h" #include #include #include +#include -CH_HANDLER UchChannel::ReadHandler = 0; -CH_HANDLER UchChannel::WriteHandler = 0; +CH_HANDLER UchChannel::ReadHandler = 0; +CH_HANDLER UchChannel::WriteHandler = 0; // // this section implements refcounts for file descriptors, so that they @@ -32,7 +34,7 @@ static int Inited = 0; /*?nodoc?*/ void -_AddRef (int fd) +UchFilDes :: Use (int fd) { if (fd < 0) return; @@ -43,28 +45,16 @@ _AddRef (int fd) Refs [0] = Refs [1] = Refs [2] = 1; } Refs [fd]++; -#ifdef SMART_DEBUG - dbgprintf ("AddRef %d -> %d\n", fd, Refs [fd]); -#endif -#ifdef _TRACE2 - _TRACE2 (fd, Refs [fd]); -#endif } /*?nodoc?*/ void -_DelRef (int fd) +UchFilDes :: Unuse (int fd) { if (fd < 0) return; if (--Refs [fd] == 0) close (fd); -#ifdef SMART_DEBUG - dbgprintf ("DelRef %d -> %d\n", fd, Refs [fd]); -#endif -#ifdef _TRACE2 - _TRACE2 (fd, Refs [fd]); -#endif } /*?nodoc?*/ @@ -135,7 +125,7 @@ UchFilDes :: Close () { } /*? -Return TRUE if the object is opened, FALSE else. +Return true if the object is opened, false else. ?*/ bool UchFilDes :: Opened () @@ -189,7 +179,8 @@ This is very useful especially in the class \typ{UchMultiplexer} because arrays Construct a closed channel. ?*/ UchChannel :: UchChannel () -: Fd (), +: UchIOS (), + Fd (), Mode (IONone), Mpx (0) { @@ -199,7 +190,8 @@ UchChannel :: UchChannel () Construct an open channel on file descriptor \var{fd} with mode \var{io}. ?*/ UchChannel :: UchChannel (int fd, IOMODE io) -: Fd (fd), +: UchIOS (), + Fd (fd), Mode (io), Mpx (0) { @@ -207,7 +199,8 @@ UchChannel :: UchChannel (int fd, IOMODE io) /*?nodoc?*/ UchChannel :: UchChannel (const UchChannel& ch) -: Fd (ch.Fd), +: UchIOS (ch), + Fd (ch.Fd), Mode (ch.Mode), Mpx (ch.Mpx) { @@ -308,14 +301,14 @@ UchChannel :: HandleRead () /*? This virtual function is called by a \typ{UchMultiplexer} before making a \fun{select} call. It is intended to handle any background task or buffered input/output associated to the channel. -If it returns TRUE, the channel set scan functions will return before performing the select call. +If it returns true, the channel set scan functions will return before performing the select call. See the class \typ{UchMultiplexer} for more details. -The default implementation does nothing but returning FALSE. +The default implementation does nothing but returning false. ?*/ bool UchChannel :: HandleSelect () { - return FALSE; + return false; } #ifdef DOC @@ -341,7 +334,7 @@ UchChannel :: Read (UchMsgBuffer& b) int l = b.FreeLength (); if (! l) return -2; - int n = read (FilDes (), (char*) b.Free (), l); + int n = read (Fd, (char*) b.Free (), l); if (n > 0) b.More (n); return n; @@ -358,7 +351,7 @@ UchChannel :: Write (UchMsgBuffer& b) int l = b.BufLength (); if (! l) return -2; - int n = write (FilDes (), (const char*) b.Buffer (), l); + int n = write (Fd, (const char*) b.Buffer (), l); if (n > 0) b.Flush (n); return n; @@ -370,40 +363,107 @@ UchChannel :: ReadBuffer (UchMsgBuffer& b) { int n; errno = 0; - while ((n = Read (b)) > 0); + while ((n = Read (b)) > 0) + ; return bool (n == -2); } /*? These functions repeatedly call \fun{Read} or \fun{Write} until the whole buffer is -transferred or a system error occurred. In case of an error, they return FALSE, else TRUE. +transferred or a system error occurred. In case of an error, they return false, else true. ?*/ bool UchChannel :: WriteBuffer (UchMsgBuffer& b) { int n; errno = 0; - while ((n = Write (b)) > 0); + while ((n = Write (b)) > 0) + ; return bool (n == -2); } -/*? -This function is intended for sockets that accept connections. -It returns a dynamically allocated new channel that is opened on the accepted connection. -If it fails, it returns 0. -?*/ -UchChannel* -UchChannel :: Accept () +void +UchChannel :: WriteLong (lword l) { - int fd; - - errno = 0; - if (Fd < 0) - return (UchChannel*) 0; - - if ((fd = accept (FilDes (), 0, 0)) < 0) - return (UchChannel*) 0; - - return new UchChannel (fd); + Write ((byte*)&l, lwsize); +} + +void +UchChannel :: WriteShort (sword s) +{ + Write ((byte*)&s, swsize); +} + +void +UchChannel :: WriteByte (byte b) +{ + Write (&b, 1); +} + +void +UchChannel :: WriteChar (char c) +{ + Write ((byte*)&c, 1); +} + +void +UchChannel :: WriteString (const char* s) +{ + Write ((byte*) s, strlen (s) + 1); +} + +void +UchChannel :: WriteBuf (const byte* b, int n) +{ + Write (b, n); +} + + + +void +UchChannel :: ReadLong (lword& l) +{ + Read ((byte*)&l, lwsize); +} + +void +UchChannel :: ReadShort (sword& s) +{ + Read ((byte*)&s, swsize); } +void +UchChannel :: ReadByte (byte& b) +{ + Read (&b, 1); +} + +void +UchChannel :: ReadChar (char& c) +{ + Read ((byte*)&c, 1); +} + +void +UchChannel :: ReadString (char* s) +{ + // this is extremely non optimal + do { + Read ((byte*)s, 1); + } while (*s++); +} + +void +UchChannel :: ReadString (CcuString& s) +{ + // this enforces an undue limitation + char c [1024]; + ReadString (c); + s = c; +} + +void +UchChannel :: ReadBuf (byte* b, int n) +{ + Read (b, n); +} -- cgit v1.1