Panda3D

Loading...
Searching...
No Matches
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
16 class Buffered_DatagramReader : protected RingBuffer
17{
18 inline bool GetMessageFromBuffer(Datagram &inmsg);
19 public:
20 inline Buffered_DatagramReader(int in_size = 8192) ;
21 inline void ReSet(void);
22 // SOCK_TYPE is used to allow for abstract socket type to be used .. see
23 // socket_tcp and socket_ssl
24
25 template < class SOCK_TYPE>
26 inline int PumpMessageReader(Datagram &inmsg, SOCK_TYPE &sck)
27 {
28 if(GetMessageFromBuffer(inmsg) == true)
29 return 1;
30 int rp = ReadPump(sck);
31 if(rp == 0)
32 return 0;
33
34 if(rp < 1)
35 return -1;
36 if(GetMessageFromBuffer(inmsg) == true)
37 return 1;
38 return 0;
39 }
40
41
42 template < class SOCK_TYPE>
43 inline int ReadPump(SOCK_TYPE &sck)
44 {
45 int answer = 0;
46 size_t readsize = BufferAvailabe();
47
48 if(readsize < 1)
49 {
50 Compress();
51 readsize = BufferAvailabe();
52 }
53
54 if(readsize > 0)
55 {
56 char * ff = GetBufferOpen();
57 int gotbytes = sck.RecvData(ff,(int)readsize);
58 if(gotbytes < 0) // some error
59 {
60 // int er = GETERROR();
61 if(!sck.ErrorIs_WouldBlocking(gotbytes) )
62 {
63 answer = -3; // hard error ?
64 nativenet_cat.error() << "buffered_datagram_reader:ReadPump socket read error -- " << GETERROR() <<", " << sck.GetPeerName().get_ip_port().c_str() << "\n";
65 }
66 else
67 {
68 answer = 0; // try again nothing to read
69 }
70 }
71 else if(gotbytes > 0) // ok got some lets process it
72 {
73
74 _EndPos += gotbytes;
75 answer = 1;
76 }
77 else // 0 mean other end disconect arggggg
78 {
79 answer = -1;
80 nativenet_cat.error() << "buffered_datagram_reader:ReadPump other end of socket closed -- " << sck.GetPeerName().get_ip_port().c_str() << "\n";
81 }
82 }
83 else
84 {
85 answer = -2;
86 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";
87 }
88 return answer;
89 }
90};
91
92#include "buffered_datagramreader.I"
93
94#endif //__BUFFEREDREADER_GM_H__
Buffered_DatagramReader::ReSet
void ReSet(void)
Reset all read content, ie.
Definition buffered_datagramreader.I:34
Buffered_DatagramReader::Buffered_DatagramReader
Buffered_DatagramReader(int in_size=8192)
Constructor.
Definition buffered_datagramreader.I:25
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
MemBuffer::GetBufferSize
size_t GetBufferSize(void) const
Access to the BUffer Size Information.
Definition membuffer.I:106
RingBuffer::Compress
void Compress(void)
Try and do a intelegent compress of the data space the algorithem is really stupid right know.
Definition ringbuffer.I:87
RingBuffer::BufferAvailabe
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
RingBuffer::AmountBuffered
size_t AmountBuffered(void)
Will report the number of unread chars in buffer.
Definition ringbuffer.I:32
config_nativenet.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.