Browse Source

Autoreset for Mojo

AGP
Evgeny Gagua 2 years ago
parent
commit
875a567fc8
  1. 2
      Projects/la_mojo/build
  2. 1
      Projects/la_mojo/la_mojo.vpj
  3. 25
      Projects/la_mojo/makefile
  4. 2
      Projects/la_serva/build
  5. 813
      Projects/la_serva/makefile
  6. 231
      lib/App/common/hardwareResourceSerialProvider.cpp
  7. 18
      lib/App/common/hardwareResourceSerialProvider.h
  8. 63
      lib/App/mojo/mojoKernel.cpp
  9. 8
      lib/App/mojo/mojoKernel.h
  10. 7
      lib/App/serva/servaKernel.cpp
  11. 59
      lib/App/serva/servaMediaEncoder.cpp
  12. 2
      lib/data/net/clientPort.cpp
  13. 13
      lib/data/net/serverPort.cpp
  14. 1
      lib/data/net/serverPort.h
  15. 16
      lib/data/transport/transportSerial.cpp
  16. 13
      lib/data/transport/transportSerial.h
  17. 184
      lib/data/transport/transportSocket.cpp
  18. 11
      lib/data/transport/transportSocket.h

2
Projects/la_mojo/build

@ -4,7 +4,7 @@
#make clean -e "CFG=Debug"
#make -e "CFG=Debug" -j$(nproc)
make clean -e "CFG=Debug86"
#make clean -e "CFG=Debug86"
make -e "CFG=Debug86" -j$(nproc)
#make clean -e "CFG=Release86"

1
Projects/la_mojo/la_mojo.vpj

@ -602,7 +602,6 @@
<F N="../../lib/data/sqlite/sqlite_class.cpp"/>
<F N="../../lib/App/common/SQLite_dataBinder.cpp"/>
<F N="../../lib/alg/generic/Transform.cpp"/>
<F N="../../lib/data/transport/transportSerial.cpp"/>
<F N="../../lib/data/transport/transportSocket.cpp"/>
<F N="../../lib/alg/generic/utm.cpp"/>
<F N="../../lib/data/media/video/videoAVLIBSource.cpp"/>

25
Projects/la_mojo/makefile

@ -76,8 +76,7 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o
OBJ=$(COMMON_OBJ) $(CFG_OBJ)
@ -129,8 +128,7 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o -lsqlite3 -lpthread -lmysqlclient -lssl -lcurl \
-lm -lcrypto -lconfig -lz -lv4l1 -lv4l2 -lhidapi-libusb `pkg-config \
@ -273,8 +271,7 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o
OBJ=$(COMMON_OBJ) $(CFG_OBJ)
@ -326,8 +323,7 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o
@ -470,8 +466,7 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o
OBJ=$(COMMON_OBJ) $(CFG_OBJ)
@ -523,8 +518,7 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o -lsqlite3 -lpthread -lmysqlclient -lssl -lcurl \
-lm -lcrypto -lconfig -lz -lv4l1 -lv4l2 -lhidapi-libusb `pkg-config \
@ -672,8 +666,7 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o
OBJ=$(COMMON_OBJ) $(CFG_OBJ)
@ -725,8 +718,7 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \
$(OUTDIR)/protocolPTZ_ONVIF.o $(OUTDIR)/protocolPTZ_PELCO.o \
$(OUTDIR)/protocolPTZ_VISCA.o $(OUTDIR)/serverPort.o \
$(OUTDIR)/sqlite_class.o $(OUTDIR)/SQLite_dataBinder.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSerial.o \
$(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/Transform.o $(OUTDIR)/transportSocket.o $(OUTDIR)/utm.o \
$(OUTDIR)/videoAVLIBSource.o $(OUTDIR)/videoLive555AVLibSource.o \
$(OUTDIR)/xmlParser.o -lsqlite3 -lpthread -lmysqlclient -lssl -lcurl \
-lm -lcrypto -lconfig -lz -lv4l1 -lv4l2 -lhidapi-libusb `pkg-config \
@ -908,7 +900,6 @@ $(OUTDIR)/serverPort.o: ../../lib/data/net/serverPort.cpp ../../lib/data/net/ser
$(OUTDIR)/sqlite_class.o: ../../lib/data/sqlite/sqlite_class.cpp ../../lib/data/sqlite/sqlite_class.h ../../lib/components/base/basicTypes.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicFunctionality.h ../../lib/components/base/basicBuildTimeStamp.h ../../lib/components/base/basicTypes.h
$(OUTDIR)/SQLite_dataBinder.o: ../../lib/App/common/SQLite_dataBinder.cpp ../../lib/App/common/SQLite_dataBinder.h ../../lib/components/base/basicEvent.h ../../lib/components/base/basicTypes.h ../../lib/components/base/basicPosition.h ../../lib/alg/generic/Transform.h
$(OUTDIR)/Transform.o: ../../lib/alg/generic/Transform.cpp ../../lib/alg/generic/Transform.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h
$(OUTDIR)/transportSerial.o: ../../lib/data/transport/transportSerial.cpp ../../lib/data/transport/transportSerial.h ../../lib/components/base/basicDualThread.h ../../lib/components/base/basicThread.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicTransport.h ../../lib/data/protocol/protocol.h
$(OUTDIR)/transportSocket.o: ../../lib/data/transport/transportSocket.cpp ../../lib/data/transport/transportSocket.h ../../lib/components/base/basicDualThread.h ../../lib/components/base/basicThread.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicTransport.h ../../lib/data/protocol/protocol.h
$(OUTDIR)/utm.o: ../../lib/alg/generic/utm.cpp ../../lib/alg/generic/utm.h
$(OUTDIR)/videoAVLIBSource.o: ../../lib/data/media/video/videoAVLIBSource.cpp ../../lib/data/media/video/videoAVLIBSource.h

2
Projects/la_serva/build

@ -4,7 +4,7 @@
#make clean -e "CFG=Debug"
#make -e "CFG=Debug" -j$(nproc)
make clean -e "CFG=Debug86"
#make clean -e "CFG=Debug86"
make -e "CFG=Debug86" -j$(nproc)
#make clean -e "CFG=Release86"

813
Projects/la_serva/makefile
File diff suppressed because it is too large
View File

231
lib/App/common/hardwareResourceSerialProvider.cpp

@ -35,7 +35,19 @@
memset( &m_sUartData, 0, sizeof(m_sUartData));
memset( &m_sTTY, 0, sizeof(m_sTTY));
m_iDeviceFD = -1;
m_iMasterFD = -1;
m_iMasterFD = -1;
#ifdef FIO_SELECT_MODE
m_iControlPipeFD[0]=m_iControlPipeFD[1] = -1;
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];
#endif
#ifdef __linux__
m_iInFID = -1;
m_iOutFID = -1;
@ -431,6 +443,10 @@ bool hardwareResourceSerialProvider::create_SOL(){
break;
};
};
#ifdef FIO_SELECT_MODE
FD_SET( m_iDeviceFD, &m_sReaderSet );
if ( m_nReaderSetMax < m_iDeviceFD ) m_nReaderSetMax = m_iDeviceFD;
#endif
return init();
};
@ -743,6 +759,12 @@ bool hardwareResourceSerialProvider::create_HW(){
//Set DataTerminalReady 1
#endif
setConnectionStatus( CS_CONNECTED );
#ifdef FIO_SELECT_MODE
FD_SET( m_iInFID, &m_sReaderSet );
if ( m_nReaderSetMax < m_iInFID ) m_nReaderSetMax = m_iInFID;
#endif
if( init() ) {
#ifdef DEBUG
sprintf( MsgBuffer,"Init %s [%d] OK", m_sUartData.path, m_sUartData.speed );
@ -759,6 +781,12 @@ bool hardwareResourceSerialProvider::closePort(){
char tBuff[128];
setConnectionStatus( CS_DISCONNECTED );
#ifdef FIO_SELECT_MODE
if( m_iControlPipeFD[0] > -1 ) write( m_iControlPipeFD[0], "E", 1 );
if( m_iControlPipeFD[1] > -1 ) write( m_iControlPipeFD[1], "E", 1 );
#endif
#ifndef HALF_DUPLEX
if( m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT ){
uninit();
@ -768,6 +796,11 @@ bool hardwareResourceSerialProvider::closePort(){
uninit();
};
#endif
#ifdef FIO_SELECT_MODE
if ( m_iControlPipeFD[0] > -1 ) close( m_iControlPipeFD[0] );
if ( m_iControlPipeFD[1] > -1 ) close( m_iControlPipeFD[1] );
#endif
switch( m_sUartData.type ) {
case HARDWARE_UART:{
break;
@ -816,84 +849,157 @@ bool hardwareResourceSerialProvider::closePort(){
#ifndef HALF_DUPLEX
void hardwareResourceSerialProvider::m_fThreadAProc(void){
int index;
int RetVal;
int index;
int RetVal;
#ifdef FIO_SELECT_MODE
int iActiveFDS;
fd_set sWorkingSet;
struct timeval sTimeOut;
#endif
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)){
#ifdef DEBUG_EXTRA
char TT[64];
sprintf( TT, "Have %d Bytes to send!", m_nOutputBufferSize );
LOG( TT );
#ifdef FIO_SELECT_MODE
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' ){
#endif
m_mBufferProtectorMutex.lock();
if( m_nOutputBufferSize > 0 ){
index = 0;
while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
#ifdef DEBUG_EXTRA
char TT[64];
sprintf( TT, "Have %d Bytes to send!", m_nOutputBufferSize );
LOG( TT );
#endif
RetVal = sendBlock( m_pOutputBuffer + index, m_nOutputBufferSize - index );
if( RetVal > 0 ){
index += RetVal;
} else {
//if 0 or -(error)
if( RetVal == 0 ){
usleep( 100 );
} else {
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();
#ifndef FIO_SELECT_MODE
} else {
m_mBufferProtectorMutex.unlock();
SLEEP(5);
#endif
RetVal = sendBlock( m_pOutputBuffer + index, m_nOutputBufferSize - index );
if( RetVal > 0 ){
index += RetVal;
} else {
//if 0 or -(error)
if( RetVal == 0 ){
usleep( 100 );
} else {
setConnectionStatus( CS_DISCONNECTED );
m_nOutputBufferSize = 0;
//m_bThreadBStatus = TH_ENDED;
m_bThreadAStatus = TH_ENDED;
LOG_ERROR("Connection Droped on Sending Data");
};
#ifdef FIO_SELECT_MODE
};
if ( buffer == 'E' ) {
//m_bThreadBStatus = TH_ENDED;
m_bThreadAStatus = TH_ENDED;
LOG("Requesting Destruction of Transport[s]!");
};
};
};
};
m_nOutputBufferSize = 0;
m_mBufferProtectorMutex.unlock();
} else {
m_mBufferProtectorMutex.unlock();
SLEEP(5);
};
};
};
#endif
};
};
void hardwareResourceSerialProvider::m_fThreadBProc(void){
m_bThreadBStatus = TH_WORKING;
void hardwareResourceSerialProvider::m_fThreadBProc(void){
int RetVal;
#ifdef FIO_SELECT_MODE
int iActiveFDS;
fd_set sWorkingSet;
struct timeval sTimeOut;
#endif
m_bThreadBStatus = TH_WORKING;
#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;
m_bThreadBStatus = TH_ENDED;
#ifdef DEBUG_EXTRA
LOG("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 {
#ifdef DEBUG_EXTRA
LOG( m_sUartData.path );
LOG( (char *)m_pInputBuffer );
LOG_ERROR("Try to parse data but No Parsers Assigned!");
#ifdef FIO_SELECT_MODE
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 {
#endif
m_nInputBufferSize = 0;
m_mQueueProtectorMutex.unlock();
SLEEP(100);
RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize);
if( RetVal == -1 ){
setConnectionStatus(CS_DISCONNECTED);
m_nInputBufferSize = 0;
m_bThreadBStatus = TH_ENDED;
#ifdef DEBUG_EXTRA
LOG("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 {
#ifdef DEBUG_EXTRA
LOG( m_sUartData.path );
LOG( (char *)m_pInputBuffer );
LOG_ERROR("Try to parse data but No Parsers Assigned!");
#endif
m_nInputBufferSize = 0;
m_mQueueProtectorMutex.unlock();
SLEEP(100);
};
} while( ( m_vParsers.size() == 0 ) /*&& ( m_bThreadAStatus < TH_ENDED )*/ && ( m_bThreadBStatus < TH_ENDED ) );
#ifndef FIO_SELECT_MODE
} else {
SLEEP(5);
#endif
};
#ifdef FIO_SELECT_MODE
};
};
} while( ( m_vParsers.size() == 0 ) /*&& ( m_bThreadAStatus < TH_ENDED )*/ && ( m_bThreadBStatus < TH_ENDED ) );
} else {
SLEEP(5);
};
};
};
#endif
};
};
@ -926,6 +1032,9 @@ bool hardwareResourceSerialProvider::closePort(){
memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
m_nOutputBufferSize += DataSize;
m_mBufferProtectorMutex.unlock();
#ifdef FIO_SELECT_MODE
if( m_iControlPipeFD[1] > -1 ) write( m_iControlPipeFD[1], "A", 1 );
#endif
return true;
};
#else // HALF_DUPLEX

18
lib/App/common/hardwareResourceSerialProvider.h

@ -6,6 +6,7 @@
//#define HALF_DUPLEX
#define FIO_SELECT_MODE
#ifndef HALF_DUPLEX
#include "../components/base/basicDualThread.h"
@ -18,6 +19,11 @@
#include <string.h> // string function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#ifdef __linux__
#include <unistd.h> // UNIX standard function definitions
#include <termios.h> // POSIX terminal control definitions
@ -39,6 +45,7 @@
//#define DEBUG_SERIAL_COMMUNICATION
#endif
#ifndef MAXIMAL_SOCKET_PACKET_LENGTH
#define MAXIMAL_SOCKET_PACKET_LENGTH 102400
#endif
@ -88,7 +95,16 @@ typedef std::queue<SIO_TZ*> Q_SIO_TZ;
bool closePort();
int sendBlock(unsigned char *Data, int DataSize);
int recptBlock(unsigned char *Buffer, int *BufferLength);
protected:
protected:
#ifdef FIO_SELECT_MODE
int m_nSenderSetMax;
int m_nReaderSetMax;
fd_set m_sSenderSet;
fd_set m_sReaderSet;
int m_iControlPipeFD[2];
#endif
#ifndef HALF_DUPLEX
U8 m_pInputBuffer[MAXIMAL_SOCKET_PACKET_LENGTH];//100KB
U8 m_pOutputBuffer[MAXIMAL_SOCKET_PACKET_LENGTH];

63
lib/App/mojo/mojoKernel.cpp

@ -63,6 +63,11 @@ mojoKernel::mojoKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
m_bKeepLocalReport = true;
m_bMediaMode = 0;
m_pRPC = NULL;
#ifdef EMBED_AUTORESET
m_bEnableAutoReset = false;
m_nSessionCounter = 0;
m_nAutoResetTime = 0;
#endif
//setWM( WM_NONE );
};
@ -166,6 +171,21 @@ bool mojoKernel::create(){
sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
LOG( tBuffer );
};
#ifdef EMBED_AUTORESET
if( !config_setting_lookup_int( m_pSettings, "AUTORESET", &tVal ) ){
m_bEnableAutoReset = false;
tVal = m_nAutoResetTime = 0;
pSetting = config_setting_add( m_pSettings, "AUTORESET", CONFIG_TYPE_INT );
config_setting_set_int( pSetting, tVal );
basicSettingsProvider::getInstance()->compromise();
LOG("Autoreset not defined - Set Disabled!");
} else {
m_bEnableAutoReset = true;
m_nAutoResetTime = tVal;
sprintf( tBuffer, "App Autoreset Read From Settings: %d", m_nAutoResetTime );
LOG( tBuffer );
};
#endif
} else {
LOG_ERROR("Failed Read Config Settings!");
return false;
@ -343,6 +363,9 @@ void mojoKernel::stopSession( bool forced ){
m_pEventHeap->broadcast( bEVT );
};
m_bSessionStarted = false;
#ifdef EMBED_AUTORESET
m_nSessionCounter = 0;
#endif
};
m_mSessionProtector.unlock();
};
@ -354,6 +377,9 @@ void mojoKernel::validateSession(){
if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
m_nNoAlarmCounter++;
} else {
#ifdef EMBED_AUTORESET
m_nSessionCounter = 0;
#endif
DBC_EVT_RECORD tRecord;
memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
uint64_t TS_64 = getMilliseconds() + 20;
@ -371,6 +397,43 @@ void mojoKernel::validateSession(){
m_pDBC->addEvent( &tRecord );
m_bSessionStarted = false;
};
#ifdef EMBED_AUTORESET
if ( m_bEnableAutoReset ) {
if (m_nSessionCounter < m_nAutoResetTime && ( m_nNoAlarmCounter < 5 ) ) {
m_nSessionCounter ++;
} else {
m_nSessionCounter = 0;
//Reset All Sensors
DBC_INT_LIST *tL = m_pDBC->listNodes(false);
if ( tL ) {
for ( DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++ ) {
m_pDBC->pushMemCMD( *idx, 1, "'A'" );
};
delete( tL );
};
m_nNoAlarmCounter = NOTARGET_SESSION_CLOSE;//Session will be closed in 1 second
//Close Session
/*
DBC_EVT_RECORD tRecord;
memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
uint64_t TS_64 = getMilliseconds() + 20;
tRecord.ts_s = TS_64/ 1000;
tRecord.ts_ms = TS_64 % 1000;
tRecord.node_id = m_nServerNodeID;
tRecord.type = EVT_LRF_END_SESSION_TO;
CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
if( tUUID ) {
strncpy(tRecord.uuid, tUUID, 64 );
free( tUUID );
} else {
memset( tRecord.uuid, 0, 65 );
};
m_pDBC->addEvent( &tRecord );
m_bSessionStarted = false;
*/
};
};
#endif
};
m_mSessionProtector.unlock();
};

8
lib/App/mojo/mojoKernel.h

@ -21,6 +21,8 @@
#define NOTARGET_SESSION_CLOSE 300 //sec
#define EMBED_AUTORESET 300 //sec
#include "../../components/base/basicThread.h"
#include "../../components/base/basicSettingsProvider.h"
#include "../../components/base/basicEventHeap.h"
@ -76,7 +78,11 @@ class mojoKernel: public kernelPrototype{
bool m_bSessionStarted;
uint8_t m_bMediaMode;
uint32_t m_nNoAlarmCounter;
#ifdef EMBED_AUTORESET
bool m_bEnableAutoReset;
uint32_t m_nAutoResetTime;
uint32_t m_nSessionCounter;
#endif
#ifdef LA_METRO
#endif

7
lib/App/serva/servaKernel.cpp

@ -378,10 +378,11 @@ bool servaKernel::processVideoFile( uint64_t dbid ){
#endif
char tA[MAX_PATH];
char tB[MAX_PATH];
//Todo -> Instead GetSessionBase -> calc Base based on TS of video
sprintf( tA,"%s/%s.%s", m_pSM->getSessionBase( NFO->session_id ), NFO->uuid, NFO->fmt );
sprintf( tB,"%s/%s.mp4", m_pSM->getSessionBase( NFO->session_id ), NFO->uuid );
if ( ptr->create( tA, tB, NFO->fps ) ) {
#ifdef DEBUG_EXTRA
#ifdef DEBUG
LOG("Encoder Created!");
#endif
m_vEncodersPool.push_back( ptr );
@ -398,10 +399,14 @@ void servaKernel::handleEncodedVideo( servaMediaEncoder *ptr ){
if ( ptr ) {
m_pDBC->updateEncodeFormat( ptr->getDBID(), ptr->getDstFormat() );
if ( m_pSM->addVideo( m_pDBC->getRemoteMediaInfoEx( ptr->getDBID() ) ) ) m_pDBC->removeRemoteMediaInfo( ptr->getDBID() );
LOG("Delete Encoder!");
delete( ptr );
LOG("Done");
};
};
//Todo -> Startup Cleanup Media Encoder Queue (with ability to finish unfinishing jobs -> Drop just unsucessful transfers
// Make Treshold of Running Encoders to a server
#undef LOG

59
lib/App/serva/servaMediaEncoder.cpp

@ -24,12 +24,12 @@ servaMediaEncoder::servaMediaEncoder( uint64_t Ref ):basicThread(){
memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) );
} else {
memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ));
m_sEncoderSettings.CRF = 20;
strncpy( m_sEncoderSettings.preset, "medium", 15 );
m_sEncoderSettings.CRF = 28;//20;
strncpy( m_sEncoderSettings.preset, "slow", 15 );
strncpy( m_sEncoderSettings.tune, "fastdecode", 15 );
strncpy( m_sEncoderSettings.profile, "high422", 15 );
strncpy( m_sEncoderSettings.profile, "main", 15 );
m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME;
m_sEncoderSettings.bitrate = 400000;
m_sEncoderSettings.bitrate = 666;
strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 );
strncpy( m_sEncoderSettings.CODEC, "h264", 4 );
}
@ -53,6 +53,7 @@ servaMediaEncoder::~servaMediaEncoder(){
av_free( m_sEncoderVS.m_pFormatCtx );
};
if (m_sEncoderVS.m_pFormatOpts) av_free( m_sEncoderVS.m_pFormatOpts );
LOG("Destructor Exit");
};
#include <sys/stat.h>
bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int height, CSTR custom ){
@ -311,15 +312,15 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int
m_sEncoderVS.m_pCodecCtx->height = m_sDecoderVS.m_pCodecCtx->height;
//const AVRational dst_fps = { fps, 1 };
m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 10000 );// .num = 1;//av_inv_q(dst_fps);
m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 150000 );// .num = 1;//av_inv_q(dst_fps);
//m_sEncoderVS.m_pVideoStr->time_base.den = fps;
m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1);//.num = fps;
m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1 );//.num = fps;
//m_sEncoderVS.m_pVideoStr->r_frame_rate.den = 1;
m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1); //.num = fps;
m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1 ); //.num = fps;
//m_sEncoderVS.m_pVideoStr->avg_frame_rate.den = 1;
m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 10000 );//.num = 1;
m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 150000 );//.num = 1;
//m_sEncoderVS.m_pCodecCtx->time_base.den = fps;
m_sEncoderVS.m_pCodecCtx->gop_size = fps << 1; // emit one intra frame every fps*2 frames
m_sEncoderVS.m_pCodecCtx->gop_size = fps << 1; // emit one intra frame every fps*2 frames
m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
//if (codec_id == AV_CODEC_ID_H264)
@ -327,13 +328,13 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int
//av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0);
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", m_sEncoderSettings.preset, 0 );
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "profile", m_sEncoderSettings.profile, 0 );
//m_pCodecCtx->max_b_frames = 1;
//m_pCodecCtx->level = 31;
m_sEncoderVS.m_pCodecCtx->max_b_frames = m_sEncoderVS.m_pCodecCtx->gop_size * 3;
m_sEncoderVS.m_pCodecCtx->level = 31;
/// P-frames, generated by referencing data from prev and future frames.
/// [Compression up, CPU usage up]
/// [use 3/gop]
m_sEncoderVS.m_pCodecCtx->max_b_frames = 5;
//m_sEncoderVS.m_pCodecCtx->max_b_frames = 5;
/// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
/// [use 3–5 ref per P]
@ -400,20 +401,25 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int
};
void servaMediaEncoder::m_fThreadProc( void ){
AVPacket read_packet;
AVPacket encoded_packet;
uint32_t current_frame_num = 0;
uint32_t frame_count = 0;
uint8_t operc = 0;
uint64_t pack_duration = m_sEncoderVS.m_pCodecCtx->time_base.den / m_sEncoderVS.m_pCodecCtx->time_base.num / m_sEncoderVS.m_pVideoStr->avg_frame_rate.num * m_sEncoderVS.m_pVideoStr->avg_frame_rate.den;
int got_frame;
int test;
AVPacket read_packet;
AVPacket encoded_packet;
uint64_t current_frame_num = 0;
uint64_t frame_count = 0;
uint8_t operc = 0;
uint64_t pack_duration = m_sEncoderVS.m_pCodecCtx->time_base.den / m_sEncoderVS.m_pCodecCtx->time_base.num
/ m_sEncoderVS.m_pVideoStr->avg_frame_rate.num *
m_sEncoderVS.m_pVideoStr->avg_frame_rate.den + 666;
int got_frame;
//int test;
m_bStatus = THREAD_INIT;
// Allocate video frame
AVFrame *decoded_frame = av_frame_alloc();
m_bThreadStatus = TH_WORKING;
m_bStatus = ENCODER_ENCODING;
m_nReadSize = 0;
char tB[128];
sprintf( tB, "Pack Duration: %" PRIu64, pack_duration );
LOG(tB);
while( av_read_frame( m_sDecoderVS.m_pFormatCtx, &read_packet ) >= 0 && m_bThreadStatus < TH_ENDED ){
m_nReadSize += read_packet.size;
// Is this a packet from the video stream?
@ -428,7 +434,7 @@ void servaMediaEncoder::m_fThreadProc( void ){
av_init_packet( &encoded_packet );
encoded_packet.data = NULL;
encoded_packet.size = 0;
decoded_frame->pts = (int64_t)current_frame_num * pack_duration;
decoded_frame->pts = current_frame_num * pack_duration;
uint8_t perc = ( 100 * m_nReadSize ) / m_nInFileSize;
if ( operc!= perc ) {
@ -441,11 +447,14 @@ void servaMediaEncoder::m_fThreadProc( void ){
continue;
};
if( got_frame ){
//if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE ) encoded_packet.pts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
if( m_sEncoderVS.m_pCodecCtx->coded_frame->key_frame ) encoded_packet.flags |= AV_PKT_FLAG_KEY;
encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index;
encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index;
//if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE ) encoded_packet.pts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
//encoded_packet.pts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
//encoded_packet.dts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
encoded_packet.duration = pack_duration;
//if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE )
encoded_packet.duration = pack_duration;
if( av_interleaved_write_frame( m_sEncoderVS.m_pFormatCtx, &encoded_packet ) < 0 ){
m_bThreadStatus = TH_ENDED;
continue;
@ -470,7 +479,7 @@ void servaMediaEncoder::m_fThreadProc( void ){
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_ENCODE_PROGRESS, 100 );
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_READY, 100 );
unlink( m_sDecoderVS.m_pFileName );
#ifdef DEBUG_EXTRA
#ifdef DEBUG
LOG("Encoding Done!");
#endif
m_bStatus = ENCODER_FINISHED;

2
lib/data/net/clientPort.cpp

@ -90,6 +90,8 @@ bool clientPort::create(){
sprintf(tB, "Connect %s [%d] OK", m_cHostName, m_nPort);
LOG(tB);
#endif
FD_SET( m_nSocketFD, &m_sReaderSet );
if ( m_nReaderSetMax < m_nSocketFD ) m_nReaderSetMax = m_nSocketFD;
return transportSocket::create();
} else {
LOG_ERROR("Cannot Connect!");

13
lib/data/net/serverPort.cpp

@ -18,8 +18,21 @@ serverPort::serverPort( SOCKET AcceptedSocket ):transportSocket(){
serverPort::~serverPort(){
//Send Abort to controll socket that waits Input ....
};
bool serverPort::create( void ){
FD_SET( m_nSocketFD, &m_sReaderSet );
if ( m_nReaderSetMax < m_nSocketFD ) {
m_nReaderSetMax = m_nSocketFD;
};
//No need to update reader set -> the thread is not run yet!
//write( m_iControlPipeFD[1], "UPD", 4);
//close( m_iControlPipeFD[1] );
return transportSocket::create();
}
int serverPort::sendBlock(unsigned char *Data, int DataSize){
if( !m_bInitComplete || m_eConnectionStatus!= CS_CONNECTED ) return -1; // Exit after unsuccess reconnect

1
lib/data/net/serverPort.h

@ -51,6 +51,7 @@ public:
virtual ~serverPort();
void setKeepAlive(bool Param);
bool getKeepAlive(void){ return m_bKeepAlive; };
virtual bool create( void );
};
#endif // __SOCKETPORT_H_INCL__

16
lib/data/transport/transportSerial.cpp

@ -14,14 +14,30 @@ transportSerial::transportSerial() :basicTransport(), basicDualThread(){
m_pOutputBuffer = ( U8 * ) malloc ( MAXIMAL_SERIAL_PACKET_LENGTH );
if( m_pOutputBuffer != NULL ) {
m_nOutputBufferSize = 0;
//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];
};
};
};
transportSerial::~transportSerial(){
_close();
if( m_pInputBuffer != NULL ) free( m_pInputBuffer );
if( m_pOutputBuffer != NULL ) free( m_pOutputBuffer );
};
bool transportSerial::create( void ){
m_bInitComplete = init();
if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
return m_bInitComplete;
};
//void *transportSerial::senderProc( void *param ){
void transportSerial::m_fThreadAProc( void ){
m_bThreadAStatus = TH_WORKING;

13
lib/data/transport/transportSerial.h

@ -5,6 +5,10 @@
#include "../../components/base/basicMutex.h"
#include "../../components/base/basicTransport.h"
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
//#define DEBUG_COMMUNICATION
#define MAXIMAL_SERIAL_PACKET_LENGTH 1024
@ -28,12 +32,21 @@ class transportSerial: public basicTransport, public basicDualThread {
int m_nInputBufferSize;
U8 * m_pOutputBuffer;
int m_nOutputBufferSize;
int m_nSenderSetMax;
int m_nReaderSetMax;
fd_set m_sSenderSet;
fd_set m_sReaderSet;
int m_iControlPipeFD[2];
virtual int sendBlock(unsigned char *Data, int DataSize){ return 0;};
virtual int recptBlock(unsigned char *Buffer, int *BufferLength){ return 0;};
public:
transportSerial();
virtual ~transportSerial();
bool sendData( unsigned char *Data, int DataSize );
virtual bool create( void );
virtual bool _close( void );
};

184
lib/data/transport/transportSocket.cpp

@ -1,8 +1,8 @@
#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 )
#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(){
@ -10,6 +10,15 @@ transportSocket::transportSocket() : basicTransport(), basicDualThread(){
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!");
@ -33,7 +42,7 @@ transportSocket::~transportSocket(){
LOG("Destructor begin!");
# endif
m_mBufferProtectorMutex.unlock();
uninit();
_close();
# ifdef DEBUG
LOG("Destructor end!");
# endif
@ -46,71 +55,139 @@ bool transportSocket::create( void ){
return m_bInitComplete;
};
bool transportSocket::_close( void ){
setConnectionStatus( CS_DISCONNECTED );
if( m_iControlPipeFD[0] > -1 ) write( m_iControlPipeFD[0], "E", 1 );
if( m_iControlPipeFD[1] > -1 ) write( m_iControlPipeFD[1], "E", 1 );
if( m_bInitComplete ) m_bInitComplete = !uninit();
if ( m_iControlPipeFD[0] > -1 ) close( m_iControlPipeFD[0] );
if ( m_iControlPipeFD[1] > -1 ) close( m_iControlPipeFD[1] );
return true;
};
void transportSocket::m_fThreadAProc(void){
int index;
int RetVal;
int index;
int RetVal;
int iActiveFDS;
fd_set sWorkingSet;
struct timeval sTimeOut;
m_bThreadAStatus = TH_WORKING;
# ifdef DEBUG_EXTRA
#ifdef DEBUG_EXTRA
LOG("Sender Thread Started!");
#endif
while (m_bThreadAStatus < TH_ENDED) {
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);
};
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){
m_bThreadBStatus = TH_WORKING;
int RetVal;
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 ){
RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
if( RetVal == -1 ){
setConnectionStatus(CS_DISCONNECTED);
m_nInputBufferSize = 0;
m_bThreadBStatus = TH_ENDED;
m_bThreadAStatus = TH_ENDED;
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
if( m_bThreadAStatus < TH_ENDED ) usleep(100);
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;
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
//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);
};
};
};
} while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
} else {
if( m_bThreadAStatus < TH_ENDED ) usleep(100);
};
};
};
};
LOG("Recipient Thread End!");
};
@ -133,6 +210,7 @@ bool transportSocket::sendData(unsigned char *Data, int DataSize){
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;
};

11
lib/data/transport/transportSocket.h

@ -4,6 +4,11 @@
#include "../../components/base/basicDualThread.h"
#include "../../components/base/basicTransport.h"
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#define MAXIMAL_SOCKET_PACKET_LENGTH 4194304 // 4Mb
/**
@ -19,6 +24,11 @@ protected:
U8 m_pOutputBuffer[MAXIMAL_SOCKET_PACKET_LENGTH];
int m_nInputBufferSize;
int m_nOutputBufferSize;
int m_nSenderSetMax;
int m_nReaderSetMax;
fd_set m_sSenderSet;
fd_set m_sReaderSet;
int m_iControlPipeFD[2];
bool m_bInitComplete;
virtual void m_fThreadAProc(void);
@ -31,6 +41,7 @@ public:
virtual bool sendData(unsigned char *Data, int DataSize);
virtual bool reconnect( void ){ return false; };
virtual bool create( void );
virtual bool _close( void );
};

Loading…
Cancel
Save