Windows 服务未在 ASIO 阻塞套接字中停止 [英] Windows service not stoping in ASIO blocking socket
问题描述
我有一个 Windows 服务,改编自
在执行简单任务时正常启动/停止,例如将 while 循环写入文本文件,如本示例中所示
<预><代码>#include这会产生日志输出
2020-09-05,14:18:03 服务启动待定...22020-09-05,14:18:03 服务运行...42020-09-05,14:18:20 服务停止挂起状态...42020-09-05,14:18:21 服务停止待定...32020-09-05,14:18:21 服务停止...1
但是,当使用服务作为阻塞套接字上的 ASIO 服务器时,当请求停止服务时,它永远不会到达停止状态,只有挂起停止状态
缩写服务器代码
<预><代码>asio::io_service io_service;tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port_num));而 (WaitForSingleObject(service_stop_event, 1000) != WAIT_OBJECT_0){tcp::socket 袜子(io_service);接受者.接受(袜子);///阻塞调用}服务代码功能
////////////////////////////////////////////////////////////////////////////////////////////////////句柄///////////////////////////////////////////////////////////////////////////////////////////////////外部 SERVICE_STATUS_HANDLE 服务句柄;外部句柄 service_stop_event;extern DWORD current_state;/////////////////////////////////////////////////////////////////////////////////////////////////////服务处理程序//处理停止服务////////////////////////////////////////////////////////////////////////////////////////////////////VOID WINAPI service_handler(DWORD 代码){开关(代码){案例 SERVICE_CONTROL_SHUTDOWN:案例SERVICE_CONTROL_STOP:events::log(服务停止挂起状态..." + std::to_string(current_state));报告状态(SERVICE_STOP_PENDING);SetEvent(service_stop_event);休息;默认:events::log(服务运行状态..." + std::to_string(current_state));报告状态(当前状态);休息;}返回;}/////////////////////////////////////////////////////////////////////////////////////////////////////report_status///////////////////////////////////////////////////////////////////////////////////////////////////void report_status(DWORD 状态){当前状态 = 状态;SERVICE_STATUS service_status = {SERVICE_WIN32_OWN_PROCESS,当前状态,状态 == SERVICE_START_PENDING ?0 : SERVICE_ACCEPT_STOP |SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,0,0,0,};如果 (SetServiceStatus(service_handle, &service_status) == FALSE){返回;}}
通过发送dummy"解决方案服务器上的自我请求,解除阻塞 acceptor.accept(sock);
SetEvent(service_stop_event);发送关闭(端口);
关键时刻在哪里
<预><代码>/////////////////////////////////////////////////////////////////////////////////////////////////////临时客户端连接到本地本地主机上的self以解除对acceptor.accept()的阻塞///////////////////////////////////////////////////////////////////////////////////////////////////void http::send_close(const std::string& port_num){asio::io_service io_service;asio::ip::tcp::resolver 解析器(io_service);asio::ip::tcp::socket socket(io_service);asio::connect(socket, resolver.resolve(127.0.0.1", port_num));std::string request("GET/stop_service HTTP/1.1\r\n\r\n");asio::write(socket, asio::buffer(request, request.size()));socket.shutdown(asio::ip::tcp::socket::shutdown_both);socket.close();events::log("sent GET/stop_service");}I have a Windows service , adapted from
that starts/stops normally when doing a simple task, like writing in a while loop to a text file, like in this sample
#include <windows.h>
#include <string>
#include "service.hh"
#include "log.hh"
LPSTR service_name = "_service_test";
std::string log_program_name("M:\\" + std::string(service_name));
VOID WINAPI service_main(DWORD, LPTSTR*);
void write_txt_file(const std::string& file_name, const std::string& input);
SERVICE_STATUS_HANDLE service_handle = NULL;
HANDLE service_stop_event = INVALID_HANDLE_VALUE;
DWORD current_state = 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////
//main
//create service in administrator shell with
//sc create _service_test binPath= "L:\net\build\Debug\test_service.exe"
/////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
events::start_log();
//A service process has a SERVICE_TABLE_ENTRY structure for each service that it can start.
//The structure specifies the service name and a pointer to the service main function for that service.
//The main function of a service program calls the StartServiceCtrlDispatcher
//function to connect to the service control manager (SCM)
SERVICE_TABLE_ENTRY service_table[] =
{
{ service_name, (LPSERVICE_MAIN_FUNCTION)service_main },
{ NULL, NULL }
};
if (StartServiceCtrlDispatcher(service_table))
{
return 0;
}
else
{
return 1;
}
}
void WINAPI service_main(DWORD argc, LPTSTR* argv)
{
service_handle = RegisterServiceCtrlHandler(service_name, service_handler);
if (service_handle == NULL)
{
return;
}
report_status(SERVICE_START_PENDING);
events::log("service start pending..." + std::to_string(current_state));
service_stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (service_stop_event == NULL)
{
return;
}
report_status(SERVICE_RUNNING);
events::log("service running..." + std::to_string(current_state));
int i = 0;
while (WaitForSingleObject(service_stop_event, 1000) != WAIT_OBJECT_0)
{
write_txt_file("M:\\log.txt", "writing...#" + std::to_string(i));
Sleep(1000);
i++;
}
report_status(SERVICE_STOP_PENDING);
events::log("service stop pending..." + std::to_string(current_state));
CloseHandle(service_stop_event);
report_status(SERVICE_STOPPED);
events::log("service stopped..." + std::to_string(current_state));
}
void write_txt_file(const std::string& file_name, const std::string& input)
{
FILE* f = fopen(file_name.c_str(), "a+");
fprintf(f, "%s\n", input.c_str());
fclose(f);
}
this produces the log output
2020-09-05,14:18:03 service start pending...2
2020-09-05,14:18:03 service running...4
2020-09-05,14:18:20 service stop pending state...4
2020-09-05,14:18:21 service stop pending...3
2020-09-05,14:18:21 service stopped...1
However , when using the service as a ASIO server on a blocking socket, when a service stop is requested , it never reaches the stop state, only the pending stop state
abbreviated server code
asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port_num));
while (WaitForSingleObject(service_stop_event, 1000) != WAIT_OBJECT_0)
{
tcp::socket sock(io_service);
acceptor.accept(sock); /// blocking call
}
service code functions
/////////////////////////////////////////////////////////////////////////////////////////////////////
//handles
/////////////////////////////////////////////////////////////////////////////////////////////////////
extern SERVICE_STATUS_HANDLE service_handle;
extern HANDLE service_stop_event;
extern DWORD current_state;
/////////////////////////////////////////////////////////////////////////////////////////////////////
//service_handler
//handles stop service
/////////////////////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI service_handler(DWORD code)
{
switch (code)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
events::log("service stop pending state..." + std::to_string(current_state));
report_status(SERVICE_STOP_PENDING);
SetEvent(service_stop_event);
break;
default:
events::log("service running state..." + std::to_string(current_state));
report_status(current_state);
break;
}
return;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//report_status
/////////////////////////////////////////////////////////////////////////////////////////////////////
void report_status(DWORD state)
{
current_state = state;
SERVICE_STATUS service_status = {
SERVICE_WIN32_OWN_PROCESS,
current_state,
state == SERVICE_START_PENDING ? 0 : SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN,
NO_ERROR,
0,
0,
0,
};
if (SetServiceStatus(service_handle, &service_status) == FALSE)
{
return;
}
}
Solved by sending a "dummy" self request on the server, that unblocks acceptor.accept(sock);
SetEvent(service_stop_event);
send_close(port);
where the close call is
/////////////////////////////////////////////////////////////////////////////////////////////////////
//temporary client connection to self on localhost to unblock acceptor.accept()
/////////////////////////////////////////////////////////////////////////////////////////////////////
void http::send_close(const std::string& port_num)
{
asio::io_service io_service;
asio::ip::tcp::resolver resolver(io_service);
asio::ip::tcp::socket socket(io_service);
asio::connect(socket, resolver.resolve("127.0.0.1", port_num));
std::string request("GET /stop_service HTTP/1.1\r\n\r\n");
asio::write(socket, asio::buffer(request, request.size()));
socket.shutdown(asio::ip::tcp::socket::shutdown_both);
socket.close();
events::log("sent GET /stop_service");
}
这篇关于Windows 服务未在 ASIO 阻塞套接字中停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!