mercredi 18 mars 2015

C++: Best way to use vector in send bytes over tcp/ip


I have a proplem for transfering big data over tcp/ip. The size of vector of byte to transfer is always about 500000. I have 2 solution to deal with this, but I dont know what is the better or having another solution.


For 2 solution, I have 2 class to store data. Each class have 2 methods is:



  1. push(std::vector) ;

  2. vectorpop();


define BYTE_T unsigned char


Way 1:Use vector



class buffDdata1 {
vector<BYTE_T> listData;
vector<BYTE_T> listDataTmp;
public:
buffDdata1 () {
listData.reserver(500000);
listDataTmp.reserver(500000);
}
void push(vector<BYTE_T>& data) {
lock();
listData.insert(listData.end(), data.begin(), data.end());
unlock();
}

vector<BYTE_T>& pop() {
lock();
listDataTmp.clear();
listDataTmp.swap(listData);
unlock();
return listDataTmp;
}
};


Way 2: Use vector of vector



class buffData2 {

vector<vector <BYTE_T> > listData;
int nCount;
public:
buffData2() {
listData.reserver(200000);
}
void push(vector <BYTE_T>& data) {
lock();
listData.push_back(data);nCount++;
unlock();
}
vector <BYTE_T> pop() {
vector <BYTE_T> listRet;
lock();
for(int i=0; i< nCount; ++i) {
listRet.insert(listRet.end(), listData[i].begin(), listData[i].end());
}
unlock();
return listRet;
}
};



Aucun commentaire:

Enregistrer un commentaire