Commit c77eedd4 authored by John Selbie's avatar John Selbie

First stable TCP commit

parent 50059b0d
#include "commonincludes.h"
#include "stuncore.h"
#include "stunconnection.h"
CConnectionPool::CConnectionPool() :
_freelist(NULL)
{
}
CConnectionPool::~CConnectionPool()
{
Reset();
}
void CConnectionPool::Reset()
{
StunConnection* pConn;
while (_freelist)
{
pConn = _freelist;
_freelist = _freelist->pNext;
delete pConn;
}
}
HRESULT CConnectionPool::Grow()
{
const size_t c_growrate = 50;
for (size_t i = 0; i < c_growrate; i++)
{
StunConnection* pConn = NULL;
pConn = new StunConnection();
if (pConn == NULL)
{
return E_OUTOFMEMORY;
}
pConn->_spOutputBuffer = CRefCountedBuffer(new CBuffer(MAX_STUN_MESSAGE_SIZE));
pConn->_spReaderBuffer = CRefCountedBuffer(new CBuffer(MAX_STUN_MESSAGE_SIZE));
if ((pConn->_spOutputBuffer == NULL) || (pConn->_spReaderBuffer == NULL))
{
delete pConn;
return E_OUTOFMEMORY;
}
pConn->pNext = _freelist;
_freelist = pConn;
}
return S_OK;
}
StunConnection* CConnectionPool::GetConnection(int sock, SocketRole role)
{
StunConnection* pConn = NULL;
if (_freelist == NULL)
{
Grow();
if (_freelist == NULL)
{
return NULL; // out of memory ?
}
}
pConn = _freelist;
_freelist = pConn->pNext;
pConn->pNext = NULL;
// prep this connection for usage
pConn->_reader.Reset();
pConn->_reader.GetStream().Attach(pConn->_spReaderBuffer, true);
pConn->_state = ConnectionState_Receiving;
pConn->_stunsocket.Attach(sock);
pConn->_stunsocket.SetRole(role);
pConn->_txCount = 0;
pConn->_timeStart = time(NULL);
pConn->_idHashTable = -1;
return pConn;
}
void CConnectionPool::ReleaseConnection(StunConnection* pConn)
{
ASSERT(pConn->_stunsocket.IsValid() == false); // not the pool's job to close a socket!
pConn->pNext = _freelist;
_freelist = pConn;
}
#ifndef STUN_CONNECTION_H
#define STUN_CONNECTION_H
#include "stuncore.h"
#include "stunsocket.h"
enum StunConnectionState
{
ConnectionState_Idle,
ConnectionState_Receiving,
ConnectionState_Transmitting,
ConnectionState_Closing, // shutdown has been called, waiting for close notification on other end
};
struct StunConnection
{
time_t _timeStart;
StunConnectionState _state;
CStunSocket _stunsocket;
CStunMessageReader _reader;
CRefCountedBuffer _spReaderBuffer;
CRefCountedBuffer _spOutputBuffer; // contains the response
size_t _txCount; // number of bytes of response transmitted thus far
int _idHashTable; // hints at which hash table the connection got inserted into
StunConnection* pNext; // next item in pool - meaningless outside of the pool
};
class CConnectionPool
{
private:
StunConnection* _freelist;
HRESULT Grow();
public:
CConnectionPool();
~CConnectionPool();
StunConnection* GetConnection(int sock, SocketRole role);
void ReleaseConnection(StunConnection* pConn);
void Reset();
};
#endif
\ No newline at end of file
#!/usr/bin/perl
# xxdperl.pl
# emulates "xxd -i" in perl (stdin and stdout only)
$count = 0;
while ($input = <STDIN>) {
@characters = split(//, $input);
foreach (@characters) {
if ($count != 0) {
print ',';
if (($count % 12)==0) {
print "\n ";
}
}
else {
print ' ';
}
print sprintf(" 0x%.2x", ord($_));
$count++;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment