⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpiptrcdlg.cpp

📁 analog device vdsp LAN driver and examples
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	m_StopAcquiring = true;
	m_StopAcqCtrl.EnableWindow(false);
	m_StartAcqCtrl.EnableWindow(true);
	m_SaveAllCtrl.EnableWindow(true);
	m_ReportCtrl.SetWindowText("");

	if (m_Trc!=NULL) {
		char em[512];

		fclose(m_Trc);
		m_Trc = NULL;
		sprintf(em,"Traced events have been saved in %s",m_SaveAlFileName);
		MessageBox(em,"TCP/IP Tracing");

	}
	Stop();
}

void CTCPIPTrcDlg::OnBrowse() 
{
	OPENFILENAME fn;
	char filename[512];

	m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
	memset(&fn,0,sizeof(fn));
	fn.lStructSize = sizeof(fn);
	fn.lpstrFilter = "Trace dump files\x00*.trc\x00";
	fn.lpstrFile = filename;
	fn.nMaxFile = sizeof(filename);
	fn.lpstrTitle = "File the trace information is to be saved to";
	fn.Flags = OFN_CREATEPROMPT|OFN_LONGNAMES|OFN_PATHMUSTEXIST;
	if (GetSaveFileName(&fn)) {
		m_FileNameCtrl.SetWindowText(filename);
	}
}

void CTCPIPTrcDlg::OnChangeChannel() 
{
	CString fname;
	CString chn,noevs;

	
	
}

void CTCPIPTrcDlg::TidyUp(void)
{
	TraceLine *lin,*nxt;

	// free resources
	EnterCriticalSection(&m_Critical);
	m_EventListCtrl.ResetContent();
	lin = m_TraceHead;
	while (lin != NULL) {
		if (lin->Data) free(lin->Data);
		nxt = lin->Next;
		free(lin);
		lin = nxt;
	}
	m_TraceListSize = 0;
	m_TrcIndex = 0;
	m_NoEntries = 0;
	m_TraceHead = NULL;
	m_TraceTail = NULL;

	m_StopAcquiring = false;

	LeaveCriticalSection(&m_Critical);

}

void CTCPIPTrcDlg::Recv(void)
{
	USES_CONVERSION;

	typedef struct TraceData {
		unsigned int			Active;				// if non-zero some tracing is active
		unsigned int			ActClasses[8];		// Each bit represents one of 256 trace classes
		unsigned int			MaxTrcBytes;		// maximum number of trace bytes
		unsigned int			NoTrcBytes;			// the no of trace bytes to be read by the host
		unsigned char			TrcBytes[1];		// start of the trace buffer
						
	} TraceData;

	int ActPoint=0;

	m_Stopped = false;
	m_ReportCtrl.SetWindowText("Tracing from BTC");

	CoInitialize(NULL);
	try {

		SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_BELOW_NORMAL);

		IADspApplicationPtr pApp("VisualDSP.ADspApplication");
		IADspPluginPtr pBtcPlugin = pApp->PluginList->GetItem("BtcManager");
		IADspBtcManagerPtr pBtcManager = pBtcPlugin->Open();
		IADspProcessorPtr    pProc;
		IADspDebugSessionPtr pSession;
		DWORD adrs=0,lnth=0;
		int nfound = 1;
		unsigned int *rdbuf=NULL;
		int maxlnth,maxelem;
		int nbytes,nwords;
		long indx;
		int actionok;


		pSession = pApp->ActiveSession;
		//  get the first processor in the session
		pProc = pSession->ActiveProcessor;
		ActPoint=-2;
		// wait till the processor is running
		while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
			Sleep(10);
		}
		Sleep(1000);
		while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
			Sleep(10);
		}

		while ((!m_StopAcquiring) && (nfound || (lnth==0) || (adrs==0))) {
			ActPoint=-1;
			// wait till the processor is running
			while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
				Sleep(10);
			}
			if (m_StopAcquiring) break;
			IADspBtcChannelListPtr pBtcChannelList = pBtcManager->ChannelList((long)0);
			DWORD dwChanCount = pBtcChannelList->Count;

			for (DWORD i = 0; i < dwChanCount; ++i)	{
				ActPoint=0;
				// wait till the processor is running
				while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
					Sleep(10);
				}
				if (m_StopAcquiring) break;
				IADspBtcChannelPtr pBtcChannel = pBtcChannelList->GetItem((long)i);

				_bstr_t bstrSupportedWidths = "";
				DWORD dwSupportedWidths = pBtcChannel->SupportedWidths;
				if (dwSupportedWidths & btcWidth32) {
					if (strcmp("TRACE_DATA_STRUCT",OLE2A(pBtcChannel->Name))==0) {
						adrs = pBtcChannel->Address;
						lnth = pBtcChannel->Length;
						nfound = 0;
						break;
					}
				}

			}

		}
		if (m_StopAcquiring) lnth = 0;
		if (lnth>0) {

			rdbuf = (unsigned int *)malloc(((lnth+3)>>2)<<2);
		}
		if ((lnth>0) && (rdbuf!=NULL)) {
			{
				// set the acquiring data to be all ones
				LPSAFEARRAY psaWrite = SafeArrayCreateVector( VT_VARIANT, 0, 9 );

				int cj = -1;
				long i;
				for( i=0; i<9; i++ ) {
					CComVariant var = cj;
					SafeArrayPutElement( psaWrite, &i, &var );
				}

				CComVariant varWrArray;
				varWrArray.vt = VT_ARRAY|VT_VARIANT;
				varWrArray.parray = psaWrite;

				ActPoint=1;
				// wait till the processor is running
#if 0
				while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
					Sleep(10);
				}
				if (m_StopAcquiring) break;

				Sleep(1000);
#endif
				// wait till the processor is running
				while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
					Sleep(10);
				}
				if (!m_StopAcquiring) {
					pBtcManager->Write((long)0, (long)0, adrs, 9, btcWidth32, varWrArray);
				}
			}

			maxlnth = lnth - (sizeof(TraceData) -1  - offsetof(TraceData,MaxTrcBytes));
			maxlnth = (maxlnth+3)&(~0x3);
			maxelem = maxlnth >> 2;			// no. of integers

			while (!m_StopAcquiring) {
				// loop reading the thread data info
				CComVariant varRdArray;

				ActPoint=2;
				while ((!m_StopAcquiring) && (pProc->State != stateRunning)) {
					Sleep(10);
				}
				if (m_StopAcquiring) break;

				nwords = 0;
				actionok = 1;
				try {
					varRdArray = pBtcManager->Read((long)0, (long)0, adrs+9*sizeof(int), maxelem, btcWidth32);
				} catch ( _com_error err )
				{
					actionok = 0; 
				}
				if ((actionok) && (varRdArray.vt != VT_EMPTY))
				{
					CComVariant va;
					indx = 1;

					SafeArrayGetElement(varRdArray.parray,&indx,&va);
					nbytes = V_UI4(&va);
					nwords = (nbytes+3)>>2;
					if (nwords<=maxelem) {
						for (indx = 0; indx<nwords+2;indx++) {
							CComVariant va;

							SafeArrayGetElement(varRdArray.parray,&indx,&va);
							rdbuf[indx] = V_UI4(&va);
						}
					} else {
						nwords = 0;		// ignore if more than is valid
					}
				} else {
					nwords = 0;
				}
				if (nwords>0) {
					// we have got the data 
					unsigned int max,no;

					max = rdbuf[0];
					no = rdbuf[1];

					if ((no>0) && (no<=max) && 
						(no<=  (lnth-(sizeof(TraceData) - offsetof(TraceData,MaxTrcBytes))-2*sizeof(int)))
									) {
						unsigned char *buf = (unsigned char *)(rdbuf+2);
						unsigned int nxt,cnt=0;
						
						// process the data
						nxt = 0;
						while (nxt<no) {
							cnt++;
							if (cnt>80) {
								int gg=3;
							}
							// we have a complete trace to dump
							DumpTraceEntry(buf+nxt);
							nxt = nxt + buf[nxt+1]+4;
						}


						// tell the DSP the data has been processed
						{
							// set the acquiring data to zero
							LPSAFEARRAY psaWrite = SafeArrayCreateVector( VT_VARIANT, 0, 1 );

							int cj = 0x0;
							long i;
							for( i=0; i<1; i++ ) {
								CComVariant var = cj;
								SafeArrayPutElement( psaWrite, &i, &var );
							}

							CComVariant varWrArray;
							varWrArray.vt = VT_ARRAY|VT_VARIANT;
							varWrArray.parray = psaWrite;

							ActPoint=3;
							while ((pProc->State != stateRunning)) {
								Sleep(10);
							}
							pBtcManager->Write((long)0, (long)0, adrs+10*sizeof(int), 1, btcWidth32, varWrArray);
						}
					}

				}	// end of processing the read data

			}	// end of acquiring data 

			{
				// set the acquiring data control to zero
				LPSAFEARRAY psaWrite = SafeArrayCreateVector( VT_VARIANT, 0, 9 );

				int cj = 0x0;
				long i;
				for( i=0; i<9; i++ ) {
					CComVariant var = cj;
					SafeArrayPutElement( psaWrite, &i, &var );
				}

				CComVariant varWrArray;
				varWrArray.vt = VT_ARRAY|VT_VARIANT;
				varWrArray.parray = psaWrite;

				ActPoint =4;
				while (pProc->State != stateRunning) {
					Sleep(10);
				}

				try {
					pBtcManager->Write((long)0, (long)0, adrs, 9, btcWidth32, varWrArray);
				} catch ( _com_error err )
				{
					actionok = 0; 
				}
			}




		}

		pBtcPlugin->Close(pBtcManager);

		if (rdbuf) free(rdbuf);

	} catch ( _com_error err )
	{
		AfxMessageBox(err.ErrorMessage()); 
	}

	CoUninitialize();	
	
	m_Stopped = true;





}




int SetUpListen(int portno,SOCKET *sock)
{
	SOCKET WinSocket;
	char szError[512];
	SOCKADDR_IN local_sin;

	*sock = INVALID_SOCKET;

	WinSocket = socket(AF_INET, SOCK_STREAM, 0);

	if (WinSocket == INVALID_SOCKET) 
	{
	  wsprintf (szError, TEXT("Allocating socket failed. Error: %d"), 
				WSAGetLastError ());
	  MessageBox (NULL, szError, TEXT("Error"), MB_OK);
	  return FALSE;
	}
	// Fill out the local socket address data.
   	memset((char *)&local_sin,0,sizeof(local_sin));
	local_sin.sin_family = AF_INET;
	local_sin.sin_port = htons (portno);
	local_sin.sin_addr.s_addr = INADDR_ANY;

	// Associate the local address with WinSocket.
	if (bind (WinSocket, 
			  (struct sockaddr *) &local_sin, 
			  sizeof (local_sin)) == SOCKET_ERROR) 
	{
	  wsprintf (szError, TEXT("Binding socket failed. Error: %d"), 
				WSAGetLastError ());
	  MessageBox (NULL, szError, TEXT("Error"), MB_OK);
	  closesocket (WinSocket);
	  return FALSE;
	}
	if (listen (WinSocket, 5) == SOCKET_ERROR) 
	{
	  wsprintf (szError, 
				TEXT("Listening to the client failed. Error: %d"),
				WSAGetLastError ());
	  MessageBox (NULL, szError, TEXT("Error"), MB_OK);
	  closesocket (WinSocket);
	  return FALSE;
	}
	*sock = WinSocket;
	return 1;
}


void CTCPIPTrcDlg::TcpRecv(void)
{


	typedef struct TraceData {
		unsigned int			Active;				// if non-zero some tracing is active
		unsigned int			ActClasses[8];		// Each bit represents one of 256 trace classes
		unsigned int			MaxTrcBytes;		// maximum number of trace bytes
		unsigned int			NoTrcBytes;			// the no of trace bytes to be read by the host
		unsigned char			TrcBytes[1];		// start of the trace buffer
						
	} TraceData;
	SOCKET lstn,client;
	SOCKADDR_IN accept_sin;
	int accept_sin_len;
	//char szError[512];
#define BUFSIZE 0x10000
	unsigned char buffer[BUFSIZE];

	m_Stopped = false;

	if (SetUpListen(m_Channel,&lstn)) {
		m_Listen = lstn;

		m_ReportCtrl.SetWindowText("");
		while (!m_StopAcquiring) {
			accept_sin_len = sizeof (accept_sin);

			// Accept an incoming connection attempt on WinSocket.
			m_Client = client = accept (lstn, 
								 (struct sockaddr *) &accept_sin, 
								 (int *) &accept_sin_len);

			if (client == INVALID_SOCKET) 
			{
			  //wsprintf (szError, TEXT("Accepting connection with client failed.") TEXT(" Error: %d"), WSAGetLastError ());
			  //::MessageBox (NULL, szError, TEXT("Error"), MB_OK);
			  break;
			} else {
				int req[9];
				int lnth,nxt,nnxt;
				BOOL bOptVal = TRUE;
				int bOptLen = sizeof(BOOL);
				char cname[256];
				char rep[512];


				strcpy(cname,inet_ntoa(accept_sin.sin_addr));
				strcpy(rep,"Tracing is coming from ");
				strcat(rep,cname);
				m_ReportCtrl.SetWindowText(rep);

				// process the data
				setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, bOptLen); // detect when other end fails
				// send the start tracing request
				memset(req,0xff,sizeof(req));
				lnth = send(client,(const char *)req,sizeof(req),0);
				if (lnth==sizeof(req)) {
					// trace start sent
					nxt = 0;
					while (!m_StopAcquiring) {
						// process the data
						lnth = recv(client,(char *)(buffer+nxt),BUFSIZE-nxt,0);
						if (lnth<0) {
							m_Client = INVALID_SOCKET;
							closesocket(client);
							client = INVALID_SOCKET;
							break;
						} else {
							lnth += nxt;
							nxt = 0;
							while (nxt<lnth) {
								nnxt = nxt+buffer[nxt+1]+4;
								if (nnxt<=lnth) {
									// we have a complete trace to dump
									DumpTraceEntry(buffer+nxt);
									nxt = nxt + buffer[nxt+1]+4;
									if (nxt >= lnth) {
										nxt = 0;
										lnth = 0;
									}
								} else {
									// only a partial trace entry present
									memcpy(buffer,buffer+nxt,lnth-nxt);
									nxt = lnth-nxt;
									lnth =0;
								}
							}
						}

					}
					// send stop tracing
					if (client != INVALID_SOCKET) {
						memset(req,0,sizeof(req));
						lnth = send(client,(const char *)req,sizeof(req),0);
					}
				}
				if (m_Client != INVALID_SOCKET) {
					m_Client = INVALID_SOCKET;
					closesocket(client);
				}
				m_ReportCtrl.SetWindowText("");
			}
		}
		// Stop listening for connections from clients.
		closesocket (lstn);


	} else {
		// listen failed
	}
	m_Stopped = true;

}


VOID ReaderThread(LPVOID lpvParam)
{
	CTCPIPTrcDlg *dlg = (CTCPIPTrcDlg *)lpvParam;

	if (dlg->m_Channel == 0) {
		dlg->Recv();
	} else {
		dlg->TcpRecv();
	}

}




void CTCPIPTrcDlg::OnSaveall() 
{
	if (m_Trc!=NULL) {
		char em[512];

		fclose(m_Trc);
		m_Trc = NULL;
		sprintf("Traced events have been saved in %s",m_SaveAlFileName);
		MessageBox(em,"TCP/IP Tracing");

	} else {
		// we need to open the trace file
		char filename[512];

		m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
		if (filename[0]==0) {
			OPENFILENAME fn;

			memset(&fn,0,sizeof(fn));
			fn.lStructSize = sizeof(fn);
			fn.lpstrFilter = "Trace dump files\x00*.trc\x00";
			fn.lpstrFile = filename;
			fn.nMaxFile = sizeof(filename);
			fn.lpstrTitle = "File the trace information is to be saved to";
			fn.Flags = OFN_CREATEPROMPT|OFN_LONGNAMES|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
			if (GetSaveFileName(&fn)) {
				m_FileNameCtrl.SetWindowText(filename);
			}
		}
		m_FileNameCtrl.GetWindowText(filename,sizeof(filename));
		if (filename[0]!=0) {
			strcpy(m_SaveAlFileName,filename);
			m_Trc =  fopen(filename,"w");
			if (m_Trc == NULL) {
				char err[1024];
				sprintf(err,"Unable to open %s for writing",filename);
				MessageBox(err,"TCP/IP Tracing");
			} else {
				SaveTraceFileIntoRegistry(filename);
			}
		}

	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -