public bool FillPack(byte[] buffer, ref int offset, ref int count) { if (!this.HeaderBuffer.IsFull) { int fillSize = HeaderBuffer.Fill(buffer, offset, count, true); offset += fillSize; count -= fillSize; if (HeaderBuffer.IsFull) { _ParseHeader();//解析头以后才知道body的长度 BodyBuffer = CBuffer.Create((int)this.Length); } else { //头部数据没接收完成,继续解析 return(false); } } if (!this.BodyBuffer.IsFull) { int fillSize = BodyBuffer.Fill(buffer, offset, count, true); offset += fillSize; count -= fillSize; } return(this.BodyBuffer.IsFull); }
public CBuffer Clone() { CBuffer buf = CBuffer.Create(BufSize); buf.Fill(this); return(buf); }
//public const int MAX_POOL = 100; //public const int BUFFER_SIZE = 1024; //private static readonly CBuffer[] m_Pools = new CBuffer[MAX_POOL]; //public static void ClearEmpty() //{ // lock (m_Pools) // { // for (int i = 0; i < m_Pools.Length; i++) // m_Pools[i] = null; // } //} //private BufferPool() { } public static CBuffer GetBuffer(int bufSize) { CBuffer tempBuffer = null; //lock (m_Pools) //{ // for (int i = 0; i < m_Pools.Length; i++) // { // tempBuffer = m_Pools[i]; // if (tempBuffer != null && tempBuffer.MemorySize >= bufSize) // { // m_Pools[i] = null; // tempBuffer.Reset(bufSize); // break; // } // } //} //if (tempBuffer == null) //{ //int memorySize = System.Math.Max(bufSize, BUFFER_SIZE); tempBuffer = new CBuffer(bufSize, bufSize); //} return(tempBuffer); }
public CBuffer Assembly() { CBuffer buf = CBuffer.Create(HeaderLength + BodyBuffer.Length); buf.Fill(HeaderBuffer); buf.Fill(BodyBuffer); return(buf); }
public KcpNetPack(ushort msgID, ushort msgLen) { HeadID = msgID; Length = msgLen; HeaderBuffer = CBuffer.Create(HeaderLength); _AssemblyHeader(); }
//public T Deserialize<T>() //{ // using (var ms = new MemoryStream(this.BodyBuffer.Bytes, 0, this.BodyBuffer.BufSize)) // return ProtoBuf.Serializer.Deserialize<T>(ms); //} //void Send<T>(Protocol type, T content) where T : Google.Protobuf.IMessage //{ // GameMessage message = new GameMessage(); // message.Type = type; // message.Data = ByteString.CopyFrom(content.ToByteArray()); // _UnityUdpSocket.Send(message.ToByteArray()); //} public static KcpNetPack SerializeToPack <T>(T content, ushort msg_id) where T : Google.Protobuf.IMessage { int size = content.CalculateSize();//注意检查长度不能超过 65,535-HeaderLength KcpNetPack pack = new KcpNetPack(msg_id, (ushort)size); pack.BodyBuffer = CBuffer.Create(size); pack.BodyBuffer.Fill(content.ToByteArray(), size); return(pack); }
public NetPack(uint msgID, uint cmd, uint msgLen) { HeadID = msgID; CMD = cmd; Length = msgLen; HeaderBuffer = CBuffer.Create(HeaderLength); _AssemblyHeader(); }
//public T Deserialize<T>() //{ // using (var ms = new MemoryStream(this.BodyBuffer.Bytes, 0, this.BodyBuffer.BufSize)) // return ProtoBuf.Serializer.Deserialize<T>(ms); //} //void Send<T>(Protocol type, T content) where T : Google.Protobuf.IMessage //{ // GameMessage message = new GameMessage(); // message.Type = type; // message.Data = ByteString.CopyFrom(content.ToByteArray()); // _UnityUdpSocket.Send(message.ToByteArray()); //} public static NetPack SerializeToPack <T>(T content, uint msg_id, uint cmd) where T : Google.Protobuf.IMessage { int size = content.CalculateSize(); NetPack pack = new NetPack(msg_id, cmd, (uint)size); pack.BodyBuffer = CBuffer.Create(size); pack.BodyBuffer.Fill(content.ToByteArray(), size); return(pack); }
public static void ReleaseBuffer(CBuffer buffer) { //if (buffer == null) // return; //lock (m_Pools) //{ // for (int i = 0; i < m_Pools.Length; i++) // { // if (m_Pools[i] == null) // { // m_Pools[i] = buffer; // break; // } // } //} //buffer = null; }
public KcpNetPack() { HeaderBuffer = CBuffer.Create(HeaderLength); }
public int Fill(CBuffer buffer, bool limitBufSize = false) { return(Fill(buffer.Bytes, buffer.Length, limitBufSize)); }