|
|
|
@ -0,0 +1,74 @@ |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Net; |
|
|
|
|
using System.Text; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
namespace Core |
|
|
|
|
{ |
|
|
|
|
public class LoginRequestPacket : IPacket |
|
|
|
|
{ |
|
|
|
|
public string Id { get; private set; } |
|
|
|
|
public string NickName { get; private set; } |
|
|
|
|
|
|
|
|
|
public LoginRequestPacket(string id, string nickname) |
|
|
|
|
{ |
|
|
|
|
Id = id; |
|
|
|
|
NickName = nickname; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public LoginRequestPacket(byte[] buffer) |
|
|
|
|
{ |
|
|
|
|
int cursor = 2; |
|
|
|
|
|
|
|
|
|
short idSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor)); |
|
|
|
|
cursor += sizeof(short); |
|
|
|
|
|
|
|
|
|
Id = Encoding.UTF8.GetString(buffer, cursor, idSize); |
|
|
|
|
cursor += idSize; |
|
|
|
|
|
|
|
|
|
short nicknameSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor)); |
|
|
|
|
cursor += sizeof(short); |
|
|
|
|
|
|
|
|
|
NickName = Encoding.UTF8.GetString(buffer, cursor, nicknameSize); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public byte[] Serialize() |
|
|
|
|
{ |
|
|
|
|
// 2bytes header |
|
|
|
|
// data: 2bytes packetType + 2bytes id size + id + 2bytes nickname size + nickname |
|
|
|
|
|
|
|
|
|
byte[] packetType = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)PacketType.LoginRequest)); |
|
|
|
|
byte[] id = Encoding.UTF8.GetBytes(Id); |
|
|
|
|
byte[] idSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)id.Length)); |
|
|
|
|
byte[] nickname = Encoding.UTF8.GetBytes(NickName); |
|
|
|
|
byte[] nicknameSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)nickname.Length)); |
|
|
|
|
|
|
|
|
|
short dataSize = (short)(packetType.Length + id.Length + idSize.Length + nickname.Length + nicknameSize.Length); |
|
|
|
|
byte[] header = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize)); |
|
|
|
|
|
|
|
|
|
byte[] buffer = new byte[2 + dataSize]; |
|
|
|
|
|
|
|
|
|
int cursor = 0; |
|
|
|
|
Array.Copy(header, 0, buffer, cursor, header.Length); |
|
|
|
|
cursor += header.Length; |
|
|
|
|
|
|
|
|
|
Array.Copy(packetType, 0, buffer, cursor, packetType.Length); |
|
|
|
|
cursor += packetType.Length; |
|
|
|
|
|
|
|
|
|
Array.Copy(idSize, 0, buffer, cursor, idSize.Length); |
|
|
|
|
cursor += idSize.Length; |
|
|
|
|
|
|
|
|
|
Array.Copy(id, 0, buffer, cursor, id.Length); |
|
|
|
|
cursor += id.Length; |
|
|
|
|
|
|
|
|
|
Array.Copy(nicknameSize, 0, buffer, cursor, nicknameSize.Length); |
|
|
|
|
cursor += nicknameSize.Length; |
|
|
|
|
|
|
|
|
|
Array.Copy(nickname, 0, buffer, cursor, nickname.Length); |
|
|
|
|
|
|
|
|
|
return buffer; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |