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.
237 lines
8.7 KiB
237 lines
8.7 KiB
#include "transportSocket.h"
|
|
#include "../../data/protocol/protocol.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "TSOC", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "TSOC", this, _LOG_ERROR, X )
|
|
|
|
// Constructor implementation
|
|
transportSocket::transportSocket() : basicTransport(), basicDualThread(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_nInputBufferSize = 0;
|
|
m_nOutputBufferSize = 0;
|
|
m_bInitComplete = false;
|
|
//For Select IO Mode
|
|
pipe( m_iControlPipeFD );
|
|
FD_ZERO( &m_sSenderSet );
|
|
FD_ZERO( &m_sReaderSet );
|
|
|
|
FD_SET( m_iControlPipeFD[0], &m_sSenderSet );
|
|
m_nSenderSetMax = m_iControlPipeFD[0];
|
|
FD_SET( m_iControlPipeFD[1], &m_sReaderSet );
|
|
m_nReaderSetMax = m_iControlPipeFD[1];
|
|
/*
|
|
#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_EXTRA
|
|
LOG("Destructor begin!");
|
|
# endif
|
|
m_mBufferProtectorMutex.unlock();
|
|
_close();
|
|
# ifdef DEBUG_EXTRA
|
|
LOG("Destructor end!");
|
|
# endif
|
|
};
|
|
|
|
|
|
bool transportSocket::create( void ){
|
|
m_bInitComplete = init();
|
|
if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
|
|
return m_bInitComplete;
|
|
};
|
|
|
|
bool transportSocket::_close( void ){
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
//LOG("Sending Close Req!");
|
|
if( m_iControlPipeFD[0] > -1 ){
|
|
//LOG("Sending Close Req[1]!");
|
|
write( m_iControlPipeFD[0], "E", 1 );
|
|
};
|
|
if( m_iControlPipeFD[1] > -1 ){
|
|
//LOG("Sending Close Req[2]!");
|
|
write( m_iControlPipeFD[1], "E", 1 );
|
|
};
|
|
if( m_bInitComplete ) m_bInitComplete = !uninit();
|
|
//LOG("Closeing FIDs!");
|
|
if ( m_iControlPipeFD[0] > -1 ){
|
|
//LOG("Closeing FIDs[1]!");
|
|
close( m_iControlPipeFD[0] );
|
|
};
|
|
if ( m_iControlPipeFD[1] > -1 ){
|
|
//LOG("Closeing FIDs[2]!");
|
|
close( m_iControlPipeFD[1] );
|
|
};
|
|
return true;
|
|
};
|
|
|
|
void transportSocket::m_fThreadAProc(void){
|
|
int index;
|
|
int RetVal;
|
|
int iActiveFDS;
|
|
fd_set sWorkingSet;
|
|
struct timeval sTimeOut;
|
|
m_bThreadAStatus = TH_WORKING;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Sender Thread Started!");
|
|
#endif
|
|
while (m_bThreadAStatus < TH_ENDED) {
|
|
sTimeOut.tv_sec = 5;
|
|
sTimeOut.tv_usec = 0;
|
|
memcpy( &sWorkingSet, &m_sSenderSet, sizeof(m_sSenderSet));
|
|
iActiveFDS = select( m_nSenderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut );
|
|
if( iActiveFDS == 0 ){
|
|
} else if( iActiveFDS == -1){
|
|
//m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
LOG_ERROR("Failure during Select[s]!");
|
|
} else {
|
|
for ( int i =0; i <= m_nSenderSetMax && iActiveFDS > 0; i++ ) {
|
|
if( FD_ISSET( i, &sWorkingSet ) ){
|
|
iActiveFDS --;
|
|
// Successful
|
|
if( i == m_iControlPipeFD[0] ){
|
|
char buffer;
|
|
RetVal = read( i, &buffer, 1 );
|
|
if ( buffer == 'A' ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_nOutputBufferSize > 0 && m_bThreadAStatus < TH_ENDED ){
|
|
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();
|
|
//if( m_bThreadAStatus < TH_ENDED ) usleep(100);
|
|
};
|
|
};
|
|
if ( buffer == 'E' ) {
|
|
//m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
//LOG("Requesting Destruction of Transport[s]!");
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
//LOG("Sender Thread End!");
|
|
};
|
|
|
|
void transportSocket::m_fThreadBProc(void){
|
|
int RetVal;
|
|
int iActiveFDS;
|
|
fd_set sWorkingSet;
|
|
struct timeval sTimeOut;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Recipient Thread Started!");
|
|
#endif
|
|
m_bThreadBStatus = TH_WORKING;
|
|
while( m_bThreadBStatus < TH_ENDED ){
|
|
sTimeOut.tv_sec = 5;
|
|
sTimeOut.tv_usec = 0;
|
|
memcpy( &sWorkingSet, &m_sReaderSet, sizeof(m_sReaderSet));
|
|
iActiveFDS = select( m_nReaderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut );
|
|
if( iActiveFDS == 0 ){
|
|
} else if( iActiveFDS == -1){
|
|
m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
LOG_ERROR("Failure during Select[r]!");
|
|
} else {
|
|
for ( int i =0; i <= m_nReaderSetMax && iActiveFDS > 0; i++ ) {
|
|
if( FD_ISSET( i, &sWorkingSet ) ){
|
|
if ( i == m_iControlPipeFD[1] ) {
|
|
char buffer;
|
|
RetVal = read( i, &buffer, 1 );
|
|
if ( buffer == 'E' ) {
|
|
m_bThreadBStatus = TH_ENDED;
|
|
LOG("Requesting Destruction of Transport[r]!");
|
|
};
|
|
} else {
|
|
RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
|
|
if( RetVal == -1 ){
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_nInputBufferSize = 0;
|
|
m_bThreadBStatus = TH_ENDED;
|
|
//m_bThreadAStatus = TH_ENDED;
|
|
#ifdef DEBUG_EXTRA
|
|
LOG_ERROR("Connection Droped on Reception Data");
|
|
#endif
|
|
} 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
|
|
//if( m_bThreadAStatus < TH_ENDED ) usleep(100);
|
|
};
|
|
} while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
|
|
} else {
|
|
// if( m_bThreadAStatus < TH_ENDED ) usleep(100);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Recipient Thread End!");
|
|
#endif
|
|
};
|
|
|
|
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();
|
|
if( m_iControlPipeFD[1] > -1 ) write( m_iControlPipeFD[1], "A", 1 );
|
|
return true;
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|