From 3c7339498c5a47e912f6e6009c197291acd7e1fd Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 03 六月 2021 11:27:39 +0800
Subject: [PATCH] change mq shm name prefix.
---
box/tcp_proxy.cpp | 66 +++++----------------------------
1 files changed, 10 insertions(+), 56 deletions(-)
diff --git a/box/tcp_proxy.cpp b/box/tcp_proxy.cpp
index 2e95a3b..b4ec497 100644
--- a/box/tcp_proxy.cpp
+++ b/box/tcp_proxy.cpp
@@ -16,64 +16,18 @@
* =====================================================================================
*/
#include "tcp_proxy.h"
-#include "defs.h"
-#include "shm_socket.h"
#include "tcp_connection.h"
-TcpProxy::TcpProxy() :
- run_(false) {}
-
-TcpProxy::~TcpProxy() {}
-
-bool TcpProxy::Start(bhome_shm::SharedMemory &shm)
-{
- Stop();
- bool cur = false;
- if (!run_.compare_exchange_strong(cur, true)) { return false; }
-
- auto &mq = GetCenterInfo(shm)->mq_tcp_proxy_;
- local_.reset(new ShmSocket(mq.offset_, shm, mq.id_));
- auto localProc = [this](ShmSocket &sock, MsgI &msg, BHMsgHead &head) {
- auto &dest = head.dest();
- if (dest.ip().empty() || dest.port() == 0) { return; }
- bool r = Send(dest.ip(), dest.port(), msg.content());
- // TODO check send fail.
- };
- local_->Start(1, localProc);
-
- auto proxyProc = [this]() {
- while (run_) {
- io_context_.run_one_for(std::chrono::milliseconds(100));
- }
- };
- return true;
-}
-
-void TcpProxy::Stop()
-{
- local_.reset();
-
- bool cur = true;
- if (run_.compare_exchange_strong(cur, false)) {
- if (worker_.joinable()) {
- worker_.join();
- }
- }
-}
-
-bool TcpProxy::Send(const std::string &ip, int port, std::string &&content)
+bool TcpProxy::Request(const std::string &ip, int port, std::string &&content, ReplyCB const &cb)
{
if (content.empty()) { return false; }
-
- tcp::endpoint dest(ip::address::from_string(ip), port);
- TcpRequest1::Create(io_context_, dest, std::move(content));
-
- // char tag[sizeof(kBHTcpServerTag)] = {0};
-
- // int n = read(sock, tag, sizeof(tag));
- // if (n == sizeof(tag) && memcmp(tag, &kBHTcpServerTag, sizeof(tag)) == 0) {
- // send(sock, content.data(), content.size(), 0);
- // connections_[addr].io_info_.h_ = [this, sock](int events) { OnReply(sock); };
- // // success
- // }
+ try {
+ tcp::endpoint dest(ip::address::from_string(ip), port);
+ TcpRequest1::Create(io_, dest, std::move(content), cb);
+ LOG_TRACE() << "tcp request start " << ip << ':' << port;
+ return true;
+ } catch (std::exception &e) {
+ LOG_ERROR() << "proxy request exception: " << e.what();
+ return false;
+ }
}
--
Gitblit v1.8.0