令牌协议加解密,编译通过,未测试

This commit is contained in:
nnhy 2016-05-03 03:07:24 +00:00
parent 472611efda
commit c2a02be0c8
4 changed files with 24 additions and 5 deletions

View File

@ -166,5 +166,11 @@ bool Controller::SendInternal(const Message& msg)
msg.Write(ms); msg.Write(ms);
Buffer bs(ms.GetBuffer(), ms.Position()); Buffer bs(ms.GetBuffer(), ms.Position());
return Port->Write(bs, msg.State); //return Port->Write(bs, msg.State);
return SendInternal(bs, msg.State);
}
bool Controller::SendInternal(const Buffer& bs, const void* state)
{
return Port->Write(bs, state);
} }

View File

@ -45,6 +45,7 @@ public:
protected: protected:
bool SendInternal(const Message& msg); bool SendInternal(const Message& msg);
virtual bool SendInternal(const Buffer& bs, const void* state);
}; };
#endif #endif

View File

@ -196,7 +196,7 @@ static bool Decrypt(Buffer& data, const Buffer& pass)
RC4::Encrypt(bs, pass); RC4::Encrypt(bs, pass);
// 新的加密指令最后有2字节的明文校验码 // 新的加密指令最后有2字节的明文校验码
if(ms.Position() + 2 <= ms.Length) if(ms.Position() + 2 > ms.Length)
{ {
debug_printf("不支持旧版本指令解密!"); debug_printf("不支持旧版本指令解密!");
return false; return false;
@ -228,7 +228,9 @@ bool TokenController::OnReceive(Message& msg)
//ShowMessage("Recv$", msg); //ShowMessage("Recv$", msg);
// 加解密。握手不加密,登录响应不加密 // 加解密。握手不加密,登录响应不加密
Encrypt(msg, Key); //Encrypt(msg, Key);
Buffer bs(msg.Data, msg.Length + 2);
if(!Decrypt(bs, Key)) return false;
ShowMessage("Recv", msg); ShowMessage("Recv", msg);
@ -254,7 +256,7 @@ bool TokenController::Send(Message& msg)
ShowMessage("Send", msg); ShowMessage("Send", msg);
// 加解密。握手不加密,登录响应不加密 // 加解密。握手不加密,登录响应不加密
Encrypt(msg, Key); //Encrypt(msg, Key);
// 加入统计 // 加入统计
if(!msg.Reply) StartSendStat(msg.Code); if(!msg.Reply) StartSendStat(msg.Code);
@ -262,6 +264,15 @@ bool TokenController::Send(Message& msg)
return Controller::Send(msg); return Controller::Send(msg);
} }
bool TokenController::SendInternal(const Buffer& bs, const void* state)
{
ByteArray arr(bs.Length() + 2);
if(!Encrypt(arr, Key)) return false;
return Controller::SendInternal(arr, state);
}
void TokenController::ShowMessage(const char* action, const Message& msg) void TokenController::ShowMessage(const char* action, const Message& msg)
{ {
#if MSG_DEBUG #if MSG_DEBUG

View File

@ -21,6 +21,7 @@ protected:
virtual bool Valid(const Message& msg); virtual bool Valid(const Message& msg);
// 接收处理函数 // 接收处理函数
virtual bool OnReceive(Message& msg); virtual bool OnReceive(Message& msg);
virtual bool SendInternal(const Buffer& bs, const void* state);
public: public:
uint Token; // 令牌 uint Token; // 令牌