Commit 44988452 authored by jan.koester's avatar jan.koester
Browse files

new

parent e7e73800
Loading
Loading
Loading
Loading
+24 −19
Original line number Diff line number Diff line
@@ -61,15 +61,15 @@ namespace netplus {
			api = eapi;
			api->CreateConnetion(&CurCon);
			OpCode = 0;
			memset(&m_pol, 0, sizeof(m_pol));
			CurCon->csock->_Overlapped=new WSAOVERLAPPED;
			memset((void*)CurCon->csock->_Overlapped, 0, sizeof(WSAOVERLAPPED));
		};

		~client() {
			delete CurCon->csock->_Overlapped;
			api->deleteConnetion(CurCon);
		};

		OVERLAPPED  m_pol;

		std::mutex         ConLock;
		int                OpCode; //will be used by the worker thread to decide what operation to perform
		eventapi* api;
@@ -159,7 +159,7 @@ namespace netplus {
				wbuf.len = BLOCKSIZE;

				int nBytesRecv = WSARecv(pClientContext->CurCon->csock->fd(), &wbuf, 1,
					&dwBytes, &dwFlags, &pClientContext->m_pol, nullptr);
					&dwBytes, &dwFlags,pClientContext->CurCon->csock->_Overlapped, nullptr);

				if (SOCKET_ERROR == nBytesRecv) {
					if (WSA_IO_PENDING != WSAGetLastError()) {
@@ -321,7 +321,7 @@ namespace netplus {

						//Overlapped send
						ret = WSASend(pClientContext->CurCon->csock->fd(), &wbuf, 1,
							&dwBytes, dwFlags, &pClientContext->m_pol, nullptr);
							&dwBytes, dwFlags, pClientContext->CurCon->csock->_Overlapped, nullptr);

						if ((SOCKET_ERROR == ret) && (WSA_IO_PENDING != WSAGetLastError())) {
							//Let's not work with this client
@@ -349,17 +349,19 @@ namespace netplus {

						//Get the data.
						ret = WSARecv(pClientContext->CurCon->csock->fd(), &wbuf, 1,
							&dwBytes, &dwFlags, &pClientContext->m_pol, nullptr);
							&dwBytes, &dwFlags, pClientContext->CurCon->csock->_Overlapped, nullptr);

						if ((SOCKET_ERROR == ret) && (WSA_IO_PENDING != WSAGetLastError())) {
						if ((SOCKET_ERROR == ret)) {
							std::cerr << "Thread " << tid << " : Error occurred while executing WSARecv()." << std::endl;
							//Let's not work with this client
							if (WSA_IO_PENDING != WSAGetLastError()) {
								eargs->event->DisconnectEvent(pClientContext->CurCon, tid, args);
								eargs->evpoll->RemoveFromClientList(pClientContext);
							}
							continue;
						}

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

						delete wbuf.buf;

@@ -385,8 +387,8 @@ namespace netplus {

						//Overlapped send
						ret = WSASend(pClientContext->CurCon->csock->fd(), &wbuf, 1,
							&dwBytes, dwFlags, &pClientContext->m_pol, NULL);

							&dwBytes, dwFlags, pClientContext->CurCon->csock->_Overlapped, NULL);
TEST2:
						if (ret == SOCKET_ERROR) {
							std::cerr << "Thread " << tid << " <<: Error occurred while executing WSASend()." << std::endl;

@@ -394,11 +396,10 @@ namespace netplus {
							if (WSA_IO_PENDING != WSAGetLastError())
								eargs->evpoll->RemoveFromClientList(pClientContext);
							else
								eunlock(pClientContext);
							continue;
								goto TEST2;
						}

						pClientContext->CurCon->SendData.resize(dwBytes);
						pClientContext->CurCon->SendData.resize(ret);

						try {
							eargs->event->ResponseEvent(pClientContext->CurCon, tid, args);
@@ -423,15 +424,19 @@ namespace netplus {
						wbuf.len = BLOCKSIZE;

						//Get the data.

TEST3:
						ret = WSARecv(pClientContext->CurCon->csock->fd(), &wbuf, 1,
							&dwBytes, &dwFlags, &pClientContext->m_pol, nullptr);
							&dwBytes, &dwFlags, pClientContext->CurCon->csock->_Overlapped, nullptr);

						if ((SOCKET_ERROR == ret)) {							
							if (WSA_IO_PENDING != WSAGetLastError()) {
								eargs->event->DisconnectEvent(pClientContext->CurCon, tid, args);
								eargs->evpoll->RemoveFromClientList(pClientContext);
							}
							else {
								Sleep(1);
								goto TEST3;
							}
							std::cerr << "Thread " << tid << " : Error occurred while executing WSARecv: " << WSAGetLastError() << std::endl;
							continue;
						}
@@ -440,6 +445,7 @@ namespace netplus {

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

						std::cout << "Test: " << std::endl;;
						std::cout.write(wbuf.buf, dwBytes) << std::endl;

						delete wbuf.buf;
@@ -542,8 +548,7 @@ namespace netplus {
				if ((WSAEvents.lNetworkEvents & FD_ACCEPT) && (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT])) {
					try {
						evpoll.AcceptConnection(0, args);
					}
					catch (NetException& e) {
					} catch (NetException& e) {
						std::cerr << e.what() << std::endl;
					}
				}
+3 −0
Original line number Diff line number Diff line
@@ -74,6 +74,9 @@ namespace netplus {
            int                      _Type;
            ULONG_PTR                _Extension;
            static std::atomic<int>  _InitCount;
#ifdef Windows 
            WSAOVERLAPPED           *_Overlapped;
#endif
        protected:
            void copyAddrInfo(ULONG_PTR *dest,ULONG_PTR src);
        };
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ netplus::socket::socket(){
    _Extension = 0;
    _Type=-1;
    _Socket = -1;
    _Overlapped = nullptr;
    if (_InitCount<1) {
        if (WSAStartup(MAKEWORD(2, 2), &_WSAData) != 0) {
            NetException exception;
+28 −22
Original line number Diff line number Diff line
@@ -154,27 +154,30 @@ void netplus::tcp::bind(){
}


size_t netplus::tcp::sendData(socket *csock, void* data, unsigned long size){
    return sendData(csock,data,size,0);
size_t netplus::tcp::sendData(void* data, unsigned long size){
    return sendData(data,size,-1);
}

size_t netplus::tcp::sendData(socket *csock, void* data, unsigned long size,int flags){
size_t netplus::tcp::sendData(void* data, unsigned long size,int flags){

    NetException exception;

    SOCKET rval=::sendto(csock->_Socket,
                 (char*)data,
                        size,
                        flags,
                        ((struct addrinfo*)csock->_SocketInfo)->ai_addr ,
                        ((struct addrinfo*)csock->_SocketInfo)->ai_addrlen
                     );
    WSABUF buf;

    buf.buf = (char*)data;
    buf.len = size;

    DWORD dwBytes = 0;
    DWORD dwFlags = 0;

    SOCKET rval=::WSASend(_Socket,&buf,1,&dwBytes,flags,_Overlapped,nullptr);

    if(rval == SOCKET_ERROR){
        int etype=NetException::Error;
        if(GetLastError() == WSA_IO_PENDING)
            etype=NetException::Note;

        exception[etype] << "Socket senddata failed on Socket: " << (int) csock->_Socket
        exception[etype] << "Socket senddata failed on Socket: " << (int) _Socket
                                       << " ErrorMsg: " <<  GetLastError();
        throw exception;
    }
@@ -182,28 +185,31 @@ size_t netplus::tcp::sendData(socket *csock, void* data, unsigned long size,int
}


size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size){
    return recvData(csock,data,size,0);
size_t netplus::tcp::recvData(void* data, unsigned long size){
    return recvData(data,size,0);
}

size_t netplus::tcp::recvData(socket *csock, void* data, unsigned long size,int flags){
size_t netplus::tcp::recvData(void* data, unsigned long size,int flags){

    NetException exception;

    int recvsize=::recvfrom(csock->_Socket,
                     (char*)data,
                            size,
                            flags,
                            ((struct addrinfo*)csock->_SocketInfo)->ai_addr,
                            (int*)&((struct addrinfo*)csock->_SocketInfo)->ai_addrlen
                         );
    WSABUF buf;

    buf.buf = (char*)data;
    buf.len = size;

    DWORD dwBytes = 0;
    DWORD dwFlags = 0;

    int recvsize=::WSARecv(_Socket, &buf, 1, &dwBytes,(LPDWORD) &flags, _Overlapped, nullptr);

    if(recvsize<0){
        int etype=NetException::Error;

        if(GetLastError() == EAGAIN)
            etype=NetException::Note;

        exception[etype] << "Socket recvdata failed on Socket: " << (int) csock->_Socket
        exception[etype] << "Socket recvdata failed on Socket: " << (int)_Socket
                                       << " ErrorMsg: " <<  GetLastError();
        throw exception;
    }
+28 −19
Original line number Diff line number Diff line
@@ -154,25 +154,29 @@ void netplus::udp::bind() {
}


size_t netplus::udp::sendData(socket* csock, void* data, unsigned long size) {
    return sendData(csock, data, size, 0);
size_t netplus::udp::sendData(void* data, unsigned long size) {
    return sendData(data, size, 0);
}

size_t netplus::udp::sendData(socket* csock, void* data, unsigned long size, int flags) {

size_t netplus::udp::sendData(void* data, unsigned long size, int flags) {
    NetException exception;

    SOCKET rval = ::send(csock->_Socket,
        (char*)data,
        size,
        flags
    );
    WSABUF buf;

    buf.buf = (char*)data;
    buf.len = size;

    DWORD dwBytes = 0;
    DWORD dwFlags = 0;

    SOCKET rval = ::WSASend(_Socket, &buf, 1, &dwBytes, flags, _Overlapped, nullptr);

    if (rval == SOCKET_ERROR) {
        int etype = NetException::Error;
        if (GetLastError() == WSA_IO_PENDING)
            etype = NetException::Note;

        exception[etype] << "Socket senddata failed on Socket: " << (int)csock->_Socket
        exception[etype] << "Socket senddata failed on Socket: " << (int)_Socket
            << " ErrorMsg: " << GetLastError();
        throw exception;
    }
@@ -180,26 +184,31 @@ size_t netplus::udp::sendData(socket* csock, void* data, unsigned long size, int
}


size_t netplus::udp::recvData(socket* csock, void* data, unsigned long size) {
    return recvData(csock, data, size, 0);
size_t netplus::udp::recvData(void* data, unsigned long size) {
    return recvData(data, size, 0);
}

size_t netplus::udp::recvData(socket* csock, void* data, unsigned long size, int flags) {
size_t netplus::udp::recvData(void* data, unsigned long size, int flags) {

    NetException exception;
    
    int recvsize = ::recv(csock->_Socket,
        (char*)data,
        size,
        flags
    );
    WSABUF buf;

    buf.buf = (char*)data;
    buf.len = size;

    DWORD dwBytes = 0;
    DWORD dwFlags = 0;

    int recvsize = ::WSARecv(_Socket, &buf, 1, &dwBytes, (LPDWORD)&flags, _Overlapped, nullptr);

    if (recvsize < 0) {
        int etype = NetException::Error;

        if (GetLastError() == EAGAIN)
            etype = NetException::Note;

        exception[etype] << "Socket recvdata failed on Socket: " << (int)csock->_Socket
        exception[etype] << "Socket recvdata failed on Socket: " << (int)_Socket
            << " ErrorMsg: " << GetLastError();
        throw exception;
    }