Loading src/socket.h +1 Changes for src/socket.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -117,6 +117,7 @@ namespace netplus { WSAOVERLAPPED _Overlapped; WSABUF _wsaBuf; bool _Wait; int _Timeout; #endif protected: void copyAddrInfo(ULONG_PTR *dest,ULONG_PTR src,size_t srclen); Loading src/windows/socket.cpp +13 −2 Changes for src/windows/socket.cpp: 13 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -57,7 +57,10 @@ netplus::socket::socket(){ _Wait = false; _wsaBuf.buf = nullptr; _wsaBuf.len = 0; _Timeout = WSA_INFINITE; memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _Overlapped.hEvent = WSACreateEvent(); if (_InitCount<1) { if (WSAStartup(MAKEWORD(2, 2), &_WSAData) != 0) { NetException exception; Loading @@ -70,10 +73,9 @@ netplus::socket::socket(){ netplus::socket::~socket(){ int zero = 0; if (_InitCount.compare_exchange_strong(zero, std::memory_order_release)) { WSACleanup(); WSACloseEvent(_Overlapped.hEvent); } --_InitCount; free((void*)_SocketInfo); } Loading @@ -93,6 +95,9 @@ SOCKET netplus::socket::fd() { void netplus::socket::copyAddrInfo(ULONG_PTR* dest, ULONG_PTR src, size_t srclen) { if (dest == nullptr || src == nullptr) return; memcpy((void*)*dest, (void*)src, srclen); ((struct addrinfo*)*dest)->ai_addr = (struct sockaddr*)malloc(((struct addrinfo*)src)->ai_addrlen); Loading @@ -104,4 +109,10 @@ void netplus::socket::copyAddrInfo(ULONG_PTR* dest, ULONG_PTR src, size_t srclen ((struct addrinfo*)(struct addrinfo*)*dest)->ai_addrlen ); ((struct addrinfo*)*dest)->ai_addrlen = ((struct addrinfo*)src)->ai_addrlen; ((struct addrinfo*)*dest)->ai_family = ((struct addrinfo*)src)->ai_family; } void netplus::socket::setTimeout(int sec) { _Timeout = sec * 1000; } No newline at end of file src/windows/tcp.cpp +63 −36 Changes for src/windows/tcp.cpp: 63 added lines, 36 removed lines. Original line number Diff line number Diff line Loading @@ -53,7 +53,7 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko _Maxconnections=maxconnections; if (sockopts == -1) sockopts = SO_REUSEADDR; sockopts = SO_REUSEADDR|WSA_FLAG_OVERLAPPED; struct addrinfo hints,*result,*rp; Loading @@ -78,7 +78,7 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko for (rp = result; rp != nullptr; rp = rp->ai_next) { _Socket = ::WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol,nullptr, 0, 0); rp->ai_protocol,nullptr, 0, sockopts); if (_Socket == -1) continue; break; Loading @@ -88,8 +88,6 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko ::freeaddrinfo(result); int on = 1; setsockopt(_Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); _Type=sockettype::TCP; } Loading Loading @@ -190,8 +188,8 @@ void netplus::tcp::bind(){ } } size_t netplus::tcp::sendData(buffer& data, int flags) { NetException exception; if (data.ptr) _wsaBuf.buf = (char*)data.data.ptr; else Loading @@ -199,32 +197,56 @@ size_t netplus::tcp::sendData(buffer &data,int flags){ _wsaBuf.len = static_cast<ULONG>(data.size); memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); DWORD dwsend = 0; WSAResetEvent(_Overlapped.hEvent); int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwsend, flags, &_Overlapped, nullptr); if (_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); DWORD dwBytes = 0; NetException exception; int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr); if (rval == SOCKET_ERROR) { switch (WSAGetLastError()) { int lastError = WSAGetLastError(); switch (lastError) { case WSAEWOULDBLOCK: return (size_t)dwsend; // Operation would block, no data transferred yet. // The caller can try again later. return 0; case WSA_IO_PENDING: return (size_t)dwsend; // The operation is pending. If _Wait is true, we block until it's done. if (_Wait) { // Wait for the overlapped I/O operation to complete. if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Get the final result of the overlapped operation. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; // Handle other synchronous errors. case WSAENETDOWN: case WSAENETRESET: case WSAENOTCONN: throw exception[NetException::Error] << "Socket senddata failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAESHUTDOWN: case WSAECONNABORTED: case WSAECONNRESET: throw exception[NetException::Error] << "Socket senddata failed WSAECONNRESET on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed: " << lastError; throw exception; default: throw exception[NetException::Error] << "Socket senddata failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed with unknown error: " << lastError; throw exception; } } return (size_t)dwsend; // If the call succeeded synchronously, return the number of bytes sent. return (size_t)dwBytes; } size_t netplus::tcp::recvData(buffer& data, int flags) { Loading @@ -235,33 +257,46 @@ size_t netplus::tcp::recvData(buffer &data,int flags){ throw exception; } memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _wsaBuf.buf = data.data.buf; _wsaBuf.len = static_cast<ULONG>(data.size); WSAResetEvent(_Overlapped.hEvent); DWORD dwBytes = 0; int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr); if (_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); if (rval == SOCKET_ERROR) { // A non-pending error occurred on the WSARecv call. switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return (size_t)dwBytes; case WSA_IO_PENDING: if (_Wait) { if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Once the wait completes, get the final result. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { // An error occurred during the overlapped operation. exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; case WSAENOTCONN: throw exception[NetException::Error] << "Socket recvData failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAECONNRESET: throw exception[NetException::Error] << "Socket recvData failed WSAECONNRESET on Socket: " << (int)_Socket; // It's not a connected socket, so these errors are less relevant for UDP. // But we'll keep the logic. exception[NetException::Error] << "Socket recvData failed: " << GetLastError(); throw exception; default: throw exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << GetLastError(); throw exception; } } return (size_t)dwBytes; } Loading @@ -270,13 +305,6 @@ void netplus::tcp::connect(std::shared_ptr<socket> &csock){ copyAddrInfo(&_SocketInfo, csock->_SocketInfo, csock->_SocketInfoLen); memset(((struct addrinfo*)_SocketInfo)->ai_addr, 0, ((struct addrinfo*)_SocketInfo)->ai_addrlen); ((struct addrinfo*)_SocketInfo)->ai_addr->sa_family = ((struct addrinfo*)csock->_SocketInfo)->ai_addr->sa_family; ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port; // Change this line to create an overlapped socket if ((_Socket = ::WSASocketW(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, ((struct addrinfo*)_SocketInfo)->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { Loading @@ -297,7 +325,6 @@ void netplus::tcp::connect(std::shared_ptr<socket> &csock){ throw exception; } _Wait = true; } Loading src/windows/udp.cpp +68 −35 Changes for src/windows/udp.cpp: 68 added lines, 35 removed lines. Original line number Diff line number Diff line Loading @@ -51,6 +51,9 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc NetException exception; _Maxconnections = maxconnections; if (sockopts == -1) sockopts = SO_REUSEADDR | WSA_FLAG_OVERLAPPED; struct addrinfo hints, * result, * rp; memset(&hints, 0, sizeof(hints)); Loading @@ -74,7 +77,7 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc for (rp = result; rp != nullptr; rp = rp->ai_next) { _Socket = ::WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED); rp->ai_protocol, nullptr, 0, sockopts); if (_Socket == -1) continue; _SocketInfoLen = sizeof(hints); Loading @@ -84,16 +87,14 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc ::freeaddrinfo(result); int on = 1; setsockopt(_Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); _Type = sockettype::UDP; } netplus::udp::~udp() { if (_Socket >= 0) if (_Socket >= 0) { ::closesocket(_Socket); } } netplus::udp::udp() : socket() { ((struct addrinfo*)_SocketInfo)->ai_family = AF_UNSPEC; Loading Loading @@ -188,6 +189,7 @@ void netplus::udp::bind() { } size_t netplus::udp::sendData(buffer &data, int flags) { NetException exception; if (data.ptr) _wsaBuf.buf = (char*)data.data.ptr; else Loading @@ -195,32 +197,56 @@ size_t netplus::udp::sendData(buffer &data, int flags) { _wsaBuf.len = static_cast<ULONG>(data.size); memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); DWORD dwsend = 0; WSAResetEvent(_Overlapped.hEvent); int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwsend, flags, &_Overlapped, nullptr); if(_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); DWORD dwBytes = 0; NetException exception; int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr); if (rval == SOCKET_ERROR) { switch (WSAGetLastError()) { int lastError = WSAGetLastError(); switch (lastError) { case WSAEWOULDBLOCK: return (size_t)dwsend; // Operation would block, no data transferred yet. // The caller can try again later. return 0; case WSA_IO_PENDING: return (size_t)dwsend; // The operation is pending. If _Wait is true, we block until it's done. if (_Wait) { // Wait for the overlapped I/O operation to complete. if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Get the final result of the overlapped operation. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; // Handle other synchronous errors. case WSAENETDOWN: case WSAENETRESET: case WSAENOTCONN: throw exception[NetException::Error] << "Socket senddata failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAESHUTDOWN: case WSAECONNABORTED: case WSAECONNRESET: throw exception[NetException::Error] << "Socket senddata failed WSAECONNRESET on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed: " << lastError; throw exception; default: throw exception[NetException::Error] << "Socket senddata failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed with unknown error: " << lastError; throw exception; } } return (size_t)dwsend; // If the call succeeded synchronously, return the number of bytes sent. return (size_t)dwBytes; } size_t netplus::udp::recvData(buffer &data, int flags) { Loading @@ -231,47 +257,54 @@ size_t netplus::udp::recvData(buffer &data, int flags) { throw exception; } memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _wsaBuf.buf = data.data.buf; _wsaBuf.len = static_cast<ULONG>(data.size); WSAResetEvent(_Overlapped.hEvent); DWORD dwBytes = 0; int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr); if(_Wait) WSAWaitForMultipleEvents(1,&_Overlapped.hEvent, FALSE, INFINITE, FALSE); if (rval == SOCKET_ERROR) { // A non-pending error occurred on the WSARecv call. switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return (size_t)dwBytes; case WSA_IO_PENDING: if(_Wait){ if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Once the wait completes, get the final result. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { // An error occurred during the overlapped operation. exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; case WSAENOTCONN: throw exception[NetException::Error] << "Socket recvData failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAECONNRESET: throw exception[NetException::Error] << "Socket recvData failed WSAECONNRESET on Socket: " << (int)_Socket; // It's not a connected socket, so these errors are less relevant for UDP. // But we'll keep the logic. exception[NetException::Error] << "Socket recvData failed: " << GetLastError(); throw exception; default: throw exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << GetLastError(); throw exception; } } return (size_t)dwBytes; } void netplus::udp::connect(std::shared_ptr<socket> &csock) { NetException exception; copyAddrInfo(&_SocketInfo, csock->_SocketInfo, csock->_SocketInfoLen); memset(((struct addrinfo*)_SocketInfo)->ai_addr, 0, ((struct addrinfo*)_SocketInfo)->ai_addrlen); ((struct addrinfo*)_SocketInfo)->ai_addr->sa_family = ((struct addrinfo*)csock->_SocketInfo)->ai_addr->sa_family; ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port; // Change this line to create an overlapped socket if ((_Socket = ::WSASocketW(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, ((struct addrinfo*)_SocketInfo)->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { Loading Loading
src/socket.h +1 Changes for src/socket.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -117,6 +117,7 @@ namespace netplus { WSAOVERLAPPED _Overlapped; WSABUF _wsaBuf; bool _Wait; int _Timeout; #endif protected: void copyAddrInfo(ULONG_PTR *dest,ULONG_PTR src,size_t srclen); Loading
src/windows/socket.cpp +13 −2 Changes for src/windows/socket.cpp: 13 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -57,7 +57,10 @@ netplus::socket::socket(){ _Wait = false; _wsaBuf.buf = nullptr; _wsaBuf.len = 0; _Timeout = WSA_INFINITE; memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _Overlapped.hEvent = WSACreateEvent(); if (_InitCount<1) { if (WSAStartup(MAKEWORD(2, 2), &_WSAData) != 0) { NetException exception; Loading @@ -70,10 +73,9 @@ netplus::socket::socket(){ netplus::socket::~socket(){ int zero = 0; if (_InitCount.compare_exchange_strong(zero, std::memory_order_release)) { WSACleanup(); WSACloseEvent(_Overlapped.hEvent); } --_InitCount; free((void*)_SocketInfo); } Loading @@ -93,6 +95,9 @@ SOCKET netplus::socket::fd() { void netplus::socket::copyAddrInfo(ULONG_PTR* dest, ULONG_PTR src, size_t srclen) { if (dest == nullptr || src == nullptr) return; memcpy((void*)*dest, (void*)src, srclen); ((struct addrinfo*)*dest)->ai_addr = (struct sockaddr*)malloc(((struct addrinfo*)src)->ai_addrlen); Loading @@ -104,4 +109,10 @@ void netplus::socket::copyAddrInfo(ULONG_PTR* dest, ULONG_PTR src, size_t srclen ((struct addrinfo*)(struct addrinfo*)*dest)->ai_addrlen ); ((struct addrinfo*)*dest)->ai_addrlen = ((struct addrinfo*)src)->ai_addrlen; ((struct addrinfo*)*dest)->ai_family = ((struct addrinfo*)src)->ai_family; } void netplus::socket::setTimeout(int sec) { _Timeout = sec * 1000; } No newline at end of file
src/windows/tcp.cpp +63 −36 Changes for src/windows/tcp.cpp: 63 added lines, 36 removed lines. Original line number Diff line number Diff line Loading @@ -53,7 +53,7 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko _Maxconnections=maxconnections; if (sockopts == -1) sockopts = SO_REUSEADDR; sockopts = SO_REUSEADDR|WSA_FLAG_OVERLAPPED; struct addrinfo hints,*result,*rp; Loading @@ -78,7 +78,7 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko for (rp = result; rp != nullptr; rp = rp->ai_next) { _Socket = ::WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol,nullptr, 0, 0); rp->ai_protocol,nullptr, 0, sockopts); if (_Socket == -1) continue; break; Loading @@ -88,8 +88,6 @@ netplus::tcp::tcp(const std::string &addr, int port,int maxconnections,int socko ::freeaddrinfo(result); int on = 1; setsockopt(_Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); _Type=sockettype::TCP; } Loading Loading @@ -190,8 +188,8 @@ void netplus::tcp::bind(){ } } size_t netplus::tcp::sendData(buffer& data, int flags) { NetException exception; if (data.ptr) _wsaBuf.buf = (char*)data.data.ptr; else Loading @@ -199,32 +197,56 @@ size_t netplus::tcp::sendData(buffer &data,int flags){ _wsaBuf.len = static_cast<ULONG>(data.size); memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); DWORD dwsend = 0; WSAResetEvent(_Overlapped.hEvent); int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwsend, flags, &_Overlapped, nullptr); if (_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); DWORD dwBytes = 0; NetException exception; int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr); if (rval == SOCKET_ERROR) { switch (WSAGetLastError()) { int lastError = WSAGetLastError(); switch (lastError) { case WSAEWOULDBLOCK: return (size_t)dwsend; // Operation would block, no data transferred yet. // The caller can try again later. return 0; case WSA_IO_PENDING: return (size_t)dwsend; // The operation is pending. If _Wait is true, we block until it's done. if (_Wait) { // Wait for the overlapped I/O operation to complete. if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Get the final result of the overlapped operation. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; // Handle other synchronous errors. case WSAENETDOWN: case WSAENETRESET: case WSAENOTCONN: throw exception[NetException::Error] << "Socket senddata failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAESHUTDOWN: case WSAECONNABORTED: case WSAECONNRESET: throw exception[NetException::Error] << "Socket senddata failed WSAECONNRESET on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed: " << lastError; throw exception; default: throw exception[NetException::Error] << "Socket senddata failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed with unknown error: " << lastError; throw exception; } } return (size_t)dwsend; // If the call succeeded synchronously, return the number of bytes sent. return (size_t)dwBytes; } size_t netplus::tcp::recvData(buffer& data, int flags) { Loading @@ -235,33 +257,46 @@ size_t netplus::tcp::recvData(buffer &data,int flags){ throw exception; } memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _wsaBuf.buf = data.data.buf; _wsaBuf.len = static_cast<ULONG>(data.size); WSAResetEvent(_Overlapped.hEvent); DWORD dwBytes = 0; int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr); if (_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); if (rval == SOCKET_ERROR) { // A non-pending error occurred on the WSARecv call. switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return (size_t)dwBytes; case WSA_IO_PENDING: if (_Wait) { if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Once the wait completes, get the final result. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { // An error occurred during the overlapped operation. exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; case WSAENOTCONN: throw exception[NetException::Error] << "Socket recvData failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAECONNRESET: throw exception[NetException::Error] << "Socket recvData failed WSAECONNRESET on Socket: " << (int)_Socket; // It's not a connected socket, so these errors are less relevant for UDP. // But we'll keep the logic. exception[NetException::Error] << "Socket recvData failed: " << GetLastError(); throw exception; default: throw exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << GetLastError(); throw exception; } } return (size_t)dwBytes; } Loading @@ -270,13 +305,6 @@ void netplus::tcp::connect(std::shared_ptr<socket> &csock){ copyAddrInfo(&_SocketInfo, csock->_SocketInfo, csock->_SocketInfoLen); memset(((struct addrinfo*)_SocketInfo)->ai_addr, 0, ((struct addrinfo*)_SocketInfo)->ai_addrlen); ((struct addrinfo*)_SocketInfo)->ai_addr->sa_family = ((struct addrinfo*)csock->_SocketInfo)->ai_addr->sa_family; ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port; // Change this line to create an overlapped socket if ((_Socket = ::WSASocketW(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, ((struct addrinfo*)_SocketInfo)->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { Loading @@ -297,7 +325,6 @@ void netplus::tcp::connect(std::shared_ptr<socket> &csock){ throw exception; } _Wait = true; } Loading
src/windows/udp.cpp +68 −35 Changes for src/windows/udp.cpp: 68 added lines, 35 removed lines. Original line number Diff line number Diff line Loading @@ -51,6 +51,9 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc NetException exception; _Maxconnections = maxconnections; if (sockopts == -1) sockopts = SO_REUSEADDR | WSA_FLAG_OVERLAPPED; struct addrinfo hints, * result, * rp; memset(&hints, 0, sizeof(hints)); Loading @@ -74,7 +77,7 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc for (rp = result; rp != nullptr; rp = rp->ai_next) { _Socket = ::WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED); rp->ai_protocol, nullptr, 0, sockopts); if (_Socket == -1) continue; _SocketInfoLen = sizeof(hints); Loading @@ -84,16 +87,14 @@ netplus::udp::udp(const std::string &addr, int port, int maxconnections, int soc ::freeaddrinfo(result); int on = 1; setsockopt(_Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); _Type = sockettype::UDP; } netplus::udp::~udp() { if (_Socket >= 0) if (_Socket >= 0) { ::closesocket(_Socket); } } netplus::udp::udp() : socket() { ((struct addrinfo*)_SocketInfo)->ai_family = AF_UNSPEC; Loading Loading @@ -188,6 +189,7 @@ void netplus::udp::bind() { } size_t netplus::udp::sendData(buffer &data, int flags) { NetException exception; if (data.ptr) _wsaBuf.buf = (char*)data.data.ptr; else Loading @@ -195,32 +197,56 @@ size_t netplus::udp::sendData(buffer &data, int flags) { _wsaBuf.len = static_cast<ULONG>(data.size); memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); DWORD dwsend = 0; WSAResetEvent(_Overlapped.hEvent); int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwsend, flags, &_Overlapped, nullptr); if(_Wait) WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, INFINITE, FALSE); DWORD dwBytes = 0; NetException exception; int rval = ::WSASend(_Socket, &_wsaBuf, 1, &dwBytes, flags, &_Overlapped, nullptr); if (rval == SOCKET_ERROR) { switch (WSAGetLastError()) { int lastError = WSAGetLastError(); switch (lastError) { case WSAEWOULDBLOCK: return (size_t)dwsend; // Operation would block, no data transferred yet. // The caller can try again later. return 0; case WSA_IO_PENDING: return (size_t)dwsend; // The operation is pending. If _Wait is true, we block until it's done. if (_Wait) { // Wait for the overlapped I/O operation to complete. if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Get the final result of the overlapped operation. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; // Handle other synchronous errors. case WSAENETDOWN: case WSAENETRESET: case WSAENOTCONN: throw exception[NetException::Error] << "Socket senddata failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAESHUTDOWN: case WSAECONNABORTED: case WSAECONNRESET: throw exception[NetException::Error] << "Socket senddata failed WSAECONNRESET on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed: " << lastError; throw exception; default: throw exception[NetException::Error] << "Socket senddata failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket sendData failed with unknown error: " << lastError; throw exception; } } return (size_t)dwsend; // If the call succeeded synchronously, return the number of bytes sent. return (size_t)dwBytes; } size_t netplus::udp::recvData(buffer &data, int flags) { Loading @@ -231,47 +257,54 @@ size_t netplus::udp::recvData(buffer &data, int flags) { throw exception; } memset(&_Overlapped, 0, sizeof(WSAOVERLAPPED)); _wsaBuf.buf = data.data.buf; _wsaBuf.len = static_cast<ULONG>(data.size); WSAResetEvent(_Overlapped.hEvent); DWORD dwBytes = 0; int rval = ::WSARecv(_Socket, &_wsaBuf, 1, &dwBytes, (LPDWORD)&flags, &_Overlapped, nullptr); if(_Wait) WSAWaitForMultipleEvents(1,&_Overlapped.hEvent, FALSE, INFINITE, FALSE); if (rval == SOCKET_ERROR) { // A non-pending error occurred on the WSARecv call. switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return (size_t)dwBytes; case WSA_IO_PENDING: if(_Wait){ if (WSAWaitForMultipleEvents(1, &_Overlapped.hEvent, FALSE, _Timeout, FALSE) == WSA_WAIT_FAILED) { exception[NetException::Error] << "WSAWaitForMultipleEvents failed: " << GetLastError(); throw exception; } // Once the wait completes, get the final result. if (!WSAGetOverlappedResult(_Socket, &_Overlapped, &dwBytes, FALSE, (LPDWORD)&flags)) { // An error occurred during the overlapped operation. exception[NetException::Error] << "WSAGetOverlappedResult failed: " << GetLastError(); throw exception; } } return (size_t)dwBytes; case WSAENOTCONN: throw exception[NetException::Error] << "Socket recvData failed WSAENOTCONN on Socket: " << (int)_Socket; case WSAECONNRESET: throw exception[NetException::Error] << "Socket recvData failed WSAECONNRESET on Socket: " << (int)_Socket; // It's not a connected socket, so these errors are less relevant for UDP. // But we'll keep the logic. exception[NetException::Error] << "Socket recvData failed: " << GetLastError(); throw exception; default: throw exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << (int)_Socket; exception[NetException::Error] << "Socket recvData failed some Error on Socket: " << GetLastError(); throw exception; } } return (size_t)dwBytes; } void netplus::udp::connect(std::shared_ptr<socket> &csock) { NetException exception; copyAddrInfo(&_SocketInfo, csock->_SocketInfo, csock->_SocketInfoLen); memset(((struct addrinfo*)_SocketInfo)->ai_addr, 0, ((struct addrinfo*)_SocketInfo)->ai_addrlen); ((struct addrinfo*)_SocketInfo)->ai_addr->sa_family = ((struct addrinfo*)csock->_SocketInfo)->ai_addr->sa_family; ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port; // Change this line to create an overlapped socket if ((_Socket = ::WSASocketW(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, ((struct addrinfo*)_SocketInfo)->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { Loading