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

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;
}