123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- using System.Buffers.Binary;
- using System.Runtime.InteropServices;
- namespace System.Net.Sockets.Kcp
- {
- /// <summary>
- /// 调整了没存布局,直接拷贝块提升性能。
- /// <para>结构体保存内容只有一个指针,不用担心参数传递过程中的性能</para>
- /// https://github.com/skywind3000/kcp/issues/118#issuecomment-338133930
- /// <para>不要对没有初始化的KcpSegment(内部指针为0,所有属性都将指向位置区域) 进行任何赋值操作,可能导致内存损坏。
- /// 出于性能考虑,没有对此项进行安全检查。</para>
- /// </summary>
- public struct KcpSegment : IKcpSegment
- {
- internal readonly unsafe byte* ptr;
- public unsafe KcpSegment(byte* intPtr, uint appendDateSize)
- {
- this.ptr = intPtr;
- len = appendDateSize;
- }
- /// <summary>
- /// 使用完必须显示释放,否则内存泄漏
- /// </summary>
- /// <param name="appendDateSize"></param>
- /// <returns></returns>
- public static KcpSegment AllocHGlobal(int appendDateSize)
- {
- var total = LocalOffset + HeadOffset + appendDateSize;
- IntPtr intPtr = Marshal.AllocHGlobal(total);
- unsafe
- {
- ///清零 不知道是不是有更快的清0方法?
- Span<byte> span = new Span<byte>(intPtr.ToPointer(), total);
- span.Clear();
- return new KcpSegment((byte*)intPtr.ToPointer(), (uint)appendDateSize);
- }
- }
- /// <summary>
- /// 释放非托管内存
- /// </summary>
- /// <param name="seg"></param>
- public static void FreeHGlobal(KcpSegment seg)
- {
- unsafe
- {
- Marshal.FreeHGlobal((IntPtr)seg.ptr);
- }
- }
- /// 以下为本机使用的参数
- /// <summary>
- /// offset = 0
- /// </summary>
- public uint resendts
- {
- get
- {
- unsafe
- {
- return *(uint*)(ptr + 0);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(ptr + 0) = value;
- }
- }
- }
- /// <summary>
- /// offset = 4
- /// </summary>
- public uint rto
- {
- get
- {
- unsafe
- {
- return *(uint*)(ptr + 4);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(ptr + 4) = value;
- }
- }
- }
- /// <summary>
- /// offset = 8
- /// </summary>
- public uint fastack
- {
- get
- {
- unsafe
- {
- return *(uint*)(ptr + 8);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(ptr + 8) = value;
- }
- }
- }
- /// <summary>
- /// offset = 12
- /// </summary>
- public uint xmit
- {
- get
- {
- unsafe
- {
- return *(uint*)(ptr + 12);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(ptr + 12) = value;
- }
- }
- }
- ///以下为需要网络传输的参数
- public const int LocalOffset = 4 * 4;
- public const int HeadOffset = KcpConst.IKCP_OVERHEAD;
- /// <summary>
- /// offset = <see cref="LocalOffset"/>
- /// </summary>
- /// https://github.com/skywind3000/kcp/issues/134
- public uint conv
- {
- get
- {
- unsafe
- {
- return *(uint*)(LocalOffset + 0 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(LocalOffset + 0 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// offset = <see cref="LocalOffset"/> + 4
- /// </summary>
- public byte cmd
- {
- get
- {
- unsafe
- {
- return *(LocalOffset + 4 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(LocalOffset + 4 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// offset = <see cref="LocalOffset"/> + 5
- /// </summary>
- public byte frg
- {
- get
- {
- unsafe
- {
- return *(LocalOffset + 5 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(LocalOffset + 5 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// offset = <see cref="LocalOffset"/> + 6
- /// </summary>
- public ushort wnd
- {
- get
- {
- unsafe
- {
- return *(ushort*)(LocalOffset + 6 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(ushort*)(LocalOffset + 6 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// offset = <see cref="LocalOffset"/> + 8
- /// </summary>
- public uint ts
- {
- get
- {
- unsafe
- {
- return *(uint*)(LocalOffset + 8 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(LocalOffset + 8 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// <para> SendNumber? </para>
- /// offset = <see cref="LocalOffset"/> + 12
- /// </summary>
- public uint sn
- {
- get
- {
- unsafe
- {
- return *(uint*)(LocalOffset + 12 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(LocalOffset + 12 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// offset = <see cref="LocalOffset"/> + 16
- /// </summary>
- public uint una
- {
- get
- {
- unsafe
- {
- return *(uint*)(LocalOffset + 16 + ptr);
- }
- }
- set
- {
- unsafe
- {
- *(uint*)(LocalOffset + 16 + ptr) = value;
- }
- }
- }
- /// <summary>
- /// <para> AppendDateSize </para>
- /// offset = <see cref="LocalOffset"/> + 20
- /// </summary>
- public uint len
- {
- get
- {
- unsafe
- {
- return *(uint*)(LocalOffset + 20 + ptr);
- }
- }
- private set
- {
- unsafe
- {
- *(uint*)(LocalOffset + 20 + ptr) = value;
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// https://github.com/skywind3000/kcp/issues/35#issuecomment-263770736
- public Span<byte> data
- {
- get
- {
- unsafe
- {
- return new Span<byte>(LocalOffset + HeadOffset + ptr, (int)len);
- }
- }
- }
- /// <summary>
- /// 将片段中的要发送的数据拷贝到指定缓冲区
- /// </summary>
- /// <param name="buffer"></param>
- /// <returns></returns>
- public int Encode(Span<byte> buffer)
- {
- var datelen = (int)(HeadOffset + len);
- ///备用偏移值 现阶段没有使用
- const int offset = 0;
- if (KcpConst.IsLittleEndian)
- {
- if (BitConverter.IsLittleEndian)
- {
- ///小端可以一次拷贝
- unsafe
- {
- ///要发送的数据从LocalOffset开始。
- ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
- Span<byte> sendDate = new Span<byte>(ptr + LocalOffset, datelen);
- sendDate.CopyTo(buffer);
- }
- }
- else
- {
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), conv);
- buffer[offset + 4] = cmd;
- buffer[offset + 5] = frg;
- BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(offset + 6), wnd);
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 8), ts);
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 12), sn);
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 16), una);
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 20), len);
- data.CopyTo(buffer.Slice(HeadOffset));
- }
- }
- else
- {
- if (BitConverter.IsLittleEndian)
- {
- BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset), conv);
- buffer[offset + 4] = cmd;
- buffer[offset + 5] = frg;
- BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(offset + 6), wnd);
- BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 8), ts);
- BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 12), sn);
- BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 16), una);
- BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 20), len);
- data.CopyTo(buffer.Slice(HeadOffset));
- }
- else
- {
- ///大端可以一次拷贝
- unsafe
- {
- ///要发送的数据从LocalOffset开始。
- ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
- Span<byte> sendDate = new Span<byte>(ptr + LocalOffset, datelen);
- sendDate.CopyTo(buffer);
- }
- }
- }
- return datelen;
- }
- }
- }
|