Exemple #1
0
/*
============================================================================
Saves the log data to a file using the current window width
============================================================================
*/
void VTTpddServerLog::SaveFile(MString filename)
{
	FILE*			fd;
	int				count, c, i, dataIdx;
	VTTpddLogEntry*	pEntry;
	MString			fmt, hexFmt;

	// Validate we have data
	if (m_log.GetSize() == 0)
		return;

	// Test if the file exists
	if ((fd = fopen((const char *) filename, "r")) != NULL)
	{
		// Close the file
		fclose(fd);
		int ans = fl_choice("Overwrite existing file %s?", "Ok", "Cancel", NULL, 
				fl_filename_name((const char *) filename));	
		if (ans == 1)
			return;
	}

	// Try to open the file
	if ((fd = fopen((const char *) filename, "w")) == NULL)
	{
		MString err;

		err.Format("Unable to create file %s\n", (const char *) filename);
		fl_message("%s", (const char *) err);
		return;
	}

	// Test if any leftover RX or TX data not logged to an entry yet
	if (m_rxCount > 0)
		AddNewEntry(TPDD_LOG_RX, m_rxCount, m_rxBuffer);
	if (m_txCount > 0)
		AddNewEntry(TPDD_LOG_TX, m_txCount, m_txBuffer);
	m_txCount = 0;
	m_rxCount = 0;
	
	// Now loop for all entries
	count = m_log.GetSize();
	for (c = 0; c < count; c++)
	{
		// Get the next entry
		pEntry = (VTTpddLogEntry *) m_log[c];
		fmt.Format("%4d: %s: ", pEntry->m_ref, pEntry->m_rxTx ? "TX" : "RX");
		i = 0;
		dataIdx = 0;
		while (dataIdx < pEntry->m_count)
		{
			if (dataIdx != 0)
				fmt = "          ";
			// "Print" the HEX data to the line
			for (i = 0; i < m_bytesPerLine && dataIdx + i < pEntry->m_count; i++)
			{
				hexFmt.Format("%02X ", (unsigned char) pEntry->m_pData[dataIdx + i]);
				fmt += hexFmt;
			}

			// Pad with spaces if less then m_bytesPerLine
			for ( ; i < m_bytesPerLine; i++)
				fmt += (char *) "   ";

			// "Print" the ASCII data to the line
			fmt += (char *) "  ";
			for (i = 0; i < m_bytesPerLine && dataIdx + i < pEntry->m_count; i++)
			{
				// Test if it's actual ASCII data or not
				if (pEntry->m_pData[dataIdx + i] >= ' ' && pEntry->m_pData[dataIdx + i] <= '~')
					hexFmt.Format("%c", (unsigned char) pEntry->m_pData[dataIdx + i]);
				else
					hexFmt = '.';
				fmt += hexFmt;
			}

			// Save to the file
			fprintf(fd, "%s\n", (const char *) fmt);
			dataIdx += i;
		}
	}

	// Close the file
	fclose(fd);
}
Exemple #2
0
void * __stdcall ClientComm::ClientCommThreadFun( void * pIn )
{
	ClientComm				*	classptr;
	fd_set							readfdset;
	int								maxselectno;
	char							tempbuf[8192];
	register int					errorcode;
	MCounter						counter;
	MCounter						TimeoutCount;
	bool							recvdataflag = false;

	classptr = ( ClientComm * )pIn;

	counter.SetCurTickCount();
	while ( classptr->m_ClientCommThread.GetThreadStopFlag( ) == false )
	{
		try
		{
			//检查连接状况
			if( !classptr->GetLinkFlg() )
			{
				if( !classptr->CreatLink( ) )
				{
					classptr->m_sCurrentServerPos++;

					continue;
				}

				MThread::Sleep(5000);

				if( classptr->OnConnectSrvSuc( ) < 0 )
				{
					continue;
				}

				classptr->m_bConnectFlg = true;	//	GUOGUO 20110518
				
				TimeoutCount.SetCurTickCount();
			}
			
			if( counter.GetDuration() >= (Global_Option.GetHeartIntervalTime() * 1000) )
			{
				counter.SetCurTickCount();
				classptr->OnTime();
			}
			maxselectno = (int)classptr->m_clientsocket;

			FD_ZERO(&readfdset);
			FD_SET(classptr->m_clientsocket,&readfdset);
			if( MSocket::Select( classptr->m_clientsocket,&readfdset,NULL,NULL,1000 ) != 1 )
			{
				//if( TimeoutCount.GetDuration() > 60000 )
				if( TimeoutCount.GetDuration() > (Global_Option.GetTimeOut() * 1000))
				{
					MString	strErr;
					strErr.Format("长达%d秒没有数据到达,关闭当前连接", Global_Option.GetTimeOut());
					classptr->OnConnectClose( strErr.c_str() );
					continue;
				}
				continue;
			}

			if( FD_ISSET( classptr->m_clientsocket,&readfdset ) )
			{
				TimeoutCount.SetCurTickCount();
				if ( ( errorcode = recv( classptr->m_clientsocket,tempbuf,8192,0 ) ) <= 0 )
				{
					//对方已经关闭连接
					classptr->CloseLink( false );
					classptr->OnConnectClose( ( MString("连接被断开,")+MErrorCode::GetErrString( MErrorCode::GetSysErr() )).c_str() );
					continue;
				}

				if ( classptr->OnRecvData( tempbuf,errorcode ) < 0 )
				{
					classptr->CloseLink( false );
					classptr->OnConnectClose( "错误的数据处理, 主动断开连接" );
				}

			}
		}
		catch( ... )
		{
			slib_WriteError( Global_UnitNo, 0, "[通讯单元]:通讯接收线程发生未知异常。");
		}
	}

	return(0);
}