client_proto_head.h 1.69 KB
//客户端协议的包头	
#pragma once

#include <common.h>
#include <proto_header.h>

//////////////////////////////////////////////////////////////////////////
//协议包头格式	
struct cli_proto_head_t : public el::lib_proto_head_base_t{
	PROTO_LEN length;    //all of pack
	uint32_t seq;
	CMD_ID cmd;
	uint32_t result;
	USER_ID id;

	static const uint32_t PROTO_HEAD_LEN = 24;
	virtual uint32_t get_proto_head_len(){
		return cli_proto_head_t::PROTO_HEAD_LEN;
	}
	virtual void unpack(const void* data){
		cli_recv_data_t in(data);
		in>>this->length
			>>this->seq
			>>this->cmd
			>>this->result
			>>this->id;
	}
	virtual uint32_t len_offset(){
		return 0;
	}
	virtual void* get_len_pointer(){
		return &this->length;
	}
	virtual uint32_t get_all_len(){
		return this->length;
	}
	virtual uint32_t get_body_len(){
		return this->length - cli_proto_head_t::PROTO_HEAD_LEN;
	}
	virtual uint32_t get_cmd(){
		return this->cmd;
	}
	virtual void set_len(uint32_t len){
		this->length = (PROTO_LEN)len;
	}
	virtual bool is_all_len(){
		return true;
	}
	virtual void* get_data_pointer(){
		return &this->length;
	}
	//////////////////////////////////////////////////////////////////////////

	cli_proto_head_t(){
		this->clear();
	}
	cli_proto_head_t(CMD_ID cmd){
		this->clear();
		this->cmd = cmd;
	}
	void clear(){
		this->length= 0;
		this->seq = 0;
		this->cmd = 0;
		this->result = 0;
		this->id = 0;
	}
	static inline uint32_t get_cmd_pos(){
		return sizeof(PROTO_LEN)+sizeof(uint32_t);
	}
	//获取包头长度的最小包长	
	static inline uint32_t get_pkg_len_min_len(){
		return cli_proto_head_t::PROTO_HEAD_LEN;
	}
	//获取包头长度的开始位置	
	static inline uint32_t get_pkg_len_pos(){
		return 0;
	}
};