Added initialization, data type defenitons and started with TcpClient

This commit is contained in:
xX-TheDoctor-Xx
2016-07-16 23:26:56 +01:00
parent d9be5d3f2b
commit 97262ee8c6
20 changed files with 595 additions and 36 deletions

2
TODO Normal file
View File

@ -0,0 +1,2 @@
initialization code for other operating systems - currently VoidNet only supports windows
initialization code for tcp client for other operating systems - "

View File

@ -1,28 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidNet", "VoidNet\VoidNet.vcxproj", "{DC4D9247-92B6-4C52-B47C-41C34CF5D611}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Debug|x64.ActiveCfg = Debug|x64
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Debug|x64.Build.0 = Debug|x64
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Debug|x86.ActiveCfg = Debug|Win32
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Debug|x86.Build.0 = Debug|Win32
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Release|x64.ActiveCfg = Release|x64
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Release|x64.Build.0 = Release|x64
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Release|x86.ActiveCfg = Release|Win32
{DC4D9247-92B6-4C52-B47C-41C34CF5D611}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

28
VoidNetVS/VoidNetVS.sln Normal file
View File

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidNetVS", "VoidNetVS\VoidNetVS.vcxproj", "{5172321E-CCB0-4A77-9F3D-FAAF0084F434}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Debug|x64.ActiveCfg = Debug|x64
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Debug|x64.Build.0 = Debug|x64
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Debug|x86.ActiveCfg = Debug|Win32
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Debug|x86.Build.0 = Debug|Win32
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x64.ActiveCfg = Release|x64
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x64.Build.0 = Release|x64
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.ActiveCfg = Release|Win32
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Debug|Win32|F:\VoidNet\VoidNetVS\|

View File

@ -18,14 +18,34 @@
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\Config.hpp" />
<ClInclude Include="..\..\include\Defs.hpp" />
<ClInclude Include="..\..\include\Init.hpp" />
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
<ClInclude Include="..\..\include\TcpClient.hpp" />
<ClInclude Include="..\..\include\Utility.hpp" />
<ClInclude Include="..\..\include\VoidNet.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Config.cpp" />
<ClCompile Include="..\..\src\Init.cpp" />
<ClCompile Include="..\..\src\InitWindows.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
<ClCompile Include="..\..\src\Utility.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{DC4D9247-92B6-4C52-B47C-41C34CF5D611}</ProjectGuid>
<RootNamespace>VoidNet</RootNamespace>
<ProjectGuid>{5172321E-CCB0-4A77-9F3D-FAAF0084F434}</ProjectGuid>
<RootNamespace>VoidNetVS</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
@ -74,6 +94,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>F:\VoidNet\include</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -90,14 +111,16 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalIncludeDirectories>F:\VoidNet\include;F:\VoidNet\src</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
</Link>
<Lib>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="include">
<UniqueIdentifier>{a0077c03-a272-4e35-99ef-6011b4489da3}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{4c99f44e-3ff9-4d5c-a3a1-27d1f02b0b7f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\Init.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\Defs.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\VoidNet.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\TcpClient.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\Utility.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\Config.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\NetworkMessage.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Init.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\InitWindows.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\TcpClient.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\TcpClientWindows.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Utility.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Config.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO" />
</ItemGroup>
</Project>

17
include/Config.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef CONFIG_HPP
#define CONFIG_HPP
#ifdef _MSC_VER
#pragma once
#endif
struct Config
{
static void SetUsingConsole(bool value);
static bool GetUsingConsole();
private:
static bool using_console;
};
#endif

188
include/Defs.hpp Normal file
View File

@ -0,0 +1,188 @@
#ifndef DEFS_HPP
#define DEFS_HPP
#ifdef _MSC_VER
#pragma once
#endif
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
WSADATA wsa_data;
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed __int64 int64;
typedef unsigned __int64 uint64;
#ifdef _WIN64
typedef signed __int64 int_ptr;
typedef unsigned __int64 uint_ptr;
#else
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#endif // win64
#elif defined(__GNUC__) || defined(__clang__)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
#ifdef _WIN64
typedef signed long long int_ptr, int64;
typedef unsigned long long uint_ptr, uint64;
#else
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#ifdef __LP64__
typedef signed long int64;
typedef unsigned long uint64;
#else
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // __LP64__
#endif // win64
#elif defined(__DECCXX)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed __int64 int64;
typedef unsigned __int64 uint64;
#ifdef __VMS
#ifdef __32BITS
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#else
typedef signed __int64 int_ptr;
typedef unsigned __int64 uint_ptr;
#endif // __32BITS
#else
typedef signed long int_ptr;
typedef unsigned long uint_ptr
#endif // __VMS
#elif defined(__HP_aCC)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#ifdef __LP64__
typedef signed long int64;
typedef unsigned long uint64;
#else
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // __LP64__
#elif defined(__SUNPRO_CC)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long int_ptr;
typedef unsigned long uint_otr
#ifdef __sparcv9
typedef signed long int64;
typedef unsigned long uint64;
#else
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // __sparcv9
#elif defined(__IBMCPP__)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#ifdef __64BIT__
typedef signed long int64;
typedef unsigned long uint64;
#else
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // __64BIT__
#elif defined(__sgi)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
#if _MIPS_SZLONG == 64
typedef signed long int64;
typedef unsigned long uint64;
#else
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // _MIPS_SZLONG
#elif defined(_DIAB_TOOL)
typedef signed char int8, sbyte;
typedef unsigned char uint8, byte;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int in32;
typedef unsigned int uint32;
typedef signed long int_ptr;
typedef unsigned long uint_ptr;
typedef signed long long int64;
typedef unsigned long long uint64;
#endif // compiler data type defenitions
const uint16 default_port = 60250;
enum VoidCode
{
VOID_SUCCESS,
VOID_WSA_INIT_FAILED,
VOID_TCP_INIT_FAILED,
VOID_INVALID_IP_ADDRESS,
VOID_INVALID_PORT,
VOID_COULDNT_CONNECT,
};
#endif // DEFS_HPP

12
include/Init.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef INIT_HPP
#define INIT_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include "Defs.hpp"
VoidCode initialize();
#endif

View File

@ -0,0 +1,16 @@
#ifndef NETWORK_MESSAGE_HPP
#define NETWORK_MESSAGE_HPP
#ifdef _MSC_VER
#pragma once
#endif
class NetworkMessage
{
public:
NetworkMessage();
private:
};
#endif

47
include/TcpClient.hpp Normal file
View File

@ -0,0 +1,47 @@
#ifndef TCP_CLIENT_HPP
#define TCP_CLIENT_HPP
#include "Defs.hpp"
#include "NetworkMessage.hpp"
#include <string>
#ifdef _MSC_VER
#pragma once
#endif
class TcpClient
{
public:
TcpClient();
TcpClient(const std::string &ip, uint16 port = default_port);
~TcpClient();
const std::string &GetIP();
void SetIP(const std::string &ip);
uint16 GetPort();
void SetPort(uint16 port);
VoidCode Connect(); // this function is used if Initialize was not called
char *ReceiveDataArray();
const NetworkMessage &ReceiveData();
bool SendData(const NetworkMessage &message);
private:
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
std::string ip;
uint16 port = 0;
bool initialized;
#ifdef _MSC_VER
SOCKET socket = INVALID_SOCKET;
struct addrinfo *result = nullptr;
struct addrinfo *ptr = nullptr;
struct addrinfo hints;
#endif
};
#endif

14
include/Utility.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef UTILITY_HPP
#define UTILITY_HPP
#ifdef _MSC_VER
#pragma once
#endif
struct Utility
{
static void Delete(void *pointer);
static void DeleteArray(void *pointer);
};
#endif

11
include/VoidNet.hpp Normal file
View File

@ -0,0 +1,11 @@
#ifndef VOID_NET_HPP
#define VOID_NET_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include <Defs.hpp>
#include <Init.hpp>
#endif

11
src/Config.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Config.hpp"
void Config::SetUsingConsole(bool value)
{
using_console = value;
}
bool Config::GetUsingConsole()
{
return using_console;
}

3
src/Init.cpp Normal file
View File

@ -0,0 +1,3 @@
#ifdef _MSC_VER
#include "InitWindows.cpp"
#endif

16
src/InitWindows.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "Init.hpp"
#include "Config.hpp"
#include <iostream>
VoidCode initialize()
{
uint16 code = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (code != 0)
{
if (Config::GetUsingConsole())
std::cerr << code << std::endl; // display some more information too
return VOID_WSA_INIT_FAILED;
}
return VOID_SUCCESS;
}

3
src/TcpClient.cpp Normal file
View File

@ -0,0 +1,3 @@
#ifdef _MSC_VER
#include "TcpClientWindows.cpp"
#endif

122
src/TcpClientWindows.cpp Normal file
View File

@ -0,0 +1,122 @@
#include "TcpClient.hpp"
#include "Utility.hpp"
#include "Config.hpp"
#include <iostream>
VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
{
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
return VOID_INVALID_IP_ADDRESS;
if (port == 0)
return VOID_INVALID_PORT;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
uint16 code = getaddrinfo(ip.c_str(), std::to_string(port).c_str(), &hints, &result);
if (code != 0)
{
if (Config::GetUsingConsole())
std::cerr << code << std::endl; // display more info
WSACleanup();
return VOID_TCP_INIT_FAILED;
}
ptr = result;
socket = ::socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (socket == INVALID_SOCKET)
{
if (Config::GetUsingConsole())
std::cerr << WSAGetLastError() << std::endl; // display more info
freeaddrinfo(result);
WSACleanup();
return VOID_TCP_INIT_FAILED;
}
initialized = true;
return VOID_SUCCESS;
}
TcpClient::TcpClient()
{
}
TcpClient::TcpClient(const std::string &ip, uint16 port) :
ip(ip), port(port)
{
}
TcpClient::~TcpClient()
{
freeaddrinfo(result);
WSACleanup();
Utility::Delete(result);
Utility::Delete(ptr);
}
const std::string & TcpClient::GetIP()
{
return ip;
}
void TcpClient::SetIP(const std::string & ip)
{
this->ip = ip;
}
uint16 TcpClient::GetPort()
{
return port;
}
void TcpClient::SetPort(uint16 port)
{
this->port = port;
}
VoidCode TcpClient::Connect()
{
if (!initialized)
{
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
return VOID_INVALID_IP_ADDRESS;
if (port == 0)
return VOID_INVALID_PORT;
VoidCode code = Initialize(ip, port);
if (code != VOID_SUCCESS)
return code;
}
uint16 connect_code = ::connect(socket, ptr->ai_addr, ptr->ai_addrlen);
if (connect_code == SOCKET_ERROR)
return VOID_COULDNT_CONNECT;
}
char *TcpClient::ReceiveDataArray()
{
char *header = new char[4]();
int remote_buffer_size;
do
{
remote_buffer_size = recv(socket, header, 4, 0);
if (WSAGetLastError() != 0)
{
// there was a problem receiving
}
}
while (remote_buffer_size > 0);
}
const NetworkMessage &TcpClient::ReceiveData()
{
}
bool TcpClient::SendData(const NetworkMessage &message)
{
return false;
}

17
src/Utility.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "Utility.hpp"
void Utility::Delete(void *pointer)
{
if (pointer == nullptr)
return;
delete pointer;
pointer = nullptr;
}
void Utility::DeleteArray(void *pointer)
{
if (pointer == nullptr)
return;
delete[] pointer;
pointer = nullptr;
}