Panda3D
 All Classes Functions Variables Enumerations
bufferedwriter_growable.h
1 #ifndef __BufferedWriter_Growable_H__
2 #define __BufferedWriter_Growable_H__
3 ///////////////////////////////////////////////////////
4 // this class is for the usage of growable output...
5 // it is slower than buffered writer but more robust..
6 // it also allows for writes to take more time to the out putt..
7 // ie.. Write buffering.. Not just one write..
8 ///////////////////////////////////////////////////
9 
10 class BufferedWriter_Growable : public std::string
11 {
12  int _write_offset;
13 public:
14 
17  int AmountBuffered(void);
18  void AppendData(const char * buf, int len);
19  void Reset() { clear(); _write_offset = 0; };
20  const char * GetMessageHead(void);
21  int Flush(Socket_TCP &sck) ; // this is the ugly part
22 };
23 
24 
25 //////////////////////////////////////////////////////////////
26 // Function name : BufferedWriter_Growable::BufferedWriter_Growable
27 // Description :
28 // Return type : inline
29 // Argument : void
30 //////////////////////////////////////////////////////////////
32 {
33  _write_offset = 0;
34 };
35 
36 
37 //////////////////////////////////////////////////////////////
38 // Function name : ~BufferedWriter_Growable::BufferedWriter_Growable
39 // Description :
40 // Return type : inline
41 // Argument : void
42 //////////////////////////////////////////////////////////////
44 {
45 }
46 
47 
48 //////////////////////////////////////////////////////////////
49 // Function name : BufferedWriter_Growable::AmountBuffered
50 // Description :
51 // Return type : inline int
52 // Argument : void
53 //////////////////////////////////////////////////////////////
55 {
56  return (int) (size() - _write_offset);
57 }
58 
59 
60 //////////////////////////////////////////////////////////////
61 // Function name : BufferedWriter_Growable::AppendData
62 // Description :
63 // Return type : inline void
64 // Argument : const char * buf
65 // Argument : int len
66 //////////////////////////////////////////////////////////////
67 inline void BufferedWriter_Growable::AppendData(const char * buf, int len)
68 {
69  append(buf, len);
70 }
71 
72 
73 //////////////////////////////////////////////////////////////
74 // Function name : char * BufferedWriter_Growable::GetMessageHead
75 // Description :
76 // Return type : inline const
77 // Argument : void
78 //////////////////////////////////////////////////////////////
79 inline const char * BufferedWriter_Growable::GetMessageHead(void)
80 {
81  return data() + _write_offset;
82 }
83 
84 
85 //////////////////////////////////////////////////////////////
86 // Function name : BufferedWriter_Growable::Flush
87 // Description :
88 // Return type : inline int
89 // Argument : SocketTCP_Gm &sck
90 //////////////////////////////////////////////////////////////
91 inline int BufferedWriter_Growable::Flush(Socket_TCP &sck) // this is the ugly part
92 {
93  int answer = 0;
94  int Writesize = AmountBuffered();
95 
96  if(Writesize > 0)
97  {
98  const char * out1 = GetMessageHead();
99  int Writen = sck.SendData(out1,Writesize);
100  if(Writen > 0)
101  {
102  _write_offset += Writen;
103  answer = 1;
104  }
105  else if(Writen < 0)
106  {
107  if(GETERROR() != LOCAL_BLOCKING_ERROR)
108  answer = -1;
109  }
110  }
111  return answer;
112 }
113 
114 
115 #endif //__BufferedWriter_Growable_H__
116 
117 
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
Definition: socket_tcp.h:15
const char * GetMessageHead(void)
Return type : inline const Argument : void.
BufferedWriter_Growable(void)
Return type : inline Argument : void.
int AmountBuffered(void)
Return type : inline int Argument : void.
int Flush(Socket_TCP &sck)
Return type : inline int Argument : SocketTCP_Gm &amp;sck.
void AppendData(const char *buf, int len)
Return type : inline void Argument : const char * buf Argument : int len.
~BufferedWriter_Growable(void)
Return type : inline Argument : void.