Removed VoidCode enum, added missing tcp client windos code and missing if macro in TcpClient.hpp

This commit is contained in:
xX-TheDoctor-Xx
2016-07-20 22:20:50 +01:00
parent c5d565698e
commit 3f99e8a178
10 changed files with 33 additions and 33 deletions

View File

@ -169,16 +169,6 @@ typedef unsigned long long uint64;
const uint16 default_port = 60250; 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,
};
enum DistributionType enum DistributionType
{ {
All = 1, // Others and Server All = 1, // Others and Server

View File

@ -10,7 +10,7 @@
struct Initialization struct Initialization
{ {
static VoidCode initialize(); static bool initialize();
static WSADATA wsa_data; static WSADATA wsa_data;
}; };

View File

@ -10,9 +10,8 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma once #pragma once
#endif
#undef SendMessage #undef SendMessage
#endif
class TcpClient class TcpClient
{ {
@ -27,7 +26,7 @@ public:
uint16 GetPort(); uint16 GetPort();
void SetPort(uint16 port); void SetPort(uint16 port);
VoidCode Connect(); bool Connect();
void ReceiveMessages(); void ReceiveMessages();
void SendMessage(const NetworkMessage &message); void SendMessage(const NetworkMessage &message);
@ -40,7 +39,7 @@ private:
const NetworkBuffer &ReceiveDataArray(); const NetworkBuffer &ReceiveDataArray();
static void ReceiveData(TcpClient *client); static void ReceiveData(TcpClient *client);
static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client); static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
VoidCode Initialize(const std::string &ip, uint16 port = default_port); bool Initialize(const std::string &ip, uint16 port = default_port);
std::string ip; std::string ip;
uint16 port = 0; uint16 port = 0;
@ -55,10 +54,12 @@ private:
#endif #endif
}; };
#ifdef _MSC_VER
#ifdef UNICODE #ifdef UNICODE
#define SendMessage SendMessageW #define SendMessage SendMessageW
#else #else
#define SendMessage SendMessageA #define SendMessage SendMessageA
#endif // !UNICODE #endif // !UNICODE
#endif
#endif #endif

View File

@ -3,14 +3,14 @@
#include <iostream> #include <iostream>
VoidCode Initialization::initialize() bool Initialization::initialize()
{ {
uint16 code = WSAStartup(MAKEWORD(2, 2), &wsa_data); uint16 code = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (code != 0) if (code != 0)
{ {
if (Config::GetUsingConsole()) if (Config::GetUsingConsole())
std::cerr << code << std::endl; // display some more information too std::cerr << code << std::endl; // display some more information too
return VOID_WSA_INIT_FAILED; return false;
} }
return VOID_SUCCESS; return true;
} }

View File

@ -10,12 +10,12 @@
#undef SendMessage #undef SendMessage
VoidCode TcpClient::Initialize(const std::string &ip, uint16 port) bool TcpClient::Initialize(const std::string &ip, uint16 port)
{ {
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4) if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
return VOID_INVALID_IP_ADDRESS; return false;
if (port == 0) if (port == 0)
return VOID_INVALID_PORT; return false;
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
@ -27,7 +27,7 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
if (Config::GetUsingConsole()) if (Config::GetUsingConsole())
std::cerr << code << std::endl; // display more info std::cerr << code << std::endl; // display more info
WSACleanup(); WSACleanup();
return VOID_TCP_INIT_FAILED; return false;
} }
ptr = result; ptr = result;
@ -39,11 +39,21 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
std::cerr << WSAGetLastError() << std::endl; // display more info std::cerr << WSAGetLastError() << std::endl; // display more info
freeaddrinfo(result); freeaddrinfo(result);
WSACleanup(); WSACleanup();
return VOID_TCP_INIT_FAILED; return false;
}
code = bind(tcp_socket, result->ai_addr, (int)result->ai_addrlen);
if (code == SOCKET_ERROR)
{
if (Config::GetUsingConsole())
std::cerr << WSAGetLastError() << std::endl; // display more info
freeaddrinfo(result);
WSACleanup();
return false;
} }
initialized = true; initialized = true;
return VOID_SUCCESS; return true;
} }
TcpClient::TcpClient() : port(default_port) TcpClient::TcpClient() : port(default_port)
@ -83,23 +93,22 @@ void TcpClient::SetPort(uint16 port)
this->port = port; this->port = port;
} }
VoidCode TcpClient::Connect() bool TcpClient::Connect()
{ {
if (!initialized) if (!initialized)
{ {
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4) if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
return VOID_INVALID_IP_ADDRESS; return false;
if (port == 0) if (port == 0)
return VOID_INVALID_PORT; return false;
VoidCode code = Initialize(ip, port); if (Initialize(ip, port) != true)
if (code != VOID_SUCCESS) return false;
return code;
} }
uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen); uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen);
if (connect_code == SOCKET_ERROR) if (connect_code == SOCKET_ERROR)
return VOID_COULDNT_CONNECT; return false;
receive = true; receive = true;
return VOID_SUCCESS; return true;
} }
const NetworkBuffer &TcpClient::ReceiveDataArray() const NetworkBuffer &TcpClient::ReceiveDataArray()

View File

@ -8,7 +8,7 @@ bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
{ {
client->SetIP(ip); client->SetIP(ip);
client->SetPort(port); client->SetPort(port);
return client->Connect() == VOID_SUCCESS; return client->Connect();
} }
void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data) void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data)