(9)应用层协议解析模块(上)

应用层协议解析模块

解析器设计的关键问题

  • 初步的解决方案
    定义一个模块用于从字节流解析Message;
    – 可从指定内存从指定文件描述符读取并解析;
    – 当至少存在12个字节时开始解析;

1:首先解析协议中的头信息和数据区长度(length);
2:根据数据区长度继续从字节流读取数据(payload);
3:当协议数据解析完成时,创建Message并返回,否则返回NULL;

  • 协议解析模块的初步设计
    解析器接口定义
typedef void MParser;
MParser* MParser_New();//创建协议解析器
Message* MParser_ReadMem(MParser* parser, unsigned char* mem, unsigned int length);//从指定内存中解析数据
Message* MParser_ReadFd(Mparser* parser, int fd);//从fd中解析数据
void MParser_Reset(MParser* parser);//重置解析器的状态
void MParser_Del(MParser* parser);//销毁指定解析器

解析器实现细节

解析器数据结构

typedef struct msg_parser
{
	Message cache;//缓存已解析的消息头
	int header;//标识消息头是否解析成功
	int need;//标识还需要多少字节才能完成解析
	Message* msg;//解析中的协议消息(半成品)
}MsgParser;

从内存中解析协议数据
(1) 条件:内存长度至少连续12个字节

memcpy(&p->cache, mem, p->need);

// 从网络字节序转换到本机字节序
p->cache.type = ntohs(p->cache.type);
p->cache.cmd = ntohs(p->cache.cmd);
p->cache.index = ntohs(p->cache.index);
p->cache.total = ntohs(p->cache.total);
p->cache.length = ntohs(p->cache.length);

mem += p->need;//指针移动12个字节
length -= p->need;//长度减少12

p->header = 1;
p->need = p->cache.length;

(2)从内存中读取payload中的数据(可多次读取

//成功解析消息头之后,创建Message
if( !p->msg )
{
	p->msg = malloc(sizeof(p->cache) + p->need);
	if(p->msg)
	{
		*p->msg = p->cache;
	}
}

if( p->msg )
{
	unsigned int len = (p->need < length) ? p->need : length;//length:已有数据的长度,p->need:需要数据的长度
	unsigned int offset = p->msg->length - p->need;//读了一轮数据后,指针的偏移
	
	memcpy(p->msg->payload + offset, mem, len);//将获取的值拷进payload中,由于可能不止一次,所以需要offset来定位
	
	p->need -= len;
}

解析的实现

  • msg_parser.c:
#include <malloc.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "msg_parser.h"

typedef struct msg_parser
{
	Message cache;//缓存已解析的消息头
	int header;//标识消息头是否解析成功
	int need;//标识还需要多少字节才能完成解析
	Message* msg;//解析中的协议消息(半成品)
}MsgParser;

MParser* MParser_New()//创建协议解析器
{
	MParser* ret = calloc(1, sizeof(MsgParser));
	
	MParser_Reset(ret);
	
	return ret;
}

Message* MParser_ReadMem(MParser* parser, unsigned char* mem, unsigned int length)//从指定内存中解析数据
{
	Message* ret = NULL;
	MsgParser* p = (MsgParser*)parser;//数据来源

	if( p && mem && length)
	{
		if( !p->header )
		{
			if( p->need <= length)
			{
				memcpy(&p->cache, mem, p->need);
				
				p->cache.type = ntohs(p->cache.type);
				p->cache.cmd = ntohs(p->cache.cmd);
				p->cache.index = ntohs(p->cache.index);
				p->cache.total = ntohs(p->cache.total);
				p->cache.length = ntohs(p->cache.length);

				mem += p->need;//mem是存储地址,指针移动12个字节
				length -= p->need;//长度减少12

				p->header = 1;
				p->need = p->cache.length;
			
				ret = MParser_ReadMem(p, mem, length);
			}
			else
			{
				if( !p->msg )
				{
					p->msg = malloc(sizeof(p->cache) + p->need);
					
					if(p->msg)
					{
						*p->msg = p->cache;
					}
				}

				if( p->msg )
				{
					unsigned int len = (p->need < length) ? p->need : length;
					unsigned int offset = p->msg->length - p->msg;

					memcpy(p->msg->payload, mem, len);
					
					p->need -= len;
				}
	
				if( !p->need )
				{
					ret = p->msg;	
					p->msg = NULL;
					MParser_Reset(p);
				}
			}
		}
	}
	
	return ret;
}

Message* MParser_ReadFd(Mparser* parser, int fd)//从fd中解析数据
{
	Message* ret = NULL;
	
	return ret;
}

void MParser_Reset(MParser* parser)//重置解析器的状态
{
	MsgParser* p = (MsgParser*)parser;
	
	if( p )
	{
		p->header = 0;
		p->need = sizeof(p->cache);
		
		if(p->msg){
			free(p->msg);
		}

		p->msg = NULL;
	}
}

void MParser_Del(MParser* parser)//销毁指定解析器
{
	MsgParser* p = (MsgParser*)parser;

	if( p )
	{
		if( p->msg )
		{
			free(p->msg);
			free(p);
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值