From 61a36d53e11e704616a3b97f2c473515564a914d Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Sun, 21 Apr 2024 15:28:35 -0700 Subject: [PATCH 1/6] add vscode settings --- .gitignore | 1 - .vscode/settings.json | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 749b288b8..12910296e 100644 --- a/.gitignore +++ b/.gitignore @@ -117,7 +117,6 @@ cmake-build-debug builddir builddir-linux coverage.info -.vscode/settings.json .vscode/launch.json /vs .vscode/tasks.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..34967ac5f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "numbers": "cpp", + "cstring": "cpp" + }, + "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild", + "editor.inlayHints.enabled": "offUnlessPressed" +} From bc63589cca6ce923229a9b48a9f2bf99de97dee8 Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Sun, 21 Apr 2024 15:35:21 -0700 Subject: [PATCH 2/6] refactor udp --- src/iptux-core/CoreThread.cpp | 2 +- src/iptux-core/internal/{UdpDataService.cpp => UdpServer.cpp} | 2 +- src/iptux-core/internal/{UdpDataService.h => UdpServer.h} | 0 .../internal/{UdpDataServiceTest.cpp => UdpServerTest.cpp} | 2 +- src/iptux-core/meson.build | 4 ++-- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/iptux-core/internal/{UdpDataService.cpp => UdpServer.cpp} (98%) rename src/iptux-core/internal/{UdpDataService.h => UdpServer.h} (100%) rename src/iptux-core/internal/{UdpDataServiceTest.cpp => UdpServerTest.cpp} (97%) diff --git a/src/iptux-core/CoreThread.cpp b/src/iptux-core/CoreThread.cpp index dc37dbed0..1c86d027d 100644 --- a/src/iptux-core/CoreThread.cpp +++ b/src/iptux-core/CoreThread.cpp @@ -21,7 +21,7 @@ #include "iptux-core/internal/SendFile.h" #include "iptux-core/internal/TcpData.h" #include "iptux-core/internal/UdpData.h" -#include "iptux-core/internal/UdpDataService.h" +#include "iptux-core/internal/UdpServer.h" #include "iptux-core/internal/ipmsg.h" #include "iptux-core/internal/support.h" #include "iptux-utils/output.h" diff --git a/src/iptux-core/internal/UdpDataService.cpp b/src/iptux-core/internal/UdpServer.cpp similarity index 98% rename from src/iptux-core/internal/UdpDataService.cpp rename to src/iptux-core/internal/UdpServer.cpp index 50548a4ea..210c266d4 100644 --- a/src/iptux-core/internal/UdpDataService.cpp +++ b/src/iptux-core/internal/UdpServer.cpp @@ -1,4 +1,4 @@ -#include "UdpDataService.h" +#include "UdpServer.h" #include "iptux-utils/output.h" #include "iptux-utils/utils.h" diff --git a/src/iptux-core/internal/UdpDataService.h b/src/iptux-core/internal/UdpServer.h similarity index 100% rename from src/iptux-core/internal/UdpDataService.h rename to src/iptux-core/internal/UdpServer.h diff --git a/src/iptux-core/internal/UdpDataServiceTest.cpp b/src/iptux-core/internal/UdpServerTest.cpp similarity index 97% rename from src/iptux-core/internal/UdpDataServiceTest.cpp rename to src/iptux-core/internal/UdpServerTest.cpp index 7f57739ae..bfa4d4299 100644 --- a/src/iptux-core/internal/UdpDataServiceTest.cpp +++ b/src/iptux-core/internal/UdpServerTest.cpp @@ -1,7 +1,7 @@ #include "gtest/gtest.h" #include "iptux-core/TestHelper.h" -#include "iptux-core/internal/UdpDataService.h" +#include "iptux-core/internal/UdpServer.h" #include "iptux-utils/utils.h" using namespace std; diff --git a/src/iptux-core/meson.build b/src/iptux-core/meson.build index dfa21e4b1..91327a050 100644 --- a/src/iptux-core/meson.build +++ b/src/iptux-core/meson.build @@ -26,7 +26,7 @@ core_sources += files([ 'internal/TcpData.cpp', 'internal/TransAbstract.cpp', 'internal/UdpData.cpp', - 'internal/UdpDataService.cpp', + 'internal/UdpServer.cpp', ]) inc = include_directories('..', '../api') @@ -78,7 +78,7 @@ core_test_sources = files([ 'internal/CommandTest.cpp', 'internal/supportTest.cpp', 'internal/UdpDataTest.cpp', - 'internal/UdpDataServiceTest.cpp', + 'internal/UdpServerTest.cpp', 'IptuxConfigTest.cpp', 'ModelsTest.cpp', 'ProgramDataTest.cpp', From 595e1e3f2e7abdd03a72086cb94b598443488af0 Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Sun, 21 Apr 2024 15:37:04 -0700 Subject: [PATCH 3/6] rename symbol --- src/iptux-core/CoreThread.cpp | 3 +-- src/iptux-core/internal/UdpServer.cpp | 8 ++++---- src/iptux-core/internal/UdpServer.h | 6 +++--- src/iptux-core/internal/UdpServerTest.cpp | 8 ++++---- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/iptux-core/CoreThread.cpp b/src/iptux-core/CoreThread.cpp index 1c86d027d..4eb0e4c01 100644 --- a/src/iptux-core/CoreThread.cpp +++ b/src/iptux-core/CoreThread.cpp @@ -20,7 +20,6 @@ #include "iptux-core/internal/RecvFileData.h" #include "iptux-core/internal/SendFile.h" #include "iptux-core/internal/TcpData.h" -#include "iptux-core/internal/UdpData.h" #include "iptux-core/internal/UdpServer.h" #include "iptux-core/internal/ipmsg.h" #include "iptux-core/internal/support.h" @@ -140,7 +139,7 @@ CoreThread::CoreThread(shared_ptr data) if (config->GetBool("debug_dont_broadcast")) { pImpl->debugDontBroadcast = true; } - pImpl->udp_data_service = make_unique(*this); + pImpl->udp_data_service = make_unique(*this); pImpl->me = make_shared(); pImpl->me->ipv4 = inAddrFromString("127.0.0.1"); (*pImpl->me) diff --git a/src/iptux-core/internal/UdpServer.cpp b/src/iptux-core/internal/UdpServer.cpp index 210c266d4..8ace1b60d 100644 --- a/src/iptux-core/internal/UdpServer.cpp +++ b/src/iptux-core/internal/UdpServer.cpp @@ -6,17 +6,17 @@ using namespace std; namespace iptux { -UdpDataService::UdpDataService(CoreThread& coreThread) +UdpServer::UdpServer(CoreThread& coreThread) : core_thread_(coreThread) {} -unique_ptr UdpDataService::process(in_addr ipv4, +unique_ptr UdpServer::process(in_addr ipv4, int port, const char buf[], size_t size) { return process(ipv4, port, buf, size, true); } -unique_ptr UdpDataService::process(in_addr ipv4, +unique_ptr UdpServer::process(in_addr ipv4, int port, const char buf[], size_t size, @@ -37,7 +37,7 @@ unique_ptr UdpDataService::process(in_addr ipv4, return udata; } -void UdpDataService::process(UdpData& udata) { +void UdpServer::process(UdpData& udata) { /* 如果开启了黑名单处理功能,且此地址正好被列入了黑名单 */ if (core_thread_.IsBlocked(udata.getIpv4())) { LOG_INFO("address is blocked: %s", udata.getIpv4String().c_str()); diff --git a/src/iptux-core/internal/UdpServer.h b/src/iptux-core/internal/UdpServer.h index ccc199466..ea33d731f 100644 --- a/src/iptux-core/internal/UdpServer.h +++ b/src/iptux-core/internal/UdpServer.h @@ -6,9 +6,9 @@ namespace iptux { -class UdpDataService { +class UdpServer { public: - explicit UdpDataService(CoreThread& coreThread); + explicit UdpServer(CoreThread& coreThread); std::unique_ptr process(in_addr ipv4, int port, @@ -27,7 +27,7 @@ class UdpDataService { CoreThread& core_thread_; }; -using UdpDataService_U = std::unique_ptr; +using UdpDataService_U = std::unique_ptr; } // namespace iptux diff --git a/src/iptux-core/internal/UdpServerTest.cpp b/src/iptux-core/internal/UdpServerTest.cpp index bfa4d4299..307789a4b 100644 --- a/src/iptux-core/internal/UdpServerTest.cpp +++ b/src/iptux-core/internal/UdpServerTest.cpp @@ -9,13 +9,13 @@ using namespace iptux; TEST(UdpDataService, process) { auto core = newCoreThread(); - auto service = new UdpDataService(*core.get()); + auto service = new UdpServer(*core.get()); service->process(inAddrFromString("127.0.0.1"), 1234, "", 0, true); } TEST(UdpDataService, SomeoneEntry) { auto core = newCoreThread(); - auto service = new UdpDataService(*core.get()); + auto service = new UdpServer(*core.get()); const char* data = "iptux 0.8.0:1:lidaobing:lidaobing.lan:257:lidaobing"; service->process(inAddrFromString("127.0.0.1"), 1234, data, strlen(data), true); @@ -28,7 +28,7 @@ TEST(UdpDataService, CreatePalInfo) { "1_iptux " "0.8.0-b1:6:lidaobing:LIs-MacBook-Pro.local:259:中\xe4\xb8\x00\x00icon-" "tux.png\x00utf-8\x00"; - auto service = new UdpDataService(*core.get()); + auto service = new UdpServer(*core.get()); auto udp = service->process(inAddrFromString("127.0.0.1"), 1234, data, strlen(data), false); auto pal = udp->CreatePalInfo(); @@ -43,7 +43,7 @@ TEST(UdpDataService, CreatePalInfo) { "1_iptux " "0.8.0-b1:6:中\xe4\xb8:LIs-MacBook-Pro.local:259:" "中\xe4\xb8\x00\x00icon-tux.png\x00utf-8\x00"; - auto service = new UdpDataService(*core.get()); + auto service = new UdpServer(*core.get()); auto udp = service->process(inAddrFromString("127.0.0.1"), 1234, data, strlen(data), false); auto pal = udp->CreatePalInfo(); From bb7694bfb09dbcd689531a483ec229d4089ada7c Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Sun, 21 Apr 2024 16:14:14 -0700 Subject: [PATCH 4/6] update --- .vscode/settings.json | 15 +++++----- src/iptux-core/CoreThread.cpp | 8 ++++-- src/iptux-core/internal/UdpServer.cpp | 41 +++++++++++++++++++++++++++ src/iptux-core/internal/UdpServer.h | 13 +++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 34967ac5f..ccd8aa807 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,9 @@ { - "files.associations": { - "numbers": "cpp", - "cstring": "cpp" - }, - "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild", - "editor.inlayHints.enabled": "offUnlessPressed" -} + "files.associations": { + "numbers": "cpp", + "cstring": "cpp" + }, + "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild", + "editor.inlayHints.enabled": "offUnlessPressed", + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/src/iptux-core/CoreThread.cpp b/src/iptux-core/CoreThread.cpp index 4eb0e4c01..5b0dfb8de 100644 --- a/src/iptux-core/CoreThread.cpp +++ b/src/iptux-core/CoreThread.cpp @@ -85,7 +85,7 @@ void init_iptux_environment() { struct CoreThread::Impl { PPalInfo me; - UdpDataService_U udp_data_service; + UdpDataService_U udp_server; GSList* blacklist{nullptr}; //黑名单链表 bool debugDontBroadcast{false}; @@ -139,7 +139,7 @@ CoreThread::CoreThread(shared_ptr data) if (config->GetBool("debug_dont_broadcast")) { pImpl->debugDontBroadcast = true; } - pImpl->udp_data_service = make_unique(*this); + pImpl->udp_server = make_unique(*this); pImpl->me = make_shared(); pImpl->me->ipv4 = inAddrFromString("127.0.0.1"); (*pImpl->me) @@ -175,6 +175,8 @@ void CoreThread::start() { async([](CoreThread* ct) { ct->processEvents(); }, this); pImpl->notifyToAllFuture = async([](CoreThread* ct) { SendNotifyToAll(ct); }, this); + + pImpl->udp_server->start(); } void CoreThread::bind_iptux_port() { @@ -254,7 +256,7 @@ void CoreThread::RecvUdpData(CoreThread* self) { if (size != MAX_UDPLEN) buf[size] = '\0'; auto port = ntohs(addr.sin_port); - self->pImpl->udp_data_service->process(addr.sin_addr, port, buf, size); + self->pImpl->udp_server->process(addr.sin_addr, port, buf, size); } } diff --git a/src/iptux-core/internal/UdpServer.cpp b/src/iptux-core/internal/UdpServer.cpp index 8ace1b60d..3a87aa5ec 100644 --- a/src/iptux-core/internal/UdpServer.cpp +++ b/src/iptux-core/internal/UdpServer.cpp @@ -1,7 +1,11 @@ #include "UdpServer.h" +#include "glib/gi18n.h" +#include "iptux-core/Exception.h" #include "iptux-utils/output.h" #include "iptux-utils/utils.h" +#include "iptux-core/internal/support.h" +#include using namespace std; namespace iptux { @@ -85,4 +89,41 @@ void UdpServer::process(UdpData& udata) { } } +bool UdpServer::start() { + if (status != UdpServerStatus::INITED) { + LOG_ERROR("udp server status is not inited"); + return false; + } + + int udpSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(udpSock < 0) { + LOG_ERROR("create udp socket failed: %s", strerror(errno)); + status = UdpServerStatus::START_FAILED; + return false; + } + + socket_enable_reuse(udpSock); + socket_enable_broadcast(udpSock); + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(bind_port); + addr.sin_addr = inAddrFromString(bind_ip); + + if (::bind(udpSock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { + int ec = errno; + close(udpSock); + auto errmsg = + stringFormat(_("Fatal Error!! Failed to bind the UDP port(%s:%d)!\n%s"), + bind_ip.c_str(), bind_port, strerror(ec)); + LOG_ERROR("%s", errmsg.c_str()); + throw Exception(UDP_BIND_FAILED, errmsg); + } else { + LOG_INFO("bind UDP port(%s:%d) success.", bind_ip.c_str(), bind_port); + } + + status = UdpServerStatus::RUNNING; + return true; +} + } // namespace iptux diff --git a/src/iptux-core/internal/UdpServer.h b/src/iptux-core/internal/UdpServer.h index ea33d731f..6a2b68a55 100644 --- a/src/iptux-core/internal/UdpServer.h +++ b/src/iptux-core/internal/UdpServer.h @@ -6,6 +6,13 @@ namespace iptux { +enum class UdpServerStatus { + INITED, + RUNNING, + STOPPED, + START_FAILED, +}; + class UdpServer { public: explicit UdpServer(CoreThread& coreThread); @@ -23,8 +30,14 @@ class UdpServer { void process(UdpData& udpData); + bool start(); + bool stop(); + private: CoreThread& core_thread_; + std::string bind_ip; + int bind_port; + UdpServerStatus status = UdpServerStatus::INITED; }; using UdpDataService_U = std::unique_ptr; From 4403c6d64d9a3d6b3e4e853d3cbce8884cd7281b Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Mon, 22 Apr 2024 22:38:08 -0700 Subject: [PATCH 5/6] fix --- src/iptux-core/internal/UdpServer.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/iptux-core/internal/UdpServer.cpp b/src/iptux-core/internal/UdpServer.cpp index 3a87aa5ec..65a4c8a6b 100644 --- a/src/iptux-core/internal/UdpServer.cpp +++ b/src/iptux-core/internal/UdpServer.cpp @@ -1,30 +1,31 @@ #include "UdpServer.h" -#include "glib/gi18n.h" +#include +#include +#include + #include "iptux-core/Exception.h" +#include "iptux-core/internal/support.h" #include "iptux-utils/output.h" #include "iptux-utils/utils.h" -#include "iptux-core/internal/support.h" -#include using namespace std; namespace iptux { -UdpServer::UdpServer(CoreThread& coreThread) - : core_thread_(coreThread) {} +UdpServer::UdpServer(CoreThread& coreThread) : core_thread_(coreThread) {} unique_ptr UdpServer::process(in_addr ipv4, - int port, - const char buf[], - size_t size) { + int port, + const char buf[], + size_t size) { return process(ipv4, port, buf, size, true); } unique_ptr UdpServer::process(in_addr ipv4, - int port, - const char buf[], - size_t size, - bool run) { + int port, + const char buf[], + size_t size, + bool run) { if (Log::IsDebugEnabled()) { LOG_DEBUG("received udp message from %s:%d, size %zu\n%s", inAddrToString(ipv4).c_str(), port, size, @@ -96,7 +97,7 @@ bool UdpServer::start() { } int udpSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if(udpSock < 0) { + if (udpSock < 0) { LOG_ERROR("create udp socket failed: %s", strerror(errno)); status = UdpServerStatus::START_FAILED; return false; From dbae907c64479992484d57df0dca6407a2561ce8 Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Mon, 22 Apr 2024 22:39:49 -0700 Subject: [PATCH 6/6] rename --- src/iptux-core/CoreThread.cpp | 10 +++++----- src/iptux-core/internal/UdpServer.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/iptux-core/CoreThread.cpp b/src/iptux-core/CoreThread.cpp index 5b0dfb8de..d362f0694 100644 --- a/src/iptux-core/CoreThread.cpp +++ b/src/iptux-core/CoreThread.cpp @@ -85,11 +85,11 @@ void init_iptux_environment() { struct CoreThread::Impl { PPalInfo me; - UdpDataService_U udp_server; + UdpServer_U udp_server; - GSList* blacklist{nullptr}; //黑名单链表 + GSList* blacklist{nullptr}; // 黑名单链表 bool debugDontBroadcast{false}; - vector> pallist; //好友链表(成员不能被删除) + vector> pallist; // 好友链表(成员不能被删除) map> privateFiles; int lastTransTaskId{0}; @@ -514,10 +514,10 @@ bool CoreThread::SendMessage(CPPalInfo pal, const ChipData& chipData) { return false; } Command(*this).SendSublayer(sock, pal, IPTUX_MSGPICOPT, ptr); - close(sock); //关闭网络套接口 + close(sock); // 关闭网络套接口 /*/* 删除此图片 */ if (chipData.GetDeleteFileAfterSent()) { - unlink(ptr); //此文件已无用处 + unlink(ptr); // 此文件已无用处 } return true; default: diff --git a/src/iptux-core/internal/UdpServer.h b/src/iptux-core/internal/UdpServer.h index 6a2b68a55..619e3777c 100644 --- a/src/iptux-core/internal/UdpServer.h +++ b/src/iptux-core/internal/UdpServer.h @@ -40,7 +40,7 @@ class UdpServer { UdpServerStatus status = UdpServerStatus::INITED; }; -using UdpDataService_U = std::unique_ptr; +using UdpServer_U = std::unique_ptr; } // namespace iptux