From 9243710ca372de26823c2225c7b46b072458c671 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 28 五月 2021 17:18:33 +0800
Subject: [PATCH] tcp proxy requests, need more test.

---
 box/io_service.cpp |   33 ++++++++-------------------------
 1 files changed, 8 insertions(+), 25 deletions(-)

diff --git a/box/io_service.cpp b/box/io_service.cpp
index 1d531e0..5640d50 100644
--- a/box/io_service.cpp
+++ b/box/io_service.cpp
@@ -16,33 +16,16 @@
  * =====================================================================================
  */
 #include "io_service.h"
-#include <chrono>
-using namespace std::chrono_literals;
 
-bool IoService::Start()
+IoService::IoService() :
+    guard_(io_.get_executor())
 {
-	Stop();
-	bool cur = false;
-	if (!run_.compare_exchange_strong(cur, true)) {
-		return false;
-	}
-
-	auto proc = [this]() {
-		while (run_) {
-			io_.run_one_for(100ms);
-		}
-		OnStop();
-	};
-	std::thread(proc).swap(worker_);
-	return true;
+	std::thread([this]() { io_.run(); }).swap(worker_);
 }
-
-void IoService::Stop()
+IoService::~IoService()
 {
-	bool cur = true;
-	if (run_.compare_exchange_strong(cur, false)) {
-		if (worker_.joinable()) {
-			worker_.join();
-		}
-	}
+	guard_.reset();
+	io_.stop(); // normally not needed, but make sure run() exits.
+	if (worker_.joinable())
+		worker_.join();
 }

--
Gitblit v1.8.0