[M] sync n3
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class TDTimeout : MonoBehaviour
|
||||
{
|
||||
// Use this for initialization
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static void SetTimeout(int timeout, Action<object> action, object obj)
|
||||
{
|
||||
GameObject gameObject = new GameObject("TDTimeout");
|
||||
var tdTimeout = gameObject.AddComponent<TDTimeout>();
|
||||
tdTimeout._setTimeout(timeout, action, obj);
|
||||
}
|
||||
|
||||
private void _setTimeout(int timeout, Action<object> action, object obj)
|
||||
{
|
||||
StartCoroutine(_wait(timeout, action, obj));
|
||||
}
|
||||
|
||||
private IEnumerator _wait(int timeout, Action<object> action, object obj)
|
||||
{
|
||||
yield return new WaitForSeconds(timeout);
|
||||
if (action != null)
|
||||
{
|
||||
action(obj);
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c4b6a31edd864c43a12c23d11ac6ddd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using ThinkingSDK.PC.Utils;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKCalibratedTime : ThinkingSDKTimeInter
|
||||
{
|
||||
private ThinkingSDKTimeCalibration mCalibratedTime;
|
||||
private long mSystemElapsedRealtime;
|
||||
private TimeZoneInfo mTimeZone;
|
||||
private DateTime mDate;
|
||||
public ThinkingSDKCalibratedTime(ThinkingSDKTimeCalibration calibrateTimeInter,TimeZoneInfo timeZoneInfo)
|
||||
{
|
||||
this.mCalibratedTime = calibrateTimeInter;
|
||||
this.mTimeZone = timeZoneInfo;
|
||||
this.mDate = mCalibratedTime.NowDate();
|
||||
}
|
||||
public string GetTime(TimeZoneInfo timeZone)
|
||||
{
|
||||
if (timeZone == null)
|
||||
{
|
||||
return ThinkingSDKUtil.FormatDate(mDate, mTimeZone);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ThinkingSDKUtil.FormatDate(mDate, timeZone);
|
||||
}
|
||||
}
|
||||
|
||||
public double GetZoneOffset(TimeZoneInfo timeZone)
|
||||
{
|
||||
if (timeZone == null)
|
||||
{
|
||||
return ThinkingSDKUtil.ZoneOffset(mDate, mTimeZone);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ThinkingSDKUtil.ZoneOffset(mDate, timeZone);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 46014ab35934a4a90b8329f357801cdf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKDefinedTime : ThinkingSDKTimeInter
|
||||
{
|
||||
private string mTime;
|
||||
private double mZoneOffset;
|
||||
public ThinkingSDKDefinedTime(string time,double zoneOffset)
|
||||
{
|
||||
this.mTime = time;
|
||||
this.mZoneOffset = zoneOffset;
|
||||
}
|
||||
public string GetTime(TimeZoneInfo timeZone)
|
||||
{
|
||||
return this.mTime;
|
||||
}
|
||||
|
||||
public double GetZoneOffset(TimeZoneInfo timeZone)
|
||||
{
|
||||
return this.mZoneOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86eabc3f1cff146f89092cd516702db9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Linq;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKNTPCalibration : ThinkingSDKTimeCalibration
|
||||
{
|
||||
public ThinkingSDKNTPCalibration(string ntpServer) {
|
||||
double totalMilliseconds = ConvertDateTimeInt(DateTime.UtcNow);
|
||||
this.mStartTime = (long)totalMilliseconds;
|
||||
this.mSystemElapsedRealtime = Environment.TickCount;
|
||||
|
||||
// request scoket time
|
||||
Socket socket = GetNetworkTimeSync(ntpServer, this);
|
||||
// set scoket timeout
|
||||
TDTimeout.SetTimeout(3, new Action<object>(ScoketTimeout), (object)socket);
|
||||
}
|
||||
|
||||
private void ScoketTimeout(object obj)
|
||||
{
|
||||
if (obj is Socket)
|
||||
{
|
||||
Socket socket = (Socket)obj;
|
||||
if (socket.Connected == true)
|
||||
{
|
||||
socket.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static new double ConvertDateTimeInt(System.DateTime time)
|
||||
{
|
||||
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
return (double)(time - startTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
private static Socket GetNetworkTimeSync(string ntpServer, ThinkingSDKTimeCalibration timeCalibration)
|
||||
{
|
||||
// NTP message size - 16 bytes of the digest (RFC 2030)
|
||||
var ntpData = new byte[48];
|
||||
|
||||
//Setting the Leap Indicator, Version Number and Mode values
|
||||
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
|
||||
|
||||
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
|
||||
var addressFirst = addresses.First(e => e.AddressFamily == AddressFamily.InterNetwork);
|
||||
if (addressFirst == null)
|
||||
{
|
||||
addressFirst = addresses[0];
|
||||
}
|
||||
|
||||
//The UDP port number assigned to NTP is 123
|
||||
var ipEndPoint = new IPEndPoint(addressFirst, 123);
|
||||
//NTP uses UDP
|
||||
var socket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
|
||||
|
||||
socket.Connect(ipEndPoint);
|
||||
|
||||
SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
|
||||
socketAsyncEventArgs.SetBuffer(ntpData, 0, ntpData.Length);
|
||||
socketAsyncEventArgs.UserToken = timeCalibration;
|
||||
socketAsyncEventArgs.RemoteEndPoint = ipEndPoint;
|
||||
socketAsyncEventArgs.Completed += SocketAsyncEventArgs_Completed;
|
||||
// send socket request
|
||||
socket.SendAsync(socketAsyncEventArgs);
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
private static void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs eventArgs)
|
||||
{
|
||||
Socket socket = (Socket)sender;
|
||||
if (eventArgs.SocketError == SocketError.Success)
|
||||
{
|
||||
if (eventArgs.LastOperation == SocketAsyncOperation.Send)
|
||||
{
|
||||
socket.ReceiveAsync(eventArgs);
|
||||
}
|
||||
else if (eventArgs.LastOperation == SocketAsyncOperation.Receive)
|
||||
{
|
||||
if (eventArgs.SocketError == SocketError.Success && eventArgs.Buffer.Length > 0)
|
||||
{
|
||||
DateTime ntpTime = ParseDateTimeWithNTPData(eventArgs.Buffer);
|
||||
double totalMilliseconds = ConvertDateTimeInt(ntpTime);
|
||||
ThinkingSDKTimeCalibration timeCalibration = (ThinkingSDKTimeCalibration)eventArgs.UserToken;
|
||||
timeCalibration.mStartTime = (long)totalMilliseconds;
|
||||
}
|
||||
socket.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
socket.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
socket.Close();
|
||||
}
|
||||
}
|
||||
|
||||
static uint SwapEndianness(ulong x)
|
||||
{
|
||||
return (uint)(((x & 0x000000ff) << 24) + ((x & 0x0000ff00) << 8) + ((x & 0x00ff0000) >> 8) + ((x & 0xff000000) >> 24));
|
||||
}
|
||||
|
||||
private static DateTime ParseDateTimeWithNTPData(byte[] ntpData)
|
||||
{
|
||||
//Offset to get to the "Transmit Timestamp" field (time at which the reply
|
||||
//departed the server for the client, in 64-bit timestamp format."
|
||||
const byte serverReplyTime = 40;
|
||||
|
||||
//Get the seconds part
|
||||
ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
|
||||
|
||||
//Get the seconds fraction
|
||||
ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
|
||||
|
||||
//Convert From big-endian to little-endian
|
||||
intPart = SwapEndianness(intPart);
|
||||
fractPart = SwapEndianness(fractPart);
|
||||
|
||||
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
|
||||
|
||||
//**UTC** time
|
||||
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
|
||||
return networkDateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 610a0d13d17714116b2d436c4daf69a9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using ThinkingSDK.PC.Utils;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKTime : ThinkingSDKTimeInter
|
||||
{
|
||||
private TimeZoneInfo mTimeZone;
|
||||
private DateTime mDate;
|
||||
|
||||
public ThinkingSDKTime(TimeZoneInfo timezone, DateTime date)
|
||||
{
|
||||
this.mTimeZone = timezone;
|
||||
this.mDate = date;
|
||||
}
|
||||
|
||||
public string GetTime(TimeZoneInfo timeZone)
|
||||
{
|
||||
if (timeZone == null)
|
||||
{
|
||||
return ThinkingSDKUtil.FormatDate(mDate, mTimeZone);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ThinkingSDKUtil.FormatDate(mDate, timeZone);
|
||||
}
|
||||
}
|
||||
|
||||
public double GetZoneOffset(TimeZoneInfo timeZone)
|
||||
{
|
||||
if (timeZone == null)
|
||||
{
|
||||
return ThinkingSDKUtil.ZoneOffset(mDate, mTimeZone);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ThinkingSDKUtil.ZoneOffset(mDate, timeZone);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 482ba0db5b92b4f9e9475acfda6e496c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKTimeCalibration
|
||||
{
|
||||
/// <summary>
|
||||
/// Timestamp when time was calibrated
|
||||
/// </summary>
|
||||
public long mStartTime;
|
||||
/// <summary>
|
||||
/// System boot time when calibrating time
|
||||
/// </summary>
|
||||
public long mSystemElapsedRealtime;
|
||||
public DateTime NowDate()
|
||||
{
|
||||
long nowTime = Environment.TickCount;
|
||||
long timestamp = nowTime - this.mSystemElapsedRealtime + this.mStartTime;
|
||||
// DateTime dt = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime;
|
||||
// return dt;
|
||||
|
||||
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
return dt.AddMilliseconds(timestamp);
|
||||
}
|
||||
|
||||
protected static double ConvertDateTimeInt(System.DateTime time)
|
||||
{
|
||||
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
return (double)(time - startTime).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9bb8b764cd4d47bd91702ea2c9a5ad5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public interface ThinkingSDKTimeInter
|
||||
{
|
||||
string GetTime(TimeZoneInfo timeZone);
|
||||
Double GetZoneOffset(TimeZoneInfo timeZone);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0cc5c74f7de224d3aa2fa4668ae7a9f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using ThinkingSDK.PC.Config;
|
||||
using ThinkingSDK.PC.Utils;
|
||||
|
||||
namespace ThinkingSDK.PC.Time
|
||||
{
|
||||
public class ThinkingSDKTimestampCalibration : ThinkingSDKTimeCalibration
|
||||
{
|
||||
|
||||
public ThinkingSDKTimestampCalibration(long timestamp)
|
||||
{
|
||||
DateTime dateTimeUtcNow = DateTime.UtcNow;
|
||||
this.mStartTime = timestamp;
|
||||
this.mSystemElapsedRealtime = Environment.TickCount;
|
||||
|
||||
double time_offset = (ConvertDateTimeInt(dateTimeUtcNow) - timestamp) / 1000;
|
||||
if (ThinkingSDKPublicConfig.IsPrintLog()) ThinkingSDKLogger.Print("Time Calibration with NTP (" + timestamp + "), diff = " + time_offset.ToString("0.000s"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aef0ebc4f720643f3878a26fc71b6ddd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user