c++非常有用的解析协议用的一个辅助类

2 篇文章 0 订阅
2 篇文章 0 订阅

vs2013下编译通过 

ByteStream.h

#pragma once
#include "Define.h"

class ByteStream
{
public:
	ByteStream(uint32_t reserveSize);
	~ByteStream();

	void writeUInt32(uint32_t value);
	void writeInt32(int32_t value);

	void writeUInt16(uint16_t value);
	void writeInt16(int16_t value);

	void writeUInt8(uint8_t value);
	void writeInt8(int8_t value);

	void writeBytes(const char* value, uint32_t size);

	uint32_t readUInt32(uint32_t pos) const;
	int32_t readInt32(int32_t pos) const;

	uint16_t readUInt16(uint32_t pos) const;
	int16_t readInt16(int32_t pos) const;

	uint8_t readUInt8(uint32_t pos) const;
	int8_t readInt8(int32_t pos) const;

	char* getBuffer(uint32_t pos = 0);
	void readBytes(char* dest, uint32_t destSize, uint32_t pos) const;
	void useSize(uint32_t size);

	uint32_t size() const;
	uint32_t reserveSize() const;

	void remove(uint32_t size);
	void clear();

private:
	template<typename T>
	void writeValue(T value);
	template<typename T>
	T readValue(uint32_t pos) const;
	void extend(uint32_t newSize);
	bool bufferEnough(uint32_t size) const;

private:
	char* buffer;
	uint32_t bufferSize;
	uint32_t usedSize;
};


ByteStream.cpp

#include "ByteStream.h"
#include <memory>
#include <algorithm>
#include <assert.h>

ByteStream::ByteStream(uint32_t reserveSize)
{
	usedSize = 0;
	bufferSize = reserveSize;
	if (bufferSize != 0)
	{
		buffer = new char[reserveSize];
		memset(buffer, 0, sizeof(buffer));
	}
	else
	{
		buffer = NULL;
	}
}


ByteStream::~ByteStream()
{
	delete[] buffer;
	buffer = NULL;
}

void ByteStream::writeUInt32(uint32_t value)
{
	writeValue<uint32_t>(value);
}

void ByteStream::writeInt32(int32_t value)
{
	writeValue<int32_t>(value);
}

void ByteStream::writeUInt16(uint16_t value)
{
	writeValue<uint16_t>(value);
}

void ByteStream::writeInt16(int16_t value)
{
	writeValue<int16_t>(value);
}


void ByteStream::writeUInt8(uint8_t value)
{
	writeValue<uint8_t>(value);
}

void ByteStream::writeInt8(int8_t value)
{
	writeValue<int8_t>(value);
}

uint32_t ByteStream::readUInt32(uint32_t pos) const
{
	return readValue<uint32_t>(pos);
}

int32_t ByteStream::readInt32(int32_t pos) const
{
	return readValue<int32_t>(pos);
}

uint16_t ByteStream::readUInt16(uint32_t pos) const
{
	return readValue<uint16_t>(pos);
}

int16_t ByteStream::readInt16(int32_t pos) const
{
	return readValue<int16_t>(pos);
}

uint8_t ByteStream::readUInt8(uint32_t pos) const
{
	return readValue<uint8_t>(pos);
}

int8_t ByteStream::readInt8(int32_t pos) const
{
	return readValue<int8_t>(pos);
}

template<typename T>
void ByteStream::writeValue(T value)
{
	if (!bufferEnough(sizeof(T)))
	{
		extend(bufferSize + sizeof(T) + bufferSize / 2);
	}

	T* buff = (T*)(buffer + usedSize);
	*buff = value;
	usedSize += sizeof(T);
}

template<typename T>
T ByteStream::readValue(uint32_t pos) const
{
	ASSERT_T(usedSize > pos);
	return *((T*)(buffer + pos));
}

void ByteStream::writeBytes(const char* value, uint32_t size)
{
	if (!bufferEnough(size))
	{
		extend(bufferSize + size + bufferSize / 2);
	}

	memcpy_s(buffer + usedSize, size, value, size);
	usedSize += size;
}

char* ByteStream::getBuffer(uint32_t pos/* = 0*/)
{
	return buffer + pos;
}

void ByteStream::readBytes(char* dest, uint32_t destSize, uint32_t pos) const
{
	memcpy_s(dest, destSize, buffer + pos, destSize);
}

void ByteStream::extend(uint32_t newSize)
{
	if (newSize <= bufferSize)
		return;

	char* newBuffer = new char[newSize];
	memcpy_s(newBuffer, newSize, buffer, usedSize);
	delete[] buffer;
	buffer = newBuffer;
	bufferSize = newSize;
}

uint32_t ByteStream::size() const
{
	return usedSize;
}

uint32_t ByteStream::reserveSize() const
{
	return bufferSize;
}

void ByteStream::remove(uint32_t size)
{
	if (usedSize < size)
		return;

	uint32_t remainSize = usedSize - size;
	for (uint32_t i = 0; i < remainSize; ++i)
	{
		buffer[i] = buffer[i + size];
	}
	usedSize = remainSize;
}

bool ByteStream::bufferEnough(uint32_t size) const
{
	return usedSize + size <= bufferSize;
}

void ByteStream::useSize(uint32_t size)
{
	ASSERT_T(bufferEnough(size));
	usedSize += size;
}

void ByteStream::clear()
{
	usedSize = 0;
}

Define.h 有些不要的可以删除

#pragma once
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "uv.h"

#define FLAG 1010
#define VER 0
#define HEAD_SIZE 8

#define ASSERT_T(x) assert(x)

static const int KEEP_ALIVE_SEC = 10;



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值