Trying to implement the sender but its not working the way i want
This commit is contained in:
3
TODO
3
TODO
@ -1,2 +1,3 @@
|
|||||||
initialization code for other operating systems - currently VoidNet only supports windows
|
initialization code for other operating systems - currently VoidNet only supports windows
|
||||||
initialization code for tcp client for other operating systems - "
|
initialization code for tcp client for other operating systems - "
|
||||||
|
implement thread pool - god dammit so many tries and it doesnt work
|
||||||
Binary file not shown.
BIN
TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog
Normal file
BIN
TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog
Normal file
Binary file not shown.
BIN
TestsVS/Tests/Release/Tests.tlog/CL.write.1.tlog
Normal file
BIN
TestsVS/Tests/Release/Tests.tlog/CL.write.1.tlog
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -117,6 +117,7 @@
|
|||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<AdditionalIncludeDirectories>F:\VoidNet\include;</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>F:\VoidNet\include;</AdditionalIncludeDirectories>
|
||||||
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -27,7 +27,6 @@
|
|||||||
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
|
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
|
||||||
<ClInclude Include="..\..\include\Serializer.hpp" />
|
<ClInclude Include="..\..\include\Serializer.hpp" />
|
||||||
<ClInclude Include="..\..\include\TcpClient.hpp" />
|
<ClInclude Include="..\..\include\TcpClient.hpp" />
|
||||||
<ClInclude Include="..\..\include\ThreadPool.hpp" />
|
|
||||||
<ClInclude Include="..\..\include\Utility.hpp" />
|
<ClInclude Include="..\..\include\Utility.hpp" />
|
||||||
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
|
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
|
||||||
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
|
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
|
||||||
@ -42,7 +41,6 @@
|
|||||||
<ClCompile Include="..\..\src\Serializer.cpp" />
|
<ClCompile Include="..\..\src\Serializer.cpp" />
|
||||||
<ClCompile Include="..\..\src\TcpClient.cpp" />
|
<ClCompile Include="..\..\src\TcpClient.cpp" />
|
||||||
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
|
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
|
||||||
<ClCompile Include="..\..\src\ThreadPool.cpp" />
|
|
||||||
<ClCompile Include="..\..\src\Utility.cpp" />
|
<ClCompile Include="..\..\src\Utility.cpp" />
|
||||||
<ClCompile Include="..\..\src\VoidNetClient.cpp" />
|
<ClCompile Include="..\..\src\VoidNetClient.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -45,9 +45,6 @@
|
|||||||
<ClInclude Include="..\..\src\Tags.hpp">
|
<ClInclude Include="..\..\src\Tags.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\ThreadPool.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\src\Init.cpp">
|
<ClCompile Include="..\..\src\Init.cpp">
|
||||||
@ -80,9 +77,6 @@
|
|||||||
<ClCompile Include="..\..\src\VoidNetClient.cpp">
|
<ClCompile Include="..\..\src\VoidNetClient.cpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\src\ThreadPool.cpp">
|
|
||||||
<Filter>src</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Text Include="..\..\TODO" />
|
<Text Include="..\..\TODO" />
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
#include "ThreadPool.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -30,17 +30,20 @@ public:
|
|||||||
|
|
||||||
NetworkBuffer ReceiveDataArray();
|
NetworkBuffer ReceiveDataArray();
|
||||||
const NetworkMessage &ReceiveData();
|
const NetworkMessage &ReceiveData();
|
||||||
|
void StartSender();
|
||||||
VoidCode SendMessage(const NetworkMessage &message);
|
VoidCode SendMessage(const NetworkMessage &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static VoidCode SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
|
void SendNetworkMessage(const NetworkMessage &message);
|
||||||
|
void SendNetworkMessageNow(const NetworkMessage &message);
|
||||||
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
||||||
|
|
||||||
std::string ip;
|
std::string ip;
|
||||||
uint16 port = 0;
|
uint16 port = 0;
|
||||||
bool initialized;
|
bool initialized = false;
|
||||||
|
bool run_sender = false;
|
||||||
|
|
||||||
ThreadPool thread_pool;
|
std::vector<NetworkMessage> network_message_queue;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
SOCKET tcp_socket = INVALID_SOCKET;
|
SOCKET tcp_socket = INVALID_SOCKET;
|
||||||
|
|||||||
@ -1,33 +0,0 @@
|
|||||||
#ifndef THREAD_POOL_HPP
|
|
||||||
#define THREAD_POOL_HPP
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma once
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <queue>
|
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include <condition_variable>
|
|
||||||
|
|
||||||
class ThreadPool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ThreadPool(int threads);
|
|
||||||
~ThreadPool();
|
|
||||||
void Enqueue(std::function<void()> f);
|
|
||||||
void Shutdown();
|
|
||||||
const ThreadPool &operator=(ThreadPool &pool);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<std::thread> threadPool;
|
|
||||||
std::queue<std::function<void()>> tasks;
|
|
||||||
std::mutex tasksMutex;
|
|
||||||
std::condition_variable condition;
|
|
||||||
bool terminate;
|
|
||||||
bool stopped;
|
|
||||||
void Invoke();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -11,7 +11,6 @@
|
|||||||
#include "Callbacks.hpp"
|
#include "Callbacks.hpp"
|
||||||
#include "NetworkBuffer.hpp"
|
#include "NetworkBuffer.hpp"
|
||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
#include "ThreadPool.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include "Tags.hpp"
|
#include "Tags.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#undef SendMessage
|
#undef SendMessage
|
||||||
|
|
||||||
@ -45,12 +46,12 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
|||||||
return VOID_SUCCESS;
|
return VOID_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient::TcpClient() : port(default_port), thread_pool(50)
|
TcpClient::TcpClient() : port(default_port)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient::TcpClient(const std::string &ip, uint16 port) :
|
TcpClient::TcpClient(const std::string &ip, uint16 port) :
|
||||||
ip(ip), port(port), thread_pool(50)
|
ip(ip), port(port)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,16 +135,26 @@ const NetworkMessage &TcpClient::ReceiveData()
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
VoidCode TcpClient::SendNetworkMessage(const NetworkMessage &message, TcpClient *client)
|
void TcpClient::SendNetworkMessage(const NetworkMessage &message)
|
||||||
|
{
|
||||||
|
network_message_queue.emplace_back(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpClient::SendNetworkMessageNow(const NetworkMessage &message)
|
||||||
{
|
{
|
||||||
NetworkBuffer buffer = message.EncodeMessage(message);
|
NetworkBuffer buffer = message.EncodeMessage(message);
|
||||||
int32 sent_bytes = send(client->tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0);
|
int32 sent_bytes = send(tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0);
|
||||||
|
if (sent_bytes != buffer.body_size)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VoidCode TcpClient::SendMessage(const NetworkMessage &message)
|
VoidCode TcpClient::SendMessage(const NetworkMessage &message)
|
||||||
{
|
{
|
||||||
thread_pool.Enqueue([]()
|
}
|
||||||
{
|
|
||||||
//SendNetworkMessage(message, this);
|
void TcpClient::StartSender()
|
||||||
});
|
{
|
||||||
|
//std::thread thread = std::thread(&SendNetworkMessageNow, message);
|
||||||
}
|
}
|
||||||
@ -1,57 +0,0 @@
|
|||||||
#include "ThreadPool.hpp"
|
|
||||||
|
|
||||||
ThreadPool::ThreadPool(int threads) :
|
|
||||||
terminate(false),
|
|
||||||
stopped(false)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < threads; i++)
|
|
||||||
threadPool.emplace_back(std::thread(&ThreadPool::Invoke, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ThreadPool::Enqueue(std::function<void()> f)
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(tasksMutex);
|
|
||||||
tasks.push(f);
|
|
||||||
condition.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ThreadPool::Invoke() {
|
|
||||||
|
|
||||||
std::function<void()> task;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(tasksMutex);
|
|
||||||
condition.wait(lock, [this] { return !tasks.empty() || terminate; });
|
|
||||||
if (terminate && tasks.empty())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
task = tasks.front();
|
|
||||||
tasks.pop();
|
|
||||||
task();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ThreadPool::Shutdown()
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(tasksMutex);
|
|
||||||
terminate = true;
|
|
||||||
condition.notify_all();
|
|
||||||
|
|
||||||
for (std::thread &thread : threadPool)
|
|
||||||
thread.join();
|
|
||||||
|
|
||||||
threadPool.empty();
|
|
||||||
stopped = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ThreadPool &ThreadPool::operator=(ThreadPool &pool)
|
|
||||||
{
|
|
||||||
return pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
ThreadPool::~ThreadPool()
|
|
||||||
{
|
|
||||||
if (!stopped)
|
|
||||||
Shutdown();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user