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