Added missing global TcpClient.cpp file

Fixed Serializer from_bytes parentisis missing
Fixed client shutdown, now it sends a message saying its disconnected in TcpServerWindows
This commit is contained in:
xX-TheDoctor-Xx
2016-08-02 12:57:56 +01:00
parent 81a065b98b
commit 255f3551cc
14 changed files with 38 additions and 16 deletions

View File

@ -0,0 +1,3 @@
#ifdef _MSC_VER
#include "TcpClientWindows.cpp"
#endif

View File

@ -51,6 +51,7 @@
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\Utility.cpp" /> <ClCompile Include="..\..\src\Utility.cpp" />
<ClCompile Include="..\..\src\VoidNetClient.cpp" /> <ClCompile Include="..\..\src\VoidNetClient.cpp" />
<ClCompile Include="TcpClient.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="..\..\DEV_INFO" /> <Text Include="..\..\DEV_INFO" />

View File

@ -83,6 +83,9 @@
<ClCompile Include="..\..\src\Handshake.cpp"> <ClCompile Include="..\..\src\Handshake.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="TcpClient.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="..\..\TODO" /> <Text Include="..\..\TODO" />

View File

@ -1,7 +1,6 @@
#include "NetworkMessage.hpp" #include "NetworkMessage.hpp"
#include "Utility.hpp" #include "Utility.hpp"
#include "Serializer.hpp" #include "Serializer.hpp"
#include "Tags.hpp"
#include <vector> #include <vector>

View File

@ -14,7 +14,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
template<typename T> const T& Serializer::from_bytes(const std::vector<byte> &bytes, T& object) template<typename T> const T& Serializer::from_bytes(const std::vector<byte> &bytes, T& object)
{ {
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type"); static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
std::copy(bytes.begin(), bytes.end(), reinterpret_cast<const byte*>(std::addressof(object)); std::copy(bytes.begin(), bytes.end(), reinterpret_cast<const byte*>(std::addressof(object)));
return object; return object;
} }

View File

@ -2,7 +2,6 @@
#include "Utility.hpp" #include "Utility.hpp"
#include "Config.hpp" #include "Config.hpp"
#include "NetworkBuffer.hpp" #include "NetworkBuffer.hpp"
#include "Tags.hpp"
#include "Handshake.hpp" #include "Handshake.hpp"
#include <iostream> #include <iostream>

View File

@ -72,7 +72,10 @@ TcpServer::~TcpServer()
void TcpServer::Shutdown() void TcpServer::Shutdown()
{ {
for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it) for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it)
(*it).Shutdown(); {
TcpClient client = *it;
client.Shutdown();
}
} }
bool TcpServer::StartServer(bool accept_connections) bool TcpServer::StartServer(bool accept_connections)
@ -201,7 +204,8 @@ uint16 TcpServer::AllocateID() // this function is only used in the AddToClients
bool flag = true; bool flag = true;
for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it) for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it)
{ {
if ((*it).GetID() == i) TcpClient client = *it;
if (client.GetID() == i)
{ {
flag = false; flag = false;
break; break;
@ -235,7 +239,7 @@ void TcpServer::RejectConnection(TcpClient &client)
{ {
Handshake handshake(client.GetID(), ConnectionCode::Reject); Handshake handshake(client.GetID(), ConnectionCode::Reject);
client.SendBytes(Handshake::EncodeHandshake(handshake)); client.SendBytes(Handshake::EncodeHandshake(handshake));
client.Shutdown(); CloseSocket(client);
} }
void TcpServer::AcceptConnection(TcpClient & client) void TcpServer::AcceptConnection(TcpClient & client)
@ -246,13 +250,26 @@ void TcpServer::AcceptConnection(TcpClient & client)
void TcpServer::CloseSocket(TcpClient & client) void TcpServer::CloseSocket(TcpClient & client)
{ {
NetworkMessage message; bool removed;
message.sender = -1; for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it)
message.distribution_mode = ID; {
message.destination_id = client.GetID(); TcpClient it_client = *it;
message.tag = DISCONNECT; if (client.GetID() == it_client.GetID())
SendMessage(message); {
clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end()); clients.erase(it);
removed = true;
}
}
if (removed)
{
NetworkMessage message;
message.sender = -1;
message.distribution_mode = ID;
message.destination_id = client.GetID();
message.tag = DISCONNECT;
SendMessage(message);
}
} }
void TcpServer::CloseSocket(uint16 id) void TcpServer::CloseSocket(uint16 id)
@ -266,8 +283,9 @@ const TcpClient & TcpServer::GetClientByID(uint16 id)
{ {
for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it) for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it)
{ {
if ((*it).GetID() == id) TcpClient client = *it;
return *it; if (client.GetID() == id)
return client;
} }
return TcpClient(); return TcpClient();
} }

View File

@ -1,6 +1,5 @@
#include "VoidNetClient.hpp" #include "VoidNetClient.hpp"
#include "Utility.hpp" #include "Utility.hpp"
#include "Tags.hpp"
#include <future> #include <future>