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

@ -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;
}