Skip to content

Commit

Permalink
Merge pull request #493 from iptux-src/udpdata
Browse files Browse the repository at this point in the history
refactor Udpdata
  • Loading branch information
lidaobing authored Nov 16, 2021
2 parents 434fcea + 5a9dfa9 commit ea5d098
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 165 deletions.
7 changes: 6 additions & 1 deletion src/iptux-core/CoreThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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/ipmsg.h"
#include "iptux-core/internal/support.h"
#include "iptux-utils/output.h"
Expand Down Expand Up @@ -84,6 +85,9 @@ void init_iptux_environment() {

struct CoreThread::Impl {
PPalInfo me;

UdpDataService_U udp_data_service;

GSList* blacklist{nullptr}; //黑名单链表
bool debugDontBroadcast{false};
vector<shared_ptr<PalInfo>> pallist; //好友链表(成员不能被删除)
Expand Down Expand Up @@ -136,6 +140,7 @@ CoreThread::CoreThread(shared_ptr<ProgramData> data)
if (config->GetBool("debug_dont_broadcast")) {
pImpl->debugDontBroadcast = true;
}
pImpl->udp_data_service = make_unique<UdpDataService>(*this);
pImpl->me = make_shared<PalInfo>();
pImpl->me->ipv4 = inAddrFromString("127.0.0.1");
(*pImpl->me)
Expand Down Expand Up @@ -250,7 +255,7 @@ void CoreThread::RecvUdpData(CoreThread* self) {
if (size != MAX_UDPLEN)
buf[size] = '\0';
auto port = ntohs(addr.sin_port);
UdpData::UdpDataEntry(*self, addr.sin_addr, port, buf, size);
self->pImpl->udp_data_service->process(addr.sin_addr, port, buf, size);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/iptux-core/internal/CommandMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CommandMode {
public:
explicit CommandMode(int mode) : mode(mode) {}
std::string toString() const;
int getMode() const { return mode; }

private:
int mode;
Expand Down
129 changes: 31 additions & 98 deletions src/iptux-core/internal/UdpData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,110 +39,31 @@ namespace iptux {
/**
* 类构造函数.
*/
UdpData::UdpData(CoreThread& coreThread)
: coreThread(coreThread), ipv4({0}), size(0), encode(NULL) {}

/**
* 类析构函数.
*/
UdpData::~UdpData() {
g_free(encode);
}

/**
* UDP数据解析入口.
* @param ipv4 ipv4
* @param buf[] 数据缓冲区
* @param size 数据有效长度
*/
unique_ptr<UdpData> UdpData::UdpDataEntry(CoreThread& coreThread,
in_addr ipv4,
int port,
const char buf[],
size_t size) {
return UdpDataEntry(coreThread, ipv4, port, buf, size, true);
}

unique_ptr<UdpData> UdpData::UdpDataEntry(CoreThread& coreThread,
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,
stringDumpAsCString(string(buf, size)).c_str());
} else {
LOG_INFO("received udp message from %s:%d, size %zu",
inAddrToString(ipv4).c_str(), port, size);
UdpData::UdpData(CoreThread& coreThread,
in_addr ipv4,
const char buf_[],
size_t size_)
: coreThread(coreThread),
ipv4(ipv4),
size(size_ < MAX_UDPLEN ? size_ : MAX_UDPLEN),
encode(NULL) {
memcpy(buf, buf_, size);
if (size != MAX_UDPLEN) {
buf[size] = '\0';
}
auto udata = make_unique<UdpData>(coreThread);
}

udata->ipv4 = ipv4;
udata->size = size < MAX_UDPLEN ? size : MAX_UDPLEN;
memcpy(udata->buf, buf, size);
if (size != MAX_UDPLEN)
udata->buf[size] = '\0';
if (run) {
udata->DispatchUdpData();
}
return udata;
UdpData::UdpData(const string& buf_, const string& ipv4String)
: coreThread(*(CoreThread*)NULL), size(buf_.size()), encode(nullptr) {
this->ipv4 = inAddrFromString(ipv4String);
memcpy(buf, &buf_[0], buf_.size());
}

/**
* 分派UDP数据到相应的函数去进行处理.
* 类析构函数.
*/
void UdpData::DispatchUdpData() {
uint32_t commandno;

/* 如果开启了黑名单处理功能,且此地址正好被列入了黑名单 */
/* 嘿嘿,那就不要怪偶心狠手辣了 */
if (coreThread.IsBlocked(ipv4)) {
LOG_INFO("address is blocked: %s", inAddrToString(ipv4).c_str());
return;
}

/* 决定消息去向 */
commandno = iptux_get_dec_number(buf, ':', 4);
auto commandMode = GET_MODE(commandno);
LOG_INFO("command NO.: [0x%x] %s", commandno,
CommandMode(commandMode).toString().c_str());
switch (commandMode) {
case IPMSG_BR_ENTRY:
SomeoneEntry();
break;
case IPMSG_BR_EXIT:
SomeoneExit();
break;
case IPMSG_ANSENTRY:
SomeoneAnsEntry();
break;
case IPMSG_BR_ABSENCE:
SomeoneAbsence();
break;
case IPMSG_SENDMSG:
SomeoneSendmsg();
break;
case IPMSG_RECVMSG:
SomeoneRecvmsg();
break;
case IPTUX_ASKSHARED:
SomeoneAskShared();
break;
case IPTUX_SENDICON:
SomeoneSendIcon();
break;
case IPTUX_SEND_SIGN:
SomeoneSendSign();
break;
case IPTUX_SENDMSG:
SomeoneBcstmsg();
break;
default:
LOG_WARN("unknown command mode: 0x%lx", commandMode);
break;
}
UdpData::~UdpData() {
g_free(encode);
}

/**
Expand Down Expand Up @@ -782,4 +703,16 @@ void UdpData::ThreadAskSharedFile(CoreThread* coreThread, PPalInfo pal) {
}
}

uint32_t UdpData::getCommandNo() const {
return iptux_get_dec_number(buf, ':', 4);
}

string UdpData::getIpv4String() const {
return inAddrToString(ipv4);
}

CommandMode UdpData::getCommandMode() const {
return CommandMode(GET_MODE(getCommandNo()));
}

} // namespace iptux
28 changes: 10 additions & 18 deletions src/iptux-core/internal/UdpData.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,27 @@
#include "iptux-core/CoreThread.h"
#include "iptux-core/IptuxConfig.h"
#include "iptux-core/Models.h"
#include "iptux-core/internal/CommandMode.h"
#include "iptux-core/internal/ipmsg.h"

namespace iptux {

class UdpData {
public:
explicit UdpData(CoreThread& coreThread);
UdpData(CoreThread& coreThread, in_addr ipv4, const char buf[], size_t size);
UdpData(const std::string& buf, const std::string& ipv4String);
~UdpData();

static std::unique_ptr<UdpData> UdpDataEntry(CoreThread& coreThread,
in_addr ipv4,
int port,
const char buf[],
size_t size);
static std::unique_ptr<UdpData> UdpDataEntry(
CoreThread& coreThread,
in_addr ipv4,
int port,
const char buf[],
size_t size,
bool run /** if run is false, don't do DispatchUdpData */
);
in_addr getIpv4() const { return ipv4; }
std::string getIpv4String() const;

uint32_t getCommandNo() const;
CommandMode getCommandMode() const;

// for test
public:
std::shared_ptr<PalInfo> CreatePalInfo();

private:
void DispatchUdpData();

public:
void SomeoneLost();
void SomeoneEntry();
void SomeoneExit();
Expand All @@ -59,6 +50,7 @@ class UdpData {
void SomeoneSendSign();
void SomeoneBcstmsg();

private:
void UpdatePalInfo(PalInfo* pal);

void InsertMessage(PPalInfo pal, GroupBelongType btype, const char* msg);
Expand Down
88 changes: 88 additions & 0 deletions src/iptux-core/internal/UdpDataService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "UdpDataService.h"

#include "iptux-utils/output.h"
#include "iptux-utils/utils.h"

using namespace std;
namespace iptux {

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

unique_ptr<UdpData> UdpDataService::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) {
if (Log::IsDebugEnabled()) {
LOG_DEBUG("received udp message from %s:%d, size %zu\n%s",
inAddrToString(ipv4).c_str(), port, size,
stringDumpAsCString(string(buf, size)).c_str());
} else {
LOG_INFO("received udp message from %s:%d, size %zu",
inAddrToString(ipv4).c_str(), port, size);
}
auto udata = make_unique<UdpData>(core_thread_, ipv4, buf, size);

if (run) {
process(*udata);
}
return udata;
}

void UdpDataService::process(UdpData& udata) {
/* 如果开启了黑名单处理功能,且此地址正好被列入了黑名单 */
if (core_thread_.IsBlocked(udata.getIpv4())) {
LOG_INFO("address is blocked: %s", udata.getIpv4String().c_str());
return;
}

/* 决定消息去向 */
auto commandMode = udata.getCommandMode();
LOG_INFO("command NO.: [0x%x] %s", udata.getCommandNo(),
commandMode.toString().c_str());
switch (commandMode.getMode()) {
case IPMSG_BR_ENTRY:
udata.SomeoneEntry();
break;
case IPMSG_BR_EXIT:
udata.SomeoneExit();
break;
case IPMSG_ANSENTRY:
udata.SomeoneAnsEntry();
break;
case IPMSG_BR_ABSENCE:
udata.SomeoneAbsence();
break;
case IPMSG_SENDMSG:
udata.SomeoneSendmsg();
break;
case IPMSG_RECVMSG:
udata.SomeoneRecvmsg();
break;
case IPTUX_ASKSHARED:
udata.SomeoneAskShared();
break;
case IPTUX_SENDICON:
udata.SomeoneSendIcon();
break;
case IPTUX_SEND_SIGN:
udata.SomeoneSendSign();
break;
case IPTUX_SENDMSG:
udata.SomeoneBcstmsg();
break;
default:
LOG_WARN("unknown command mode: 0x%x", commandMode.getMode());
break;
}
}

} // namespace iptux
34 changes: 34 additions & 0 deletions src/iptux-core/internal/UdpDataService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef IPTUX_UDP_DATA_SERVICE_H
#define IPTUX_UDP_DATA_SERVICE_H

#include "iptux-core/CoreThread.h"
#include "iptux-core/internal/UdpData.h"

namespace iptux {

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

std::unique_ptr<UdpData> process(in_addr ipv4,
int port,
const char buf[],
size_t size);

std::unique_ptr<UdpData> process(in_addr ipv4,
int port,
const char buf[],
size_t size,
bool run);

void process(UdpData& udpData);

private:
CoreThread& core_thread_;
};

using UdpDataService_U = std::unique_ptr<UdpDataService>;

} // namespace iptux

#endif
Loading

0 comments on commit ea5d098

Please sign in to comment.