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

📄 gap_b3.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:

			if( pcap_sendpacket( Lib_Handle, 
				( BYTE * )&ackframe, MIN_LEN ) == 0 )
			{
				sentack = ack;
				sentwindow = my_window;
			}
			dbg_log( "sendack my_window:%d  id:%d  ack:%d  length:%d  sign:%d oppwindow:%d\n", ackframe.window, 
				*( unsigned short int *)ackframe.id, *( unsigned short int *)ackframe.ack,
				*( unsigned short int *)ackframe.length, ackframe.sign, opp_window);
		}		
		else
		//Sleep( 2000 );
		{
			dbg_log( "no packet to be send output_out:%d, id:%d, state:%d opp_window:%d\n", 
				output_out - output_queue, *( unsigned short int *)output_out -> frame.id ,
				output_out -> state, opp_window);
			EnterCriticalSection( &lock_send );
		
			waitingsend = 1;
			if( WaitForSingleObject( send_ok, TIMEOUT*1000)==WAIT_OBJECT_0 )
			{	ResetEvent(send_ok);
				dbg_log( "have message, send!\n");
				LeaveCriticalSection( &lock_send );
				waitingsend = 0;
				continue;
			}
			
			dbg_log( "send timeout.\n");
			LeaveCriticalSection( &lock_send );
			waitingsend = 0;
			
			/* timeout, send ack */
			memset( ( char *)&ackframe, '\0', sizeof( Frame ) );
			*( unsigned short int *)ackframe.id = 0;
			ackframe.window = my_window;
			*( unsigned short int *)ackframe.ack = ack;
			*( unsigned short int *)ackframe.length = 0;
			ackframe.no = myno;
			get_flag( ( char *)&ackframe, HEAD_LEN - FLAG_LEN );

			if( pcap_sendpacket( Lib_Handle, 
				( u_char *)&ackframe, MIN_LEN ) == 0 )
			{
				sentack = ack;
				sentwindow = my_window;
			}			
			
			/* check queue and resend */
			tmp = output_in;
			for( ; ; )
			{
				id = *( unsigned short int *)tmp->frame.id;
				if( tmp -> state == SENT )
				{
					if( tmp -> times >= 10 )
					{
						dbg_log( "send 10 times but no ack, restart!" );
						do_kill( send_id );
					}								

					tmp -> times ++;
					frm = &(tmp -> frame);
					packet_len = HEAD_LEN + *( unsigned short int *)frm -> length;
					if( packet_len < MIN_LEN )packet_len = MIN_LEN;
					frm-> window = my_window;
					*( unsigned short int *)frm -> ack = ack;
					frm -> no = myno;
					get_flag( ( char *)frm, HEAD_LEN - FLAG_LEN );
					
					if( pcap_sendpacket( Lib_Handle, 
						( u_char * )frm, packet_len ) == 0 )
					{
						sentack = ack;
						sentwindow = my_window;
					}
					resendpackets ++;
					dbg_log( "==================resend packets = %lu\n", resendpackets);
					
					dbg_log( "***resendpacket my_window:%d  id:%d  ack:%d  length:%d  sign:%d oppwindow:%d\n", frm -> window, 
					*( unsigned short int *)frm -> id, *( unsigned short int *)frm -> ack,
					*( unsigned short int *)frm -> length, frm -> sign, opp_window);
					Sleep( 1 );
				}
				tmp ++ ;
				if( tmp == output_queue + QUEUE_LEN )tmp = output_queue;
				if( tmp == output_in )
					break;
			}
		}
	}
	pcap_close( Lib_Handle );
	//EndSend();
	return;
}

void DecodeEthPkt( u_char *p, struct pcap_pkthdr *pkthdr, u_char *pkt )
{
	Frame			*frm;
	unsigned short int	len, id;
	unsigned short int	short_ack, notacked;
	
	unsigned short int	source, m_len;
	char			*ptr;

	frm = ( Frame * )pkt;
	if( frm -> no != oppno )
	{
		dbg_log( "opp number error oppno:%d realno:%d myno:%d\n", frm -> no, oppno, myno);
		return;
	}
	
	/* do auth */
	{
		m_len = HEAD_LEN - FLAG_LEN;
		ptr = ( char * )pkt;
		ptr += 2;
		source = ( *(unsigned short *) ptr - SOURCE ) ^ SOURCE;
		ptr += 2;
		while( m_len > 1 )
		{
			source = *(unsigned short *) ptr ^ source;
			ptr += 2;
			m_len -= 2;
		}
		
		if( m_len == 1 )
		{
			source = *ptr ^ source;
			p++;
		}	
		source = PASSWORD ^ source;	
		if( *( unsigned short int* )pkt != source )
		{
			dbg_log( "auth failed\n");
			return;
		}
	}
	
	if( ( frm->sign & HELLO ) || ( frm->sign & RSP ) )
	{
		if( frm->sign & HELLO )
		{
			dbg_log( "received HELLO\n");
			if( GLBSTATE == RUNNING )
			{
				dbg_log("unexpected HELLO, restart!");
				do_kill( recv_id );
			}
			GLBSTATE = GLBSTATE | RECVHELLO;
			hellonum ++;			
		}
		else
		{
			dbg_log( "received rsp\n");
			GLBSTATE = GLBSTATE | RECVRSP;			
		}
		return;
	}

	id = *(unsigned short int *)frm -> id;
	acked = *(unsigned short int *)frm -> ack;

	notacked = sentid - acked; /* even if sentid<acked, sentid-acked == 65535+sentid-acked */
	EnterCriticalSection( &lock_oppwd );
	opp_window = frm -> window - notacked;
	LeaveCriticalSection( &lock_oppwd );
	dbg_log( "received id = %d ack = %d oppwindow = %d ack = %d sentack = %d\n", id, acked, opp_window, ack, sentack);
	if( opp_window > QUEUE_LEN )
	{
		dbg_log( "opp_window error!" );
		opp_window = 0;
		return;
	}
	
	len = *(unsigned short int *)frm -> length;
	short_ack = ack + 1;					/* 注意当 id,ack达到65535时作处理 */
	if( len > 0 && len <= DATA_LEN && id == short_ack )
	{
		/* 正常的,顺序接收到的包 */
		if( my_window > 0 && input_in -> state == READ )  /* 否则丢掉此包 */
		{
			memcpy( &input_in -> frame, pkt, len + HEAD_LEN );
			input_in -> state = NOREAD;
			input_in++;
			if( input_in == input_queue + QUEUE_LEN )input_in = input_queue;
			EnterCriticalSection( &lock_mywd );
			my_window--;  
			LeaveCriticalSection( &lock_mywd );
			ack++;
		}

		if( waitingoutput == 1 )
		{
			EnterCriticalSection( &lock_output );
			SetEvent(output_ok );
			LeaveCriticalSection( &lock_output );
		}
	}
	
	if( waitingsend == 1 )
	{				
		EnterCriticalSection( &lock_send );
		SetEvent(send_ok );
		LeaveCriticalSection( &lock_send );
	}
	dbg_log( "当前位置:%d    窗口大小:%d\n", input_in - input_queue, my_window);
}

int Recv_Thread(  LPARAM lParam )
{
	char ebuf[ LIBNET_ERRBUF_SIZE ];
	pcap_t	*pd;
	
	char errbuf[PCAP_ERRBUF_SIZE+1];
	char netchardsource [1024];
	

	pd = GetOpenLink();
	
	if( pd == 0 )
	{
		dbg_log( "pcap_open_live error, please config IP!" );
	
		return -1;
	}
   	
	if( pcap_loop( pd, -1, (pcap_handler)DecodeEthPkt, NULL) < 0 )
 	{
		dbg_log( "read error" );
	
		return -1;
	}
	
	return 1;	
}

void Output_Thread(  LPARAM lParam )
{
	int	size, datalen;
	
	char	buf[ DATA_LEN ];

	

	for( ; ; )
	{
		if( input_out -> state == NOREAD )
		{
			/* have packets in input queue */
			datalen = *(unsigned short int *)input_out->frame.length;
			if (input_out->frame.cFlag == 1) {
				CloseHandle(outputfd);
				break;
			}
			WriteFile( outputfd, input_out->frame.data, datalen,(LPDWORD)&size ,NULL  );
					if( size < 0 )
					{
						dbg_log( "write file failed!!\n" );
						break;
					}
					else if( size < datalen )   /* write all data */
					{
						dbg_log( "have not write all data, try again %d - %d !\n", datalen, size );
						datalen -= size;
						memcpy( buf, input_out->frame.data + size, datalen );
						memcpy( input_out->frame.data, buf, datalen );
						*(unsigned short int *)input_out->frame.length = datalen;
						break;
					}

					dbg_log( "写出一个包id=%d my_window:%d\n", *(unsigned short int *)input_out->frame.id, my_window + 1);
					input_out -> state = READ;
					input_out++;
					if( input_out == input_queue + QUEUE_LEN )input_out = input_queue;
					EnterCriticalSection( &lock_mywd );
					if( my_window < QUEUE_LEN )my_window++;
					LeaveCriticalSection( &lock_mywd );

					if( waitingsend == 1 )
					{
						EnterCriticalSection( &lock_send );
						SetEvent(send_ok );
						LeaveCriticalSection( &lock_send );
					}
		}/* if( input_out -> state == NOREAD ) */
		else
		//Sleep( 2000 );
		{
			EnterCriticalSection( &lock_output );
			
			waitingoutput = 1;
			if( WaitForSingleObject( output_ok,TIMEOUT*1000)==WAIT_TIMEOUT )
			{
				dbg_log( "output timeout. continue!\n");
				LeaveCriticalSection( &lock_output );
				waitingoutput = 0;
				continue;
			}
			ResetEvent(output_ok);
			dbg_log( "have output message!\n");
			LeaveCriticalSection( &lock_output );
			waitingoutput = 0;
			continue;
		}	
	}	
	//EndRecv();
}
int InitRecv(char* output_fifo,int _myno,int _oppno)
{
	char	*progname;
	int	c, no;

	dbg_log( "为输出数据,正在打开FIFO... %s\n", output_fifo);
	outputfd = CreateFile( output_fifo,
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(outputfd==INVALID_HANDLE_VALUE)return 0;
	dbg_log( "打开FIFO成功\n");

	myno = _myno;
	oppno = _oppno;
	

	dbg_log( "program start!" );
	dbg_log( "init param!\n");
	init_param();
	dbg_log( "init param ok!\n");
	DWORD dwThread;
	//input_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Input_Thread,NULL,0,&dwThread);  
	output_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Output_Thread,NULL,0,&dwThread);  
	recv_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Recv_Thread,NULL,0,&dwThread);  
	send_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Send_Thread,NULL,0,&dwThread);  
	return 0;
}
void EndSend()
{
	CloseHandle(outputfd);

	DeleteCriticalSection(&lock_mywd);

	DeleteCriticalSection(&lock_oppwd);

	DeleteCriticalSection(&lock_send);

	DeleteCriticalSection(&lock_output);
	DeleteCriticalSection(&lock_input);

	CloseHandle(input_ok);	
	CloseHandle(output_ok);
	CloseHandle(send_ok);


}
void EndRecv()
{
	CloseHandle(inputfd);

	DeleteCriticalSection(&lock_mywd);

	DeleteCriticalSection(&lock_oppwd);

	DeleteCriticalSection(&lock_send);

	DeleteCriticalSection(&lock_output);
	DeleteCriticalSection(&lock_input);

	CloseHandle(input_ok);	
	CloseHandle(output_ok);
	CloseHandle(send_ok);
}
int InitSend( char* input_fifo,int _myno,int _oppno)
{
	char	*progname;
	int	c, no;

	inputfd=CreateFile(input_fifo,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(inputfd==INVALID_HANDLE_VALUE)return 0;
	
	dbg_log( "打开FIFO成功\n");


	dbg_log( "program start!" );
	dbg_log( "init param!\n");
	init_param();	
	myno = _myno;
	oppno = _oppno;
	dbg_log( "init param ok!\n");
    DWORD dwThread;
	input_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Input_Thread,NULL,0,&dwThread);  
	//output_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Output_Thread,NULL,0,&dwThread);  
	recv_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Recv_Thread,NULL,0,&dwThread);  
	send_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Send_Thread,NULL,0,&dwThread);  


	


	


	
	
	return 0;
}

⌨️ 快捷键说明

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