Panda3D
buffered_datagramwriter.h
1 #ifndef __BufferedWriter_H__
2 #define __BufferedWriter_H__
3 
4 #include "ringbuffer.h"
5 ////////////////////////////////////////////////////////////////////
6 // Class : Buffered_DatagramWriter
7 // Description : This is the buffered writer.. it is used to buffer up
8 // Coremessages and arbitrary data..
9 //
10 // GmCoreMessage
11 //
12 //
13 // You must commit all rights to a socket with flush and
14 // flush may be called internall if the buffersize is about
15 // to overrun.. This class does guaranty no partial message
16 // rights at least to the TCP layer..
17 //
18 ////////////////////////////////////////////////////////////////////
20 {
21  int _flush_point;
22 public:
23  inline void ReSet(void); // destroy all buffered data
24 
25  Buffered_DatagramWriter( size_t in_size , int in_flush_point = -1);
26  inline int AddData(const void * data, size_t len, Socket_TCP &sck);
27  inline int AddData(const void * data, size_t len);
28 // THE FUNCTIONS THAT TAKE A SOCKET NEED TO BE TEMPLATED TO WORK..
29 
30  template < class SOCK_TYPE>
31  int FlushNoBlock(SOCK_TYPE &sck) { // this is the ugly part
32 
33  int answer = 0;
34  size_t Writesize = AmountBuffered();
35 
36  if(Writesize > 0) {
37  int Writen = sck.SendData(GetMessageHead(),(int)Writesize);
38  if(Writen > 0) {
39  _StartPos += Writen;
40  FullCompress();
41  if(AmountBuffered() > 0) // send 0 if empty else send 1 for more to do
42  answer = 1;
43  }
44  else if(Writen < 0) {
45  if(!sck.ErrorIs_WouldBlocking(Writen))
46  answer = -1;
47  else
48  answer = 1; // 1 = more to do.....
49  }
50  }
51  return answer;
52  };
53 
54 
55  template < class SOCK_TYPE>
56  inline int Flush(SOCK_TYPE &sck) {
57  int answer = 0;
58  size_t Writesize = AmountBuffered();
59 
60  if(Writesize > 0) {
61  int Writen = sck.SendData(GetMessageHead(),(int)Writesize);
62 
63  if(Writen > 0) {
64  _StartPos += Writen;
65  FullCompress();
66  if(AmountBuffered() > 0) //send 0 if empty else send 1 for more to do
67  answer = 1;
68  }
69  else if(Writen < 0) {
70  if(sck.ErrorIs_WouldBlocking(Writen) != true)
71  answer = -1;
72  }
73  }
74 
75  return answer;
76  };
77 };
78 
79 ///////////////////////////////////////////////////////
80 // Function name : Buffered_DatagramWriter::ReSet
81 // Description : used to clear the buffrers ...
82 // use of this in mid stream is a very bad thing as
83 // you can not guarany network writes are message alligned
84 // Return type : void
85 ///////////////////////////////////////////////////////
86 inline void Buffered_DatagramWriter::ReSet(void) {
87  ResetContent();
88 }
89 ////////////////////////////////////////////////
90 // Buffered_DatagramWriter::Buffered_DatagramWriter
91 //
92 //
93 ////////////////////////////////////////////////
94 inline Buffered_DatagramWriter::Buffered_DatagramWriter( size_t in_size , int in_flush_point) : RingBuffer(in_size) {
95  _flush_point = in_flush_point;
96 }
97 
98 //////////////////////////////////////////////////////////////
99 // Function name : Buffered_DatagramWriter::AddData
100 // Description :
101 // Return type : inline int
102 // Argument : const void * data
103 // Argument : int len
104 // Argument : Socket_TCP &sck
105 //////////////////////////////////////////////////////////////
106 inline int Buffered_DatagramWriter::AddData(const void * data, size_t len, Socket_TCP &sck) {
107  int answer = 0;
108 
109  if(len > BufferAvailabe())
110  answer = Flush(sck);
111 
112  if(answer >= 0)
113  answer = AddData(data,len);
114 
115 
116  if(answer >= 0 && _flush_point != -1)
117  if(_flush_point < (int)AmountBuffered())
118  if(Flush(sck) < 0)
119  answer = -1;
120 
121  return answer;
122 }
123 
124 //////////////////////////////////////////////////////////////
125 // Function name : Buffered_DatagramWriter::AddData
126 // Description :
127 // Return type : inline int
128 // Argument : const char * data
129 // Argument : int len
130 //////////////////////////////////////////////////////////////
131 inline int Buffered_DatagramWriter::AddData(const void * data, size_t len)
132 {
133  int answer = -1;
134  if(BufferAvailabe() > len+2) {
135  unsigned short len1(len);
136  TS_GetInteger(len1,(char *)&len1);
137  if(Put((char *)&len1,sizeof(len1)) == true) {
138  if(Put((char *)data,len) == true) {
139  answer = 1;
140  }
141  }
142  }
143 
144  return answer;
145 }
146 #endif //__BufferedWriter_H__
147 
Base functionality for a TCP connected socket This class is pretty useless by itself but it does hide...
Definition: socket_tcp.h:15
int AddData(const void *data, size_t len, Socket_TCP &sck)
Return type : inline int Argument : const void * data Argument : int len Argument : Socket_TCP &sck...
This is the buffered writer.
This is an implemention of the membuffer with ring buffer interface on it....
Definition: ringbuffer.h:19
void ReSet(void)
used to clear the buffrers ...