Loading src/event/iocp.cpp +61 −64 Original line number Diff line number Diff line Loading @@ -77,8 +77,9 @@ namespace netplus { public: enum states { OP_READ = 0, OP_WRITE = 1 OP_ACCEPT = 0, OP_READ = 1, OP_WRITE = 2 }; poll(HANDLE iocp, eventapi* api, socket* ssock) { Loading Loading @@ -120,45 +121,32 @@ namespace netplus { void AcceptConnection(int tid, ULONG_PTR args) { client* pClientContext = new client(g_eventapi); pClientContext->OpCode = OP_READ; pClientContext->OpCode = OP_ACCEPT; if (g_serversocket->_Type == TCP) pClientContext->CurCon->csock = new tcp(); ACCEPTCON: pClientContext->CurCon->csock = new tcp; try { g_serversocket->accept(pClientContext->CurCon->csock); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) goto ACCEPTCON; delete pClientContext->CurCon->csock; delete pClientContext; throw e; } //Store this object AddToClientList(pClientContext); if (AssociateWithIOCP(pClientContext)) { g_eventapi->ConnectEvent(pClientContext->CurCon, tid, args); AssociateWithIOCP(pClientContext); } catch (NetException& e) { delete pClientContext->CurCon->csock; RemoveFromClientList(pClientContext); throw e; } pClientContext->OpCode = OP_WRITE; std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>()); int nBytesRecv = 0; int recv = pClientContext->CurCon->csock->recvData(buf.get(), BLOCKSIZE, 0); try { nBytesRecv = pClientContext->CurCon->csock->recvData(buf.get(), BLOCKSIZE, WSA_FLAG_OVERLAPPED); } catch (...) { pClientContext->CurCon->RecvData.append(buf.get(), recv); } pClientContext->CurCon->RecvData.append(buf.get(), nBytesRecv); } } private: Loading Loading @@ -203,7 +191,6 @@ namespace netplus { } _Timeout = timeout; _ServerSocket = serversocket; _ServerSocket->setFlag(WSA_FLAG_OVERLAPPED,1); _ServerSocket->setFlag(FIONBIO,1); _ServerSocket->bind(); _ServerSocket->listen(); Loading Loading @@ -290,7 +277,15 @@ namespace netplus { std::cerr << pClientContext->OpCode << std::endl; switch (pClientContext->OpCode) { case poll::OP_READ: case poll::OP_ACCEPT: std::cout.write(pClientContext->CurCon->RecvData.data(), pClientContext->CurCon->RecvData.size()); try { eargs->event->ConnectEvent(pClientContext->CurCon, tid, args); eargs->event->RequestEvent(pClientContext->CurCon, tid, args); if (!((con*)pClientContext->CurCon)->SendData.empty()) { pClientContext->OpCode = poll::OP_READ; Loading @@ -301,49 +296,48 @@ namespace netplus { std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Overlapped send ret = pClientContext->CurCon->csock->sendData(buf.get(),ssize, WSA_FLAG_OVERLAPPED); pClientContext->OpCode = poll::OP_WRITE; //Overlapped send try { ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); } catch (NetException& e) { eargs->evpoll->RemoveFromClientList(pClientContext); throw e; } pClientContext->CurCon->SendData.resize(ret); try { eargs->event->ResponseEvent(pClientContext->CurCon, tid, args); } } catch (NetException& e) { std::cerr << e.what() << std::endl; } } else { pClientContext->OpCode = poll::OP_WRITE; break; case poll::OP_READ: try { dwFlags = 0; std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Get the data. ret = pClientContext->CurCon->csock->recvData(buf.get(), 16384, WSA_FLAG_OVERLAPPED); ret = pClientContext->CurCon->csock->recvData(buf.get(), 16384, 0); pClientContext->CurCon->RecvData.append(buf.get(), ret); try { eargs->event->RequestEvent(pClientContext->CurCon, tid, args); } catch (NetException& e) { ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); pClientContext->OpCode = poll::OP_WRITE; } catch (NetException& e) { std::cerr << e.what() << std::endl; } } break; case poll::OP_WRITE: try { std::cout.write(pClientContext->CurCon->RecvData.data(),pClientContext->CurCon->RecvData.size())<<std::endl; eargs->event->RequestEvent(pClientContext->CurCon, tid, args); if(!((con*)pClientContext->CurCon)->SendData.empty()) { dwFlags = 0; ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : (ULONG)pClientContext->CurCon->SendData.size(); Loading @@ -352,7 +346,7 @@ namespace netplus { std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Overlapped send ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, WSA_FLAG_OVERLAPPED); ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); pClientContext->CurCon->SendData.resize(ret); Loading @@ -362,11 +356,14 @@ namespace netplus { catch (NetException& e) { std::cerr << e.what() << std::endl; } } else { pClientContext->OpCode = poll::OP_READ; } } catch (NetException& e) { std::cerr << e.what() << std::endl; eargs->evpoll->RemoveFromClientList(pClientContext); } pClientContext->OpCode = poll::OP_READ; break; default: eargs->evpoll->RemoveFromClientList(pClientContext); break; Loading src/windows/tcp.cpp +28 −17 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. extern "C" { #include <winsock2.h> #include <WS2tcpip.h> #include "mswsock.h" #include <fcntl.h> }; Loading Loading @@ -74,7 +75,7 @@ netplus::tcp::tcp(const char* addr, int port,int maxconnections,int sockopts) : 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, 0); if (_Socket == -1) continue; break; Loading Loading @@ -128,6 +129,8 @@ int netplus::tcp::getMaxconnections(){ void netplus::tcp::accept(socket *csock){ NetException exception; DWORD dwbytes = 0; CHAR AcceptBuffer[2 * (sizeof(SOCKADDR_IN6) + 16)]; copyAddrInfo(&csock->_SocketInfo, _SocketInfo,_SocketInfoLen); Loading @@ -136,23 +139,22 @@ void netplus::tcp::accept(socket *csock){ ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port; csock->_Socket=::WSAAccept(_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr, (LPINT)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen, nullptr, 0); if(csock->_Socket==SOCKET_ERROR){ if ((csock->_Socket = WSASocket(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, 0, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { exception[NetException::Error] << "Accept create Socket faied: failed on " << (int)csock->_Socket << " error code : " << GetLastError(); throw exception; } if (!::AcceptEx(_Socket,csock->_Socket, &AcceptBuffer, 0, sizeof(SOCKADDR_IN6) + 16, sizeof(SOCKADDR_IN6) + 16, &dwbytes, &csock->_Overlapped)) { int etype = NetException::Error; if (GetLastError() == WSA_IO_PENDING) etype = NetException::Note; exception[etype] << "Can't accept on Socket: " << GetLastError(); throw exception; } if (::getpeername(csock->_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr, (int*)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen) < 0) { NetException exception; exception[NetException::Error] << "Accept: getpeername failed on " << (int)_Socket << " error code : " << GetLastError(); exception[etype] << "AcceptEX: failed on " << (int)csock->_Socket << " error code : " << GetLastError(); throw exception; } } void netplus::tcp::bind(){ Loading @@ -173,12 +175,16 @@ size_t netplus::tcp::sendData(void* data, unsigned long size,int flags){ buf.len = size; DWORD dwFlags = 0; SENDDATA: SOCKET rval=::WSASend(_Socket,&buf,1,nullptr,flags,&_Overlapped,nullptr); if(rval == SOCKET_ERROR){ DWORD lerror=GetLastError(); if (lerror == WSA_IO_PENDING) { Sleep(1); goto SENDDATA; } exception[NetException::Error] << "Socket senddata failed on Socket: " << (int) _Socket << " ErrorMsg: " << lerror; Loading @@ -196,12 +202,17 @@ size_t netplus::tcp::recvData(void* data, unsigned long size,int flags){ buf.len = size; DWORD dwFlags = 0; RECVDATA: int recvsize=::WSARecv(_Socket, &buf, 1,nullptr,(LPDWORD) &flags, &_Overlapped, nullptr); if(recvsize==SOCKET_ERROR){ DWORD lerror = GetLastError(); if (lerror == WSA_IO_PENDING) { Sleep(1); goto RECVDATA; } exception[NetException::Error] << "Socket recvdata failed on Socket: " << (int)_Socket << " ErrorMsg: " << lerror; throw exception; Loading Loading
src/event/iocp.cpp +61 −64 Original line number Diff line number Diff line Loading @@ -77,8 +77,9 @@ namespace netplus { public: enum states { OP_READ = 0, OP_WRITE = 1 OP_ACCEPT = 0, OP_READ = 1, OP_WRITE = 2 }; poll(HANDLE iocp, eventapi* api, socket* ssock) { Loading Loading @@ -120,45 +121,32 @@ namespace netplus { void AcceptConnection(int tid, ULONG_PTR args) { client* pClientContext = new client(g_eventapi); pClientContext->OpCode = OP_READ; pClientContext->OpCode = OP_ACCEPT; if (g_serversocket->_Type == TCP) pClientContext->CurCon->csock = new tcp(); ACCEPTCON: pClientContext->CurCon->csock = new tcp; try { g_serversocket->accept(pClientContext->CurCon->csock); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) goto ACCEPTCON; delete pClientContext->CurCon->csock; delete pClientContext; throw e; } //Store this object AddToClientList(pClientContext); if (AssociateWithIOCP(pClientContext)) { g_eventapi->ConnectEvent(pClientContext->CurCon, tid, args); AssociateWithIOCP(pClientContext); } catch (NetException& e) { delete pClientContext->CurCon->csock; RemoveFromClientList(pClientContext); throw e; } pClientContext->OpCode = OP_WRITE; std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>()); int nBytesRecv = 0; int recv = pClientContext->CurCon->csock->recvData(buf.get(), BLOCKSIZE, 0); try { nBytesRecv = pClientContext->CurCon->csock->recvData(buf.get(), BLOCKSIZE, WSA_FLAG_OVERLAPPED); } catch (...) { pClientContext->CurCon->RecvData.append(buf.get(), recv); } pClientContext->CurCon->RecvData.append(buf.get(), nBytesRecv); } } private: Loading Loading @@ -203,7 +191,6 @@ namespace netplus { } _Timeout = timeout; _ServerSocket = serversocket; _ServerSocket->setFlag(WSA_FLAG_OVERLAPPED,1); _ServerSocket->setFlag(FIONBIO,1); _ServerSocket->bind(); _ServerSocket->listen(); Loading Loading @@ -290,7 +277,15 @@ namespace netplus { std::cerr << pClientContext->OpCode << std::endl; switch (pClientContext->OpCode) { case poll::OP_READ: case poll::OP_ACCEPT: std::cout.write(pClientContext->CurCon->RecvData.data(), pClientContext->CurCon->RecvData.size()); try { eargs->event->ConnectEvent(pClientContext->CurCon, tid, args); eargs->event->RequestEvent(pClientContext->CurCon, tid, args); if (!((con*)pClientContext->CurCon)->SendData.empty()) { pClientContext->OpCode = poll::OP_READ; Loading @@ -301,49 +296,48 @@ namespace netplus { std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Overlapped send ret = pClientContext->CurCon->csock->sendData(buf.get(),ssize, WSA_FLAG_OVERLAPPED); pClientContext->OpCode = poll::OP_WRITE; //Overlapped send try { ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); } catch (NetException& e) { eargs->evpoll->RemoveFromClientList(pClientContext); throw e; } pClientContext->CurCon->SendData.resize(ret); try { eargs->event->ResponseEvent(pClientContext->CurCon, tid, args); } } catch (NetException& e) { std::cerr << e.what() << std::endl; } } else { pClientContext->OpCode = poll::OP_WRITE; break; case poll::OP_READ: try { dwFlags = 0; std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Get the data. ret = pClientContext->CurCon->csock->recvData(buf.get(), 16384, WSA_FLAG_OVERLAPPED); ret = pClientContext->CurCon->csock->recvData(buf.get(), 16384, 0); pClientContext->CurCon->RecvData.append(buf.get(), ret); try { eargs->event->RequestEvent(pClientContext->CurCon, tid, args); } catch (NetException& e) { ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); pClientContext->OpCode = poll::OP_WRITE; } catch (NetException& e) { std::cerr << e.what() << std::endl; } } break; case poll::OP_WRITE: try { std::cout.write(pClientContext->CurCon->RecvData.data(),pClientContext->CurCon->RecvData.size())<<std::endl; eargs->event->RequestEvent(pClientContext->CurCon, tid, args); if(!((con*)pClientContext->CurCon)->SendData.empty()) { dwFlags = 0; ssize = BLOCKSIZE < pClientContext->CurCon->SendData.size() ? BLOCKSIZE : (ULONG)pClientContext->CurCon->SendData.size(); Loading @@ -352,7 +346,7 @@ namespace netplus { std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>()); //Overlapped send ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, WSA_FLAG_OVERLAPPED); ret = pClientContext->CurCon->csock->sendData(buf.get(), ssize, 0); pClientContext->CurCon->SendData.resize(ret); Loading @@ -362,11 +356,14 @@ namespace netplus { catch (NetException& e) { std::cerr << e.what() << std::endl; } } else { pClientContext->OpCode = poll::OP_READ; } } catch (NetException& e) { std::cerr << e.what() << std::endl; eargs->evpoll->RemoveFromClientList(pClientContext); } pClientContext->OpCode = poll::OP_READ; break; default: eargs->evpoll->RemoveFromClientList(pClientContext); break; Loading
src/windows/tcp.cpp +28 −17 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. extern "C" { #include <winsock2.h> #include <WS2tcpip.h> #include "mswsock.h" #include <fcntl.h> }; Loading Loading @@ -74,7 +75,7 @@ netplus::tcp::tcp(const char* addr, int port,int maxconnections,int sockopts) : 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, 0); if (_Socket == -1) continue; break; Loading Loading @@ -128,6 +129,8 @@ int netplus::tcp::getMaxconnections(){ void netplus::tcp::accept(socket *csock){ NetException exception; DWORD dwbytes = 0; CHAR AcceptBuffer[2 * (sizeof(SOCKADDR_IN6) + 16)]; copyAddrInfo(&csock->_SocketInfo, _SocketInfo,_SocketInfoLen); Loading @@ -136,23 +139,22 @@ void netplus::tcp::accept(socket *csock){ ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port = ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port; csock->_Socket=::WSAAccept(_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr, (LPINT)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen, nullptr, 0); if(csock->_Socket==SOCKET_ERROR){ if ((csock->_Socket = WSASocket(((struct addrinfo*)_SocketInfo)->ai_family, ((struct addrinfo*)_SocketInfo)->ai_socktype, 0, nullptr, 0, WSA_FLAG_OVERLAPPED)) < 0) { exception[NetException::Error] << "Accept create Socket faied: failed on " << (int)csock->_Socket << " error code : " << GetLastError(); throw exception; } if (!::AcceptEx(_Socket,csock->_Socket, &AcceptBuffer, 0, sizeof(SOCKADDR_IN6) + 16, sizeof(SOCKADDR_IN6) + 16, &dwbytes, &csock->_Overlapped)) { int etype = NetException::Error; if (GetLastError() == WSA_IO_PENDING) etype = NetException::Note; exception[etype] << "Can't accept on Socket: " << GetLastError(); throw exception; } if (::getpeername(csock->_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr, (int*)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen) < 0) { NetException exception; exception[NetException::Error] << "Accept: getpeername failed on " << (int)_Socket << " error code : " << GetLastError(); exception[etype] << "AcceptEX: failed on " << (int)csock->_Socket << " error code : " << GetLastError(); throw exception; } } void netplus::tcp::bind(){ Loading @@ -173,12 +175,16 @@ size_t netplus::tcp::sendData(void* data, unsigned long size,int flags){ buf.len = size; DWORD dwFlags = 0; SENDDATA: SOCKET rval=::WSASend(_Socket,&buf,1,nullptr,flags,&_Overlapped,nullptr); if(rval == SOCKET_ERROR){ DWORD lerror=GetLastError(); if (lerror == WSA_IO_PENDING) { Sleep(1); goto SENDDATA; } exception[NetException::Error] << "Socket senddata failed on Socket: " << (int) _Socket << " ErrorMsg: " << lerror; Loading @@ -196,12 +202,17 @@ size_t netplus::tcp::recvData(void* data, unsigned long size,int flags){ buf.len = size; DWORD dwFlags = 0; RECVDATA: int recvsize=::WSARecv(_Socket, &buf, 1,nullptr,(LPDWORD) &flags, &_Overlapped, nullptr); if(recvsize==SOCKET_ERROR){ DWORD lerror = GetLastError(); if (lerror == WSA_IO_PENDING) { Sleep(1); goto RECVDATA; } exception[NetException::Error] << "Socket recvdata failed on Socket: " << (int)_Socket << " ErrorMsg: " << lerror; throw exception; Loading