diff --git a/DEV_INFO b/DEV_INFO
index cc71118..eb5d275 100644
--- a/DEV_INFO
+++ b/DEV_INFO
@@ -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
\ No newline at end of file
+id -2 is invalid network message
\ No newline at end of file
diff --git a/HLAPI/HLAPI/HLAPI.vcxproj b/HLAPI/HLAPI/HLAPI.vcxproj
index 625e226..73d79f2 100644
--- a/HLAPI/HLAPI/HLAPI.vcxproj
+++ b/HLAPI/HLAPI/HLAPI.vcxproj
@@ -19,7 +19,7 @@
-
+
diff --git a/HLAPI/HLAPI/HLAPI.vcxproj.filters b/HLAPI/HLAPI/HLAPI.vcxproj.filters
index 60bef89..037db0a 100644
--- a/HLAPI/HLAPI/HLAPI.vcxproj.filters
+++ b/HLAPI/HLAPI/HLAPI.vcxproj.filters
@@ -9,9 +9,6 @@
-
- include
-
include
@@ -45,6 +42,9 @@
include
+
+ include
+
diff --git a/TODO b/TODO
index 3c5137c..470d5c7 100644
--- a/TODO
+++ b/TODO
@@ -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
\ No newline at end of file
+TcpConnectionHandler::AddClient max_connections
+
+Test udp stuff
\ No newline at end of file
diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
index f54eb0a..6fc6191 100644
--- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
+++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
@@ -49,7 +49,6 @@
-
@@ -59,6 +58,7 @@
+
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}
diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters
index a6f7b68..72f2d20 100644
--- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters
+++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters
@@ -90,9 +90,6 @@
src\Http
-
- src\Http
-
src\Http
@@ -123,5 +120,8 @@
src
+
+ src
+
\ No newline at end of file
diff --git a/include/HLAPI/BitConverter.hpp b/include/HLAPI/ByteConverter.hpp
similarity index 83%
rename from include/HLAPI/BitConverter.hpp
rename to include/HLAPI/ByteConverter.hpp
index 9984841..1957da2 100644
--- a/include/HLAPI/BitConverter.hpp
+++ b/include/HLAPI/ByteConverter.hpp
@@ -9,7 +9,7 @@
namespace std
{
- class BitConverter
+ class ByteConverter
{
public:
template
@@ -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;
diff --git a/include/HLAPI/NetworkMessage.hpp b/include/HLAPI/NetworkMessage.hpp
index a4459da..7b91a6b 100644
--- a/include/HLAPI/NetworkMessage.hpp
+++ b/include/HLAPI/NetworkMessage.hpp
@@ -1,7 +1,7 @@
#pragma once
#include "NetworkHeader.hpp"
-#include "BitConverter.hpp"
+#include "ByteConverter.hpp"
#include
#include
@@ -68,9 +68,9 @@ namespace std::net
uint8_t *bytes = new uint8_t[header.Size];
memcpy(bytes, &header, sizeOfNetHeader);
- uint8_t *sender = BitConverter::ToBytes(m_senderID); // 4
- uint8_t *destination = BitConverter::ToBytes(m_destinationID); // 4
- uint8_t *tag = BitConverter::ToBytes(m_tag); // 4
+ uint8_t *sender = ByteConverter::ToBytes(m_senderID); // 4
+ uint8_t *destination = ByteConverter::ToBytes(m_destinationID); // 4
+ uint8_t *tag = ByteConverter::ToBytes(m_tag); // 4
memcpy(bytes + sizeOfNetHeader, sender, 4);
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;
diff --git a/include/HLAPI/Server.hpp b/include/HLAPI/Server.hpp
index fc644d0..9735cbc 100644
--- a/include/HLAPI/Server.hpp
+++ b/include/HLAPI/Server.hpp
@@ -20,7 +20,5 @@ namespace std::net
private:
std::shared_ptr m_tcpServer;
-
- std::shared_ptr m_queue;
};
}
\ No newline at end of file
diff --git a/include/HLAPI/TcpConnectionHandler.hpp b/include/HLAPI/TcpConnectionHandler.hpp
index aa666be..624179d 100644
--- a/include/HLAPI/TcpConnectionHandler.hpp
+++ b/include/HLAPI/TcpConnectionHandler.hpp
@@ -7,18 +7,12 @@
#include
#include
-//#include
-
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> 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 m_queue;
std::shared_ptr m_listenerPtr;
+
+ std::vector poll_fds;
};
}
\ No newline at end of file
diff --git a/include/VoidNet/Net.hpp b/include/VoidNet/Net.hpp
index e8822f3..8bc8e1c 100644
--- a/include/VoidNet/Net.hpp
+++ b/include/VoidNet/Net.hpp
@@ -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
diff --git a/include/VoidNet/Response.hpp b/include/VoidNet/Response.hpp
index 966e5e0..a7c6a20 100644
--- a/include/VoidNet/Response.hpp
+++ b/include/VoidNet/Response.hpp
@@ -21,7 +21,7 @@ namespace std::net
return m_status;
}
- const std::string& daGetDatata() const
+ const std::string& GetData() const
{
return m_data;
}
diff --git a/include/VoidNet/TcpListener.hpp b/include/VoidNet/TcpListener.hpp
index 40fc198..f586974 100644
--- a/include/VoidNet/TcpListener.hpp
+++ b/include/VoidNet/TcpListener.hpp
@@ -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();
diff --git a/include/VoidNet/Util.hpp b/include/VoidNet/Util.hpp
index df27cd5..cf710b2 100644
--- a/include/VoidNet/Util.hpp
+++ b/include/VoidNet/Util.hpp
@@ -7,31 +7,6 @@
namespace std::net
{
- inline static std::vector Split(const std::string &str, const std::string &delimiter)
- {
- std::vector splited;
- if (str.empty() && delimiter.empty())
- return std::vector();
- 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 Split(const std::string& str, const std::string& delimiter);
+ static sockaddr_in CreateAddress(uint32_t address, uint16_t port);
}
\ No newline at end of file
diff --git a/src/HLAPI/NetworkMessage.cpp b/src/HLAPI/NetworkMessage.cpp
index 42c968c..2132b3a 100644
--- a/src/HLAPI/NetworkMessage.cpp
+++ b/src/HLAPI/NetworkMessage.cpp
@@ -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(m_senderID); // 4
- uint8_t *destination = BitConverter::ToBytes(m_destinationID); // 4
- uint8_t *tag = BitConverter::ToBytes(m_tag); // 4
+ uint8_t *sender = ByteConverter::ToBytes(m_senderID); // 4
+ uint8_t *destination = ByteConverter::ToBytes(m_destinationID); // 4
+ uint8_t *tag = ByteConverter::ToBytes(m_tag); // 4
memcpy(bytes + sizeOfNetHeader, sender, 4);
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;
diff --git a/src/HLAPI/Server.cpp b/src/HLAPI/Server.cpp
index 99d3888..99c901a 100644
--- a/src/HLAPI/Server.cpp
+++ b/src/HLAPI/Server.cpp
@@ -9,8 +9,6 @@ namespace std::net
Server::Server(uint32_t max_connections, uint16_t port)
{
m_tcpServer = std::make_shared(max_connections, port);
- m_queue = std::make_shared();
- //m_tcpServer->m_connectionHandler->m_queue = m_queue;
}
void Server::Start()
diff --git a/src/HLAPI/TcpConnectionHandler.cpp b/src/HLAPI/TcpConnectionHandler.cpp
index 52b7394..7d965b8 100644
--- a/src/HLAPI/TcpConnectionHandler.cpp
+++ b/src/HLAPI/TcpConnectionHandler.cpp
@@ -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(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 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 data(msg.SerializeData(size));
-
if (msg.GetDistributionMode() == DistributionMode::Others)
{
m_listMutex.lock();
@@ -202,8 +191,7 @@ namespace std::net
std::shared_ptr 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 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 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 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 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));
- }
}
}
\ No newline at end of file
diff --git a/src/HLAPI/main.cpp b/src/HLAPI/main.cpp
index ac90fc3..192f298 100644
--- a/src/HLAPI/main.cpp
+++ b/src/HLAPI/main.cpp
@@ -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((uint8_t*)(message.GetData()));
+ uint32_t id = std::ByteConverter::FromBytes((uint8_t*)(message.GetData()));
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((uint8_t*)(message2.GetData()));
+ uint32_t id2 = std::ByteConverter::FromBytes((uint8_t*)(message2.GetData()));
if (message2.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id2 << std::endl;
}
diff --git a/src/VoidNet/Parse.cpp b/src/VoidNet/Parse.cpp
deleted file mode 100644
index e69de29..0000000
diff --git a/src/VoidNet/Socket.cpp b/src/VoidNet/Socket.cpp
index 838ad30..2d6e1d8 100644
--- a/src/VoidNet/Socket.cpp
+++ b/src/VoidNet/Socket.cpp
@@ -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);
}
diff --git a/src/VoidNet/TcpListener.cpp b/src/VoidNet/TcpListener.cpp
index 869ad0a..f49c9af 100644
--- a/src/VoidNet/TcpListener.cpp
+++ b/src/VoidNet/TcpListener.cpp
@@ -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));
diff --git a/src/VoidNet/Uri.cpp b/src/VoidNet/Uri.cpp
index df19bf7..6d8e88d 100644
--- a/src/VoidNet/Uri.cpp
+++ b/src/VoidNet/Uri.cpp
@@ -75,7 +75,7 @@ namespace std::net
return result;
}
- static ParseResult parseAuthority(char const* str)
+ static ParseResult ParseAuthority(char const* str)
{
ParseResult result
{
@@ -99,7 +99,7 @@ namespace std::net
return result;
}
- static ParseResult parsePath(char const* str)
+ static ParseResult ParsePath(char const* str)
{
// Return query/frag as part of path for now
ParseResult 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);
diff --git a/src/VoidNet/Util.cpp b/src/VoidNet/Util.cpp
new file mode 100644
index 0000000..d0a8c6f
--- /dev/null
+++ b/src/VoidNet/Util.cpp
@@ -0,0 +1,29 @@
+#include "Util.hpp"
+
+std::vector std::net::Split(const std::string& str, const std::string& delimiter)
+{
+ std::vector splited;
+ if (str.empty() && delimiter.empty())
+ return std::vector();
+ 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;
+}