Removed VoidCode enum, added missing tcp client windos code and missing if macro in TcpClient.hpp
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -169,16 +169,6 @@ typedef unsigned long long uint64;
|
||||
|
||||
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
|
||||
{
|
||||
All = 1, // Others and Server
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
struct Initialization
|
||||
{
|
||||
static VoidCode initialize();
|
||||
static bool initialize();
|
||||
static WSADATA wsa_data;
|
||||
};
|
||||
|
||||
|
||||
@ -10,9 +10,8 @@
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#undef SendMessage
|
||||
#endif
|
||||
|
||||
class TcpClient
|
||||
{
|
||||
@ -27,7 +26,7 @@ public:
|
||||
uint16 GetPort();
|
||||
void SetPort(uint16 port);
|
||||
|
||||
VoidCode Connect();
|
||||
bool Connect();
|
||||
|
||||
void ReceiveMessages();
|
||||
void SendMessage(const NetworkMessage &message);
|
||||
@ -40,7 +39,7 @@ private:
|
||||
const NetworkBuffer &ReceiveDataArray();
|
||||
static void ReceiveData(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;
|
||||
uint16 port = 0;
|
||||
@ -55,10 +54,12 @@ private:
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef UNICODE
|
||||
#define SendMessage SendMessageW
|
||||
#else
|
||||
#define SendMessage SendMessageA
|
||||
#endif // !UNICODE
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -3,14 +3,14 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
VoidCode Initialization::initialize()
|
||||
bool Initialization::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 false;
|
||||
}
|
||||
return VOID_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
@ -10,12 +10,12 @@
|
||||
|
||||
#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)
|
||||
return VOID_INVALID_IP_ADDRESS;
|
||||
return false;
|
||||
if (port == 0)
|
||||
return VOID_INVALID_PORT;
|
||||
return false;
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
@ -27,7 +27,7 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
||||
if (Config::GetUsingConsole())
|
||||
std::cerr << code << std::endl; // display more info
|
||||
WSACleanup();
|
||||
return VOID_TCP_INIT_FAILED;
|
||||
return false;
|
||||
}
|
||||
|
||||
ptr = result;
|
||||
@ -39,11 +39,21 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
||||
std::cerr << WSAGetLastError() << std::endl; // display more info
|
||||
freeaddrinfo(result);
|
||||
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;
|
||||
return VOID_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
TcpClient::TcpClient() : port(default_port)
|
||||
@ -83,23 +93,22 @@ void TcpClient::SetPort(uint16 port)
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
VoidCode TcpClient::Connect()
|
||||
bool TcpClient::Connect()
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
|
||||
return VOID_INVALID_IP_ADDRESS;
|
||||
return false;
|
||||
if (port == 0)
|
||||
return VOID_INVALID_PORT;
|
||||
VoidCode code = Initialize(ip, port);
|
||||
if (code != VOID_SUCCESS)
|
||||
return code;
|
||||
return false;
|
||||
if (Initialize(ip, port) != true)
|
||||
return false;
|
||||
}
|
||||
uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen);
|
||||
if (connect_code == SOCKET_ERROR)
|
||||
return VOID_COULDNT_CONNECT;
|
||||
return false;
|
||||
receive = true;
|
||||
return VOID_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
const NetworkBuffer &TcpClient::ReceiveDataArray()
|
||||
|
||||
@ -8,7 +8,7 @@ bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
||||
{
|
||||
client->SetIP(ip);
|
||||
client->SetPort(port);
|
||||
return client->Connect() == VOID_SUCCESS;
|
||||
return client->Connect();
|
||||
}
|
||||
|
||||
void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data)
|
||||
|
||||
Reference in New Issue
Block a user