-
WSABUF question
Hello..
I work on a IOCP framework server, where I have to work with WSABUF (on WSARecv and WSASend)..
I have a special class for buffer which looks like:
Code:
class cbuffer {
WSABUF m_wsabuf;
char m_buf[1024];
void setup() {
memset(m_buf, 0, sizeof(m_buf));
m_wsabuf.buf = reinterpret_cast<char*>(&m_buf[0]);
m_wsabuf.len = 1024 - 1; // - 1 ?
}
};
I call setup() before I do WSARecv/Send..
Now I wonder what is the proper/best approach to set wsabuf..
Right now I memset m_buf and set wsabuf.len to 1023 (as you can see) - so theres a space for the last zero byte.. I wonder if this is the right way to do it?
All IOCP sources I took a look at set wsabuf.len to the actual size of buffer (so 1024 in this case)..
Maybe is should do memset(m_buf, 0, sizeof(m_buf) + 1) ?
My IOCP buffer concept works so that I have a static buffer size (m_buf[1024]), and I do WSARecv until there's data, and each time it gets it, I copy the data to the std::string and call setup(). Do you think this is the "good" way to do it? It seems the best and easier option for me..
-
> so theres a space for the last zero byte
Yeah, but that's only if YOU want to store a \0 after the function returns.
A \0 only makes sense if you're expecting printable text and want an easy way of using other C or C++ string functions.
There's no problem with setting the buffer size to just sizeof(m_buf), and using the return result to determine how much real data there is.
> Maybe is should do memset(m_buf, 0, sizeof(m_buf) + 1) ?
Bad idea - that's a buffer overflow.
> Do you think this is the "good" way to do it?
It's a reasonable way to do it.
-
So actually theres no need to memset m_buf to 0 each time I receive?
Im not expecting only printable text.. So I guess I dont have to memset it and then just copy bytes I get to the std::string?
-
> So actually theres no need to memset m_buf to 0 each time I receive?
Nope.