473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

std::vector<char> troubles

Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<char> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<unsigned char>
to store the data?

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

// inbByteBuffer = std::vector<char>
| // if we have got the message length and code bytes
while (inbByteBuffer.size
<http://www.php.net/manual-lookup.php?lang=en&pattern=size>() >= 8) {
int messageLength = 0;
int msgCode = 0;

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*=256;
messageLength+= inbByteBuffer[i];
}

// get the message code (second 4)
for(int i=7; i>=4; i--) {
msgCode *=256;
msgCode += inbByteBuffer[i];
}

// check message is long enough
if ((messageLength+4) > inbByteBuffer.size
<http://www.php.net/manual-lookup.php?lang=en&pattern=size>()) {
return false;
}

// create the bytes specific to this message
std::vector<char> data;
for(int i=0; i<messageLength+4; i++) {
char c = inbByteBuffer[i];
data.push_back
<http://www.php.net/manual-lookup.php?lang=en&pattern=push_back>(c);
}

// the buffer is the correct size, create a message with it
try {
Message * pmsg = MessageFactory::buildMessage
<http://www.php.net/manual-lookup.php?lang=en&pattern=buildMessage>(data);

// add it to the inbound queue
inbMessageQueue.push
<http://www.php.net/manual-lookup.php?lang=en&pattern=push>(pmsg);
} catch
<http://www.php.net/manual-lookup.php?lang=en&pattern=catch>
(logic_error e) {
cout << "Invalid message exception" << endl;
}

// get the iterator
std::vector<char>::iterator it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>() !=
inbByteBuffer.end
<http://www.php.net/manual-lookup.php?lang=en&pattern=end>();i++) {
it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();
inbByteBuffer.erase
<http://www.php.net/manual-lookup.php?lang=en&pattern=erase>(it);
}
}

|
Jul 22 '05 #1
2 4741

"Chris Thompson" <ch************@tascomi.com> wrote in message
news:10*************@corp.supernews.com...
Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<char> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<unsigned char>
to store the data?
Hard to say, but if not you should certainly cast to unsigned char in your
msgLength and msgCode calculations.

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*=256;
messageLength+= static_cast<unsigned char>(inbByteBuffer[i]);
}

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

[snip]

// get the iterator
std::vector<char>::iterator it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>() !=
inbByteBuffer.end
<http://www.php.net/manual-lookup.php?lang=en&pattern=end>();i++) {
it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();
inbByteBuffer.erase
<http://www.php.net/manual-lookup.php?lang=en&pattern=erase>(it);
}


This loop is wrong,

For a start it should say

for(int i=0; i< messageLength+4 && inbByteBuffer.begin

i.e. < not <=

But even then it is indescribably inefficient because you are copying the
whole vector repeatedly back over itself for each character you delete.

Try this, it's simpler and more efficient.

inbByteBuffer.erase(inbByteBuffer.begin(), inbByteBuffer.begin() +
messageLength + 4);

I don't believe you need to check if the buffer is long enough to delete the
message since you already make that check earlier in the code.

john
Jul 22 '05 #2
Excellent!! thanks a bunch john.

Regards
Chris

John Harrison wrote:
"Chris Thompson" <ch************@tascomi.com> wrote in message
news:10*************@corp.supernews.com...
Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<char> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<unsigned char>
to store the data?

Hard to say, but if not you should certainly cast to unsigned char in your
msgLength and msgCode calculations.

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*=256;
messageLength+= static_cast<unsigned char>(inbByteBuffer[i]);
}

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

[snip]

// get the iterator
std::vector<char>::iterator it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>() !=
inbByteBuffer.end
<http://www.php.net/manual-lookup.php?lang=en&pattern=end>();i++) {
it = inbByteBuffer.begin
<http://www.php.net/manual-lookup.php?lang=en&pattern=begin>();
inbByteBuffer.erase
<http://www.php.net/manual-lookup.php?lang=en&pattern=erase>(it);
}

This loop is wrong,

For a start it should say

for(int i=0; i< messageLength+4 && inbByteBuffer.begin

i.e. < not <=

But even then it is indescribably inefficient because you are copying the
whole vector repeatedly back over itself for each character you delete.

Try this, it's simpler and more efficient.

inbByteBuffer.erase(inbByteBuffer.begin(), inbByteBuffer.begin() +
messageLength + 4);

I don't believe you need to check if the buffer is long enough to delete the
message since you already make that check earlier in the code.

john

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steven Reddie | last post by:
I have a large char which I'd like to copy a portion of into a vector<char>. I can't seem to locate a simple method for doing so. I assume involving iterators will not result in memcpy not being...
3
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes...
6
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
8
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
6
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.