public unsafe override void Read(BufferSegment msg) { int offset = 0; ReadBegin(msg, ref offset); ClinetTimeStamp = PacketHelper.Reader.ReadLong64(msg, ref offset);// msg.ReadLong64(); //ServerRecvTimeStamp = msg.ReadLong64(); ServerTimeStamp = PacketHelper.Reader.ReadLong64(msg, ref offset);//msg.ReadLong64(); ReadEnd(msg); }
/// <summary> /// 在第2个字节后插入长度 /// </summary> /// <param name="buf"></param> /// <param name="offset"></param> protected unsafe virtual void WriteEnd(BufferSegment buf, ref int offset) { #if DEBUG11 IRQHelper.WriteLong64(buf, ref offset, this.ExtendData); #endif //插入长度 int len = PACKETLENPOS; PacketHelper.Writer.WriteUShort(buf, ref len, (ushort)(offset - 4)); //this.BufLen = offset;// this.DataLen = (ushort)(offset - 4); // Debug.Assert(this.BufLen <= this.PacketBuf.Length, "写入包的数据尺寸 " + this.BufLen.ToString() + "超过PacketBuf的最大尺寸 " + this.PacketBuf.Length.ToString()); Debug.Assert(offset <= this.Buffer.Length); //if (this.BufLen > this.PacketBuf.Length) { if (offset > this.Buffer.Length) { Logs.Error("Packet WriteEnd.Packet:" + this.ToString() + " \n写入包的数据尺寸 " + offset.ToString() + "超过PacketBuf的最大尺寸 " + this.Buffer.Length.ToString()); throw new PacketWriteException(this, PacketErrorLevel.ERR_DEATH, null); } #if DEBUG11 WriteOK = true; #endif }
protected unsafe void ReadEnd(BufferSegment msg) { //检查长度是否合法 }
/// <summary> /// 读包,读取当前包内容 注意:一个buffersegment内只有一个包 /// </summary> /// <param name="msg"></param> public unsafe abstract void Read(BufferSegment msg);
public static unsafe void WriteLong64(BufferSegment buf, ref int offset, long value) { _writeBytes(buf, offset, (byte*)&value, 8); offset += 8; return; }
public BufferSegment Get(int bufferLen) { BufferSegment buf = BufferManager.GetSegment(bufferLen); return(buf); }
/// <summary> /// Copys the data in this segment to another <see cref="BufferSegment" />. /// </summary> /// <param name="segment">the <see cref="BufferSegment" /> instance to copy to</param> /// <param name="length">the amount of bytes to copy from this segment</param> /// <exception cref="ArgumentException">an ArgumentException will be thrown if length is greater than /// the length of the segment</exception> public void CopyTo(BufferSegment segment, int length) { System.Buffer.BlockCopy(Buffer.Array, Offset, segment.Buffer.Array, segment.Offset, length); }
public unsafe static void WriteByte(BufferSegment buf, ref int offset, byte value) { _writeBytes(buf, offset, (byte *)&value, 1); offset++; }
public static unsafe byte[] ReadBytes1(BufferSegment buf, ref int offset) { int len = ReadByte(buf, ref offset); byte[] ret = new byte[len]; buf.CopyToBytes(0, ret, 0, len); offset += len; return ret; }
public static unsafe byte ReadByte(BufferSegment buf, ref int offset) { byte b = buf[offset]; offset++; return b; }
public static unsafe bool ReadBool(BufferSegment buf, ref int offset) { byte b = buf[offset]; offset++; return (b == 1); }
public SegmentStream(BufferSegment segment) { _segment = segment; m_Position = _segment.Offset; }
unsafe protected virtual void WriteBegin(BufferSegment buf, ref int offset) { PacketHelper.Writer.WriteUShort(buf, ref offset, this.PacketID); //跳过两字节,留个空位用来保存包的总体长度 offset += PACKETLENBYTES; }
/// <summary> /// 读包,读取当前包内容 注意:一个buffersegment内只有一个包 /// </summary> /// <param name="msg"></param> public unsafe abstract void Read(BufferSegment msg);
unsafe protected void ReadEnd(BufferSegment msg) { //检查长度是否合法 }
public unsafe static void WriteInt32(BufferSegment buf, ref int offset, int value) { _writeBytes(buf, offset, (byte *)&value, 4); offset += 4; }
public static unsafe void WriteLong64(BufferSegment buf, ref int offset, long value) { _writeBytes(buf, offset, (byte *)&value, 8); offset += 8; return; }
public static unsafe byte[] ReadBytes4(BufferSegment buf, ref int offset) { int len = ReadInt32(buf, ref offset); byte[] ret = new byte[len]; // Marshal.Copy((IntPtr)(void*)(buf + offset), ret, 0, len); buf.CopyToBytes(offset, ret, 0, len); offset += len; return ret; }
public static unsafe byte PeekByte(BufferSegment buf, int offset) { byte b = buf[offset]; return(b); }
public static unsafe float ReadFloat(BufferSegment buf, ref int offset) { float ret = ToSingle(buf.Buffer.Array, buf.Offset + offset); offset += 4; return ret; }
public SegmentStream(BufferSegment segment) { _segment = segment; m_Position = _segment.Offset; }
public static int ReadInt32(BufferSegment buf, ref int offset) { int ret = ToInt32(buf.Buffer.Array, buf.Offset + offset); offset += 4; return ret; }
public static unsafe void WriteInt32(BufferSegment buf, ref int offset, int value) { _writeBytes(buf, offset, (byte*)&value, 4); offset += 4; }
public static unsafe long ReadLong64(BufferSegment buf, ref int offset) { //if (((buf+offset) % 8) == 0) { long ret = ToInt64(buf.Buffer.Array, buf.Offset + offset);// *(((long*)(buf + offset))); offset += 8; return ret; }
static void nc_OnRecvDataBytes(NetClientBase sender, BufferSegment recvData, int size) { Console.WriteLine("Recived Data:"); Console.WriteLine(NetIOCPClient.Util.Utility.ToHex(0, recvData.Buffer.Array, recvData.Offset, size)); }
public static unsafe string ReadString(BufferSegment buf, ref int offset, int len, Encoding encoding) { // if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) { return string.Empty; } // string str = new string(' ', length); //fixed (char* chRef = str) { char[] chars = encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len); //} string str = new string(chars); offset += len; return str; }
protected unsafe ushort ReadBegin(BufferSegment msg, ref int offset) { ushort packid = PacketHelper.Reader.ReadUShort(msg, ref offset); System.Diagnostics.Debug.Assert(packid == this.PacketID); //检验包ID是否正确 // ushort len = PacketHelper.Reader.ReadUShort(msg, ref offset); //this.BufLen = len + 4; this.DataLen = len; return len; }
public static unsafe string ReadString2(BufferSegment buf, ref int offset, Encoding encoding) { int len = ReadUShort(buf, ref offset); if (len == 0) { return string.Empty; } if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) { return string.Empty; } string str = new string(encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len)); offset += len; return str; }
protected unsafe virtual void WriteBegin(BufferSegment buf, ref int offset) { PacketHelper.Writer.WriteUShort(buf, ref offset, this.PacketID); //跳过两字节,留个空位用来保存包的总体长度 offset += PACKETLENBYTES; }
public static unsafe ushort ReadUShort(BufferSegment buf, ref int offset) { ushort ret = ToUInt16(buf.Buffer.Array, buf.Offset + offset); offset += 2; return ret; }
public static unsafe ushort PeekUShort(BufferSegment buf, int offset) { ushort ret = ToUInt16(buf.Buffer.Array, buf.Offset + offset); return(ret); }
public static unsafe void WriteBool(BufferSegment buf, ref int offset, bool value) { byte b = value ? (byte)1 : (byte)0; _writeBytes(buf, offset, (byte*)&b, 1); offset++; }
public static float PeekFloat(BufferSegment buf, int offset) { return(ToSingle(buf.Buffer.Array, buf.Offset + offset)); }
public static unsafe void WriteByte(BufferSegment buf, ref int offset, byte value) { _writeBytes(buf, offset, (byte*)&value, 1); offset++; }
public static unsafe void WriteUShort(BufferSegment buf, ref int offset, ushort value) { _writeBytes(buf, offset, (byte *)&value, 2); offset += 2; }
public static unsafe void WriteBytes(BufferSegment buf, ref int offset, byte[] value, int size) { if (value == null) { throw new ArgumentNullException("value"); } if (buf.Offset + size > buf.Length) { throw new ArgumentOutOfRangeException("value is out len"); } //Marshal.Copy(value, 0, (IntPtr)(void*)(buf + offset), size); buf.CopyStartFromBytes(offset, value, 0, size); offset += size; }
public unsafe static void WriteFloat(BufferSegment buf, ref int offset, float f) { _writeBytes(buf, offset, (byte *)&f, 4); offset += 4; return; }
public static unsafe void WriteBytes2(BufferSegment buf, ref int offset, byte[] value, int dataoffset, int size) { if (value == null) { throw new ArgumentNullException("value"); } WriteUShort(buf, ref offset, (ushort)size); //Marshal.Copy(value, dataoffset, (IntPtr)(void*)(buf + offset), size); buf.CopyStartFromBytes(offset, value, dataoffset, size); offset += size; }
/// <summary> /// ��һ������ֶη��뵱ǰ����bufferManager�Ļ���� /// </summary> /// <param name="segment"></param> protected internal void CheckIn(BufferSegment segment) { if (m_mgr != null) { m_mgr.CheckIn(segment); } }
public static unsafe void WriteBytes2(BufferSegment buf, ref int offset, byte[] value) { ushort len = (ushort)value.Length; WriteUShort(buf, ref offset, len); //Marshal.Copy(value, 0, (IntPtr)(void*)(buf + offset), len); buf.CopyStartFromBytes(offset, value, 0, value.Length); offset += len; }
private void EnsureCapacity(int size) { // return the old segment and get a new, bigger one var newSegment = BufferManager.GetSegment(size); _segment.CopyTo(newSegment, _length); m_Position = m_Position - _segment.Offset + newSegment.Offset; _segment.DecrementUsage(); _segment = newSegment; }
public static unsafe void WriteBytes4(BufferSegment buf, ref int offset, byte[] value) { int len = value.Length; WriteInt32(buf, ref offset, len); //for (int i = 0; i < value.Length; i++) { // WriteByte(buf, ref offset, value[i]); //} //Marshal.Copy(value, 0, (IntPtr)(void*)(buf + offset), len); buf.CopyStartFromBytes(offset, value, 0, value.Length); offset += len; }
/// <summary> /// Requeues a segment into the buffer pool. /// ���¼��뵽����ء� /// </summary> /// <param name="segment">the segment to requeue</param> public void CheckIn(BufferSegment segment) { if (segment.m_uses > 1) { Logs.Error(string.Format("Checked in segment (Size: {0}, Number: {1}) that is already in use! Queue contains: {2}, Buffer amount: {3}", segment.Length, segment.Number, _availableSegments.Count, _buffers.Count)); } _availableSegments.Enqueue(segment); }
public static unsafe void WriteFloat(BufferSegment buf, ref int offset, float f) { _writeBytes(buf, offset, (byte*)&f, 4); offset += 4; return; }