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\Handshake.cpp" />
<ClCompile Include="..\..\src\Init.cpp" /> <ClCompile Include="..\..\src\Init.cpp" />
<ClCompile Include="..\..\src\InitWindows.cpp"> <ClCompile Include="..\..\src\InitWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\NetworkBuffer.cpp" /> <ClCompile Include="..\..\src\NetworkBuffer.cpp" />
<ClCompile Include="..\..\src\NetworkMessage.cpp" /> <ClCompile Include="..\..\src\NetworkMessage.cpp" />
<ClCompile Include="..\..\src\Serializer.cpp" /> <ClCompile Include="..\..\src\Serializer.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" /> <ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp"> <ClCompile Include="..\..\src\TcpClientWindows.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\TcpServer.cpp" /> <ClCompile Include="..\..\src\TcpServer.cpp" />
<ClCompile Include="..\..\src\TcpServerWindows.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>
<ClCompile Include="..\..\src\Utility.cpp" /> <ClCompile Include="..\..\src\Utility.cpp" />
<ClCompile Include="..\..\src\VoidNetClient.cpp" /> <ClCompile Include="..\..\src\VoidNetClient.cpp" />

View File

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

View File

@ -239,10 +239,10 @@ enum ConnectionCode
enum InternalTags enum InternalTags
{ {
CONNECT = 254, ConnectTag = 254,
DISCONNECT = 255, 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 #endif // DEFS_HPP

View File

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

View File

@ -5,6 +5,12 @@
#pragma once #pragma once
#endif #endif
#include "Defs.hpp"
#include "NetworkMessage.hpp"
#include <string>
#include <functional>
struct UdpClient 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.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
message.tag = buffer.body[6]; message.tag = buffer.body[6];
message.subject = Utility::BitConverter::ToUint8(buffer.body, 7); 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) if (Utility::BitConverter::ToInt32(buffer.header) < 9)
return message; return message;
void *object; void *object;
@ -94,8 +94,7 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
message.sender = Utility::BitConverter::ToUint16(buffer.body, 1); message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
message.tag = Utility::BitConverter::ToUint8(buffer.body, 3); message.tag = Utility::BitConverter::ToUint8(buffer.body, 3);
message.subject = 1; message.subject = 1;
message.valid = message.sender != -2 && (message.tag == DISCONNECT || message.tag == CONNECT || message.tag == ConnectionCode::Accept || message.valid = message.sender != -2 && IS_HANDSHAKE(message);
message.tag == ConnectionCode::Close || message.tag == ConnectionCode::Reject);
return message; return message;
break; break;
} }

View File

@ -4,10 +4,6 @@
#include <string> #include <string>
#include <future> #include <future>
TcpClient::TcpClient()
{
}
TcpClient::TcpClient(const std::string &ip) : port(default_client_port) TcpClient::TcpClient(const std::string &ip) : port(default_client_port)
{ {
initialize(ip); initialize(ip);
@ -58,9 +54,9 @@ void TcpClient::receive_data(TcpClient *client)
{ {
if (IS_HANDSHAKE(message)) 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); 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); std::async(std::launch::async, client->OnDisconnect, message.sender);
} }
else else

View File

@ -12,12 +12,10 @@ TcpClient::TcpClient(const SOCKET & socket)
bool 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 || port == 0)
return false;
if (port == 0)
return false; return false;
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
@ -82,7 +80,7 @@ bool TcpClient::Connect()
NetworkMessage message(receive_data_array()); NetworkMessage message(receive_data_array());
if (IS_HANDSHAKE(message)) if (IS_HANDSHAKE(message))
{ {
if (message.tag == ConnectionCode::Accept) if (message.tag == Accept)
{ {
receive = true; receive = true;
OnConnect(message.sender); 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 case Server: // this will only send the message to the server
{ {
if (message.tag == DISCONNECT) if (message.tag == DisconnectTag)
CloseSocket(message.sender); CloseSocket(message.sender);
for (uint16 i = 0; i < OnMessageFunctions.size(); i++) for (uint16 i = 0; i < OnMessageFunctions.size(); i++)
OnMessageFunctions[i](message); OnMessageFunctions[i](message);
@ -152,13 +152,13 @@ void TcpServer::add_to_clients_list(TcpClient & client_socket)
void TcpServer::RejectConnection(TcpClient &client) void TcpServer::RejectConnection(TcpClient &client)
{ {
Handshake handshake(client.GetID(), ConnectionCode::Reject, ID); Handshake handshake(client.GetID(), Reject, ID);
SendMessage(Handshake::HandshakeToNetworkMessage(handshake)); SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
} }
void TcpServer::AcceptConnection(uint16 id) void TcpServer::AcceptConnection(uint16 id)
{ {
Handshake handshake(id, ConnectionCode::Accept, AllAndMe); Handshake handshake(id, Accept, AllAndMe);
SendMessage(Handshake::HandshakeToNetworkMessage(handshake)); 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) 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; NetworkMessage message;
message.tag = tag; message.tag = tag;