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

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