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.
140 lines
4.0 KiB
140 lines
4.0 KiB
#include "transportSocket.h"
|
|
#include "../../data/protocol/protocol.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_nInputBufferSize = 0;
|
|
m_nOutputBufferSize = 0;
|
|
m_bInitComplete = false;
|
|
/*
|
|
#ifdef DEBUG
|
|
LOG("Init IO Threads!");
|
|
#endif
|
|
if( init() ){
|
|
int cnt = 5;
|
|
while( cnt-- && m_bThreadAStatus < TH_WORKING ) SLEEP(1); // To ensure start Thread procedures
|
|
#ifdef DEBUG
|
|
LOG("Init IO Threads Done");
|
|
#endif
|
|
setConnectionStatus( CS_CONNECTED );
|
|
} else {
|
|
LOG_ERROR("Init IO Threads ERROR!");
|
|
};
|
|
*/
|
|
};
|
|
|
|
// Destructor implementation
|
|
transportSocket::~transportSocket(){
|
|
# ifdef DEBUG
|
|
LOG("Destruct: START");
|
|
# endif
|
|
m_mBufferProtectorMutex.unlock();
|
|
#ifdef DEBUG
|
|
LOG("Destruct: DONE");
|
|
# endif
|
|
};
|
|
|
|
|
|
bool transportSocket::create( void ){
|
|
m_bInitComplete = init();
|
|
if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
|
|
return m_bInitComplete;
|
|
};
|
|
|
|
void transportSocket::m_fThreadAProc(void){
|
|
int index;
|
|
int RetVal;
|
|
m_bThreadAStatus = TH_WORKING;
|
|
#ifdef DEBUG
|
|
LOG("Sender Thread Started!");
|
|
#endif
|
|
while (m_bThreadAStatus < TH_ENDED) {
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_nOutputBufferSize > 0 ){
|
|
index = 0;
|
|
while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
|
|
RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
|
|
if( RetVal > 0 ) index += RetVal;
|
|
if( RetVal == -1 ){
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
m_nOutputBufferSize = 0;
|
|
m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
LOG_ERROR("Connection Droped on Sending Data");
|
|
};
|
|
};
|
|
m_nOutputBufferSize = 0;
|
|
m_mBufferProtectorMutex.unlock();
|
|
} else {
|
|
m_mBufferProtectorMutex.unlock();
|
|
SLEEP(5);
|
|
};
|
|
};
|
|
};
|
|
|
|
void transportSocket::m_fThreadBProc(void){
|
|
m_bThreadBStatus = TH_WORKING;
|
|
int RetVal;
|
|
#ifdef DEBUG
|
|
LOG("Recipient Thread Started!");
|
|
#endif
|
|
while( m_bThreadBStatus < TH_ENDED ){
|
|
RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
|
|
if( RetVal == -1 ){
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_nInputBufferSize = 0;
|
|
//if(!getStatus()) {
|
|
m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
//} else SLEEP(100);
|
|
LOG_ERROR("Connection Droped on Reception Data");
|
|
} else if( m_nInputBufferSize > 0 ){
|
|
do{
|
|
m_mQueueProtectorMutex.lock();
|
|
if( m_vParsers.size() ){
|
|
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
|
|
m_nInputBufferSize = 0;
|
|
m_mQueueProtectorMutex.unlock();
|
|
} else {
|
|
m_nInputBufferSize = 0;
|
|
m_mQueueProtectorMutex.unlock();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG_ERROR("Try to parse data but No Parsers Assigned!");
|
|
#endif
|
|
SLEEP(100);
|
|
};
|
|
} while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
|
|
} else {
|
|
SLEEP(5);
|
|
};
|
|
};
|
|
};
|
|
|
|
bool transportSocket::sendData(unsigned char *Data, int DataSize){
|
|
if( m_bThreadAStatus != TH_WORKING ) {
|
|
if( m_bThreadAStatus < TH_WORKING ) {
|
|
LOG_ERROR("Trying to Send Data Before Thread is Started!");
|
|
} else {
|
|
LOG_ERROR("Trying to Send Data After Thread is Dead!");
|
|
}
|
|
return false;
|
|
};
|
|
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
|