From c12d90cd48ef9a5652a2a7dd8073941782bc8522 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Sat, 26 Mar 2016 14:52:56 +0000 Subject: [PATCH] Added more functions --- VoidNet/VoidNet/Enums.h | 9 --- VoidNet/VoidNet/Socket.cpp | 109 ++++++++++++++++++++++++-------- VoidNet/VoidNet/Socket.h | 10 ++- VoidNet/VoidNet/TCPConnection.h | 11 +++- 4 files changed, 100 insertions(+), 39 deletions(-) diff --git a/VoidNet/VoidNet/Enums.h b/VoidNet/VoidNet/Enums.h index be8a05d..7bd5144 100644 --- a/VoidNet/VoidNet/Enums.h +++ b/VoidNet/VoidNet/Enums.h @@ -8,15 +8,6 @@ namespace VoidNet VOID_UDP }; - enum ErrorCode - { - VOID_SUCCESS, - VOID_WSA_STARTUP_FAIL, - VOID_GET_ADDR_INFO_FAIL, - VOID_COULDT_CREATE_SOCKET, - VOID_SOCKET_ERROR - }; - enum SocketType { VOID_CLIENT, diff --git a/VoidNet/VoidNet/Socket.cpp b/VoidNet/VoidNet/Socket.cpp index dd48b1c..d9c1d56 100644 --- a/VoidNet/VoidNet/Socket.cpp +++ b/VoidNet/VoidNet/Socket.cpp @@ -1,7 +1,8 @@ #include "Socket.h" #include "Enums.h" -VoidNet::Socket::Socket(int socketType, int protocolType) : socketType(socketType), protocolType(protocolType) +VoidNet::Socket::Socket(int socketType, int protocolType) : + socketType(socketType), protocolType(protocolType) { } @@ -14,11 +15,6 @@ VoidNet::Socket::~Socket() { } -VoidNet::Socket &VoidNet::Socket::operator=(const Socket &Socket) -{ - return (VoidNet::Socket)Socket; -} - bool VoidNet::Socket::operator==(Socket &Socket) { //return socket == con.socket; @@ -43,13 +39,10 @@ std::string VoidNet::Socket::getPeerAddress() bool VoidNet::Socket::connectToHost() { - int initialization = init(socketType, protocolType); - if (initialization != VOID_SUCCESS) + if (!init(socketType, protocolType)) return false; - int result = connect(soc, addrInfo->ai_addr, addrInfo->ai_addrlen); - - if (result != SOCKET_ERROR) + if (connect(soc, addrInfo->ai_addr, addrInfo->ai_addrlen) != SOCKET_ERROR) return true; closesocket(soc); soc = INVALID_SOCKET; @@ -58,18 +51,78 @@ bool VoidNet::Socket::connectToHost() return false; } +bool VoidNet::Socket::sendBytes(const char *buffer, int length) +{ + if (send(soc, buffer, length, 0) == SOCKET_ERROR) + return false; + return true; +} + +bool VoidNet::Socket::receiveBytes(char *buffer, int length) +{ + if (recv(soc, buffer, length, 0) == 0) + return false; + return true; +} + +bool VoidNet::Socket::shutdownReceive() +{ + if (shutdown(soc, SD_RECEIVE) == SOCKET_ERROR) + return false; + return true; +} + +bool VoidNet::Socket::shutdownSend() +{ + if (shutdown(soc, SD_SEND) == SOCKET_ERROR) + return false; + return true; +} + +bool VoidNet::Socket::dataAvailable(int &size) +{ + if (ioctlsocket(soc, FIONREAD, (u_long*)size) != NO_ERROR && size > 0) + return true; + return false; +} + +bool VoidNet::Socket::dataAvailable() +{ + int s; + return dataAvailable(s); +} + +bool VoidNet::Socket::setBlocking(bool blocking) +{ + if (ioctlsocket(soc, FIONBIO, (u_long*)(blocking ? 1 : 0)) != NO_ERROR) + return true; + return false; +} + unsigned int VoidNet::Socket::getPortNumber() { return port; } -int VoidNet::Socket::init(int socketType, int protocolType) +bool VoidNet::Socket::init(int socketType, int protocolType) { + if (soc != NULL) + { + try + { + shutdownReceive(); + shutdownSend(); + closesocket(soc); + WSACleanup(); + soc = NULL; + } + catch (std::exception) { } + } + WSADATA wsaData; - int void_result = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (void_result != 0) - return VOID_WSA_STARTUP_FAIL; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) + return false; struct addrinfo *result = nullptr, hints; @@ -105,26 +158,32 @@ int VoidNet::Socket::init(int socketType, int protocolType) } if (socketType == VOID_CLIENT) - void_result = getaddrinfo(ipAddr.c_str(), (PCSTR)port, &hints, &result); - else - void_result = getaddrinfo(nullptr, (PCSTR)port, &hints, &result); - - if (void_result != 0) { - WSACleanup(); - return VOID_GET_ADDR_INFO_FAIL; + if (getaddrinfo(ipAddr.c_str(), (PCSTR)port, &hints, &result) == SOCKET_ERROR) + { + WSACleanup(); + return false; + } + } + else + { + if (getaddrinfo(nullptr, (PCSTR)port, &hints, &result) == SOCKET_ERROR) + { + WSACleanup(); + return false; + } } - SOCKET Socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + auto Socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (Socket == INVALID_SOCKET) { freeaddrinfo(result); WSACleanup(); - return VOID_COULDT_CREATE_SOCKET; + return false; } addrInfo = result; soc = Socket; - return VOID_SUCCESS; + return true; } diff --git a/VoidNet/VoidNet/Socket.h b/VoidNet/VoidNet/Socket.h index 12ec9c2..7226347 100644 --- a/VoidNet/VoidNet/Socket.h +++ b/VoidNet/VoidNet/Socket.h @@ -10,13 +10,19 @@ namespace VoidNet Socket(int, int); Socket(std::string, unsigned int, int, int); ~Socket(); - VoidNet::Socket &operator = (const Socket&); bool operator == (Socket&); bool operator != (Socket&); unsigned int getPortNumber(); std::string getIPAddress(); std::string getPeerAddress(); bool connectToHost(); + bool sendBytes(const char*, int); + bool receiveBytes(char*, int); + bool shutdownReceive(); + bool shutdownSend(); + bool dataAvailable(int&); + bool dataAvailable(); + bool setBlocking(bool); private: SOCKET soc; struct addrinfo *addrInfo; @@ -24,6 +30,6 @@ namespace VoidNet unsigned int port; int socketType; int protocolType; - int init(int socketType, int protocolType); + bool init(int, int); }; } \ No newline at end of file diff --git a/VoidNet/VoidNet/TCPConnection.h b/VoidNet/VoidNet/TCPConnection.h index b386f9a..5260c5a 100644 --- a/VoidNet/VoidNet/TCPConnection.h +++ b/VoidNet/VoidNet/TCPConnection.h @@ -1,6 +1,11 @@ #pragma once -struct TCPConnection -{ +#include "Socket.h" -}; \ No newline at end of file +namespace VoidNet +{ + struct TCPConnection + { + + }; +} \ No newline at end of file