diff --git a/.vscode/settings.json b/.vscode/settings.json index 5c9c832..42e5956 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -72,7 +72,8 @@ "thread": "cpp", "typeindex": "cpp", "variant": "cpp", - "iostream": "cpp" + "iostream": "cpp", + "fstream": "cpp" }, "editor.tokenColorCustomizations": { "textMateRules": [ diff --git a/G2Handler.cpp b/G2Handler.cpp index 8670b32..12db48d 100644 --- a/G2Handler.cpp +++ b/G2Handler.cpp @@ -175,6 +175,8 @@ void CG2Handler::process(CAMBEData& data) void CG2Handler::clock(unsigned int ms) { + m_handler->clock(ms); + for (unsigned int i = 0U; i < m_maxRoutes; i++) { CG2Handler* route = m_routes[i]; if (route != NULL) { diff --git a/G2ProtocolHandler.cpp b/G2ProtocolHandler.cpp index ae3ac4e..cbaf541 100644 --- a/G2ProtocolHandler.cpp +++ b/G2ProtocolHandler.cpp @@ -33,8 +33,10 @@ m_socket(socket), m_type(GT_NONE), m_buffer(nullptr), m_length(0U), -m_address(destination) +m_address(destination), +m_inactivityTimer(1000U, 29U) { + m_inactivityTimer.start(); m_buffer = new unsigned char[bufferSize]; ::memset(m_buffer, 0, bufferSize); } @@ -46,6 +48,7 @@ CG2ProtocolHandler::~CG2ProtocolHandler() bool CG2ProtocolHandler::writeHeader(const CHeaderData& header) { + m_inactivityTimer.start(); unsigned char buffer[60U]; unsigned int length = header.getG2Data(buffer, 60U, true); @@ -68,6 +71,7 @@ bool CG2ProtocolHandler::writeHeader(const CHeaderData& header) bool CG2ProtocolHandler::writeAMBE(const CAMBEData& data) { + m_inactivityTimer.start(); unsigned char buffer[40U]; unsigned int length = data.getG2Data(buffer, 40U); @@ -90,6 +94,8 @@ bool CG2ProtocolHandler::setBuffer(unsigned char * buffer, int length) if(length <= 0) return false; + m_inactivityTimer.start(); + m_length = length; if (m_buffer[0] != 'D' || m_buffer[1] != 'S' || m_buffer[2] != 'V' || m_buffer[3] != 'T') { @@ -110,6 +116,7 @@ bool CG2ProtocolHandler::setBuffer(unsigned char * buffer, int length) CHeaderData* CG2ProtocolHandler::readHeader() { + m_inactivityTimer.start(); if (m_type != GT_HEADER) return nullptr; @@ -128,6 +135,7 @@ CHeaderData* CG2ProtocolHandler::readHeader() CAMBEData* CG2ProtocolHandler::readAMBE() { + m_inactivityTimer.start(); if (m_type != GT_AMBE) return NULL; @@ -142,3 +150,4 @@ CAMBEData* CG2ProtocolHandler::readAMBE() return data; } + diff --git a/G2ProtocolHandler.h b/G2ProtocolHandler.h index df50724..edcf2b3 100644 --- a/G2ProtocolHandler.h +++ b/G2ProtocolHandler.h @@ -27,6 +27,7 @@ #include "HeaderData.h" #include "AMBEData.h" #include "NetUtils.h" +#include "Timer.h" enum G2_TYPE { GT_NONE, @@ -52,16 +53,16 @@ public: bool setBuffer(unsigned char * buffer, int length); - void close(); - + void clock(unsigned int ms) { m_inactivityTimer.clock(ms); } + bool isInactive() { return m_inactivityTimer.hasExpired(); } private: - CUDPReaderWriter * m_socket; G2_TYPE m_type; unsigned char* m_buffer; unsigned int m_length; struct sockaddr_storage m_address; + CTimer m_inactivityTimer; bool readPackets(); }; diff --git a/G2ProtocolHandlerPool.cpp b/G2ProtocolHandlerPool.cpp index 92467fd..7db90d8 100644 --- a/G2ProtocolHandlerPool.cpp +++ b/G2ProtocolHandlerPool.cpp @@ -127,7 +127,10 @@ void CG2ProtocolHandlerPool::traverseNat(const std::string& address) bool CG2ProtocolHandlerPool::writeHeader(const CHeaderData& header) { - auto handler = findHandler(header.getDestination(), IMT_ADDRESS_ONLY); + auto handler = findHandler(header.getDestination(), IMT_ADDRESS_AND_PORT); + if(handler == nullptr) + handler = findHandler(header.getDestination(), IMT_ADDRESS_ONLY); + if(handler == nullptr) { handler = new CG2ProtocolHandler(&m_socket, header.getDestination(), G2_BUFFER_LENGTH); m_pool.push_back(handler); @@ -138,7 +141,10 @@ bool CG2ProtocolHandlerPool::writeHeader(const CHeaderData& header) bool CG2ProtocolHandlerPool::writeAMBE(const CAMBEData& data) { - auto handler = findHandler(data.getDestination(), IMT_ADDRESS_ONLY); + auto handler = findHandler(data.getDestination(), IMT_ADDRESS_AND_PORT); + if(handler == nullptr) + handler = findHandler(data.getDestination(), IMT_ADDRESS_ONLY); + if(handler == nullptr) { handler = new CG2ProtocolHandler(&m_socket, data.getDestination(), G2_BUFFER_LENGTH); m_pool.push_back(handler); @@ -166,4 +172,19 @@ CG2ProtocolHandler * CG2ProtocolHandlerPool::findHandler(in_addr addr, unsigned TOIPV4(addrStorage)->sin_port = port; return findHandler(addrStorage, matchType); +} + +void CG2ProtocolHandlerPool::clock(unsigned int ms) +{ + for(auto it = m_pool.begin(); it != m_pool.end();) { + (*it)->clock(ms); + if((*it)->isInactive()) { + delete (*it); + it = m_pool.erase(it); + m_index = m_pool.end(); + } + else { + it++; + } + } } \ No newline at end of file diff --git a/G2ProtocolHandlerPool.h b/G2ProtocolHandlerPool.h index afdc339..9926fc3 100644 --- a/G2ProtocolHandlerPool.h +++ b/G2ProtocolHandlerPool.h @@ -78,6 +78,8 @@ public: void traverseNat(const std::string& address); + void clock(unsigned int ms); + private: bool readPackets(); CG2ProtocolHandler * findHandler(const struct sockaddr_storage& addr, IPMATCHTYPE matchType) const;