This commit is contained in:
aozhiwei 2023-05-28 14:46:56 +08:00
parent 50fd07946c
commit 824e4dbe5d
4 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,39 @@
#include "precompile.h"
#include "iomgr.h"
static const int IC_Max = 4;
void IoMgr::Init()
{
for (int i = 0; i < IC_Max; ++i) {
io_contexts_.push_back(std::vector<std::shared_ptr<asio::io_context>>());
auto& contexts = io_contexts_.at(i);
contexts.push_back(std::make_shared<asio::io_context>());
new std::thread(&IoMgr::WorkerThreadProc, this, contexts.at(0));
}
}
void IoMgr::UnInit()
{
}
std::shared_ptr<asio::io_context> IoMgr::GetIoContext(int type)
{
if (type >= 0 && type < io_contexts_.size()) {
auto& contexts = io_contexts_.at(type);
if (!contexts.empty()) {
return contexts.at(rand() % contexts.size());
}
}
return nullptr;
}
void IoMgr::WorkerThreadProc(std::shared_ptr<asio::io_context> io_context)
{
while (true) {
io_context->run();
io_context->reset();
}
}

View File

@ -0,0 +1,26 @@
#pragma once
#include <a8/singleton.h>
#include <asio.hpp>
class IoMgr : public a8::Singleton<IoMgr>
{
private:
IoMgr() {};
friend class a8::Singleton<IoMgr>;
public:
void Init();
void UnInit();
std::shared_ptr<asio::io_context> GetIoContext(int type);
private:
void WorkerThreadProc(std::shared_ptr<asio::io_context> io_context);
private:
std::vector<std::vector<std::shared_ptr<asio::io_context>>> io_contexts_;
};

View File

@ -1,5 +1,7 @@
#include "precompile.h"
#include <a8/websocketclient.h>
#include <f8/udplog.h>
#include <f8/utils.h>
#include <f8/msgqueue.h>
@ -9,6 +11,7 @@
#include "playermgr.h"
#include "player.h"
#include "httpproxy.h"
#include "iomgr.h"
void PlayerMgr::Init()
{
@ -33,6 +36,12 @@ void PlayerMgr::Init()
abort();
}
});
auto web_socket = std::make_shared<a8::WebSocketClient>
(
*IoMgr::Instance()->GetIoContext(0),
"192.168.100.21",
7601
);
}
void PlayerMgr::UnInit()

2
third_party/a8 vendored

@ -1 +1 @@
Subproject commit 7f568dcbd9a0134fd98b59e20768086c87cda483
Subproject commit 33a65a4cf309412b9adcce44353293f06cd30010