using vector to implement buffer

I was told that using vector to implement buffer is applicable, but using string to implement buffer is not applicable, because objects in vector are continuous in memory, while in string it is not guaranteed.

It is peculiar that my following codes test OK in red hat linux, but in SUSE, it leads to coredump. I don't have the chance to investigate in SUSE any more for the time being since I've not a SUSE server at hand.

#include <vector>
#include <iostream>
using namespace std; 

class Buf {
    vector<char> buf;
public:    
    Buf();
    Buf(size_t sz);
    void setSize(size_t sz);
    size_t getSize();
    char* head(); 
    char* head2();
};

Buf::Buf(){
}

Buf::Buf(size_t sz):buf(sz) {
    
}

void Buf::setSize(size_t sz){
    buf.resize(sz);
}

size_t Buf::getSize() {
    return buf.size();
}

char* Buf::head(){
    return &buf[0];
}

char* Buf::head2(){
    return &(*buf.begin());
}

class MessageBuffer
{
public:
    enum {HEAD_SIZE = 4, MAX_MESSAGE_SIZE = 65536};
    MessageBuffer(); 
    char* body();
    char* head();
    void setBodySize(int size);
    int getBodySize();
    int getTotalSize();
private:    
    vector<char> m_buff;
};

MessageBuffer::MessageBuffer() 
{
}

char* MessageBuffer:: body() 
{
    vector<char>::iterator it=m_buff.begin();
    char *begin = &(*it) + HEAD_SIZE;
    return begin;
}

char* MessageBuffer:: head()
{
    vector<char>::iterator it=m_buff.begin();
    char *begin = &(*it) + HEAD_SIZE;
    return begin; 
}

void  MessageBuffer:: setBodySize(int size)
{
    m_buff.resize(size+HEAD_SIZE);
    char *begin = &m_buff[0];
    begin[0]=(size >> 24) & 0xff;
    begin[1]=(size >> 16) & 0xff;
    begin[2]=(size >> 8) & 0xff;
    begin[3]=size  & 0xff;
}

int   MessageBuffer:: getBodySize()
{
    int size;
    char *begin = &m_buff[0];
    size = (begin[0] << 24) |(begin[1] << 16) |(begin[2] << 8) |(begin[3] ) ;
    return size; 
}

int   MessageBuffer:: getTotalSize() 
{
    return getBodySize() + HEAD_SIZE;    
}

int main() {
    //test Buf
    Buf buf(100);
    strcpy(buf.head(),"hello");
    strcpy((buf.head()+5)," world");
    cout << buf.head() << endl;
    strcpy(buf.head2(),"hello");
    strcpy((buf.head2()+5)," world");
    cout << buf.head2() << endl;
    //test MessageBuffer
    MessageBuffer buffer;
    buffer.setBodySize(100);
    cout << buffer.getTotalSize() << endl;
    cout << buffer.getBodySize() << endl;
    strcpy(buffer.body(), "hello");
    strcpy(buffer.body()+5," whole world"); 
    cout << buffer.body() << endl; 
    return 0;
}


 

用中文,理解并说明以下内容:Custom DataStream ObjectNet provides the facility to send and receive simple and complex structured over-network ( see DataStream ). The main idea of DataStream is to write and read data without the need to write on each place, you can use some DataStream and encapsulate how much data you need. To implement your own DataStream you can check the API document on NetworkEntity class. The following piece of code example how to implement your own DataStream : public class DataToSync { public Vector3 positionValue; public Color color; public DataToSync(Vector3 position, Color color) { this.positionValue = position; this.color = color; } } public class DataStreamExample : DataHandler<Vector3> { public override int Write(DataToSync data, ref byte[] buffer, ref int offset) { int result = base.Write(data.positionValue, ref buffer, ref offset, typeof(Vector3)); result += base.Write(data.color, ref buffer, ref offset, typeof(Color)); return result; } public override DataToSync Read(byte[] buffer, ref int offset) { return new DataToSync(this.Read<Vector3>(buffer, ref offset), this.Read<Color>(buffer, ref offset)); } } After your data stream is created you need to register into stream factory to be visible to ObjectNet engine. DataStream.RegisterGlobalStream<DataToSync, DataStreamExample>(); This code tells to ObjectNet that every time that someone try to send or receive a “DataToSync” object, the “DataStreamExample” must be used. 结合上面自定义DataStream的方法,来传输一个自定义类public class ObjUnitSave { public string objName; public E_ObjParent parent; public vector3 pos; public Quaternion rot; public vector3 scale; public float colorR; public float colorG; public float colorB; public float smooth; public float metal; public float price; public float mass; }
最新发布
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值