Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
datastream.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 
19 #ifndef DATASTREAM_H
20 #define DATASTREAM_H
21 
22 #include "buffer.h"
23 
25 {
27  CBuffer* _pBuffer; // direct pointer for better performance
28  size_t _pos;
29  bool _fNoGrow;
30 
31  HRESULT Grow(size_t newsize);
32 
33 
34 public:
35  CDataStream();
37 
38  HRESULT SetSizeHint(size_t size);
39 
40  void SetNoGrow(bool fDisableGrow);
41 
42  void Reset();
43  void Attach(CRefCountedBuffer& buffer, bool fForWriting);
44 
45  HRESULT Write(const void* data, size_t size);
46  HRESULT Read(void* data, size_t size);
47 
48  HRESULT WriteUint8(uint8_t val) {return Write(&val, sizeof(val));}
49  HRESULT WriteUint16(uint16_t val) {return Write(&val, sizeof(val));}
50  HRESULT WriteUint32(uint32_t val) {return Write(&val, sizeof(val));}
51  HRESULT WriteUint64(uint64_t val) {return Write(&val, sizeof(val));}
52 
53  HRESULT WriteInt8(int8_t val) {return Write(&val, sizeof(val));}
54  HRESULT WriteInt16(int16_t val) {return Write(&val, sizeof(val));}
55  HRESULT WriteInt32(int32_t val) {return Write(&val, sizeof(val));}
56  HRESULT WriteInt64(int64_t val) {return Write(&val, sizeof(val));}
57 
58  HRESULT ReadUint8(uint8_t* pVal) {return Read(pVal, sizeof(*pVal));}
59  HRESULT ReadUint16(uint16_t* pVal) {return Read(pVal, sizeof(*pVal));}
60  HRESULT ReadUint32(uint32_t* pVal) {return Read(pVal, sizeof(*pVal));}
61  HRESULT ReadUint64(uint64_t* pVal) {return Read(pVal, sizeof(*pVal));}
62 
63  HRESULT ReadInt8(int8_t* pVal) {return Read(pVal, sizeof(*pVal));}
64  HRESULT ReadInt16(int16_t* pVal) {return Read(pVal, sizeof(*pVal));}
65  HRESULT ReadInt32(int32_t* pVal) {return Read(pVal, sizeof(*pVal));}
66  HRESULT ReadInt64(int64_t* pVal) {return Read(pVal, sizeof(*pVal));}
67 
68 
69  HRESULT GetBuffer(CRefCountedBuffer* pRefCountedBuffer);
70  uint8_t* GetDataPointerUnsafe();
71 
72  HRESULT SeekDirect(size_t pos);
73  HRESULT SeekRelative(int nOffset);
74 
75  size_t GetPos();
76  size_t GetSize();
77 
78 
79  bool IsEOF();
80 };
81 
82 
83 
84 
85 
86 #endif
size_t GetPos()
Definition: datastream.cpp:177
HRESULT ReadUint8(uint8_t *pVal)
Definition: datastream.h:58
CRefCountedBuffer _spBuffer
Definition: datastream.h:26
HRESULT WriteUint16(uint16_t val)
Definition: datastream.h:49
HRESULT WriteInt32(int32_t val)
Definition: datastream.h:55
void Reset()
Definition: datastream.cpp:47
HRESULT SeekRelative(int nOffset)
Definition: datastream.cpp:207
HRESULT Read(void *data, size_t size)
Definition: datastream.cpp:67
size_t _pos
Definition: datastream.h:28
HRESULT GetBuffer(CRefCountedBuffer *pRefCountedBuffer)
Definition: datastream.cpp:213
bool IsEOF()
Definition: datastream.cpp:171
CBuffer * _pBuffer
Definition: datastream.h:27
void SetNoGrow(bool fDisableGrow)
Definition: datastream.cpp:133
HRESULT ReadUint16(uint16_t *pVal)
Definition: datastream.h:59
bool _fNoGrow
Definition: datastream.h:29
Definition: buffer.h:27
HRESULT ReadInt32(int32_t *pVal)
Definition: datastream.h:65
HRESULT WriteUint64(uint64_t val)
Definition: datastream.h:51
HRESULT ReadInt16(int16_t *pVal)
Definition: datastream.h:64
HRESULT Grow(size_t newsize)
Definition: datastream.cpp:84
int32_t HRESULT
Definition: hresult.h:22
HRESULT ReadUint64(uint64_t *pVal)
Definition: datastream.h:61
uint8_t * GetDataPointerUnsafe()
Definition: datastream.cpp:231
HRESULT Write(const void *data, size_t size)
Definition: datastream.cpp:138
HRESULT SeekDirect(size_t pos)
Definition: datastream.cpp:187
HRESULT SetSizeHint(size_t size)
Definition: datastream.cpp:41
size_t GetSize()
Definition: datastream.cpp:182
HRESULT ReadInt64(int64_t *pVal)
Definition: datastream.h:66
HRESULT ReadUint32(uint32_t *pVal)
Definition: datastream.h:60
HRESULT ReadInt8(int8_t *pVal)
Definition: datastream.h:63
HRESULT WriteInt64(int64_t val)
Definition: datastream.h:56
HRESULT WriteInt8(int8_t val)
Definition: datastream.h:53
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
HRESULT WriteUint32(uint32_t val)
Definition: datastream.h:50
HRESULT WriteInt16(int16_t val)
Definition: datastream.h:54
void Attach(CRefCountedBuffer &buffer, bool fForWriting)
Definition: datastream.cpp:55
HRESULT WriteUint8(uint8_t val)
Definition: datastream.h:48