-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BaseChannel/GetBytes, buffer overrun potential #9
Comments
Yes, this stuff is definitely a work in progress! The whole Channel/Communication setup needs to be ironed out completely and tested fully.... |
I'd also push the buffer out to the class level and preallocate it, for performance. Less GC and frag. |
Why close the issue if the code is still fragile/incomplete? |
Hit the wrong button.. :p |
This should do it...
seems simple enough.. what do you think? |
Could run out of memory quickly. Think 57.6k for example, 1+ seconds. Otherwise it is simply correct. Sent from my Kindle Fire From: Rob Chartier [email protected] This should do it... protected byte[] GetBytes(SerialPort port) { byte[] buffer = new byte[port.BytesToRead]; port.Read(buffer, 0, buffer.Length); return buffer; } seems simple enough.. what do you think? — |
This needs some improvement to not overrun, and would be more useful if the byte array size was configurable. Also, add BytesToRead to the SP implementation, and use here for performance; push the byte content parsing up. Something like:
public int BufferLen { get; set/* use a method to change buffer size, protected with a lock, or disallow setting when port is opened*/; }
// don't worry about byte values here, just buffer and return the input, parse in caller.
protected byte[] GetBytes(SerialPort port)
{
int position = 0;
int bytesWaiting = port.BytesToRead;
bytesWaiting = Min(bytesWaiting,BufferLen);
byte[] buffer = new byte[bytesWaiting];
var bytesWaiting = port.Read(buffer,0,bytesWaiting); // returns bytes read
return buffer;
}
The text was updated successfully, but these errors were encountered: