令牌客户端每次心跳,服务器都可能返回服务器时间,客户端借此同步时间

This commit is contained in:
nnhy 2016-11-09 02:26:15 +00:00
parent 0ab8969eea
commit 6c94113e59
5 changed files with 89 additions and 71 deletions

View File

@ -154,45 +154,3 @@ String& HelloMessage::ToStr(String& str) const
return str;
}
#endif
TokenPingMessage::TokenPingMessage()
{
LocalTime = DateTime::Now().TotalMs();
}
TokenPingMessage::TokenPingMessage(const TokenPingMessage& msg)
{
LocalTime = msg.LocalTime;
}
// 从数据流中读取消息
bool TokenPingMessage::Read(Stream& ms)
{
BinaryPair bp(ms);
bp.Get("Time", LocalTime);
return true;
}
// 把消息写入数据流中
void TokenPingMessage::Write(Stream& ms) const
{
BinaryPair bp(ms);
bp.Set("Time", LocalTime);
}
#if DEBUG
// 显示消息内容
String& TokenPingMessage::ToStr(String& str) const
{
str += "Ping";
if (Reply) str += '#';
DateTime dt;
dt.ParseMs(LocalTime);
str += dt;
return str;
}
#endif

View File

@ -42,23 +42,4 @@ public:
#endif
};
class TokenPingMessage : public MessageBase
{
public:
UInt64 LocalTime; // 时间ms
TokenPingMessage();
TokenPingMessage(const TokenPingMessage& msg);
// 从数据流中读取消息
virtual bool Read(Stream& ms);
// 把消息写入数据流中
virtual void Write(Stream& ms) const;
// 显示消息内容
#if DEBUG
virtual String& ToStr(String& str) const;
#endif
};
#endif

View File

@ -12,6 +12,7 @@
#include "LoginMessage.h"
#include "RegisterMessage.h"
#include "TokenDataMessage.h"
#include "TokenPingMessage.h"
#include "ErrorMessage.h"
#include "Security\RC4.h"
@ -702,10 +703,10 @@ void TokenClient::Ping()
// 30秒内发过数据不再发送心跳
if (LastSend > 0 && LastSend + 60000 > Sys.Ms()) return;
TokenPingMessage pinMsg;
TokenPingMessage pm;
TokenMessage msg(3);
pinMsg.WriteMessage(msg);
pm.WriteMessage(msg);
Send(msg);
}
@ -716,14 +717,10 @@ bool TokenClient::OnPing(TokenMessage& msg, TokenController* ctrl)
if (!msg.Reply) return false;
#if DEBUG
TokenPingMessage pinMsg;
pinMsg.ReadMessage(msg);
UInt64 start = pinMsg.LocalTime;
TokenPingMessage pm;
pm.ReadMessage(msg);
//int cost = (int)(Sys.Ms() - start);
// 使用绝对毫秒数,让服务器知道设备本地时间
int cost = (int)(DateTime::Now().TotalMs() - start);
int cost = (int)(DateTime::Now().TotalMs() - pm.LocalTime);
if (Delay)
Delay = (Delay + cost) / 2;
@ -731,7 +728,9 @@ bool TokenClient::OnPing(TokenMessage& msg, TokenController* ctrl)
Delay = cost;
debug_printf("心跳延迟 %dms / %dms \r\n", cost, Delay);
#endif
// 同步本地时间
if (pm.ServerTime > 1000) ((TTime&)Time).SetTime(pm.ServerTime / 1000);
return true;
}

View File

@ -0,0 +1,54 @@
#include "TokenPingMessage.h"
#include "Message\BinaryPair.h"
TokenPingMessage::TokenPingMessage()
{
LocalTime = DateTime::Now().TotalMs();
ServerTime = 0;
}
TokenPingMessage::TokenPingMessage(const TokenPingMessage& msg)
{
LocalTime = msg.LocalTime;
}
// 从数据流中读取消息
bool TokenPingMessage::Read(Stream& ms)
{
BinaryPair bp(ms);
bp.Get("Time", LocalTime);
bp.Get("ServerTime", ServerTime);
return true;
}
// 把消息写入数据流中
void TokenPingMessage::Write(Stream& ms) const
{
BinaryPair bp(ms);
bp.Set("Time", LocalTime);
}
#if DEBUG
// 显示消息内容
String& TokenPingMessage::ToStr(String& str) const
{
str += "Ping";
if (Reply) str += '#';
DateTime dt;
dt.ParseMs(LocalTime);
str = str + " Time=" + dt;
// 服务器响应包含服务器时间
if(Reply)
{
DateTime dt2;
dt2.ParseMs(ServerTime);
str = str + " ServerTime=" + dt2;
}
return str;
}
#endif

View File

@ -0,0 +1,26 @@
#ifndef __TokenPingMessage_H__
#define __TokenPingMessage_H__
#include "Message\MessageBase.h"
class TokenPingMessage : public MessageBase
{
public:
UInt64 LocalTime; // 时间ms
UInt64 ServerTime; // 时间ms
TokenPingMessage();
TokenPingMessage(const TokenPingMessage& msg);
// 从数据流中读取消息
virtual bool Read(Stream& ms);
// 把消息写入数据流中
virtual void Write(Stream& ms) const;
// 显示消息内容
#if DEBUG
virtual String& ToStr(String& str) const;
#endif
};
#endif