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

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