Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
stunconnection.cpp
Go to the documentation of this file.
1 #include "commonincludes.hpp"
2 #include "stuncore.h"
3 #include "stunconnection.h"
4 
6 _freelist(NULL)
7 {
8 
9 }
10 
12 {
13  Reset();
14 }
15 
17 {
18  StunConnection* pConn;
19  while (_freelist)
20  {
21  pConn = _freelist;
23  delete pConn;
24  }
25 }
26 
28 {
29  const size_t c_growrate = 50;
30 
31  for (size_t i = 0; i < c_growrate; i++)
32  {
33  StunConnection* pConn = NULL;
34  pConn = new StunConnection();
35 
36  if (pConn == NULL)
37  {
38  return E_OUTOFMEMORY;
39  }
40 
43 
44  if ((pConn->_spOutputBuffer == NULL) || (pConn->_spReaderBuffer == NULL))
45  {
46  delete pConn;
47  return E_OUTOFMEMORY;
48  }
49 
50  pConn->pNext = _freelist;
51  _freelist = pConn;
52  }
53 
54  return S_OK;
55 }
56 
58 {
59  StunConnection* pConn = NULL;
60 
61  if (_freelist == NULL)
62  {
63  Grow();
64  if (_freelist == NULL)
65  {
66  return NULL; // out of memory ?
67  }
68  }
69 
70  pConn = _freelist;
71  _freelist = pConn->pNext;
72  pConn->pNext = NULL;
73 
74  // prep this connection for usage
75  pConn->_reader.Reset();
76  pConn->_reader.GetStream().Attach(pConn->_spReaderBuffer, true);
78  pConn->_stunsocket.Attach(sock);
79  pConn->_stunsocket.SetRole(role);
80  pConn->_txCount = 0;
81  pConn->_timeStart = time(NULL);
82  pConn->_idHashTable = -1;
83 
84  return pConn;
85 }
86 
88 {
89  ASSERT(pConn->_stunsocket.IsValid() == false); // not the pool's job to close a socket!
90 
91  pConn->pNext = _freelist;
92  _freelist = pConn;
93 }
94 
96 {
97  pConn->_reader.Reset();
98  pConn->_reader.GetStream().Attach(pConn->_spReaderBuffer, true);
100  pConn->_txCount = 0;
101 }
102 
const uint32_t MAX_STUN_MESSAGE_SIZE
Definition: stuntypes.h:178
#define S_OK
Definition: hresult.h:46
#define ASSERT(expr)
CDataStream & GetStream()
Definition: stunreader.cpp:855
StunConnection * pNext
void SetRole(SocketRole role)
Definition: stunsocket.cpp:104
CStunMessageReader _reader
void ReleaseConnection(StunConnection *pConn)
Definition: buffer.h:27
StunConnection * _freelist
HRESULT Attach(int sock)
Definition: stunsocket.cpp:56
SocketRole
Definition: socketrole.h:22
CRefCountedBuffer _spReaderBuffer
int32_t HRESULT
Definition: hresult.h:22
CRefCountedBuffer _spOutputBuffer
bool IsValid()
Definition: stunsocket.cpp:51
#define E_OUTOFMEMORY
Definition: hresult.h:50
StunConnection * GetConnection(int sock, SocketRole role)
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
CStunSocket _stunsocket
void Attach(CRefCountedBuffer &buffer, bool fForWriting)
Definition: datastream.cpp:55
void ResetConnection(StunConnection *pConn)
StunConnectionState _state