Panda3D

bufferedwriter_growable.h

00001 #ifndef __BufferedWriter_Growable_H__
00002 #define __BufferedWriter_Growable_H__
00003 ///////////////////////////////////////////////////////
00004 // this class is for the usage of growable output...
00005 // it is slower than buffered writer but more robust..
00006 // it also allows for writes to take more time to the out putt..
00007 // ie.. Write buffering.. Not just one write..
00008 ///////////////////////////////////////////////////
00009 
00010 class BufferedWriter_Growable : public std::string 
00011 {
00012   int       _write_offset;
00013 public:
00014 
00015   BufferedWriter_Growable(void); 
00016   ~BufferedWriter_Growable(void);
00017   int AmountBuffered(void);
00018   void AppendData(const char * buf, int len);
00019     void Reset() { clear(); _write_offset = 0; };
00020   const char * GetMessageHead(void);
00021   int  Flush(Socket_TCP &sck) ; // this is the ugly part
00022 };
00023 
00024 
00025 //////////////////////////////////////////////////////////////
00026 // Function name  : BufferedWriter_Growable::BufferedWriter_Growable
00027 // Description      : 
00028 // Return type    : inline 
00029 // Argument         : void
00030 //////////////////////////////////////////////////////////////
00031 inline BufferedWriter_Growable::BufferedWriter_Growable(void) 
00032 {
00033   _write_offset = 0;
00034 };
00035 
00036 
00037 //////////////////////////////////////////////////////////////
00038 // Function name  : ~BufferedWriter_Growable::BufferedWriter_Growable
00039 // Description      : 
00040 // Return type    : inline 
00041 // Argument         : void
00042 //////////////////////////////////////////////////////////////
00043 inline BufferedWriter_Growable::~BufferedWriter_Growable(void)
00044 {
00045 }
00046 
00047 
00048 //////////////////////////////////////////////////////////////
00049 // Function name  : BufferedWriter_Growable::AmountBuffered
00050 // Description      : 
00051 // Return type    : inline int 
00052 // Argument         : void
00053 //////////////////////////////////////////////////////////////
00054 inline int BufferedWriter_Growable::AmountBuffered(void)
00055 {
00056   return (int) (size() - _write_offset);
00057 }
00058 
00059 
00060 //////////////////////////////////////////////////////////////
00061 // Function name  : BufferedWriter_Growable::AppendData
00062 // Description      : 
00063 // Return type    : inline void 
00064 // Argument         : const char * buf
00065 // Argument         : int len
00066 //////////////////////////////////////////////////////////////
00067 inline void BufferedWriter_Growable::AppendData(const char * buf, int len)
00068 {
00069   append(buf, len);
00070 }
00071 
00072 
00073 //////////////////////////////////////////////////////////////
00074 // Function name  : char * BufferedWriter_Growable::GetMessageHead
00075 // Description      : 
00076 // Return type    : inline const 
00077 // Argument         : void
00078 //////////////////////////////////////////////////////////////
00079 inline const char * BufferedWriter_Growable::GetMessageHead(void)
00080 {
00081   return data() + _write_offset;
00082 }
00083 
00084 
00085 //////////////////////////////////////////////////////////////
00086 // Function name  :  BufferedWriter_Growable::Flush
00087 // Description      : 
00088 // Return type    : inline int 
00089 // Argument         : SocketTCP_Gm &sck
00090 //////////////////////////////////////////////////////////////
00091 inline int  BufferedWriter_Growable::Flush(Socket_TCP &sck)  // this is the ugly part
00092 { 
00093   int answer = 0; 
00094   int Writesize = AmountBuffered();
00095   
00096   if(Writesize > 0)
00097   {
00098     const char * out1 = GetMessageHead();
00099     int Writen = sck.SendData(out1,Writesize);
00100     if(Writen > 0)
00101     {
00102       _write_offset += Writen;
00103       answer = 1;
00104     }
00105     else if(Writen < 0)
00106     {
00107       if(GETERROR() != LOCAL_BLOCKING_ERROR) 
00108         answer = -1;
00109     }
00110   }   
00111   return answer;
00112 }
00113 
00114 
00115 #endif //__BufferedWriter_Growable_H__
00116 
00117 
 All Classes Functions Variables Enumerations