예제 #1
0
 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);
 }
예제 #2
0
 /// <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
 }
예제 #3
0
 protected unsafe void ReadEnd(BufferSegment msg)
 {
     //检查长度是否合法
 }
예제 #4
0
 /// <summary>
 /// 读包,读取当前包内容 注意:一个buffersegment内只有一个包
 /// </summary>
 /// <param name="msg"></param>
 public unsafe abstract void Read(BufferSegment msg);
예제 #5
0
 public static unsafe void WriteLong64(BufferSegment buf, ref int offset, long value)
 {
     _writeBytes(buf, offset, (byte*)&value, 8);
     offset += 8;
     return;
 }
예제 #6
0
        public BufferSegment Get(int bufferLen)
        {
            BufferSegment buf = BufferManager.GetSegment(bufferLen);

            return(buf);
        }
예제 #7
0
 /// <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);
 }
예제 #8
0
 public unsafe static void WriteByte(BufferSegment buf, ref int offset, byte value)
 {
     _writeBytes(buf, offset, (byte *)&value, 1);
     offset++;
 }
예제 #9
0
 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;
 }
예제 #10
0
 public static unsafe byte ReadByte(BufferSegment buf, ref int offset)
 {
     byte b = buf[offset];
     offset++;
     return b;
 }
예제 #11
0
 public static unsafe bool ReadBool(BufferSegment buf, ref int offset)
 {
     byte b = buf[offset];
     offset++;
     return (b == 1);
 }
예제 #12
0
 public SegmentStream(BufferSegment segment)
 {
     _segment   = segment;
     m_Position = _segment.Offset;
 }
예제 #13
0
 unsafe protected virtual void WriteBegin(BufferSegment buf, ref int offset)
 {
     PacketHelper.Writer.WriteUShort(buf, ref offset, this.PacketID);
     //跳过两字节,留个空位用来保存包的总体长度
     offset += PACKETLENBYTES;
 }
예제 #14
0
 /// <summary>
 /// 读包,读取当前包内容 注意:一个buffersegment内只有一个包
 /// </summary>
 /// <param name="msg"></param>
 public unsafe abstract void Read(BufferSegment msg);
예제 #15
0
 unsafe protected void ReadEnd(BufferSegment msg)
 {
     //检查长度是否合法
 }
예제 #16
0
 public unsafe static void WriteInt32(BufferSegment buf, ref int offset, int value)
 {
     _writeBytes(buf, offset, (byte *)&value, 4);
     offset += 4;
 }
예제 #17
0
 public static unsafe void WriteLong64(BufferSegment buf, ref int offset, long value)
 {
     _writeBytes(buf, offset, (byte *)&value, 8);
     offset += 8;
     return;
 }
예제 #18
0
 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;
 }
예제 #19
0
            public static unsafe byte PeekByte(BufferSegment buf, int offset)
            {
                byte b = buf[offset];

                return(b);
            }
예제 #20
0
 public static unsafe float ReadFloat(BufferSegment buf, ref int offset)
 {
     float ret = ToSingle(buf.Buffer.Array, buf.Offset + offset);
     offset += 4;
     return ret;
 }
예제 #21
0
 public SegmentStream(BufferSegment segment)
 {
     _segment = segment;
     m_Position = _segment.Offset;
 }
예제 #22
0
 public static int ReadInt32(BufferSegment buf, ref int offset)
 {
     int ret = ToInt32(buf.Buffer.Array, buf.Offset + offset);
     offset += 4;
     return ret;
 }
예제 #23
0
 public static unsafe void WriteInt32(BufferSegment buf, ref int offset, int value)
 {
     _writeBytes(buf, offset, (byte*)&value, 4);
     offset += 4;
 }
예제 #24
0
 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;
 }
예제 #25
0
 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));
 }
예제 #26
0
 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;
 }
예제 #27
0
 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;
 }
예제 #28
0
            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;
            }
예제 #29
0
 protected unsafe virtual void WriteBegin(BufferSegment buf, ref int offset)
 {
     PacketHelper.Writer.WriteUShort(buf, ref offset, this.PacketID);
     //跳过两字节,留个空位用来保存包的总体长度
     offset += PACKETLENBYTES;
 }
예제 #30
0
 public static unsafe ushort ReadUShort(BufferSegment buf, ref int offset)
 {
     ushort ret = ToUInt16(buf.Buffer.Array, buf.Offset + offset);
     offset += 2;
     return ret;
 }
예제 #31
0
            public static unsafe ushort PeekUShort(BufferSegment buf, int offset)
            {
                ushort ret = ToUInt16(buf.Buffer.Array, buf.Offset + offset);

                return(ret);
            }
예제 #32
0
 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++;
 }
예제 #33
0
 public static float PeekFloat(BufferSegment buf, int offset)
 {
     return(ToSingle(buf.Buffer.Array, buf.Offset + offset));
 }
예제 #34
0
 public static unsafe void WriteByte(BufferSegment buf, ref int offset, byte value)
 {
     _writeBytes(buf, offset, (byte*)&value, 1);
     offset++;
 }
예제 #35
0
 public static unsafe void WriteUShort(BufferSegment buf, ref int offset, ushort value)
 {
     _writeBytes(buf, offset, (byte *)&value, 2);
     offset += 2;
 }
예제 #36
0
 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;
 }
예제 #37
0
 public unsafe static void WriteFloat(BufferSegment buf, ref int offset, float f)
 {
     _writeBytes(buf, offset, (byte *)&f, 4);
     offset += 4;
     return;
 }
예제 #38
0
 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;
 }
예제 #39
0
 /// <summary>
 /// ��һ������ֶη��뵱ǰ����bufferManager�Ļ����
 /// </summary>
 /// <param name="segment"></param>
 protected internal void CheckIn(BufferSegment segment)
 {
     if (m_mgr != null) {
         m_mgr.CheckIn(segment);
     }
 }
예제 #40
0
 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;
 }
예제 #41
0
        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;
        }
예제 #42
0
 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;
 }
예제 #43
0
        /// <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);
        }
예제 #44
0
 public static unsafe void WriteFloat(BufferSegment buf, ref int offset, float f)
 {
     _writeBytes(buf, offset, (byte*)&f, 4);
     offset += 4;
     return;
 }