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

📄 gap_b3.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
字号:
#include "stdafx.h"
#include <stdio.h>

#include <stdarg.h>
#include <time.h>

#define HAVE_REMOTE
#include <pcap.h>
#pragma comment(lib,"Packet.lib")
#pragma comment(lib,"wpcap.lib")

#define	QUEUE_LEN	150
#define MIN_WINDOW	QUEUE_LEN / 2
#define	HEAD_LEN	14
#define	FLAG_LEN	4
#define	FRAME_LEN	1500
#define	DATA_LEN	FRAME_LEN - HEAD_LEN
#define	MAX		65536
#define MIN_LEN		46
#define	TIMEOUT		1
#define PASSWORD	0x79b9
#define	SOURCE		0xb7e1

#define RECV_DEVICE	"fxp0"
#define	SEND_DEVICE	"fxp1"
#define	OUTPUT_FIFO	"/tmp/read"
#define	INPUT_FIFO	"/tmp/write"

#define	EMPTY		0	/* 状态值,如果为0,表明队列中该列空间可用,这些状态包括:*/
#define	NOSEND		1	/* 1原本为空,2发送队列中已被确认的帧,3接收队列中已被应用层读走的帧 */
#define	SENT		2
#define	ACK		0
#define	NOREAD		3
#define	READ		0

#define	BEGIN		0	/* GLBSTATE */	
#define	SENDHELLO	1
#define	RECVHELLO	2
#define	SENDRSP		4
#define RECVRSP		8	
#define FINISH		15
#define RUNNING		16
#define	HELLO		2	/* sign in packet */	
#define	RSP		8	/* sign in packet */

#ifdef DEBUG
# define DBG(x)  ATLTRACE(x)
#else
# define DBG(x)  (void*)0
#endif



typedef struct
{
	unsigned char	flag[4];	/* 验证 */
	unsigned char	no;		/* 该通道的标记(编号) */
	unsigned char	id[2];		/* 该包的ID号 */
	unsigned char	window;		/* 向对方通报自己窗口的大小 */
	unsigned char	sign;		/* 标志位,比如,请求重发等 */
	unsigned char	ack[2];		/* 反馈信息,为一ID号,表示该ID号之前的包都已收到 */
	unsigned char	length[2];	/* 长度,不包括首部 */
	unsigned char   cFlag;     // 文件结束标志,0,非结束,1为结束
	unsigned char	data[ DATA_LEN ];
}Frame;

typedef struct
{
	unsigned char	state;
	unsigned char	times;
	Frame		frame;
}Queue;

/* global variable */
static Queue output_queue[ QUEUE_LEN ];		/* 发送队列 */
static Queue input_queue[ QUEUE_LEN ];		/* 接收队列 */
static char	input_fifo[ 128 ];
static char	output_fifo[ 128 ];
//static char	recv_device[ 128 ];
//static char	send_device[ 128 ];

static unsigned char	GLBSTATE = BEGIN;

static Queue	*input_in = input_queue;	/* 从以太接收数据,添加到接收队列的指针 */
static Queue	*input_out = input_queue;	/* 应用层从接收队列读取数据的指针 */
static Queue	*output_in = output_queue;	/* 从应用层接收数据,添加到发送队列的指针 */
static Queue	*output_out = output_queue;	/* 以太发送线程从发送队列读取数据的指针 */
static Queue	*output_acked = output_queue;	/* 指向发送队列中最后一个被确认的包 */

static unsigned short	ack = 0;		/* 即将发送给对方的ack值,表示id号小于等于ack的都已收到 */
static unsigned short	sentack = 0;		/* 已经发送过的ack */
static unsigned short	acked = 0;		/* 自己收到的ack值,表示id号小于等于ack的都已被确认 */
static unsigned short	sentid = 0;		/* 刚刚发送的那一个包的id号 */
static unsigned char	my_window = QUEUE_LEN;	/* 自己的窗口大小 */
static unsigned char	opp_window = QUEUE_LEN;	/* 对方的窗口大小 */
static unsigned char	sentwindow = QUEUE_LEN; 	/* 已经发送过的window */

static unsigned short	counter = 1;		/* id号计数器 */
static unsigned char	waitinginput = 0;	/* 如果输入线程正在等待输入,该值赋1 */
static unsigned char	waitingoutput = 0;	/* 如果输出线程正在等待输出,该值赋1 */
static unsigned char	waitingsend = 0;	/* 如果发送线程正在等信息,该值赋1 */
static unsigned char	hellonum = 0;

static unsigned char	myno = 0;		/* 该通道的编号 */
static unsigned char	oppno = 0;		/* 对方通道的编号 */

static HANDLE 	inputfd, outputfd;

static CRITICAL_SECTION		lock_mywd, lock_oppwd, lock_output;   /* 不可对my_window 和 opp_window 同时进行写操作 */
static CRITICAL_SECTION		lock_input,  lock_send;
static  HANDLE 		input_ok, output_ok, send_ok;

static HANDLE		input_id, send_id, recv_id, output_id;

static unsigned long int resendpackets = 0;
int InitRecv(char* input_fifo,int _myno,int _oppno);
void EndRecv();
int InitSend(char* input_fifo,int _myno,int _oppno);
void EndSend();

⌨️ 快捷键说明

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