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 -1 is server / valid message if other parameters are valid as well
id -2 is invalid network message id -2 is invalid network message
NetworkMessage: subject 1 is reserved for handshake validation

View File

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

View File

@ -9,9 +9,6 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\include\HLAPI\BitConverter.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp"> <ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
@ -45,6 +42,9 @@
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp"> <ClInclude Include="..\..\include\HLAPI\TcpServer.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\HLAPI\main.cpp"> <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 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 IPUtil class for other os's
maybe i should implement error codes and exceptions
plugin system - idk how im going to implement it yet plugin system - idk how im going to implement it yet
maybe i should use virtual methods for the server and client, maybe... 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\Headers.cpp" />
<ClCompile Include="..\..\src\VoidNet\Http.cpp" /> <ClCompile Include="..\..\src\VoidNet\Http.cpp" />
<ClCompile Include="..\..\src\VoidNet\IPAddress.cpp" /> <ClCompile Include="..\..\src\VoidNet\IPAddress.cpp" />
<ClCompile Include="..\..\src\VoidNet\Parse.cpp" />
<ClCompile Include="..\..\src\VoidNet\Request.cpp" /> <ClCompile Include="..\..\src\VoidNet\Request.cpp" />
<ClCompile Include="..\..\src\VoidNet\Response.cpp" /> <ClCompile Include="..\..\src\VoidNet\Response.cpp" />
<ClCompile Include="..\..\src\VoidNet\SecureSocket.cpp" /> <ClCompile Include="..\..\src\VoidNet\SecureSocket.cpp" />
@ -59,6 +58,7 @@
<ClCompile Include="..\..\src\VoidNet\TcpSocketBuilder.cpp" /> <ClCompile Include="..\..\src\VoidNet\TcpSocketBuilder.cpp" />
<ClCompile Include="..\..\src\VoidNet\UdpSocket.cpp" /> <ClCompile Include="..\..\src\VoidNet\UdpSocket.cpp" />
<ClCompile Include="..\..\src\VoidNet\Uri.cpp" /> <ClCompile Include="..\..\src\VoidNet\Uri.cpp" />
<ClCompile Include="..\..\src\VoidNet\Util.cpp" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{5172321E-CCB0-4A77-9F3D-FAAF0084F434}</ProjectGuid> <ProjectGuid>{5172321E-CCB0-4A77-9F3D-FAAF0084F434}</ProjectGuid>

View File

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

View File

@ -9,7 +9,7 @@
namespace std namespace std
{ {
class BitConverter class ByteConverter
{ {
public: public:
template<typename T> template<typename T>
@ -24,7 +24,7 @@ namespace std
inline static T FromBytes(uint8_t *data) inline static T FromBytes(uint8_t *data)
{ {
if (!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; T value;
memcpy(&value, data, sizeof(T)); memcpy(&value, data, sizeof(T));
return value; return value;

View File

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

View File

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

View File

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

View File

@ -29,11 +29,6 @@
#define SOCKET int #define SOCKET int
#define closesocket close #define closesocket close
int closesocket(SOCKET soc)
{
return close(soc);
}
#endif #endif
#define DEFAULT_SERVER_PORT 61250 #define DEFAULT_SERVER_PORT 61250

View File

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

View File

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

View File

@ -7,31 +7,6 @@
namespace std::net namespace std::net
{ {
inline static std::vector<std::string> Split(const std::string &str, const std::string &delimiter) static std::vector<std::string> Split(const std::string& str, const std::string& delimiter);
{ static sockaddr_in CreateAddress(uint32_t address, uint16_t port);
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;
}
} }

View File

@ -29,12 +29,12 @@ namespace std::net
NetworkHeader header; NetworkHeader header;
header.Size = 13 + sizeOfNetHeader + m_dataSize; 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); memcpy(bytes, &header, sizeOfNetHeader);
uint8_t *sender = BitConverter::ToBytes<uint32_t>(m_senderID); // 4 uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
uint8_t *destination = BitConverter::ToBytes<uint32_t>(m_destinationID); // 4 uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = BitConverter::ToBytes<uint32_t>(m_tag); // 4 uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
memcpy(bytes + sizeOfNetHeader, sender, 4); memcpy(bytes + sizeOfNetHeader, sender, 4);
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode; 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) Server::Server(uint32_t max_connections, uint16_t port)
{ {
m_tcpServer = std::make_shared<std::net::TcpServer>(max_connections, 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() void Server::Start()

View File

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

View File

@ -1,6 +1,6 @@
#include "Init.hpp" #include "Init.hpp"
#include "Server.hpp" #include "Server.hpp"
#include "BitConverter.hpp" #include "ByteConverter.hpp"
#include "TcpClient.hpp" #include "TcpClient.hpp"
#include "InternalTags.hpp" #include "InternalTags.hpp"
#include "NetworkMessage.hpp" #include "NetworkMessage.hpp"
@ -34,7 +34,7 @@ int main()
message.Deserialize(bytes, data_size); 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) if (message.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id << std::endl; std::cout << id << std::endl;
} }
@ -50,7 +50,7 @@ int main()
message2.Deserialize(bytes2, data_size); 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) if (message2.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id2 << std::endl; 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()) 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 writeState = HasState(SocketParam::CanWrite);
SocketReturn readState = HasState(SocketParam::CanRead, std::chrono::milliseconds(1)); SocketReturn readState = HasState(SocketParam::CanRead);
if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes) if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes)
{ {
@ -311,7 +311,7 @@ namespace std::net
sockaddr_in addr; sockaddr_in addr;
socklen_t size = sizeof(sockaddr_in); socklen_t size = sizeof(sockaddr_in);
if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0) if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0)
return 0; // invalid port throw std::runtime_error("Invalid port");
return ntohs(addr.sin_port); return ntohs(addr.sin_port);
} }

View File

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

View File

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