Code optimizations

This commit is contained in:
TheDoctor
2019-10-13 22:35:28 +01:00
parent 5e401247b1
commit 32473472e4
23 changed files with 98 additions and 136 deletions

View File

@ -1,4 +1,2 @@
id -1 is server / valid message if other parameters are valid as well
id -2 is invalid network message
NetworkMessage: subject 1 is reserved for handshake validation
id -2 is invalid network message

View File

@ -19,7 +19,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\HLAPI\BitConverter.hpp" />
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp" />
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp" />
<ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp" />
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp" />

View File

@ -9,9 +9,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\HLAPI\BitConverter.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp">
<Filter>include</Filter>
</ClInclude>
@ -45,6 +42,9 @@
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\HLAPI\main.cpp">

9
TODO
View File

@ -1,14 +1,11 @@
initialization code for other operating systems - currently VoidNet only supports windows
initialization code for tcp client and tcp server for other operating systems - currently windows only
revamped BitConverter class
new StringConverter class
IPUtil class for other os's
maybe i should implement error codes and exceptions
plugin system - idk how im going to implement it yet
maybe i should use virtual methods for the server and client, maybe...
TcpServer::SendMessage rework
TcpConnectionHandler::AddClient max_connections
Test udp stuff

View File

@ -49,7 +49,6 @@
<ClCompile Include="..\..\src\VoidNet\Headers.cpp" />
<ClCompile Include="..\..\src\VoidNet\Http.cpp" />
<ClCompile Include="..\..\src\VoidNet\IPAddress.cpp" />
<ClCompile Include="..\..\src\VoidNet\Parse.cpp" />
<ClCompile Include="..\..\src\VoidNet\Request.cpp" />
<ClCompile Include="..\..\src\VoidNet\Response.cpp" />
<ClCompile Include="..\..\src\VoidNet\SecureSocket.cpp" />
@ -59,6 +58,7 @@
<ClCompile Include="..\..\src\VoidNet\TcpSocketBuilder.cpp" />
<ClCompile Include="..\..\src\VoidNet\UdpSocket.cpp" />
<ClCompile Include="..\..\src\VoidNet\Uri.cpp" />
<ClCompile Include="..\..\src\VoidNet\Util.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{5172321E-CCB0-4A77-9F3D-FAAF0084F434}</ProjectGuid>

View File

@ -90,9 +90,6 @@
<ClCompile Include="..\..\src\VoidNet\Http.cpp">
<Filter>src\Http</Filter>
</ClCompile>
<ClCompile Include="..\..\src\VoidNet\Parse.cpp">
<Filter>src\Http</Filter>
</ClCompile>
<ClCompile Include="..\..\src\VoidNet\Request.cpp">
<Filter>src\Http</Filter>
</ClCompile>
@ -123,5 +120,8 @@
<ClCompile Include="..\..\src\VoidNet\UdpSocket.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\VoidNet\Util.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -9,7 +9,7 @@
namespace std
{
class BitConverter
class ByteConverter
{
public:
template<typename T>
@ -24,7 +24,7 @@ namespace std
inline static T FromBytes(uint8_t *data)
{
if (!data)
throw std::invalid_argument("cant have null parameter -> BitConverter::FromBytes");
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
T value;
memcpy(&value, data, sizeof(T));
return value;

View File

@ -1,7 +1,7 @@
#pragma once
#include "NetworkHeader.hpp"
#include "BitConverter.hpp"
#include "ByteConverter.hpp"
#include <cstdint>
#include <cstring>
@ -68,9 +68,9 @@ namespace std::net
uint8_t *bytes = new uint8_t[header.Size];
memcpy(bytes, &header, sizeOfNetHeader);
uint8_t *sender = BitConverter::ToBytes<uint32_t>(m_senderID); // 4
uint8_t *destination = BitConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = BitConverter::ToBytes<uint32_t>(m_tag); // 4
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
memcpy(bytes + sizeOfNetHeader, sender, 4);
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;

View File

@ -20,7 +20,5 @@ namespace std::net
private:
std::shared_ptr<std::net::TcpServer> m_tcpServer;
std::shared_ptr<MessageQueue> m_queue;
};
}

View File

@ -7,18 +7,12 @@
#include <TcpListener.hpp>
#include <TcpConnection.hpp>
//#include <BaseLibrary/SpinMutex.hpp>
namespace std::net
{
class MessageQueue;
class TcpConnection;
class Server;
namespace sockets
{
class TcpListener;
}
class TcpListener;
}
namespace std::net
@ -49,7 +43,7 @@ namespace std::net
std::vector<std::shared_ptr<TcpConnection>> m_list;
std::mutex m_listMutex;
uint32_t m_maxConnections;
uint32_t m_maxConnections = 0;
std::thread m_receiveThread;
std::thread m_sendThread;
@ -59,5 +53,7 @@ namespace std::net
std::shared_ptr<MessageQueue> m_queue;
std::shared_ptr<TcpListener> m_listenerPtr;
std::vector<pollfd> poll_fds;
};
}

View File

@ -24,16 +24,11 @@
#define SOCKET_ERROR -1
#define NO_ERROR 0
#define INVALID_SOCKET NO_ERROR
#define INVALID_SOCKET NO_ERROR
#define SOCKET int
#define closesocket close
int closesocket(SOCKET soc)
{
return close(soc);
}
#endif
#define DEFAULT_SERVER_PORT 61250

View File

@ -21,7 +21,7 @@ namespace std::net
return m_status;
}
const std::string& daGetDatata() const
const std::string& GetData() const
{
return m_data;
}

View File

@ -19,8 +19,8 @@ namespace std::net
friend class std::net::TcpConnectionHandler;
public:
TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(1));
TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(1));
TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
TcpClient *AcceptClient();

View File

@ -7,31 +7,6 @@
namespace std::net
{
inline static std::vector<std::string> Split(const std::string &str, const std::string &delimiter)
{
std::vector<std::string> splited;
if (str.empty() && delimiter.empty())
return std::vector<std::string>();
std::string::size_type lastPos = str.find_first_not_of(delimiter, 0);
std::string::size_type pos = str.find_first_of(delimiter, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
splited.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiter, pos);
pos = str.find_first_of(delimiter, lastPos);
}
return splited;
}
sockaddr_in CreateAddress(uint32_t address, uint16_t port)
{
sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = htonl(address);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
return addr;
}
static std::vector<std::string> Split(const std::string& str, const std::string& delimiter);
static sockaddr_in CreateAddress(uint32_t address, uint16_t port);
}

View File

@ -29,12 +29,12 @@ namespace std::net
NetworkHeader header;
header.Size = 13 + sizeOfNetHeader + m_dataSize;
uint8_t *bytes = new uint8_t[header.Size];
uint8_t *bytes = new uint8_t[header.Size]();
memcpy(bytes, &header, sizeOfNetHeader);
uint8_t *sender = BitConverter::ToBytes<uint32_t>(m_senderID); // 4
uint8_t *destination = BitConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = BitConverter::ToBytes<uint32_t>(m_tag); // 4
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
memcpy(bytes + sizeOfNetHeader, sender, 4);
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;

View File

@ -9,8 +9,6 @@ namespace std::net
Server::Server(uint32_t max_connections, uint16_t port)
{
m_tcpServer = std::make_shared<std::net::TcpServer>(max_connections, port);
m_queue = std::make_shared<MessageQueue>();
//m_tcpServer->m_connectionHandler->m_queue = m_queue;
}
void Server::Start()

View File

@ -28,11 +28,17 @@ namespace std::net
void TcpConnectionHandler::Start()
{
m_run.exchange(true);
pollfd master_fd;
master_fd.fd = m_listenerPtr->m_socket->GetNativeSocket();
master_fd.events = POLLRDNORM;
poll_fds.emplace_back(master_fd);
std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
m_receiveThread.swap(receive_thread);
//std::thread send_thread(&TcpConnectionHandler::HandleSendThreaded, this);
//m_sendThread.swap(send_thread);
std::thread send_thread(&TcpConnectionHandler::HandleSendThreaded, this);
m_sendThread.swap(send_thread);
}
void TcpConnectionHandler::Stop()
@ -60,14 +66,9 @@ namespace std::net
uint32_t *id_ptr = &id;
NetworkMessage msg(0, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, id_ptr, sizeof(id_ptr));
NetworkMessage msg(-1, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, id_ptr, sizeof(id_ptr));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
uint32_t serialized_size;
uint8_t *serialized_data = msg.SerializeData<uint32_t>(serialized_size);
int32_t sent;
if (!c->GetClient()->Send(serialized_data, serialized_size, sent))
if (!c->sendMessage(msg))
{
//couldnt send
return;
@ -77,6 +78,11 @@ namespace std::net
m_list.push_back(c);
m_listMutex.unlock();
pollfd client_fd;
client_fd.fd = c->m_client->m_socket->GetNativeSocket();
client_fd.events = POLLRDNORM;
poll_fds.emplace_back(client_fd);
m_queue->EnqueueConnection(msg);
}
@ -108,21 +114,6 @@ namespace std::net
void TcpConnectionHandler::HandleReceiveMsgAndConns()
{
// https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/poll.htm
std::vector<pollfd> poll_fds;
pollfd master_fd;
master_fd.fd = m_listenerPtr->m_socket->GetNativeSocket();
master_fd.events = POLLRDNORM;
poll_fds.emplace_back(master_fd);
for (size_t i = 0; i < m_list.size(); i++)
{
pollfd client_fd;
client_fd.fd = m_list.at(i)->m_client->m_socket->GetNativeSocket();
client_fd.events = POLLRDNORM;
poll_fds.emplace_back(client_fd);
}
int res = poll(poll_fds.data(), poll_fds.size(), -1);
if (res < 0)
@ -138,13 +129,9 @@ namespace std::net
for (int i = 0; i < poll_fds.size(); i++)
{
if (poll_fds.at(i).revents == 0)
if (poll_fds.at(i).revents == 0 || poll_fds[i].revents != POLLRDNORM)
continue;
if (poll_fds[i].revents != POLLRDNORM)
{
continue;
}
if (poll_fds.at(i).fd == m_listenerPtr->m_socket->GetNativeSocket())
{
TcpClient *c = m_listenerPtr->AcceptClient();
@ -173,7 +160,12 @@ namespace std::net
msg.Deserialize(buffer.get(), net_header->Size);
if (msg.GetTag() == (uint32_t)InternalTags::Disconnect)
{
// i? or i+1
poll_fds.erase(poll_fds.begin() + i);
m_queue->EnqueueDisconnection(msg);
}
else if (msg.GetTag() == (uint32_t)InternalTags::Connect)
m_queue->EnqueueConnection(msg);
else
@ -191,9 +183,6 @@ namespace std::net
{
NetworkMessage msg = m_queue->DequeueMessageToSend();
uint32_t size;
std::unique_ptr<uint8_t> data(msg.SerializeData(size));
if (msg.GetDistributionMode() == DistributionMode::Others)
{
m_listMutex.lock();
@ -202,8 +191,7 @@ namespace std::net
std::shared_ptr<TcpConnection> c = m_list.at(i);
if (c->GetID() != msg.GetSenderID())
{
int32_t sent;
if (!c->GetClient()->Send(data.get(), size, sent))
if (!c->sendMessage(msg))
{
// it failed - retry? or just disconnect right in the first try
}
@ -219,8 +207,7 @@ namespace std::net
std::shared_ptr<TcpConnection> c = m_list.at(i);
if (c->GetID() != msg.GetSenderID())
{
int32_t sent;
if (!c->GetClient()->Send(data.get(), size, sent))
if (!c->sendMessage(msg))
{
// it failed - retry? or just disconnect right in the first try
}
@ -238,8 +225,7 @@ namespace std::net
std::shared_ptr<TcpConnection> c = m_list.at(i);
if (c->GetID() == msg.GetSenderID())
{
int32_t sent;
if (!c->GetClient()->Send(data.get(), size, sent))
if (!c->sendMessage(msg))
{
// it failed - retry? or just disconnect right in the first try
}
@ -254,8 +240,7 @@ namespace std::net
{
std::shared_ptr<TcpConnection> c = m_list.at(i);
int32_t sent;
if (!c->GetClient()->Send(data.get(), size, sent))
if (!c->sendMessage(msg))
{
// it failed - retry? or just disconnect right in the first try
}
@ -268,9 +253,8 @@ namespace std::net
for (int i = 0; i < m_list.size(); i++)
{
std::shared_ptr<TcpConnection> c = m_list.at(i);
int32_t sent;
if (!c->GetClient()->Send(data.get(), size, sent))
if (!c->sendMessage(msg))
{
// it failed - retry? or just disconnect right in the first try
}
@ -289,18 +273,12 @@ namespace std::net
void TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded()
{
while (m_run.load())
{
HandleReceiveMsgAndConns();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
void TcpConnectionHandler::HandleSendThreaded()
{
while (m_run.load())
{
HandleSend();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
}

View File

@ -1,6 +1,6 @@
#include "Init.hpp"
#include "Server.hpp"
#include "BitConverter.hpp"
#include "ByteConverter.hpp"
#include "TcpClient.hpp"
#include "InternalTags.hpp"
#include "NetworkMessage.hpp"
@ -34,7 +34,7 @@ int main()
message.Deserialize(bytes, data_size);
uint32_t id = std::BitConverter::FromBytes<uint32_t>((uint8_t*)(message.GetData<void>()));
uint32_t id = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message.GetData<void>()));
if (message.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id << std::endl;
}
@ -50,7 +50,7 @@ int main()
message2.Deserialize(bytes2, data_size);
uint32_t id2 = std::BitConverter::FromBytes<uint32_t>((uint8_t*)(message2.GetData<void>()));
uint32_t id2 = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message2.GetData<void>()));
if (message2.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id2 << std::endl;
}

View File

View File

@ -194,8 +194,8 @@ namespace std::net
{
if (std::chrono::system_clock::now().time_since_epoch().count() - m_lastActivityTime > std::chrono::milliseconds(5).count())
{
SocketReturn writeState = HasState(SocketParam::CanWrite, std::chrono::milliseconds(1));
SocketReturn readState = HasState(SocketParam::CanRead, std::chrono::milliseconds(1));
SocketReturn writeState = HasState(SocketParam::CanWrite);
SocketReturn readState = HasState(SocketParam::CanRead);
if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes)
{
@ -311,7 +311,7 @@ namespace std::net
sockaddr_in addr;
socklen_t size = sizeof(sockaddr_in);
if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0)
return 0; // invalid port
throw std::runtime_error("Invalid port");
return ntohs(addr.sin_port);
}

View File

@ -43,8 +43,6 @@ namespace std::net
return new TcpClient(connectionSocket.release());
}
}
else if (hasZeroSleepTime)
std::this_thread::sleep_for(std::chrono::milliseconds(0));
}
else
std::this_thread::sleep_for(std::chrono::milliseconds(m_sleepTime));

View File

@ -75,7 +75,7 @@ namespace std::net
return result;
}
static ParseResult<Authority> parseAuthority(char const* str)
static ParseResult<Authority> ParseAuthority(char const* str)
{
ParseResult<Authority> result
{
@ -99,7 +99,7 @@ namespace std::net
return result;
}
static ParseResult<std::string> parsePath(char const* str)
static ParseResult<std::string> ParsePath(char const* str)
{
// Return query/frag as part of path for now
ParseResult<std::string> result = ParseWhile(str, [](char ch)
@ -121,8 +121,8 @@ namespace std::net
Uri uri;
auto scheme = ParseScheme(str);
auto authority = parseAuthority(scheme.ch);
auto path = parsePath(authority.ch);
auto authority = ParseAuthority(scheme.ch);
auto path = ParsePath(authority.ch);
uri.SetScheme(scheme.value);
uri.SetAuthority(authority.value);

29
src/VoidNet/Util.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "Util.hpp"
std::vector<std::string> std::net::Split(const std::string& str, const std::string& delimiter)
{
std::vector<std::string> splited;
if (str.empty() && delimiter.empty())
return std::vector<std::string>();
std::string::size_type lastPos = str.find_first_not_of(delimiter, 0);
std::string::size_type pos = str.find_first_of(delimiter, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
splited.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiter, pos);
pos = str.find_first_of(delimiter, lastPos);
}
return splited;
}
sockaddr_in std::net::CreateAddress(uint32_t address, uint16_t port)
{
sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = htonl(address);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
return addr;
}