Trying to implement the sender but its not working the way i want
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
#include "Tags.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#undef SendMessage
|
||||
|
||||
@ -45,12 +46,12 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
||||
return VOID_SUCCESS;
|
||||
}
|
||||
|
||||
TcpClient::TcpClient() : port(default_port), thread_pool(50)
|
||||
TcpClient::TcpClient() : port(default_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;
|
||||
}
|
||||
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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