From 31b4c9d083b00487c31bbb196f060f78c710a238 Mon Sep 17 00:00:00 2001 From: kedixa <1204837541@qq.com> Date: Fri, 24 Dec 2021 20:05:19 +0800 Subject: [PATCH] add get_peer_addr for network task --- doc/http.md | 2 ++ doc/mysql.md | 2 ++ doc/redis.md | 2 ++ src/http_types.cc | 1 + src/mysql_types.cc | 1 + src/network_types.h | 23 +++++++++++++++++++++++ src/redis_types.cc | 1 + workflow | 2 +- 8 files changed, 33 insertions(+), 1 deletion(-) diff --git a/doc/http.md b/doc/http.md index 42aa592..008fce0 100644 --- a/doc/http.md +++ b/doc/http.md @@ -77,6 +77,8 @@ - set_send_timeout(int) -> None - set_receive_timeout(int) -> None - set_keep_alive(int) -> None +- get_peer_addr() -> tuple(str, int) + - 返回元组(ip, port),若获取失败则ip为空字符串 - set_callback(Callable[[wf.HttpTask], None]) -> None - set_user_data(object) -> None - get_user_data() -> object diff --git a/doc/mysql.md b/doc/mysql.md index 8514f0c..2df4e40 100644 --- a/doc/mysql.md +++ b/doc/mysql.md @@ -218,6 +218,8 @@ mysqls://localhost/db1?character\_set=big5 - set_send_timeout(int) -> None - set_receive_timeout(int) -> None - set_keep_alive(int) -> None +- get_peer_addr() -> tuple(str, int) + - 返回元组(ip, port),若获取失败则ip为空字符串 - set_callback(Callable[[wf.MySQLTask], None]) -> None - set_user_data(object) -> None - get_user_data() -> object diff --git a/doc/redis.md b/doc/redis.md index c6e0547..9bb8c98 100644 --- a/doc/redis.md +++ b/doc/redis.md @@ -89,6 +89,8 @@ - set_send_timeout(int) -> None - set_receive_timeout(int) -> None - set_keep_alive(int) -> None +- get_peer_addr() -> tuple(str, int) + - 返回元组(ip, port),若获取失败则ip为空字符串 - set_callback(Callable[[wf.RedisTask], None]) -> None - set_user_data(object) -> None - get_user_data() -> object diff --git a/src/http_types.cc b/src/http_types.cc index d592e36..35351b8 100644 --- a/src/http_types.cc +++ b/src/http_types.cc @@ -52,6 +52,7 @@ void init_http_types(py::module_ &wf) { .def("set_send_timeout", &PyWFHttpTask::set_send_timeout) .def("set_receive_timeout", &PyWFHttpTask::set_receive_timeout) .def("set_keep_alive", &PyWFHttpTask::set_keep_alive) + .def("get_peer_addr", &PyWFHttpTask::get_peer_addr) .def("set_callback", &PyWFHttpTask::set_callback) .def("set_user_data", &PyWFHttpTask::set_user_data) .def("get_user_data", &PyWFHttpTask::get_user_data) diff --git a/src/mysql_types.cc b/src/mysql_types.cc index 3d77dfc..e87561e 100644 --- a/src/mysql_types.cc +++ b/src/mysql_types.cc @@ -175,6 +175,7 @@ void init_mysql_types(py::module_ &wf) { .def("set_send_timeout", &PyWFMySQLTask::set_send_timeout) .def("set_receive_timeout", &PyWFMySQLTask::set_receive_timeout) .def("set_keep_alive", &PyWFMySQLTask::set_keep_alive) + .def("get_peer_addr", &PyWFMySQLTask::get_peer_addr) .def("set_callback", &PyWFMySQLTask::set_callback) .def("set_user_data", &PyWFMySQLTask::set_user_data) .def("get_user_data", &PyWFMySQLTask::get_user_data) diff --git a/src/network_types.h b/src/network_types.h index 536f380..b803eb5 100644 --- a/src/network_types.h +++ b/src/network_types.h @@ -1,5 +1,7 @@ #ifndef PYWF_NETWORK_TYPES_H #define PYWF_NETWORK_TYPES_H +#include + #include "common_types.h" #include "workflow/HttpMessage.h" #include "workflow/HttpUtil.h" @@ -47,6 +49,27 @@ class PyWFNetworkTask : public PySubTask { void set_receive_timeout(int t) { this->get()->set_receive_timeout(t); } void set_keep_alive(int t) { this->get()->set_keep_alive(t); } + py::object get_peer_addr() const { + char ip_str[INET6_ADDRSTRLEN + 1] = { 0 }; + struct sockaddr_storage addr; + socklen_t addrlen = sizeof (addr); + uint16_t port = 0; + + if (this->get()->get_peer_addr((struct sockaddr *)&addr, &addrlen) == 0) { + if (addr.ss_family == AF_INET) { + struct sockaddr_in *sin = (struct sockaddr_in *)(&addr); + inet_ntop(AF_INET, &sin->sin_addr, ip_str, addrlen); + port = ntohs(sin->sin_port); + } + else if (addr.ss_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(&addr); + inet_ntop(AF_INET6, &sin6->sin6_addr, ip_str, addrlen); + port = ntohs(sin6->sin6_port); + } + } + return py::make_tuple(py::str(ip_str), py::int_(port)); + } + void set_callback(_py_callback_t cb) { // The deleter will destruct both cb and user_data, // but now we just want to reset cb, so we must clear user_data first, diff --git a/src/redis_types.cc b/src/redis_types.cc index bb106eb..0568297 100644 --- a/src/redis_types.cc +++ b/src/redis_types.cc @@ -138,6 +138,7 @@ void init_redis_types(py::module_ &wf) { .def("set_send_timeout", &PyWFRedisTask::set_send_timeout) .def("set_receive_timeout", &PyWFRedisTask::set_receive_timeout) .def("set_keep_alive", &PyWFRedisTask::set_keep_alive) + .def("get_peer_addr", &PyWFRedisTask::get_peer_addr) .def("set_callback", &PyWFRedisTask::set_callback) .def("set_user_data", &PyWFRedisTask::set_user_data) .def("get_user_data", &PyWFRedisTask::get_user_data) diff --git a/workflow b/workflow index dc07e38..4290fc4 160000 --- a/workflow +++ b/workflow @@ -1 +1 @@ -Subproject commit dc07e388f11880d8429758d877042c31b2c6f17d +Subproject commit 4290fc4c24c20152465a0303051b2220afe8c46b