Compare commits

...

5 Commits
new ... master

Author SHA1 Message Date
azw
7eb242f5b8 1 2023-06-09 06:57:46 +00:00
azw
571a29eebb 1 2023-06-05 09:50:17 +00:00
azw
70e076631c 1 2023-06-05 09:41:56 +00:00
azw
39b01b2794 1 2023-06-05 09:40:34 +00:00
azw
36f7099354 1 2023-06-05 09:36:21 +00:00
6 changed files with 101 additions and 30 deletions

View File

@ -24,6 +24,7 @@ message SS_CMKcpHandshake
optional string account_id = 2; //id
optional string session_id = 3; //session id
optional string team_uuid = 4; //
optional int32 secret_key_place = 5; // 0() 1kcp底层协议头之后()
}
message SS_SMKcpHandshake

View File

@ -1,6 +1,7 @@
#include "precompile.h"
#include <memory.h>
#include <string.h>
#include <f8/netmsghandler.h>
#include <f8/udplog.h>
@ -33,23 +34,22 @@ KcpSession::~KcpSession()
}
void KcpSession::Init(int socket_handle)
void KcpSession::Init(int socket_handle, int secret_key_place)
{
socket_handle_ = socket_handle;
secret_key_ = App::Instance()->NewUuid();
kcp_ = ikcp_create(socket_handle_, (void*)this);
#if 1
const KcpConf& kcp_conf = JsonDataMgr::Instance()->GetKcpConf();
ikcp_wndsize(kcp_, kcp_conf.sndwnd, kcp_conf.rcvwnd);
ikcp_nodelay(kcp_, kcp_conf.nodelay, kcp_conf.interval, kcp_conf.resend, kcp_conf.nc);
kcp_->rx_minrto = kcp_conf.rx_minrto;
kcp_->fastresend = kcp_conf.fastresend;
#else
ikcp_wndsize(kcp_, 128, 128);
ikcp_nodelay(kcp_, 2, 10, 2, 1);
kcp_->rx_minrto = 10;
kcp_->fastresend = 1;
#endif
secret_key_place_ = secret_key_place;
if (secret_key_place_ > 0) {
secret_key_place_ = 1;
} else if (secret_key_place_ < 0) {
secret_key_place_ = 0;
}
kcp_->output = UdpOutput;
init_tick_ = a8::XGetTickCount();
}
@ -75,8 +75,21 @@ void KcpSession::SendClientMsg(char* buf, int buf_len)
void KcpSession::OnRecvPacket(a8::UdpPacket* pkt)
{
const int IKCP_OVERHEAD = 24;
remote_addr_ = pkt->remote_addr;
if (GetSecretKeyPlace()) {
char* buf = (char*)malloc(pkt->buf_len - GetSecretKeyLen());
int buflen = pkt->buf_len - GetSecretKeyLen();
memmove(buf, pkt->buf, IKCP_OVERHEAD);
if (pkt->buf_len > IKCP_OVERHEAD + GetSecretKeyLen()) {
memmove(buf + IKCP_OVERHEAD, pkt->buf + IKCP_OVERHEAD + GetSecretKeyLen(), buflen - IKCP_OVERHEAD);
}
ikcp_input(kcp_, buf, buflen);
free(buf);
} else {
ikcp_input(kcp_, pkt->buf, pkt->buf_len);
}
}
void KcpSession::UpdateInput()
@ -91,8 +104,20 @@ void KcpSession::UpdateInput()
void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
{
//packagelen + msgid + magiccode + msgbody
//2 + 2 + 4+ xx + \0 + xx
if (GetSecretKeyPlace()) {
DecodeUserPacketNew(buf, offset, buflen);
} else {
DecodeUserPacketOld(buf, offset, buflen);
}
}
int KcpSession::GetSecretKeyPlace()
{
return secret_key_place_;
}
void KcpSession::DecodeUserPacketOld(char* buf, int& offset, unsigned int buflen)
{
bool warning = false;
while (buflen - offset >= sizeof(f8::PackHead) + GetSecretKeyLen()) {
long long secret_key = KcpSession::ReadSecretKey(&buf[offset], buflen);
@ -124,6 +149,37 @@ void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
}
if (warning) {
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包", {});
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包1", {});
}
}
void KcpSession::DecodeUserPacketNew(char* buf, int& offset, unsigned int buflen)
{
bool warning = false;
while (buflen - offset >= sizeof(f8::PackHead)) {
f8::PackHead* p = (f8::PackHead*)&buf[offset];
if (p->magic_code == f8::MAGIC_CODE) {
if (buflen - offset < sizeof(f8::PackHead) + p->packlen) {
break;
}
App::Instance()->AddSocketMsg(SF_Client,
socket_handle_,
0,
//saddr,
p->msgid,
p->seqid,
&buf[offset + sizeof(f8::PackHead)],
p->packlen,
ST_Udp);
offset += sizeof(f8::PackHead) + p->packlen;
} else {
warning = true;
offset++;
continue;
}
}
if (warning) {
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包2", {});
}
}

View File

@ -11,7 +11,7 @@ public:
KcpSession();
~KcpSession();
void Init(int socket_handle);
void Init(int socket_handle, int secret_key_place);
void UnInit();
void Update(long long tick);
@ -19,6 +19,7 @@ public:
int GetSocketHandle() { return socket_handle_; }
long long GetSecretKey() { return secret_key_; }
void* GetSecretKeyDataPtr() { return &secret_key_; }
int GetSecretKeyPlace();
void SendClientMsg(char* buf, int buf_len);
virtual void OnRecvPacket(a8::UdpPacket* pkt) override;
@ -31,11 +32,14 @@ public:
protected:
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override;
void DecodeUserPacketOld(char* buf, int& offset, unsigned int buflen);
void DecodeUserPacketNew(char* buf, int& offset, unsigned int buflen);
private:
void UpdateInput();
private:
int secret_key_place_ = 0;
int socket_handle_ = 0;
long long secret_key_ = 0;
ikcpcb* kcp_ = nullptr;

View File

@ -5,10 +5,13 @@
#include "longsession.h"
#include "kcpsession.h"
#include "ss_msgid.pb.h"
#include "ss_proto.pb.h"
void LongSession::Init(f8::MsgHdr& hdr, const ss::SS_CMKcpHandshake& msg)
{
kcp_session_ = std::make_shared<KcpSession>();
kcp_session_->Init(hdr.socket_handle);
kcp_session_->Init(hdr.socket_handle, msg.secret_key_place());
}
void LongSession::UnInit()
@ -25,3 +28,8 @@ void LongSession::UpdatePing()
{
last_ping_tick_ = a8::XGetTickCount();
}
int LongSession::GetSecretKeyPlace()
{
return kcp_session_->GetSecretKeyPlace();
}

View File

@ -17,6 +17,7 @@ class LongSession
std::shared_ptr<KcpSession> GetKcpSession() { return kcp_session_; }
void UpdatePing();
int GetSecretKeyPlace();
private:
long long last_ping_tick_ = 0;

View File

@ -133,9 +133,8 @@ void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt)
});
return;
}
session->GetKcpSession()->OnRecvPacket(pkt);
#if 0
if (pkt->buf_len > IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) {
if (session->GetSecretKeyPlace()) {
if (pkt->buf_len >= IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) {
long long secret_key = KcpSession::ReadSecretKey(pkt->buf + IKCP_OVERHEAD, pkt->buf_len);
if (secret_key == session->GetKcpSession()->GetSecretKey()) {
session->GetKcpSession()->OnRecvPacket(pkt);
@ -156,7 +155,9 @@ void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt)
pkt->buf_len
});
}
#endif
} else {
session->GetKcpSession()->OnRecvPacket(pkt);
}
}
void LongSessionMgr::DelSession(int socket_handle)