00001 /* **************************************************************** 00002 **************************************************************** 00003 * Com++ Professional (c) 2009 C++ World 00004 * http://www.cplusplusworld.com/ mailto:compp@cplusplusworld.com 00005 **************************************************************** 00006 **************************************************************** 00007 */ 00008 00009 #ifndef COMM_H 00010 #define COMM_H 00011 00012 #include <iostream> 00013 #include <cstring> 00014 #include "SysPlusPlus/ComException.h" 00015 00016 #define _CommDefaultReadAheadBufferSize (8*1024) 00017 00018 namespace compp { 00019 class Comm { 00020 00021 protected: 00022 00023 int fd ; // The file descriptor 00024 bool fd_attached ; // Should fd be closed in destructor ? 00025 int WriteTimeout ; // Write Timeout. Used in Writen() 00026 int ReadTimeout ; // Write Timeout. Used in Readn() 00027 int MaxNumBytesToRead ; // Max Bytes read into std::string 00028 bool IsOpen ; // Used for IsEOF 00029 bool FdClosed ; // Used for IsEOF 00030 char LineDelimiter ; // Used in ReadLN 00031 bool ReadAhead ; 00032 char * ReadAheadBuffer ; 00033 unsigned int ReadAheadBufferIndex ; 00034 unsigned int ReadAheadBufferSize ; 00035 unsigned int ReadAheadBufferTotalSize ; 00036 bool IsThrowing ; 00037 00038 public: 00039 00040 // Destructor 00041 virtual ~Comm () ; 00042 00043 // Constructors 00044 Comm ( unsigned int buffsz = _CommDefaultReadAheadBufferSize ) ; 00045 00046 //Set 00047 void SetThrowing ( const bool yes = true ); 00048 // Bind the file descriptor to the Object, which 00049 // means the file descriptor is automatically closed 00050 // by the deswtructor. This option should be on, 00051 // if exceptions are used. Any file descriptor is 00052 // attached by default. 00053 void AttachFD(); 00054 // Detach the file descriptor, so it is not closed, 00055 // when the object is deleted. 00056 void DetachFD(); 00057 // Returns true, if the socket is attached. False otherwise. 00058 bool FD_IsAttached(); 00059 // Sets the time for the Writen() method. (in 1/1000000 sec) 00060 void SetWriteTimeout( int tmout ) ; 00061 // Gets the time for the Writen() method. (in 1/1000000 sec) 00062 int GetWriteTimeout( ) const ; 00063 // Sets the time for the Readn() method. (in 1/1000000 sec) 00064 void SetReadTimeout( int tmout ) ; 00065 // Gets the time for the Readn() method. (in 1/1000000 sec) 00066 int GetReadTimeout()const; 00067 int GetMaxNumBytesToRead () const ; 00068 00069 void SetLineDelimiter ( char c ) ; 00070 void SetMaxNumBytesToRead ( int num ); 00071 00072 void SetReadAhead ( bool yn ) ; 00073 00074 virtual void Close (); 00075 00076 virtual int Write ( const char *buf, const int count ) ; 00077 virtual int Write ( const std::string &buf ) ; 00078 virtual int operator << ( const std::string &buf ) ; 00079 00080 virtual int Writen ( const char *buf, int count ) ; 00081 virtual int Writen ( const std::string &buf ) ; 00082 00083 virtual int Read ( char *buf, int count ) ; 00084 virtual int Read ( std::string & buf, int count ) ; 00085 00086 virtual int Readn ( char *buf, int count ) ; 00087 virtual int Readn ( std::string &buf, int count) ; 00088 00089 virtual bool ReadLn ( char * buf, int count ); 00090 virtual bool ReadLn ( std::string &buf ); 00091 00092 virtual bool SetOptNonBlocking ( ) ; 00093 virtual bool SetOptBlocking ( ) ; 00094 00095 // poll on it 00096 virtual bool PollSnd ( int usecs ); 00097 virtual bool PollRcv ( int usecs ); 00098 00099 bool IsEOF () const; 00100 00101 /* 00102 * Returns the systems's file descriptor (fd) for the ressource. 00103 * The fd can be used then is all OS system calls. 00104 * ATTENTION: Bypassing the functionlity of the Comm++ framework 00105 * can lead to lost synchronisation between the OS and the application 00106 * data. 00107 * Please handle this functionality with care! 00108 */ 00109 int GetFd () const; 00110 00111 } ; 00112 } 00113 00114 #endif 00115
1.6.3