/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1997 * Laboratoire de Recherche en Informatique (LRI) * * Buffered streams * * $Id$ * $CurLog$ * Created from bits of MsgStream.cc */ #include "BufStream.h" #include "error.h" /*? These constructors are similar to those of class \typ{IvlSocket}. ?*/ IvlBufStream :: IvlBufStream (IvlAddress* bindTo, IvlAddress* connectTo) : IvlStream (bindTo, connectTo), InBuffer (), OutBuffer (), OutSize (128), Sync (false) { } // *** this copy constructor might be automatically generated /*?nodoc?*/ IvlBufStream :: IvlBufStream (const IvlBufStream& ms) : IvlStream (ms), InBuffer (), OutBuffer (), OutSize (ms.OutSize), Sync (ms.Sync) { } /*?nodoc?*/ IvlBufStream :: ~IvlBufStream () { Flush (); InBuffer.Clear (); OutBuffer.Clear (); } /*?nextdoc?*/ void IvlBufStream :: InputBuffer (int min, int grow, int max) { InBuffer.SetSizes (min, grow, max); } /*? Set the input and output buffer sizes. Default sizes are used if these functions are not called. ?*/ void IvlBufStream :: OutputBuffer (int min, int grow, int max) { OutBuffer.SetSizes (min, grow, max); OutSize = max; } /*? Flush the output buffer. This function is called automatically when the buffer exceeds its flush size, or after each message when this stream is in synchronous mode. It is also called from \fun{Ask} to wait for the answer. ?*/ void IvlBufStream :: Flush () { if (OutBuffer.BufLength () == 0) return; int n = Write (OutBuffer); OutBuffer.Flush (n); } /*?nodoc?*/ void IvlBufStream :: HandleWrite () { Flush (); } // read stream into input buffer // delete the stream when an eof is received // return the number of bytes read // /*?hidden?*/ int IvlBufStream :: ReadInput () { /* read in buffer */ if (InBuffer.BufLength () == 0) InBuffer.NeedSize (128); int n = Read (InBuffer); /* handle errors */ if (n == -2) { InBuffer.Grow (); n = Read (InBuffer); } #if 0 if (n < 0) SysError (ErrWarn, "IvlBufStream::ReadInput"); if (n <= 0) Closing (n < 0 ? false : true); return n; #else /* new code taken from BusAccess.cc (27/11/2000) */ if (n <= 0) delete this; return n; #endif } /*? This virtual function is called when an end of file is read, meaning that the communication is terminated. It is also called if an error occurs while reading. You can check the value of the global variable \var{errno}: if it is non zero, \fun{Closing} was called because of an error. % wrong By default this function does nothing. ?*/ void IvlBufStream :: Closing (bool) { } void IvlBufStream :: WriteLong (lword l) { OutBuffer << l; } void IvlBufStream :: WriteShort (sword s) { OutBuffer << s; } void IvlBufStream :: WriteByte (byte b) { OutBuffer << b; } void IvlBufStream :: WriteChar (char c) { OutBuffer << c; } void IvlBufStream :: WriteString (const char* s) { OutBuffer << s; } void IvlBufStream :: WriteBuf (const byte* b, int n) { OutBuffer.WriteBuf (b, n); } bool IvlBufStream :: ReadLong (lword& l) { return InBuffer.ReadLong (l); } bool IvlBufStream :: ReadShort (sword& s) { return InBuffer.ReadShort (s); } bool IvlBufStream :: ReadByte (byte& b) { return InBuffer.ReadByte (b); } bool IvlBufStream :: ReadChar (char& c) { return InBuffer.ReadChar (c); } int IvlBufStream :: ReadString (char* s, int n) { return InBuffer.ReadString (s, n); } int IvlBufStream :: ReadString (IvlString& s) { return InBuffer.ReadString (s); } bool IvlBufStream :: ReadBuf (byte* b, int n) { return InBuffer.ReadBuf (b, n); }