Panda3D
 All Classes Functions Variables Enumerations
buffered_datagramreader.h
1 #ifndef __BUFFEREDREADER_GM_H__
2 #define __BUFFEREDREADER_GM_H__
3 
4 
5 #include "ringbuffer.h"
6 #include "datagram.h"
7 #include "config_nativenet.h"
8 
9 
10 inline unsigned short GetUnsignedShort(char * in)
11 {
12  return *((unsigned short *)in);
13 };
14 
15 
17 {
18  inline bool GetMessageFromBuffer(Datagram &inmsg);
19 public:
20  inline Buffered_DatagramReader(int in_size = 8192) ;
21  inline void ReSet(void);
22  //
23  // SOCK_TYPE is used to allow for
24  // abstract socket type to be used ..
25  // see socket_tcp and socket_ssl
26 
27  template < class SOCK_TYPE>
28  inline int PumpMessageReader(Datagram &inmsg, SOCK_TYPE &sck)
29  {
30  if(GetMessageFromBuffer(inmsg) == true)
31  return 1;
32  int rp = ReadPump(sck);
33  if(rp == 0)
34  return 0;
35 
36  if(rp < 1)
37  return -1;
38  if(GetMessageFromBuffer(inmsg) == true)
39  return 1;
40  return 0;
41  }
42 
43 
44  template < class SOCK_TYPE>
45  inline int ReadPump(SOCK_TYPE &sck)
46  {
47  int answer = 0;
48  size_t readsize = BufferAvailabe();
49 
50  if(readsize < 1)
51  {
52  Compress();
53  readsize = BufferAvailabe();
54  }
55 
56  if(readsize > 0)
57  {
58  char * ff = GetBufferOpen();
59  int gotbytes = sck.RecvData(ff,(int)readsize);
60  if(gotbytes < 0) // some error
61  {
62  //int er = GETERROR();
63  if(!sck.ErrorIs_WouldBlocking(gotbytes) )
64  {
65  answer = -3; // hard error ?
66  nativenet_cat.error() << "buffered_datagram_reader:ReadPump socket read error -- " << GETERROR() <<", " << sck.GetPeerName().get_ip_port().c_str() << "\n";
67  }
68  else
69  {
70  answer = 0; // try again nothing to read
71  }
72  }
73  else if(gotbytes > 0) // ok got some lets process it
74  {
75 
76  _EndPos += gotbytes;
77  answer = 1;
78  }
79  else // 0 mean other end disconect arggggg
80  {
81  answer = -1;
82  nativenet_cat.error() << "buffered_datagram_reader:ReadPump other end of socket closed -- " << sck.GetPeerName().get_ip_port().c_str() << "\n";
83  }
84  }
85  else
86  {
87  answer = -2;
88  nativenet_cat.error() << "buffered_datagram_reader:ReadPump Yeep! buffer has no room to read to -- " << sck.GetPeerName().get_ip_port().c_str() << "\nBufferAvaiable = " << readsize <<" AmountBuffered = " << AmountBuffered() << " BufferSize " << GetBufferSize() << "\n";
89 
90  }
91  return answer;
92  }
93 };
94 
95 #include "buffered_datagramreader.i"
96 
97 #endif //__BUFFEREDREADER_GM_H__
98 
This is an implemention of the membuffer with ring buffer interface on it....
Definition: ringbuffer.h:19
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43