uint Socket::send(const byte* buf, unsigned int sz, unsigned int& written) { const byte* pos = buf; const byte* end = pos + sz; wouldBlock_ = false; /* Remove send()/recv() hooks once non-blocking send is implemented. */ while (pos != end) { int sent = send_func_(ptr_, pos, static_cast<int>(end - pos)); if (sent == -1) { if (get_lastError() == SOCKET_EWOULDBLOCK || get_lastError() == SOCKET_EAGAIN) { wouldBlock_ = true; // would have blocked this time only nonBlocking_ = true; // nonblocking, win32 only way to tell return 0; } return static_cast<uint>(-1); } pos += sent; written += sent; } return sz; }
uint Socket::send(const byte* buf, unsigned int sz, unsigned int& written, int flags) { const byte* pos = buf; const byte* end = pos + sz; wouldBlock_ = false; while (pos != end) { int sent = ::send(socket_, reinterpret_cast<const char *>(pos), static_cast<int>(end - pos), flags); if (sent == -1) { if (get_lastError() == SOCKET_EWOULDBLOCK || get_lastError() == SOCKET_EAGAIN) { wouldBlock_ = true; // would have blocked this time only nonBlocking_ = true; // nonblocking, win32 only way to tell return 0; } return static_cast<uint>(-1); } pos += sent; written += sent; } return sz; }
uint Socket::receive(byte* buf, unsigned int sz) { wouldBlock_ = false; int recvd = recv_func_(ptr_, buf, sz); // idea to seperate error from would block by [email protected] if (recvd == -1) { if (get_lastError() == SOCKET_EWOULDBLOCK || get_lastError() == SOCKET_EAGAIN) { wouldBlock_ = true; // would have blocked this time only nonBlocking_ = true; // socket nonblocking, win32 only way to tell return 0; } } else if (recvd == 0) return static_cast<uint>(-1); return recvd; }