Commit 79cb17b8 authored by jan.koester's avatar jan.koester
Browse files

iocp acceptedex correted

parent bedd95bb
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ namespace netplus {
			}
		}

		void AcceptConnection(int tid, ULONG_PTR args) {
		void AcceptConnection(LPFN_ACCEPTEX lpfnAcceptEx,int tid, ULONG_PTR args) {
			g_amtx.lock();
			client* pClientContext = new client(g_eventapi);
		
@@ -119,7 +119,7 @@ namespace netplus {
				pClientContext->CurCon->csock = new tcp;

			try {
				g_serversocket->accept(pClientContext->CurCon->csock);
				g_serversocket->accept(lpfnAcceptEx,pClientContext->CurCon->csock);

				pClientContext->CurCon->csock->setFlag(FIONBIO, 0);

@@ -267,7 +267,7 @@ namespace netplus {
				}

				try {
					if (pClientContext->CurCon && !((con*)pClientContext->CurCon)->SendData.empty()) {
					if (!pClientContext->CurCon->SendData.empty()) {

						dwFlags = 0;

@@ -288,9 +288,9 @@ namespace netplus {
					} else {
						dwFlags = 0;

						std::shared_ptr<char[]> buf(new char[16384], std::default_delete<char[]>());
						std::shared_ptr<char[]> buf(new char[BLOCKSIZE], std::default_delete<char[]>());
						//Get the data.
						ret = pClientContext->CurCon->csock->recvData(buf.get(), 16384, 0);
						ret = pClientContext->CurCon->csock->recvData(buf.get(), BLOCKSIZE, 0);

						pClientContext->CurCon->RecvData.append(buf.get(), ret);

@@ -389,7 +389,7 @@ namespace netplus {
				WSAEnumNetworkEvents(_ServerSocket->fd(), g_hAcceptEvent, &WSAEvents);
				if ((WSAEvents.lNetworkEvents & FD_ACCEPT) && (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT])) {
					try {
						evpoll.AcceptConnection(0, args);
						evpoll.AcceptConnection(lpfnAcceptEx,0, args);
					} catch (NetException& e) {
						std::cerr << e.what() << std::endl;
					}
+10 −3
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ typedef int SOCKET;
typedef unsigned long ULONG_PTR;
#else
#include <WinSock2.h>
#include <mswsock.h>
#endif

namespace netplus {
@@ -53,6 +54,9 @@ namespace netplus {
            void                     setTimeout(int sec);
            
            virtual void             accept(socket *csock)=0;
#ifdef Windows 
            virtual void             accept(LPFN_ACCEPTEX lpfnAcceptEx, socket* ssock)=0;
#endif
            virtual void             bind()=0;
            virtual void             listen()=0;
            
@@ -92,7 +96,9 @@ namespace netplus {
            tcp(const char *addr,int port,int maxconnections,
                int sockopts);
            ~tcp();
            
#ifdef Windows 
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx,socket* ssock);
#endif
            void          accept(socket *ssock);
            void          bind();
            void          listen();
@@ -123,6 +129,9 @@ namespace netplus {
            ~udp();

            void          accept(socket * ssock);
#ifdef Windows 
            void          accept(LPFN_ACCEPTEX lpfnAcceptEx, socket* ssock);
#endif
            void          bind();
            void          listen();

@@ -153,8 +162,6 @@ namespace netplus {
            ~ssl();
            
            void          accept(socket *ssock);
            void          bind();
            void          listen();

            ssl&          operator=(int socket);
            int           getMaxconnections();
+0 −8
Original line number Diff line number Diff line
@@ -31,14 +31,6 @@ netplus::ssl::~ssl(){
void netplus::ssl::accept(socket *csock){
}

void netplus::ssl::bind(){
}

void netplus::ssl::listen(){
    //not needed beause mbedtls_net_bind bind and listen in one funciton
    return;
}

netplus::ssl& netplus::ssl::operator=(int sock){
     return *this;
};
+22 −2
Original line number Diff line number Diff line
@@ -129,6 +129,24 @@ int netplus::tcp::getMaxconnections(){

void netplus::tcp::accept(socket* csock) {
    NetException exception;

    ((struct addrinfo*)(csock->_SocketInfo))->ai_addr = (struct sockaddr*)malloc(((struct addrinfo*)(_SocketInfo))->ai_addrlen);
    ((struct addrinfo*)(csock->_SocketInfo))->ai_addrlen = ((struct addrinfo*)(_SocketInfo))->ai_addrlen;

    csock->_Socket = ::WSAAccept(_Socket, ((struct addrinfo*)csock->_SocketInfo)->ai_addr,
        (LPINT) & ((struct addrinfo*)csock->_SocketInfo)->ai_addrlen, nullptr, 0);
    if (csock->_Socket == SOCKET_ERROR) {
        int etype = NetException::Error;
        if (GetLastError() == WSA_IO_PENDING)
            etype = NetException::Note;

        exception[etype] << "Can't accept on Socket: " << GetLastError();
        throw exception;
    }
}

void netplus::tcp::accept(LPFN_ACCEPTEX lpfnAcceptEx,socket *csock){
    NetException exception;
    DWORD dwbytes = 0;
    CHAR AcceptBuffer[2 * (sizeof(SOCKADDR_IN6) + 16)];

@@ -145,7 +163,9 @@ void netplus::tcp::accept(socket *csock){
        throw exception;
    }

    if (!::AcceptEx(_Socket,csock->_Socket, &AcceptBuffer, 0,
    memset(&csock->_Overlapped, 0, sizeof(csock->_Overlapped));

    if (!lpfnAcceptEx(_Socket,csock->_Socket, &AcceptBuffer, 0,
        sizeof(SOCKADDR_IN6) + 16, sizeof(SOCKADDR_IN6) + 16, &dwbytes, &csock->_Overlapped)) {

        int etype = NetException::Error;
+32 −0
Original line number Diff line number Diff line
@@ -144,6 +144,38 @@ void netplus::udp::accept(socket* csock) {
    }
}

void netplus::udp::accept(LPFN_ACCEPTEX lpfnAcceptEx, socket* csock) {
    NetException exception;
    DWORD dwbytes = 0;
    CHAR AcceptBuffer[2 * (sizeof(SOCKADDR_IN6) + 16)];

    copyAddrInfo(&csock->_SocketInfo, _SocketInfo, _SocketInfoLen);

    memset(((struct addrinfo*)csock->_SocketInfo)->ai_addr, 0, ((struct addrinfo*)csock->_SocketInfo)->ai_addrlen);

    ((struct sockaddr_in*)((struct addrinfo*)csock->_SocketInfo)->ai_addr)->sin_port =
        ((struct sockaddr_in*)((struct addrinfo*)_SocketInfo)->ai_addr)->sin_port;

    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;
    }

    memset(&csock->_Overlapped, 0, sizeof(csock->_Overlapped));

    if (!lpfnAcceptEx(_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] << "AcceptEX: failed on " << (int)csock->_Socket << " error code : " << GetLastError();
        throw exception;
    }
}

void netplus::udp::bind() {
    NetException exception;
    if (::bind(_Socket, ((struct addrinfo*)_SocketInfo)->ai_addr, (int)((struct addrinfo*)_SocketInfo)->ai_addrlen) < 0) {