mirror of https://github.com/icedream/npsharp.git
Refactoring ticket and adding second constructor + serializer function for RPC stream usage.
parent
030f2b5f7c
commit
edd8482d63
|
@ -0,0 +1,61 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace NPSharp.RPC.Messages.Data
|
||||||
|
{
|
||||||
|
internal class Ticket
|
||||||
|
{
|
||||||
|
public Ticket(byte[] data)
|
||||||
|
{
|
||||||
|
if (data.Length < sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data buffer too short");
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var ms = new MemoryStream(data))
|
||||||
|
using (var br = new BinaryReader(ms))
|
||||||
|
{
|
||||||
|
Version = br.ReadUInt32();
|
||||||
|
ClientID = br.ReadUInt64();
|
||||||
|
ServerID = br.ReadUInt64();
|
||||||
|
Time = br.ReadUInt32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Maybe leave out arguments which are supposed to be autofilled
|
||||||
|
public Ticket(uint version, ulong clientID, ulong serverID, uint? time = null)
|
||||||
|
{
|
||||||
|
Version = version;
|
||||||
|
ClientID = clientID;
|
||||||
|
ServerID = serverID;
|
||||||
|
if (time.HasValue)
|
||||||
|
Time = time.Value;
|
||||||
|
else
|
||||||
|
Time = (uint)DateTime.Now.ToUniversalTime().ToBinary();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Serialize()
|
||||||
|
{
|
||||||
|
using (var ms = new MemoryStream(sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint)))
|
||||||
|
{
|
||||||
|
using (var bw = new BinaryWriter(ms))
|
||||||
|
{
|
||||||
|
bw.Write(Version);
|
||||||
|
bw.Write(ClientID);
|
||||||
|
bw.Write(ServerID);
|
||||||
|
bw.Write(Time);
|
||||||
|
ms.Seek(0, SeekOrigin.Begin);
|
||||||
|
return ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public uint Version { get; private set; }
|
||||||
|
|
||||||
|
public ulong ClientID { get; private set; }
|
||||||
|
|
||||||
|
public ulong ServerID { get; private set; }
|
||||||
|
|
||||||
|
public uint Time { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,33 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace NPSharp
|
|
||||||
{
|
|
||||||
internal class Ticket
|
|
||||||
{
|
|
||||||
public Ticket(byte[] data)
|
|
||||||
{
|
|
||||||
if (data.Length < sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint))
|
|
||||||
{
|
|
||||||
throw new ArgumentException("Data buffer too short");
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var ms = new MemoryStream(data))
|
|
||||||
using (var br = new BinaryReader(ms))
|
|
||||||
{
|
|
||||||
Version = br.ReadUInt32();
|
|
||||||
ClientID = br.ReadUInt64();
|
|
||||||
ServerID = br.ReadUInt64();
|
|
||||||
Time = br.ReadUInt32();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public uint Version { get; private set; }
|
|
||||||
|
|
||||||
public ulong ClientID { get; private set; }
|
|
||||||
|
|
||||||
public ulong ServerID { get; private set; }
|
|
||||||
|
|
||||||
public uint Time { get; private set; }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue