斗地主游戏实现7:代码之二(通信协议)

1. game_prot.h

#ifndef GAME_PROT
#define GAME_PROT
#define GM_VERSION3 1
#define REQ_RES4 2
#define SEQUENCE5 0xFF
#define SEQUENCE6 0xFF
#define ODERR_COUNT7 1
#define ODERR_BODY_LEN8 0
#define ODERR_BODY_LEN9 0
#define ORDER14 0xFF
#define ORDER_LEN15 0
#define ORDER_LEN16 0

#define ORDER_INDEX 10
#define OderFlagLen 4
#define GameFlagLen 3
#define protComlen  10
#define PRO_TYPE_REQUEST 1
#define PRO_TYPE_RESPONSE 2

#define PRO_TYPE_ERROR_RESPONSE 3

#define GAME_R_OP & 0x7F
#define GAME_S_OP | 0x80

unsigned char *commonHeadLine;
unsigned char *pFflag;
/*
unsigned char * initProtocol(unsigned char *,int *,int *);
*/
unsigned char *getFitMem(unsigned char *,int,int *, int *);

unsigned char *setGameFlag(unsigned char *,int *,int *);
void reSetGameFlag(unsigned char *);
unsigned char *getGameFlag(unsigned char *);

unsigned char *setVesion(unsigned char *,unsigned char,int *,int *);
void reSetVesion(unsigned char *,unsigned char);
unsigned char getVesion(unsigned char *);

unsigned char *setOrderType(unsigned char *,unsigned char,int *,int *);
void reSetSequence(unsigned char *,int);
unsigned char getOrderType(unsigned char *);

unsigned char *setSequence(unsigned char *,int,int *,int *);
void reSetSequence(unsigned char *,int);
int  getSequence(unsigned char *);

unsigned char *setOrderCount(unsigned char *,unsigned char,int *,int *);
void reSetOrderCount(unsigned char *,unsigned char);
unsigned char getOrderCount(unsigned char *);

unsigned char *setBodylen(unsigned char *,int,int *,int *);
void reSetBodylen(unsigned char *,int);
int  getBodylen(unsigned char *);

unsigned char *setOderFlag(unsigned char *,int,int *,int *);
void reSetOderFlag(unsigned char *,int);
unsigned char *getOderFlag(unsigned char *,int);

int getOrderBegin(unsigned char *,int);
int getOrderContentBegin(int);
unsigned char *setOrder(unsigned char *,unsigned char,int,int *,int *);
unsigned char  getOrder(unsigned char *,int);

unsigned char *setOrderLen(unsigned char *,int,int,int *,int *);
unsigned char *setOrderContent(unsigned char *,unsigned char *,int,int,int *,int *);
int  getOrderLen(unsigned char *,int);

int  getRealBodylen(unsigned char *);

#endif /* GAME_PROT */

 

 

 

2. game_prot.c

#include "game.h"

unsigned char commonHeadLine1[] = {
	'D','D','Z',GM_VERSION3,REQ_RES4,SEQUENCE5,SEQUENCE6,
	ODERR_COUNT7,ODERR_BODY_LEN8,ODERR_BODY_LEN9};/*,0xFD,0xFF,0xFF,0xFF ,ORDER14,ORDER_LEN15,ORDER_LEN16 */

unsigned char pFflag1[] = {'D','D','Z',0xFD,0xFF,0xFF,0xFF,'\0'};
unsigned char *commonHeadLine = commonHeadLine1;
unsigned char *pFflag = pFflag1;

unsigned char *initProtocol(unsigned char *prot,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,protComlen,noffset,resMaxLen);
	bzero(prot,*resMaxLen);
	memcpy(prot,commonHeadLine,protComlen);
	return prot;
}
/* game flag */
unsigned char *setGameFlag(unsigned char *prot,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,GameFlagLen,noffset,resMaxLen);
	memcpy(prot,pFflag,GameFlagLen);
	return prot;
}
void reSetGameFlag(unsigned char *prot) {
	memcpy(prot,pFflag,GameFlagLen);
}
unsigned char *getGameFlag(unsigned char *prot) {
	return prot;
}
/* game version */
unsigned char *setVesion(unsigned char *prot,unsigned char ver,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,1,noffset,resMaxLen);
	prot[3] = ver;
	return prot;
}
void reSetVesion(unsigned char *prot,unsigned char ver) {
	prot[3] = ver;
}
unsigned char getVesion(unsigned char *prot) {
	return prot[3];
}
/* Request Type */
unsigned char *setOrderType(unsigned char *prot,unsigned char req,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,1,noffset,resMaxLen);
	prot[4] = req;
	return prot;
}
void reSetOrderType(unsigned char *prot,unsigned char req) {
	prot[4] = req;
}
unsigned char getOrderType(unsigned char *prot) {
	return prot[4];
}

unsigned char *setSequence(unsigned char *prot,int slen,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,2,noffset,resMaxLen);
	prot[5] = (unsigned char)(slen GAME_S_OP);
	prot[6] = (unsigned char) ((slen >> 7) GAME_S_OP);
	return prot;
}
void reSetSequence(unsigned char *prot,int slen) {
	prot[5] = (unsigned char)(slen GAME_S_OP);
	prot[6] = (unsigned char) ((slen >> 7) GAME_S_OP);
}
int  getSequence(unsigned char *prot) {
	return (prot[5] GAME_R_OP) | ((((int)prot[6]) GAME_R_OP) << 7);
	/*return (prot[5] GAME_R_OP) | ((((int)prot[6]) << 7) GAME_R_OP);*/
}
/* order count */
unsigned char *setOrderCount(unsigned char *prot,unsigned char ordc,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,1,noffset,resMaxLen);
	prot[7] = ordc GAME_S_OP;
	return prot;
}
void reSetOrderCount(unsigned char *prot,unsigned char ordc) {
	prot[7] = ordc GAME_S_OP;
}
unsigned char getOrderCount(unsigned char *prot) {
	return prot[7] GAME_R_OP;
}
/* body length */
unsigned char *setBodylen(unsigned char *prot,int blen,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,2,noffset,resMaxLen);
	prot[8] =(unsigned char) (blen GAME_S_OP);
	prot[9] = (unsigned char) ((blen >> 7) GAME_S_OP);
	return prot;
}
void reSetBodylen(unsigned char *prot,int blen) {
	prot[8] =(unsigned char) (blen GAME_S_OP);
	prot[9] = (unsigned char) ((blen >> 7) GAME_S_OP);
}
int  getBodylen(unsigned char *prot) {
	return (prot[8] GAME_R_OP)| ((((int)prot[9]) GAME_R_OP) << 7);
}
int  getRealBodylen(unsigned char *prot) {
	int i = 1,tmplen = ORDER_INDEX+OderFlagLen+1,rvalue = 0;
	int ocount = getOrderCount(prot);
	if(ocount > 0) {
		rvalue += OderFlagLen + 1 + 2 + ((prot[tmplen] GAME_R_OP)| ((((int)prot[tmplen+1]) GAME_R_OP) << 7));
		for(; i < ocount; i++) {
			tmplen = tmplen + OderFlagLen + 1 + 2 + ((prot[tmplen] GAME_R_OP)| ((((int)prot[tmplen+1]) GAME_R_OP) << 7));
			rvalue += OderFlagLen + 1 + 2 + ((prot[tmplen] GAME_R_OP)| ((((int)prot[tmplen+1]) GAME_R_OP) << 7));
		}
	}
	return rvalue;
}
/* order flag */
unsigned char *setOderFlag(unsigned char *prot,int oindex,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,OderFlagLen,noffset,resMaxLen);
	memcpy(prot+getOrderBegin(prot,oindex),pFflag+GameFlagLen,OderFlagLen);
	return prot;
}
void reSetOderFlag(unsigned char *prot,int oindex) {
	memcpy(prot+getOrderBegin(prot,oindex),pFflag,GameFlagLen);
}
unsigned char *getOderFlag(unsigned char *prot,int oindex) {
	return prot+getOrderBegin(prot,oindex);
}

int getOrderBegin(unsigned char *prot,int oindex) {
	int i = 1,tmplen = ORDER_INDEX;
	for(; i < oindex; i++) {
		tmplen = tmplen + OderFlagLen + 1 + 2 + ((prot[tmplen+1] GAME_R_OP) | ((((int)prot[tmplen+2]) GAME_R_OP) << 7));
	}
	return tmplen;
}
int getOrderContentBegin(int orderBegin) {
	return orderBegin + OderFlagLen + 1 + 2;
}
/* order */
unsigned char *setOrder(unsigned char *prot,unsigned char ord,int oindex,int *noffset,int *resMaxLen) {
	prot = getFitMem(prot,1,noffset,resMaxLen);
	prot[getOrderBegin(prot,oindex) + OderFlagLen] = ord;
	return prot;
}
unsigned char  getOrder(unsigned char *prot,int oindex) {
	return prot[getOrderBegin(prot,oindex) + OderFlagLen];
}
/* order length */
unsigned char *setOrderLen(unsigned char *prot,int odl,int oindex,int *noffset,int *resMaxLen) {
	int beginOL = getOrderBegin(prot,oindex) + OderFlagLen + 1;
	prot = getFitMem(prot,2,noffset,resMaxLen);
	prot[beginOL] = (unsigned char) (odl GAME_S_OP);
	prot[beginOL + 1] = (unsigned char) ((odl >> 7) GAME_S_OP);
	return prot;
}
unsigned char *setOrderContent(unsigned char *prot,unsigned char *content,int size,int oindex,int *noffset,int *resMaxLen) {
	int beginOL = getOrderBegin(prot,oindex) + OderFlagLen + 1+2;
	prot = getFitMem(prot,size,noffset,resMaxLen);
	memcpy(prot+beginOL,content,size);
	return prot;
}
int getOrderLen(unsigned char *prot,int oindex) {
	int obegin = getOrderBegin(prot,oindex);
	return (prot[obegin + OderFlagLen + 1] GAME_R_OP) | ((((int)prot[obegin + OderFlagLen + 2]) GAME_R_OP) << 7);
}

unsigned char *getFitMem(unsigned char *prot,int size,int *noffset, int *resMaxLen) {
	if(*noffset + size + 2 > *resMaxLen) {
		prot = (unsigned char *) memReallocBuf(prot, (*resMaxLen) * 2, resMaxLen);
	}
	(*noffset) += size;
	return prot;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值