Worked on Socket class and added TCP and UDP Connection and Server classes
This commit is contained in:
@ -1,45 +0,0 @@
|
||||
#include "Connection.h"
|
||||
|
||||
VoidNet::Connection::Connection()
|
||||
{
|
||||
}
|
||||
|
||||
VoidNet::Connection::Connection(std::string IPAddress, unsigned int PortNumber) :
|
||||
IPAddress(IPAddress), PortNumber(PortNumber)
|
||||
{
|
||||
}
|
||||
|
||||
VoidNet::Connection::~Connection()
|
||||
{
|
||||
delete socket;
|
||||
}
|
||||
|
||||
VoidNet::Connection &VoidNet::Connection::operator=(const Connection &con)
|
||||
{
|
||||
return (VoidNet::Connection)con;
|
||||
|
||||
VoidNet::Connection asd;
|
||||
VoidNet::Connection dsa;
|
||||
}
|
||||
|
||||
bool VoidNet::Connection::operator==(Connection &con)
|
||||
{
|
||||
//return socket == con.socket;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VoidNet::Connection::operator!=(Connection &con)
|
||||
{
|
||||
//return socket != con.socket;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string VoidNet::Connection::getIPAddress()
|
||||
{
|
||||
return IPAddress;
|
||||
}
|
||||
|
||||
unsigned int VoidNet::Connection::getPortNumber()
|
||||
{
|
||||
return PortNumber;
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
#include <WinSock2.h>
|
||||
#include <string>
|
||||
|
||||
namespace VoidNet
|
||||
{
|
||||
struct Connection
|
||||
{
|
||||
// Default constructor
|
||||
Connection();
|
||||
|
||||
// Using this constructor will connect automatically
|
||||
// PortNumber grater than 0
|
||||
// IPAddress cant be null
|
||||
Connection(std::string, unsigned int);
|
||||
|
||||
// Destructor
|
||||
~Connection();
|
||||
|
||||
// Assignment operator
|
||||
VoidNet::Connection &operator = (const Connection&);
|
||||
bool operator == (Connection&);
|
||||
bool operator != (Connection&);
|
||||
|
||||
// this will return the IPAddress of the client connected
|
||||
std::string getIPAddress();
|
||||
|
||||
// this will return the Port Number of the client connected
|
||||
unsigned int getPortNumber();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
// connection socket
|
||||
SOCKET *socket;
|
||||
|
||||
// client ip address
|
||||
std::string IPAddress;
|
||||
|
||||
// client port
|
||||
unsigned int PortNumber;
|
||||
};
|
||||
}
|
||||
@ -4,7 +4,22 @@ namespace VoidNet
|
||||
{
|
||||
enum Protocol
|
||||
{
|
||||
TCP,
|
||||
UDP
|
||||
VOID_TCP,
|
||||
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,
|
||||
VOID_SERVER
|
||||
};
|
||||
}
|
||||
130
VoidNet/VoidNet/Socket.cpp
Normal file
130
VoidNet/VoidNet/Socket.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include "Socket.h"
|
||||
#include "Enums.h"
|
||||
|
||||
VoidNet::Socket::Socket(int socketType, int protocolType) : socketType(socketType), protocolType(protocolType)
|
||||
{
|
||||
}
|
||||
|
||||
VoidNet::Socket::Socket(std::string ipAddr, unsigned int port, int socketType, int protocolType) :
|
||||
ipAddr(ipAddr), port(port), socketType(socketType), protocolType(protocolType)
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VoidNet::Socket::operator!=(Socket &Socket)
|
||||
{
|
||||
//return socket != con.socket;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string VoidNet::Socket::getIPAddress()
|
||||
{
|
||||
return ipAddr;
|
||||
}
|
||||
|
||||
std::string VoidNet::Socket::getPeerAddress()
|
||||
{
|
||||
return ipAddr + ":" + std::to_string(port);
|
||||
}
|
||||
|
||||
bool VoidNet::Socket::connectToHost()
|
||||
{
|
||||
int initialization = init(socketType, protocolType);
|
||||
if (initialization != VOID_SUCCESS)
|
||||
return false;
|
||||
|
||||
int result = connect(soc, addrInfo->ai_addr, addrInfo->ai_addrlen);
|
||||
|
||||
if (result != SOCKET_ERROR)
|
||||
return true;
|
||||
closesocket(soc);
|
||||
soc = INVALID_SOCKET;
|
||||
freeaddrinfo(addrInfo);
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int VoidNet::Socket::getPortNumber()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
int VoidNet::Socket::init(int socketType, int protocolType)
|
||||
{
|
||||
WSADATA wsaData;
|
||||
|
||||
int void_result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
if (void_result != 0)
|
||||
return VOID_WSA_STARTUP_FAIL;
|
||||
|
||||
struct addrinfo *result = nullptr, hints;
|
||||
|
||||
if (socketType == VOID_CLIENT && protocolType == VOID_TCP)
|
||||
{
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
}
|
||||
else if (socketType == VOID_CLIENT && protocolType == VOID_UDP)
|
||||
{
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_UDP;
|
||||
}
|
||||
else if (socketType == VOID_SERVER && protocolType == VOID_TCP)
|
||||
{
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
}
|
||||
else if (socketType == VOID_SERVER && protocolType == VOID_UDP)
|
||||
{
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_UDP;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
SOCKET Socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
|
||||
if (Socket == INVALID_SOCKET)
|
||||
{
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return VOID_COULDT_CREATE_SOCKET;
|
||||
}
|
||||
|
||||
addrInfo = result;
|
||||
soc = Socket;
|
||||
return VOID_SUCCESS;
|
||||
}
|
||||
29
VoidNet/VoidNet/Socket.h
Normal file
29
VoidNet/VoidNet/Socket.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <WS2tcpip.h>
|
||||
#include <string>
|
||||
|
||||
namespace VoidNet
|
||||
{
|
||||
struct Socket
|
||||
{
|
||||
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();
|
||||
private:
|
||||
SOCKET soc;
|
||||
struct addrinfo *addrInfo;
|
||||
std::string ipAddr;
|
||||
unsigned int port;
|
||||
int socketType;
|
||||
int protocolType;
|
||||
int init(int socketType, int protocolType);
|
||||
};
|
||||
}
|
||||
1
VoidNet/VoidNet/TCPConnection.cpp
Normal file
1
VoidNet/VoidNet/TCPConnection.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "TCPConnection.h"
|
||||
6
VoidNet/VoidNet/TCPConnection.h
Normal file
6
VoidNet/VoidNet/TCPConnection.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
struct TCPConnection
|
||||
{
|
||||
|
||||
};
|
||||
1
VoidNet/VoidNet/TCPServer.cpp
Normal file
1
VoidNet/VoidNet/TCPServer.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "TCPServer.h"
|
||||
6
VoidNet/VoidNet/TCPServer.h
Normal file
6
VoidNet/VoidNet/TCPServer.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
struct TCPServer
|
||||
{
|
||||
|
||||
};
|
||||
0
VoidNet/VoidNet/UDPClient.cpp
Normal file
0
VoidNet/VoidNet/UDPClient.cpp
Normal file
1
VoidNet/VoidNet/UDPClient.h
Normal file
1
VoidNet/VoidNet/UDPClient.h
Normal file
@ -0,0 +1 @@
|
||||
#pragma once
|
||||
0
VoidNet/VoidNet/UDPServer.cpp
Normal file
0
VoidNet/VoidNet/UDPServer.cpp
Normal file
1
VoidNet/VoidNet/UDPServer.h
Normal file
1
VoidNet/VoidNet/UDPServer.h
Normal file
@ -0,0 +1 @@
|
||||
#pragma once
|
||||
@ -31,7 +31,7 @@
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
@ -95,6 +95,9 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
@ -110,11 +113,19 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="TCPConnection.h" />
|
||||
<ClInclude Include="Socket.h" />
|
||||
<ClInclude Include="Enums.h" />
|
||||
<ClInclude Include="TCPServer.h" />
|
||||
<ClInclude Include="UDPClient.h" />
|
||||
<ClInclude Include="UDPServer.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Connection.cpp" />
|
||||
<ClCompile Include="TCPConnection.cpp" />
|
||||
<ClCompile Include="Socket.cpp" />
|
||||
<ClCompile Include="TCPServer.cpp" />
|
||||
<ClCompile Include="UDPClient.cpp" />
|
||||
<ClCompile Include="UDPServer.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@ -15,15 +15,39 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h">
|
||||
<ClInclude Include="Enums.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Enums.h">
|
||||
<ClInclude Include="Socket.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TCPServer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TCPConnection.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="UDPServer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="UDPClient.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Connection.cpp">
|
||||
<ClCompile Include="Socket.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TCPServer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TCPConnection.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UDPServer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UDPClient.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user