Changed InternalTags names to be lower case

Removed parameter less constructor from TcpClient
Added UdpClient files
This commit is contained in:
xX-TheDoctor-Xx
2016-08-21 15:24:28 +01:00
parent 996b88c0f6
commit 74d9d5db87
16 changed files with 40 additions and 26 deletions

View File

@ -38,18 +38,22 @@
<ClCompile Include="..\..\src\Handshake.cpp" />
<ClCompile Include="..\..\src\Init.cpp" />
<ClCompile Include="..\..\src\InitWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\NetworkBuffer.cpp" />
<ClCompile Include="..\..\src\NetworkMessage.cpp" />
<ClCompile Include="..\..\src\Serializer.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\TcpServer.cpp" />
<ClCompile Include="..\..\src\TcpServerWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\UdpClient.cpp" />
<ClCompile Include="..\..\src\UdpClientWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\Utility.cpp" />
<ClCompile Include="..\..\src\VoidNetClient.cpp" />

View File

@ -89,6 +89,12 @@
<ClCompile Include="..\..\src\TcpClient.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\UdpClient.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\UdpClientWindows.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO" />

View File

@ -239,10 +239,10 @@ enum ConnectionCode
enum InternalTags
{
CONNECT = 254,
DISCONNECT = 255,
ConnectTag = 254,
DisconnectTag = 255,
};
#define IS_HANDSHAKE(name) name.subject == 1 && (name.tag == DISCONNECT || name.tag == CONNECT || name.tag == ConnectionCode::Accept || name.tag == ConnectionCode::Close || name.tag == ConnectionCode::Reject)
#define IS_HANDSHAKE(name) name.subject == 1 && (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject)
#endif // DEFS_HPP

View File

@ -14,7 +14,6 @@
struct TcpClient
{
TcpClient();
TcpClient(const SOCKET &socket);
TcpClient(const std::string &ip);
TcpClient(const std::string &ip, uint16 port = default_client_port);

View File

@ -5,6 +5,12 @@
#pragma once
#endif
#include "Defs.hpp"
#include "NetworkMessage.hpp"
#include <string>
#include <functional>
struct UdpClient
{

View File

@ -81,7 +81,7 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
message.tag = buffer.body[6];
message.subject = Utility::BitConverter::ToUint8(buffer.body, 7);
message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT;
message.valid = message.sender != -2 && message.tag != ConnectTag && message.tag != DisconnectTag;
if (Utility::BitConverter::ToInt32(buffer.header) < 9)
return message;
void *object;
@ -94,8 +94,7 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
message.tag = Utility::BitConverter::ToUint8(buffer.body, 3);
message.subject = 1;
message.valid = message.sender != -2 && (message.tag == DISCONNECT || message.tag == CONNECT || message.tag == ConnectionCode::Accept ||
message.tag == ConnectionCode::Close || message.tag == ConnectionCode::Reject);
message.valid = message.sender != -2 && IS_HANDSHAKE(message);
return message;
break;
}

View File

@ -4,10 +4,6 @@
#include <string>
#include <future>
TcpClient::TcpClient()
{
}
TcpClient::TcpClient(const std::string &ip) : port(default_client_port)
{
initialize(ip);
@ -58,9 +54,9 @@ void TcpClient::receive_data(TcpClient *client)
{
if (IS_HANDSHAKE(message))
{
if (message.tag == CONNECT) // some user has connected - not us, never
if (message.tag == ConnectTag) // some user has connected - not us, never
std::async(std::launch::async, client->OnConnect, message.sender);
else if (message.tag == DISCONNECT || message.tag == ConnectionCode::Close) // some user has disconnected
else if (message.tag == DisconnectTag || message.tag == Close) // some user has disconnected
std::async(std::launch::async, client->OnDisconnect, message.sender);
}
else

View File

@ -12,12 +12,10 @@ TcpClient::TcpClient(const SOCKET & socket)
bool TcpClient::initialize(const std::string &ip, uint16 port)
{
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
return false;
if (port == 0)
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4 || port == 0)
return false;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
@ -82,7 +80,7 @@ bool TcpClient::Connect()
NetworkMessage message(receive_data_array());
if (IS_HANDSHAKE(message))
{
if (message.tag == ConnectionCode::Accept)
if (message.tag == Accept)
{
receive = true;
OnConnect(message.sender);

View File

@ -80,7 +80,7 @@ void TcpServer::SendMessage(const NetworkMessage & message)
}
case Server: // this will only send the message to the server
{
if (message.tag == DISCONNECT)
if (message.tag == DisconnectTag)
CloseSocket(message.sender);
for (uint16 i = 0; i < OnMessageFunctions.size(); i++)
OnMessageFunctions[i](message);
@ -152,13 +152,13 @@ void TcpServer::add_to_clients_list(TcpClient & client_socket)
void TcpServer::RejectConnection(TcpClient &client)
{
Handshake handshake(client.GetID(), ConnectionCode::Reject, ID);
Handshake handshake(client.GetID(), Reject, ID);
SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
}
void TcpServer::AcceptConnection(uint16 id)
{
Handshake handshake(id, ConnectionCode::Accept, AllAndMe);
Handshake handshake(id, Accept, AllAndMe);
SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
}

5
src/UdpClient.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "UdpClient.hpp"
#ifdef _MSC_VER
#include "UdpClientWindows.cpp"
#endif

1
src/UdpClientWindows.cpp Normal file
View File

@ -0,0 +1 @@
#include "UdpClient.hpp"

View File

@ -37,7 +37,7 @@ void VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data)
void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data)
{
if (tag != CONNECT && tag != DISCONNECT)
if (tag != ConnectTag && tag != DisconnectTag)
{
NetworkMessage message;
message.tag = tag;