Compare commits

..

1 Commits

Author SHA1 Message Date
aozhiwei
8355d06c0c 1 2023-09-27 17:56:27 +08:00
7 changed files with 33 additions and 101 deletions

View File

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

View File

@ -22,6 +22,9 @@ public:
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override
{ {
#if 1
is_activite = true;
#endif
//packagelen + msgid + magiccode + msgbody //packagelen + msgid + magiccode + msgbody
//2 + 2 + 4+ xx + \0 + xx //2 + 2 + 4+ xx + \0 + xx
bool warning = false; bool warning = false;

View File

@ -1,7 +1,6 @@
#include "precompile.h" #include "precompile.h"
#include <memory.h> #include <memory.h>
#include <string.h>
#include <f8/netmsghandler.h> #include <f8/netmsghandler.h>
#include <f8/udplog.h> #include <f8/udplog.h>
@ -34,22 +33,23 @@ KcpSession::~KcpSession()
} }
void KcpSession::Init(int socket_handle, int secret_key_place) void KcpSession::Init(int socket_handle)
{ {
socket_handle_ = socket_handle; socket_handle_ = socket_handle;
secret_key_ = App::Instance()->NewUuid(); secret_key_ = App::Instance()->NewUuid();
kcp_ = ikcp_create(socket_handle_, (void*)this); kcp_ = ikcp_create(socket_handle_, (void*)this);
#if 1
const KcpConf& kcp_conf = JsonDataMgr::Instance()->GetKcpConf(); const KcpConf& kcp_conf = JsonDataMgr::Instance()->GetKcpConf();
ikcp_wndsize(kcp_, kcp_conf.sndwnd, kcp_conf.rcvwnd); ikcp_wndsize(kcp_, kcp_conf.sndwnd, kcp_conf.rcvwnd);
ikcp_nodelay(kcp_, kcp_conf.nodelay, kcp_conf.interval, kcp_conf.resend, kcp_conf.nc); ikcp_nodelay(kcp_, kcp_conf.nodelay, kcp_conf.interval, kcp_conf.resend, kcp_conf.nc);
kcp_->rx_minrto = kcp_conf.rx_minrto; kcp_->rx_minrto = kcp_conf.rx_minrto;
kcp_->fastresend = kcp_conf.fastresend; kcp_->fastresend = kcp_conf.fastresend;
secret_key_place_ = secret_key_place; #else
if (secret_key_place_ > 0) { ikcp_wndsize(kcp_, 128, 128);
secret_key_place_ = 1; ikcp_nodelay(kcp_, 2, 10, 2, 1);
} else if (secret_key_place_ < 0) { kcp_->rx_minrto = 10;
secret_key_place_ = 0; kcp_->fastresend = 1;
} #endif
kcp_->output = UdpOutput; kcp_->output = UdpOutput;
init_tick_ = a8::XGetTickCount(); init_tick_ = a8::XGetTickCount();
} }
@ -75,21 +75,8 @@ void KcpSession::SendClientMsg(char* buf, int buf_len)
void KcpSession::OnRecvPacket(a8::UdpPacket* pkt) void KcpSession::OnRecvPacket(a8::UdpPacket* pkt)
{ {
const int IKCP_OVERHEAD = 24;
remote_addr_ = pkt->remote_addr; remote_addr_ = pkt->remote_addr;
if (GetSecretKeyPlace()) { ikcp_input(kcp_, pkt->buf, pkt->buf_len);
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() void KcpSession::UpdateInput()
@ -104,20 +91,8 @@ void KcpSession::UpdateInput()
void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen) void KcpSession::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
{ {
if (GetSecretKeyPlace()) { //packagelen + msgid + magiccode + msgbody
DecodeUserPacketNew(buf, offset, buflen); //2 + 2 + 4+ xx + \0 + xx
} 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; bool warning = false;
while (buflen - offset >= sizeof(f8::PackHead) + GetSecretKeyLen()) { while (buflen - offset >= sizeof(f8::PackHead) + GetSecretKeyLen()) {
long long secret_key = KcpSession::ReadSecretKey(&buf[offset], buflen); long long secret_key = KcpSession::ReadSecretKey(&buf[offset], buflen);
@ -149,37 +124,6 @@ void KcpSession::DecodeUserPacketOld(char* buf, int& offset, unsigned int buflen
} }
if (warning) { if (warning) {
f8::UdpLog::Instance()->Warning("收到kcp client非法数据包1", {}); f8::UdpLog::Instance()->Warning("收到kcp client非法数据包", {});
}
}
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();
~KcpSession(); ~KcpSession();
void Init(int socket_handle, int secret_key_place); void Init(int socket_handle);
void UnInit(); void UnInit();
void Update(long long tick); void Update(long long tick);
@ -19,7 +19,6 @@ public:
int GetSocketHandle() { return socket_handle_; } int GetSocketHandle() { return socket_handle_; }
long long GetSecretKey() { return secret_key_; } long long GetSecretKey() { return secret_key_; }
void* GetSecretKeyDataPtr() { return &secret_key_; } void* GetSecretKeyDataPtr() { return &secret_key_; }
int GetSecretKeyPlace();
void SendClientMsg(char* buf, int buf_len); void SendClientMsg(char* buf, int buf_len);
virtual void OnRecvPacket(a8::UdpPacket* pkt) override; virtual void OnRecvPacket(a8::UdpPacket* pkt) override;
@ -32,14 +31,11 @@ public:
protected: protected:
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override; 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: private:
void UpdateInput(); void UpdateInput();
private: private:
int secret_key_place_ = 0;
int socket_handle_ = 0; int socket_handle_ = 0;
long long secret_key_ = 0; long long secret_key_ = 0;
ikcpcb* kcp_ = nullptr; ikcpcb* kcp_ = nullptr;

View File

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

View File

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

View File

@ -133,31 +133,30 @@ void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt)
}); });
return; return;
} }
if (session->GetSecretKeyPlace()) { session->GetKcpSession()->OnRecvPacket(pkt);
if (pkt->buf_len >= IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) { #if 0
long long secret_key = KcpSession::ReadSecretKey(pkt->buf + IKCP_OVERHEAD, pkt->buf_len); if (pkt->buf_len > IKCP_OVERHEAD + KcpSession::GetSecretKeyLen()) {
if (secret_key == session->GetKcpSession()->GetSecretKey()) { long long secret_key = KcpSession::ReadSecretKey(pkt->buf + IKCP_OVERHEAD, pkt->buf_len);
session->GetKcpSession()->OnRecvPacket(pkt); if (secret_key == session->GetKcpSession()->GetSecretKey()) {
} else { session->GetKcpSession()->OnRecvPacket(pkt);
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d socket_handle%d secret_key:%d secret_error",
{
inet_ntoa(pkt->remote_addr.sin_addr),
pkt->remote_addr.sin_port,
socket_handle,
secret_key
});
}
} else { } else {
f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d buflen:%d bufflen_error", f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d socket_handle%d secret_key:%d secret_error",
{ {
inet_ntoa(pkt->remote_addr.sin_addr), inet_ntoa(pkt->remote_addr.sin_addr),
pkt->remote_addr.sin_port, pkt->remote_addr.sin_port,
pkt->buf_len socket_handle,
secret_key
}); });
} }
} else { } else {
session->GetKcpSession()->OnRecvPacket(pkt); f8::UdpLog::Instance()->Warning("ProcUdpPacket host:%s port:%d buflen:%d bufflen_error",
{
inet_ntoa(pkt->remote_addr.sin_addr),
pkt->remote_addr.sin_port,
pkt->buf_len
});
} }
#endif
} }
void LongSessionMgr::DelSession(int socket_handle) void LongSessionMgr::DelSession(int socket_handle)