Skip to content

Refactor udp #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ cmake-build-debug
builddir
builddir-linux
coverage.info
.vscode/settings.json
.vscode/launch.json
/vs
.vscode/tasks.json
Expand Down
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"numbers": "cpp",
"cstring": "cpp"
},
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild",
"editor.inlayHints.enabled": "offUnlessPressed",
"editor.formatOnSave": true
}
11 changes: 6 additions & 5 deletions src/iptux-core/CoreThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#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/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"
Expand Down Expand Up @@ -88,7 +87,7 @@ struct CoreThread::Impl {

PPalInfo me;

UdpDataService_U udp_data_service;
UdpServer_U udp_server;

GSList* blacklist{nullptr}; // 黑名单链表
bool debugDontBroadcast{false};
Expand Down Expand Up @@ -118,7 +117,7 @@ CoreThread::CoreThread(shared_ptr<ProgramData> data)
pImpl->debugDontBroadcast = true;
}
pImpl->port = programData->port();
pImpl->udp_data_service = make_unique<UdpDataService>(*this);
pImpl->udp_server = make_unique<UdpServer>(*this);
pImpl->me = make_shared<PalInfo>("127.0.0.1", port());
(*pImpl->me)
.setUser(g_get_user_name())
Expand Down Expand Up @@ -151,6 +150,8 @@ void CoreThread::start() {
pImpl->tcpFuture = async([](CoreThread* ct) { RecvTcpData(ct); }, this);
pImpl->notifyToAllFuture =
async([](CoreThread* ct) { SendNotifyToAll(ct); }, this);

pImpl->udp_server->start();
}

void CoreThread::bind_iptux_port() {
Expand Down Expand Up @@ -230,7 +231,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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
#include "UdpDataService.h"
#include "UdpServer.h"

#include <glib/gi18n.h>
#include <netinet/in.h>
#include <unistd.h>

#include "iptux-core/Exception.h"
#include "iptux-core/internal/support.h"
#include "iptux-utils/output.h"
#include "iptux-utils/utils.h"

using namespace std;
namespace iptux {

UdpDataService::UdpDataService(CoreThread& coreThread)
: core_thread_(coreThread) {}
UdpServer::UdpServer(CoreThread& coreThread) : core_thread_(coreThread) {}

unique_ptr<UdpData> UdpDataService::process(in_addr ipv4,
int port,
const char buf[],
size_t size) {
unique_ptr<UdpData> UdpServer::process(in_addr ipv4,
int port,
const char buf[],
size_t size) {
return process(ipv4, port, buf, size, true);
}

unique_ptr<UdpData> UdpDataService::process(in_addr ipv4,
int port,
const char buf[],
size_t size,
bool run) {
unique_ptr<UdpData> UdpServer::process(in_addr ipv4,
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,
Expand All @@ -37,7 +42,7 @@ unique_ptr<UdpData> 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());
Expand Down Expand Up @@ -85,4 +90,41 @@ void UdpDataService::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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

namespace iptux {

class UdpDataService {
enum class UdpServerStatus {
INITED,
RUNNING,
STOPPED,
START_FAILED,
};

class UdpServer {
public:
explicit UdpDataService(CoreThread& coreThread);
explicit UdpServer(CoreThread& coreThread);

std::unique_ptr<UdpData> process(in_addr ipv4,
int port,
Expand All @@ -23,11 +30,17 @@ class UdpDataService {

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<UdpDataService>;
using UdpServer_U = std::unique_ptr<UdpServer>;

} // namespace iptux

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#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;
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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/iptux-core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand Down