Код: Выделить всё
void Socket::read(const String &delim) {
boost::asio::async_read_until(_sock, boost::asio::dynamic_buffer(_recBuf), delim,
boost::asio::bind_executor(_readStrand,
[this, delim = delim, self = shared_from_this()] (const boost::system::error_code& ec, const std::size_t bytes) {
// LOGIC FOR HANDLING THE READ ...
Socket::handleMsg();
Socket::read(delim);
}));
}
void Socket::doWrite(std::shared_ptr str) {
asio::async_write(_sock, asio::buffer(*str),
asio::bind_executor(_writeStrand,
[this, str, self = shared_from_this()] (const boost::system::error_code& ec, const std::size_t bytes) {
// LOGIC TO HANDLE WRITE
// WILL EITHER POP NEXT MESSAGE OFF QUEUE OR EXIT
}));
}
Код: Выделить всё
bool Socket::handleMsg(const object::String &msg) {
try {
auto self = shared_from_this();
co_spawn(_asyncHandleMsgStrand, [this, self, m] () -> boost::asio::awaitable {
try {
co_await handleMsgInt(m);
}
catch (const std::exception &e) {
lh.e(fmt::format("{:s}", e.what()));
stop_int();
}
},
[this, self] (std::exception_ptr e) {
if (e) {
_ioc.post([e, self] {
std::rethrow_exception(e);
});
}
});
}
catch (const std::exception &e) {
lh.e(fmt::format("{:s}", e.what()));
stop_int();
return false;
}
return true;
}
Что-то еще, с чем я столкнулся, чего может просто не хватать знаний с моей стороны заключается в том, что после создания сеанса здесь:
Код: Выделить всё
inline static std::shared_ptr create(boost::asio::io_context &ioc, boost::asio::ip::tcp::socket &sock, Server &server) {
return std::shared_ptr(new Socket(ioc, sock, server));
}
Код: Выделить всё
(2024/05/31 18:19:02.775665476) (trace) Socket: this: 0x7fffe8001000
(2024/05/31 18:19:02.775706976) (trace) Server: shared_ptr before adding to sessions set: 0x7fffe8001150, ref: 1
(2024/05/31 18:19:02.775727176) (trace) Server: shared_ptr after adding to sessions: 0x7fffe8001150, ref: 2
(2024/05/31 18:19:02.775739376) (trace) Server: sessions.begin()->get(): 0x7fffe8001150, ref: 2
(2024/05/31 18:19:02.775747376) (trace) Server: shared from this: 0x7fffe8001218, ref: 3
Забыл указать, что класс сокета имеет виртуальное ромбовидное наследование базового класса, который наследуется от Enable_shared_from_this. Я не знаю, изменит ли это поведение.
Подробнее здесь: https://stackoverflow.com/questions/785 ... -it-exists
Мобильная версия