You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
3.0 KiB

#include "transportSocket.h"
#define LOG( X ) m_pLog->Log( "TransportSocket", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "TransportSocket", this, _LOG_ERROR, X )
// Constructor implementation
transportSocket::transportSocket() : basicTransport(), basicDualThread(){
m_pLog = basicLogCollection::getInstance();
m_pInputBuffer = (U8 *)malloc(MAXIMAL_SOCKET_PACKET_LENGTH);
if (m_pInputBuffer != NULL) {
m_nInputBufferSize = 0;
m_pOutputBuffer = (U8 *)malloc(MAXIMAL_SOCKET_PACKET_LENGTH);
if (m_pOutputBuffer != NULL) {
m_nOutputBufferSize = 0;
init();
};
};
};
// Destructor implementation
transportSocket::~transportSocket(){
m_mBufferProtectorMutex.unlock();
SLEEP(1);
if( m_bThreadAStatus > TH_ARMED ) {
waitThreadAEnd( true );
};
m_mQueueProtectorMutex.unlock();
SLEEP(1);
if( m_bThreadBStatus > TH_ARMED ) {
waitThreadBEnd( true );
};
if (m_pInputBuffer != NULL) {
free(m_pInputBuffer);
};
if (m_pOutputBuffer != NULL) {
free(m_pOutputBuffer);
};
};
void transportSocket::m_fThreadAProc(void){
m_bThreadAStatus = TH_WORKING;
while (m_bThreadAStatus < TH_ENDED) {
m_mBufferProtectorMutex.lock();
if( m_nOutputBufferSize > 0 ){
int index;
int RetVal;
index = 0;
while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
if (RetVal > 0) index += RetVal;
};
m_nOutputBufferSize = 0;
m_mBufferProtectorMutex.unlock();
} else {
m_mBufferProtectorMutex.unlock();
SLEEP(1);
};
};
};
void transportSocket::m_fThreadBProc(void){
m_bThreadBStatus = TH_WORKING;
int RetVal;
while( m_bThreadBStatus < TH_ENDED ){
try{
RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
if (m_nInputBufferSize > 0){
m_mQueueProtectorMutex.lock();
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++){
(*index)->parse(m_pInputBuffer, m_nInputBufferSize);
};
m_mQueueProtectorMutex.unlock();
} else if (RetVal == -1){
m_bThreadBStatus = TH_ENDED;//
m_bThreadAStatus = TH_ENDED;//
LOG_ERROR("Connection Error");
} else {
SLEEP(1);
};
}
catch (exception e){
m_bThreadBStatus = TH_ENDED;//
m_bThreadAStatus = TH_ENDED;//
LOG_ERROR("rcptBlock Error");
};
};
LOG("Ended");
};
void transportSocket::setConnectionStatus( CONNECTION_STATUS CS ){
m_mQueueProtectorMutex.lock();
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++){
(*index)->setConnectionStatus( CS );
};
m_mQueueProtectorMutex.unlock();
}
bool transportSocket::sendData(unsigned char *Data, int DataSize){
m_mBufferProtectorMutex.lock();
if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH) {
m_mBufferProtectorMutex.unlock();
LOG_ERROR("Data Too BIG");
return false;
};
memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
m_nOutputBufferSize += DataSize;
m_mBufferProtectorMutex.unlock();
return true;
};
#undef LOG
#undef LOG_ERROR