关闭

网游服务器框架(2)Stream

449人阅读 评论(0) 收藏 举报



为了方便序列化数据,特建立了InputStream  和 outStream 2种数据容器。

每个流在初始化的时候。都带有字节序属性, 在写入和读取的时候,先处理再返回,这样逻辑部分可以解决字节序等问题。

Output 在初始化的时候  建立一个固定大小的缓存区。 保持一个流指针。 每次写入新的数据,都copy数据到流指针处,然后指针向后偏移写入的大小


Input  同上。每次读取。都偏移指针位置。


#define DEFAULT_BUFFER_MAX 128

class FSOutputStream
{

private:

	FS_DEFINE_READ_ONLY(size_t, m_iBufferLength, BufferLength);
	FS_DEFINE_READ_ONLY(size_t, m_iPos, Pos);
	FS_DEFINE_READ_ONLY(void*, m_pData, Data);

private:
	void scaleBuffer();

public:
	
	FSOutputStream(size_t length=DEFAULT_BUFFER_MAX);
	virtual ~FSOutputStream(){
		if(m_pData){
			free(m_pData);
			m_pData = NULL;
		}
	}

public:
	
	void toBytes(void*);

	size_t getLength(){ return m_iPos; };

	void subBuffer(size_t start, size_t len);

	void writeLongLong(unsigned long long);
	void writeInt(int val);
	void writeLong(long val);
	void writeShort(short val);


	void writeUInt(unsigned int);
	void writeUShort(unsigned short);


	void writeFloat(float val);
	void writeByte(BYTE val);
	void writeDatas(const void*, size_t);
	void writeString(char*, int);
	void writeString(const char*);
	void writeString(std::string str){  writeString((char*)str.c_str(), str.length()); };

};


#include "FSOutputStream.h"


FSOutputStream::FSOutputStream(size_t length):m_iBufferLength(length), m_iPos(0), m_pData(malloc(length)){
}

void FSOutputStream::toBytes(void* out){
	memcpy(out, m_pData, getLength());
}

void FSOutputStream::scaleBuffer(){

	m_iBufferLength = m_iBufferLength << 1;
	void* newBuffer = ::malloc(m_iBufferLength);
	memcpy(newBuffer, m_pData, m_iPos);

	void* tmp = m_pData;
	m_pData = newBuffer;
	free(tmp);

}

void FSOutputStream::subBuffer(size_t start, size_t len){

	void* newBuffer = ::malloc(len == 0 ? DEFAULT_BUFFER_MAX : len);
	memcpy(newBuffer, (BYTE*)m_pData + start, len);

	m_iBufferLength = (len == 0 ? DEFAULT_BUFFER_MAX : len);

	free(m_pData);
	m_pData = newBuffer;

	m_iPos = len;

}
void FSOutputStream::writeString(const char* data){
	int len = strnlen(data, 65535);
	writeInt(len);
	writeDatas(data, len);
}

void FSOutputStream::writeString(char* data, int len){
	writeInt(len);
	writeDatas(data, len);
}
void FSOutputStream::writeInt(int val){
	writeDatas(&val, sizeof(int));
}

void FSOutputStream::writeLong(long val){
	
	writeDatas(&val, sizeof(long));
}


void FSOutputStream::writeLongLong(unsigned long long v){

	int height = v >> 32;
	int low = v & 0xffffffff;
	writeInt(height);
	writeInt(low);
}

void FSOutputStream::writeShort(short val){
	writeDatas(&val, sizeof(short));

}
void FSOutputStream::writeUInt(unsigned int val){
	writeDatas(&val, sizeof(unsigned int));
}
void FSOutputStream::writeUShort(unsigned short val){
	writeDatas(&val, sizeof(unsigned short));
}

void FSOutputStream::writeFloat(float val){
	writeDatas(&val, sizeof(float));
}

void FSOutputStream::writeByte(BYTE val){
	
	writeDatas(&val, sizeof(BYTE));
}

void FSOutputStream::writeDatas(const void* data, size_t len){

	while(m_iPos + len >= m_iBufferLength){
		scaleBuffer();
	}

	memcpy((BYTE*)m_pData + m_iPos, data, len);

	m_iPos += len;

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32135次
    • 积分:720
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论