// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** namespace G { using System.IO; using F.Network; /// /// /// public class GameNetworkAdapter : INetworkAdapter { /// /// /// private IConnector _connector; /// /// /// private MemoryStream _stream = new MemoryStream(2048); /// /// /// /// public void Initialize(IConnector connector) { _connector = connector; } public void Close() { } /// /// 获取token接口 /// /// public string GetToken() { return "token"; } public bool SendPing() { _connector.Send(new GamePacket { //id = msg.C2SHeartBeat.MessageType, //data = new msg.C2SHeartBeat() }); return true; } /// /// /// /// /// /// /// public bool Serialize(T packet, Stream destination) where T : IPacket { try { F.IO.LittleEndianBinaryWriter.WriteUInt16(destination, (ushort)packet.id); F.IO.LittleEndianBinaryWriter.WriteUInt16(destination, 0);//ERROR F.IO.LittleEndianBinaryWriter.WriteUInt16(destination, 0);//FLAG _stream.SetLength(0); F.IO.LittleEndianBinaryWriter.WriteString(_stream, GetToken()); //ProtoBuf.Serializer.Serialize(_stream, packet.data); F.IO.LittleEndianBinaryWriter.WriteUInt16(destination, (ushort)_stream.Length);//lENGTH //写入token _stream.WriteTo(destination); return true; } catch (System.Exception ex) { UnityEngine.Debug.LogException(ex); return false; } } /// /// 反序列化包头 /// /// /// /// public IPacketHeader DeserializePacketHeader(Stream source, out object customErrorData) { var header = new GamePacketHeader(); try { header.id = F.IO.LittleEndianBinaryReader.ReadUInt16(source); header.error = F.IO.LittleEndianBinaryReader.ReadUInt16(source); header.packetFlag = F.IO.LittleEndianBinaryReader.ReadUInt16(source); header.packetLength = F.IO.LittleEndianBinaryReader.ReadUInt16(source); customErrorData = null; if (header.packetLength > 1024 * 1024) { customErrorData = "packet length too large."; } } catch (System.Exception ex) { customErrorData = ex.Message; } return header; } /// /// /// /// /// /// /// public IPacket DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData) { var packet = new GamePacket(); try { var header = (GamePacketHeader)packetHeader; if (header.error == GamePacket.ERROR_CODE_OK) { packet.id = header.id; var packetLength = packetHeader.packetLength; if (source.Length - source.Position >= packetLength) { if (_stream.Capacity < packetLength) { _stream.Capacity = ((packetLength >> 10) << 11); } _stream.Position = 0; _stream.SetLength(packetLength); source.Read(_stream.GetBuffer(), 0, packetLength); packet.token = F.IO.LittleEndianBinaryReader.ReadString(_stream); //packet.data = msg.MessageHelper.Deserialize(_stream, header.id); customErrorData = null; } else { customErrorData = "DeserializePacket: packet length is invalid."; } } else { packet.id = header.id; packet.data = null; packet.error = header.error; var packetLength = packetHeader.packetLength; if (source.Length - source.Position >= packetLength) { source.Position += packetLength; } customErrorData = null; } } catch (System.Exception ex) { customErrorData = ex.Message; } return packet; } /// /// 固定长度 /// public int packetHeaderLength { get { return 8; } } } }