Added more functions

This commit is contained in:
xX-TheDoctor-Xx
2016-03-26 14:52:56 +00:00
parent 9f42dbdb6f
commit c12d90cd48
4 changed files with 100 additions and 39 deletions

View File

@ -8,15 +8,6 @@ namespace VoidNet
VOID_UDP VOID_UDP
}; };
enum ErrorCode
{
VOID_SUCCESS,
VOID_WSA_STARTUP_FAIL,
VOID_GET_ADDR_INFO_FAIL,
VOID_COULDT_CREATE_SOCKET,
VOID_SOCKET_ERROR
};
enum SocketType enum SocketType
{ {
VOID_CLIENT, VOID_CLIENT,

View File

@ -1,7 +1,8 @@
#include "Socket.h" #include "Socket.h"
#include "Enums.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) bool VoidNet::Socket::operator==(Socket &Socket)
{ {
//return socket == con.socket; //return socket == con.socket;
@ -43,13 +39,10 @@ std::string VoidNet::Socket::getPeerAddress()
bool VoidNet::Socket::connectToHost() bool VoidNet::Socket::connectToHost()
{ {
int initialization = init(socketType, protocolType); if (!init(socketType, protocolType))
if (initialization != VOID_SUCCESS)
return false; return false;
int result = connect(soc, addrInfo->ai_addr, addrInfo->ai_addrlen); if (connect(soc, addrInfo->ai_addr, addrInfo->ai_addrlen) != SOCKET_ERROR)
if (result != SOCKET_ERROR)
return true; return true;
closesocket(soc); closesocket(soc);
soc = INVALID_SOCKET; soc = INVALID_SOCKET;
@ -58,18 +51,78 @@ bool VoidNet::Socket::connectToHost()
return false; 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() unsigned int VoidNet::Socket::getPortNumber()
{ {
return port; 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; WSADATA wsaData;
int void_result = WSAStartup(MAKEWORD(2, 2), &wsaData); if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
if (void_result != 0) return false;
return VOID_WSA_STARTUP_FAIL;
struct addrinfo *result = nullptr, hints; struct addrinfo *result = nullptr, hints;
@ -105,26 +158,32 @@ int VoidNet::Socket::init(int socketType, int protocolType)
} }
if (socketType == VOID_CLIENT) 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(); if (getaddrinfo(ipAddr.c_str(), (PCSTR)port, &hints, &result) == SOCKET_ERROR)
return VOID_GET_ADDR_INFO_FAIL; {
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) if (Socket == INVALID_SOCKET)
{ {
freeaddrinfo(result); freeaddrinfo(result);
WSACleanup(); WSACleanup();
return VOID_COULDT_CREATE_SOCKET; return false;
} }
addrInfo = result; addrInfo = result;
soc = Socket; soc = Socket;
return VOID_SUCCESS; return true;
} }

View File

@ -10,13 +10,19 @@ namespace VoidNet
Socket(int, int); Socket(int, int);
Socket(std::string, unsigned int, int, int); Socket(std::string, unsigned int, int, int);
~Socket(); ~Socket();
VoidNet::Socket &operator = (const Socket&);
bool operator == (Socket&); bool operator == (Socket&);
bool operator != (Socket&); bool operator != (Socket&);
unsigned int getPortNumber(); unsigned int getPortNumber();
std::string getIPAddress(); std::string getIPAddress();
std::string getPeerAddress(); std::string getPeerAddress();
bool connectToHost(); bool connectToHost();
bool sendBytes(const char*, int);
bool receiveBytes(char*, int);
bool shutdownReceive();
bool shutdownSend();
bool dataAvailable(int&);
bool dataAvailable();
bool setBlocking(bool);
private: private:
SOCKET soc; SOCKET soc;
struct addrinfo *addrInfo; struct addrinfo *addrInfo;
@ -24,6 +30,6 @@ namespace VoidNet
unsigned int port; unsigned int port;
int socketType; int socketType;
int protocolType; int protocolType;
int init(int socketType, int protocolType); bool init(int, int);
}; };
} }

View File

@ -1,6 +1,11 @@
#pragma once #pragma once
struct TCPConnection #include "Socket.h"
{
}; namespace VoidNet
{
struct TCPConnection
{
};
}