| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568 |
- using System;
- using System.Diagnostics.CodeAnalysis;
- using System.Numerics;
- using System.Runtime.CompilerServices;
- using System.Text;
- namespace Bright.Serialization
- {
- public enum EDeserializeError
- {
- OK,
- NOT_ENOUGH,
- EXCEED_SIZE,
- // UNMARSHAL_ERR,
- }
- public class SerializationException : Exception
- {
- public SerializationException() { }
- public SerializationException(string msg) : base(msg) { }
- public SerializationException(string message, Exception innerException) : base(message, innerException)
- {
- }
- }
- public readonly struct SegmentSaveState
- {
- public SegmentSaveState(int readerIndex, int writerIndex)
- {
- ReaderIndex = readerIndex;
- WriterIndex = writerIndex;
- }
- public int ReaderIndex { get; }
- public int WriterIndex { get; }
- }
- public sealed class ByteBuf : ICloneable, IEquatable<ByteBuf>
- {
- public ByteBuf()
- {
- Bytes = Array.Empty<byte>();
- ReaderIndex = WriterIndex = 0;
- }
- public ByteBuf(int capacity)
- {
- Bytes = capacity > 0 ? new byte[capacity] : Array.Empty<byte>();
- ReaderIndex = 0;
- WriterIndex = 0;
- }
- public ByteBuf(byte[] bytes)
- {
- Bytes = bytes;
- ReaderIndex = 0;
- WriterIndex = Capacity;
- }
- public ByteBuf(byte[] bytes, int readIndex, int writeIndex)
- {
- Bytes = bytes;
- ReaderIndex = readIndex;
- WriterIndex = writeIndex;
- }
- public ByteBuf(int capacity, Action<ByteBuf> releaser) : this(capacity)
- {
- _releaser = releaser;
- }
- public static ByteBuf Wrap(byte[] bytes)
- {
- return new ByteBuf(bytes, 0, bytes.Length);
- }
- public void Replace(byte[] bytes)
- {
- Bytes = bytes;
- ReaderIndex = 0;
- WriterIndex = Capacity;
- }
- public void Replace(byte[] bytes, int beginPos, int endPos)
- {
- Bytes = bytes;
- ReaderIndex = beginPos;
- WriterIndex = endPos;
- }
- public int ReaderIndex { get; set; }
- public int WriterIndex { get; set; }
- private readonly Action<ByteBuf> _releaser;
- public int Capacity => Bytes.Length;
- public int Size { get { return WriterIndex - ReaderIndex; } }
- public bool Empty => WriterIndex <= ReaderIndex;
- public bool NotEmpty => WriterIndex > ReaderIndex;
- public void AddWriteIndex(int add)
- {
- WriterIndex += add;
- }
- public void AddReadIndex(int add)
- {
- ReaderIndex += add;
- }
- #pragma warning disable CA1819 // 属性不应返回数组
- public byte[] Bytes { get; private set; }
- #pragma warning restore CA1819 // 属性不应返回数组
- public byte[] CopyData()
- {
- var n = Remaining;
- if (n > 0)
- {
- var arr = new byte[n];
- Buffer.BlockCopy(Bytes, ReaderIndex, arr, 0, n);
- return arr;
- }
- else
- {
- return Array.Empty<byte>();
- }
- }
- public int Remaining { get { return WriterIndex - ReaderIndex; } }
- public void DiscardReadBytes()
- {
- WriterIndex -= ReaderIndex;
- Array.Copy(Bytes, ReaderIndex, Bytes, 0, WriterIndex);
- ReaderIndex = 0;
- }
- public int NotCompactWritable { get { return Capacity - WriterIndex; } }
- public void WriteBytesWithoutSize(byte[] bs)
- {
- WriteBytesWithoutSize(bs, 0, bs.Length);
- }
- public void WriteBytesWithoutSize(byte[] bs, int offset, int len)
- {
- EnsureWrite(len);
- Buffer.BlockCopy(bs, offset, Bytes, WriterIndex, len);
- WriterIndex += len;
- }
- public void Clear()
- {
- ReaderIndex = WriterIndex = 0;
- }
- private const int MIN_CAPACITY = 16;
- private static int PropSize(int initSize, int needSize)
- {
- for (int i = Math.Max(initSize, MIN_CAPACITY); ; i <<= 1)
- {
- if (i >= needSize)
- {
- return i;
- }
- }
- }
- private void EnsureWrite0(int size)
- {
- var needSize = WriterIndex + size - ReaderIndex;
- if (needSize < Capacity)
- {
- WriterIndex -= ReaderIndex;
- Array.Copy(Bytes, ReaderIndex, Bytes, 0, WriterIndex);
- ReaderIndex = 0;
- }
- else
- {
- int newCapacity = PropSize(Capacity, needSize);
- var newBytes = new byte[newCapacity];
- WriterIndex -= ReaderIndex;
- Buffer.BlockCopy(Bytes, ReaderIndex, newBytes, 0, WriterIndex);
- ReaderIndex = 0;
- Bytes = newBytes;
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void EnsureWrite(int size)
- {
- if (WriterIndex + size > Capacity)
- {
- EnsureWrite0(size);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void EnsureRead(int size)
- {
- if (ReaderIndex + size > WriterIndex)
- {
- throw new SerializationException();
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private bool CanRead(int size)
- {
- return (ReaderIndex + size <= WriterIndex);
- }
- public void Append(byte x)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = x;
- }
- public void WriteBool(bool b)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = (byte)(b ? 1 : 0);
- }
- public bool ReadBool()
- {
- EnsureRead(1);
- return Bytes[ReaderIndex++] != 0;
- }
- public void WriteByte(byte x)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = x;
- }
- public byte ReadByte()
- {
- EnsureRead(1);
- return Bytes[ReaderIndex++];
- }
- public void WriteShort(short x)
- {
- if (x >= 0)
- {
- if (x < 0x80)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = (byte)x;
- return;
- }
- else if (x < 0x4000)
- {
- EnsureWrite(2);
- Bytes[WriterIndex + 1] = (byte)x;
- Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
- WriterIndex += 2;
- return;
- }
- }
- EnsureWrite(3);
- Bytes[WriterIndex] = 0xff;
- Bytes[WriterIndex + 2] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- WriterIndex += 3;
- }
- public short ReadShort()
- {
- EnsureRead(1);
- int h = Bytes[ReaderIndex];
- if (h < 0x80)
- {
- ReaderIndex++;
- return (short)h;
- }
- else if (h < 0xc0)
- {
- EnsureRead(2);
- int x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
- ReaderIndex += 2;
- return (short)x;
- }
- else if ((h == 0xff))
- {
- EnsureRead(3);
- int x = (Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- ReaderIndex += 3;
- return (short)x;
- }
- else
- {
- throw new SerializationException();
- }
- }
- public short ReadFshort()
- {
- EnsureRead(2);
- short x;
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[ReaderIndex])
- {
- x = *(short*)b;
- }
- }
- #else
- x = (short)((Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex]);
- #endif
- ReaderIndex += 2;
- return x;
- }
- public void WriteFshort(short x)
- {
- EnsureWrite(2);
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[WriterIndex])
- {
- *(short*)b = x;
- }
- }
- #else
- Bytes[WriterIndex] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- #endif
- WriterIndex += 2;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void WriteInt(int x)
- {
- WriteUint((uint)x);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public int ReadInt()
- {
- return (int)ReadUint();
- }
- public void WriteUint(uint x)
- {
- // 如果有修改,记得也把 EndWriteSegment改了
- // 0 111 1111
- if (x < 0x80)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = (byte)x;
- }
- else if (x < 0x4000) // 10 11 1111, -
- {
- EnsureWrite(2);
- Bytes[WriterIndex + 1] = (byte)x;
- Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
- WriterIndex += 2;
- }
- else if (x < 0x200000) // 110 1 1111, -,-
- {
- EnsureWrite(3);
- Bytes[WriterIndex + 2] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- Bytes[WriterIndex] = (byte)((x >> 16) | 0xc0);
- WriterIndex += 3;
- }
- else if (x < 0x10000000) // 1110 1111,-,-,-
- {
- EnsureWrite(4);
- Bytes[WriterIndex + 3] = (byte)x;
- Bytes[WriterIndex + 2] = (byte)(x >> 8);
- Bytes[WriterIndex + 1] = (byte)(x >> 16);
- Bytes[WriterIndex] = (byte)((x >> 24) | 0xe0);
- WriterIndex += 4;
- }
- else
- {
- EnsureWrite(5);
- Bytes[WriterIndex] = 0xf0;
- Bytes[WriterIndex + 4] = (byte)x;
- Bytes[WriterIndex + 3] = (byte)(x >> 8);
- Bytes[WriterIndex + 2] = (byte)(x >> 16);
- Bytes[WriterIndex + 1] = (byte)(x >> 24);
- WriterIndex += 5;
- }
- }
- public uint ReadUint()
- {
- ///
- /// 警告! 如有修改,记得调整 TryDeserializeInplaceOctets
- EnsureRead(1);
- uint h = Bytes[ReaderIndex];
- if (h < 0x80)
- {
- ReaderIndex++;
- return h;
- }
- else if (h < 0xc0)
- {
- EnsureRead(2);
- uint x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
- ReaderIndex += 2;
- return x;
- }
- else if (h < 0xe0)
- {
- EnsureRead(3);
- uint x = ((h & 0x1f) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- ReaderIndex += 3;
- return x;
- }
- else if (h < 0xf0)
- {
- EnsureRead(4);
- uint x = ((h & 0x0f) << 24) | ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
- ReaderIndex += 4;
- return x;
- }
- else
- {
- EnsureRead(5);
- uint x = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)(Bytes[ReaderIndex + 2] << 16)) | ((uint)Bytes[ReaderIndex + 3] << 8) | Bytes[ReaderIndex + 4];
- ReaderIndex += 5;
- return x;
- }
- }
- public unsafe void WriteUint_Unsafe(uint x)
- {
- // 0 111 1111
- if (x < 0x80)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = (byte)(x << 1);
- }
- else if (x < 0x4000)// 10 11 1111, -
- {
- EnsureWrite(2);
- fixed (byte* wb = &Bytes[WriterIndex])
- {
- *(uint*)(wb) = (x << 2 | 0b01);
- }
- WriterIndex += 2;
- }
- else if (x < 0x200000) // 110 1 1111, -,-
- {
- EnsureWrite(3);
- fixed (byte* wb = &Bytes[WriterIndex])
- {
- *(uint*)(wb) = (x << 3 | 0b011);
- }
- WriterIndex += 3;
- }
- else if (x < 0x10000000) // 1110 1111,-,-,-
- {
- EnsureWrite(4);
- fixed (byte* wb = &Bytes[WriterIndex])
- {
- *(uint*)(wb) = (x << 4 | 0b0111);
- }
- WriterIndex += 4;
- }
- else
- {
- EnsureWrite(5);
- fixed (byte* wb = &Bytes[WriterIndex])
- {
- *(uint*)(wb) = (x << 5 | 0b01111);
- }
- WriterIndex += 5;
- }
- }
- public unsafe uint ReadUint_Unsafe()
- {
- ///
- /// 警告! 如有修改,记得调整 TryDeserializeInplaceOctets
- EnsureRead(1);
- uint h = Bytes[ReaderIndex];
- if ((h & 0b1) == 0b0)
- {
- ReaderIndex++;
- return (h >> 1);
- }
- else if ((h & 0b11) == 0b01)
- {
- EnsureRead(2);
- fixed (byte* rb = &Bytes[ReaderIndex])
- {
- ReaderIndex += 2;
- return (*(uint*)rb) >> 2;
- }
- }
- else if ((h & 0b111) == 0b011)
- {
- EnsureRead(3);
- fixed (byte* rb = &Bytes[ReaderIndex])
- {
- ReaderIndex += 3;
- return (*(uint*)rb) >> 3;
- }
- }
- else if ((h & 0b1111) == 0b0111)
- {
- EnsureRead(4);
- fixed (byte* rb = &Bytes[ReaderIndex])
- {
- ReaderIndex += 4;
- return (*(uint*)rb) >> 4;
- }
- }
- else
- {
- EnsureRead(5);
- fixed (byte* rb = &Bytes[ReaderIndex])
- {
- ReaderIndex += 5;
- return (*(uint*)rb) >> 5;
- }
- }
- }
- public int ReadFint()
- {
- EnsureRead(4);
- int x;
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[ReaderIndex])
- {
- x = *(int*)b;
- }
- }
- #else
- x = (Bytes[ReaderIndex + 3] << 24) | (Bytes[ReaderIndex + 2] << 16) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
- #endif
- ReaderIndex += 4;
- return x;
- }
- public void WriteFint(int x)
- {
- EnsureWrite(4);
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[WriterIndex])
- {
- *(int*)b = x;
- }
- }
- #else
- Bytes[WriterIndex] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- Bytes[WriterIndex + 2] = (byte)(x >> 16);
- Bytes[WriterIndex + 3] = (byte)(x >> 24);
- #endif
- WriterIndex += 4;
- }
- public int ReadFint_Safe()
- {
- EnsureRead(4);
- int x;
- x = (Bytes[ReaderIndex + 3] << 24) | (Bytes[ReaderIndex + 2] << 16) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
- ReaderIndex += 4;
- return x;
- }
- public void WriteFint_Safe(int x)
- {
- EnsureWrite(4);
- Bytes[WriterIndex] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- Bytes[WriterIndex + 2] = (byte)(x >> 16);
- Bytes[WriterIndex + 3] = (byte)(x >> 24);
- WriterIndex += 4;
- }
- public void WriteLong(long x)
- {
- WriteUlong((ulong)x);
- }
- public long ReadLong()
- {
- return (long)ReadUlong();
- }
- public void WriteNumberAsLong(double x)
- {
- WriteLong((long)x);
- }
- public double ReadLongAsNumber()
- {
- return ReadLong();
- }
- private void WriteUlong(ulong x)
- {
- // 0 111 1111
- if (x < 0x80)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = (byte)x;
- }
- else if (x < 0x4000) // 10 11 1111, -
- {
- EnsureWrite(2);
- Bytes[WriterIndex + 1] = (byte)x;
- Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
- WriterIndex += 2;
- }
- else if (x < 0x200000) // 110 1 1111, -,-
- {
- EnsureWrite(3);
- Bytes[WriterIndex + 2] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- Bytes[WriterIndex] = (byte)((x >> 16) | 0xc0);
- WriterIndex += 3;
- }
- else if (x < 0x10000000) // 1110 1111,-,-,-
- {
- EnsureWrite(4);
- Bytes[WriterIndex + 3] = (byte)x;
- Bytes[WriterIndex + 2] = (byte)(x >> 8);
- Bytes[WriterIndex + 1] = (byte)(x >> 16);
- Bytes[WriterIndex] = (byte)((x >> 24) | 0xe0);
- WriterIndex += 4;
- }
- else if (x < 0x800000000L) // 1111 0xxx,-,-,-,-
- {
- EnsureWrite(5);
- Bytes[WriterIndex + 4] = (byte)x;
- Bytes[WriterIndex + 3] = (byte)(x >> 8);
- Bytes[WriterIndex + 2] = (byte)(x >> 16);
- Bytes[WriterIndex + 1] = (byte)(x >> 24);
- Bytes[WriterIndex] = (byte)((x >> 32) | 0xf0);
- WriterIndex += 5;
- }
- else if (x < 0x40000000000L) // 1111 10xx,
- {
- EnsureWrite(6);
- Bytes[WriterIndex + 5] = (byte)x;
- Bytes[WriterIndex + 4] = (byte)(x >> 8);
- Bytes[WriterIndex + 3] = (byte)(x >> 16);
- Bytes[WriterIndex + 2] = (byte)(x >> 24);
- Bytes[WriterIndex + 1] = (byte)(x >> 32);
- Bytes[WriterIndex] = (byte)((x >> 40) | 0xf8);
- WriterIndex += 6;
- }
- else if (x < 0x200000000000L) // 1111 110x,
- {
- EnsureWrite(7);
- Bytes[WriterIndex + 6] = (byte)x;
- Bytes[WriterIndex + 5] = (byte)(x >> 8);
- Bytes[WriterIndex + 4] = (byte)(x >> 16);
- Bytes[WriterIndex + 3] = (byte)(x >> 24);
- Bytes[WriterIndex + 2] = (byte)(x >> 32);
- Bytes[WriterIndex + 1] = (byte)(x >> 40);
- Bytes[WriterIndex] = (byte)((x >> 48) | 0xfc);
- WriterIndex += 7;
- }
- else if (x < 0x100000000000000L) // 1111 1110
- {
- EnsureWrite(8);
- Bytes[WriterIndex + 7] = (byte)x;
- Bytes[WriterIndex + 6] = (byte)(x >> 8);
- Bytes[WriterIndex + 5] = (byte)(x >> 16);
- Bytes[WriterIndex + 4] = (byte)(x >> 24);
- Bytes[WriterIndex + 3] = (byte)(x >> 32);
- Bytes[WriterIndex + 2] = (byte)(x >> 40);
- Bytes[WriterIndex + 1] = (byte)(x >> 48);
- Bytes[WriterIndex] = 0xfe;
- WriterIndex += 8;
- }
- else // 1111 1111
- {
- EnsureWrite(9);
- Bytes[WriterIndex] = 0xff;
- Bytes[WriterIndex + 8] = (byte)x;
- Bytes[WriterIndex + 7] = (byte)(x >> 8);
- Bytes[WriterIndex + 6] = (byte)(x >> 16);
- Bytes[WriterIndex + 5] = (byte)(x >> 24);
- Bytes[WriterIndex + 4] = (byte)(x >> 32);
- Bytes[WriterIndex + 3] = (byte)(x >> 40);
- Bytes[WriterIndex + 2] = (byte)(x >> 48);
- Bytes[WriterIndex + 1] = (byte)(x >> 56);
- WriterIndex += 9;
- }
- }
- public ulong ReadUlong()
- {
- EnsureRead(1);
- uint h = Bytes[ReaderIndex];
- if (h < 0x80)
- {
- ReaderIndex++;
- return h;
- }
- else if (h < 0xc0)
- {
- EnsureRead(2);
- uint x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
- ReaderIndex += 2;
- return x;
- }
- else if (h < 0xe0)
- {
- EnsureRead(3);
- uint x = ((h & 0x1f) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- ReaderIndex += 3;
- return x;
- }
- else if (h < 0xf0)
- {
- EnsureRead(4);
- uint x = ((h & 0x0f) << 24) | ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
- ReaderIndex += 4;
- return x;
- }
- else if (h < 0xf8)
- {
- EnsureRead(5);
- uint xl = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)(Bytes[ReaderIndex + 2] << 16)) | ((uint)Bytes[ReaderIndex + 3] << 8) | (Bytes[ReaderIndex + 4]);
- uint xh = h & 0x07;
- ReaderIndex += 5;
- return ((ulong)xh << 32) | xl;
- }
- else if (h < 0xfc)
- {
- EnsureRead(6);
- uint xl = ((uint)Bytes[ReaderIndex + 2] << 24) | ((uint)(Bytes[ReaderIndex + 3] << 16)) | ((uint)Bytes[ReaderIndex + 4] << 8) | (Bytes[ReaderIndex + 5]);
- uint xh = ((h & 0x03) << 8) | Bytes[ReaderIndex + 1];
- ReaderIndex += 6;
- return ((ulong)xh << 32) | xl;
- }
- else if (h < 0xfe)
- {
- EnsureRead(7);
- uint xl = ((uint)Bytes[ReaderIndex + 3] << 24) | ((uint)(Bytes[ReaderIndex + 4] << 16)) | ((uint)Bytes[ReaderIndex + 5] << 8) | (Bytes[ReaderIndex + 6]);
- uint xh = ((h & 0x01) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- ReaderIndex += 7;
- return ((ulong)xh << 32) | xl;
- }
- else if (h < 0xff)
- {
- EnsureRead(8);
- uint xl = ((uint)Bytes[ReaderIndex + 4] << 24) | ((uint)(Bytes[ReaderIndex + 5] << 16)) | ((uint)Bytes[ReaderIndex + 6] << 8) | (Bytes[ReaderIndex + 7]);
- uint xh = /*((h & 0x01) << 24) |*/ ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
- ReaderIndex += 8;
- return ((ulong)xh << 32) | xl;
- }
- else
- {
- EnsureRead(9);
- uint xl = ((uint)Bytes[ReaderIndex + 5] << 24) | ((uint)(Bytes[ReaderIndex + 6] << 16)) | ((uint)Bytes[ReaderIndex + 7] << 8) | (Bytes[ReaderIndex + 8]);
- uint xh = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)Bytes[ReaderIndex + 2] << 16) | ((uint)Bytes[ReaderIndex + 3] << 8) | Bytes[ReaderIndex + 4];
- ReaderIndex += 9;
- return ((ulong)xh << 32) | xl;
- }
- }
- public void WriteFlong(long x)
- {
- EnsureWrite(8);
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[WriterIndex])
- {
- *(long*)b = x;
- }
- }
- #else
- Bytes[WriterIndex] = (byte)x;
- Bytes[WriterIndex + 1] = (byte)(x >> 8);
- Bytes[WriterIndex + 2] = (byte)(x >> 16);
- Bytes[WriterIndex + 3] = (byte)(x >> 24);
- Bytes[WriterIndex + 4] = (byte)(x >> 32);
- Bytes[WriterIndex + 5] = (byte)(x >> 40);
- Bytes[WriterIndex + 6] = (byte)(x >> 48);
- Bytes[WriterIndex + 7] = (byte)(x >> 56);
- #endif
- WriterIndex += 8;
- }
- public long ReadFlong()
- {
- EnsureRead(8);
- long x;
- #if CPU_SUPPORT_MEMORY_NOT_ALIGN
- unsafe
- {
- fixed (byte* b = &Bytes[ReaderIndex])
- {
- x = *(long*)b;
- }
- }
- #else
- int xl = (Bytes[ReaderIndex + 3] << 24) | ((Bytes[ReaderIndex + 2] << 16)) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
- int xh = (Bytes[ReaderIndex + 7] << 24) | (Bytes[ReaderIndex + 6] << 16) | (Bytes[ReaderIndex + 5] << 8) | Bytes[ReaderIndex + 4];
- x = ((long)xh << 32) | (long)xl;
- #endif
- ReaderIndex += 8;
- return x;
- }
- private static unsafe void Copy8(byte* dst, byte* src)
- {
- dst[0] = src[0];
- dst[1] = src[1];
- dst[2] = src[2];
- dst[3] = src[3];
- dst[4] = src[4];
- dst[5] = src[5];
- dst[6] = src[6];
- dst[7] = src[7];
- }
- private static unsafe void Copy4(byte* dst, byte* src)
- {
- dst[0] = src[0];
- dst[1] = src[1];
- dst[2] = src[2];
- dst[3] = src[3];
- }
- //const bool isLittleEndian = true;
- public void WriteFloat(float x)
- {
- EnsureWrite(4);
- unsafe
- {
- fixed (byte* b = &Bytes[WriterIndex])
- {
- #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
- if ((long)b % 4 == 0)
- {
- *(float*)b = x;
- }
- else
- {
- Copy4(b, (byte*)&x);
- }
- #else
- *(float*)b = x;
- #endif
- }
- }
- //if (!BitConverter.IsLittleEndian)
- //{
- // Array.Reverse(data, endPos, 4);
- //}
- WriterIndex += 4;
- }
- public float ReadFloat()
- {
- EnsureRead(4);
- //if (!BitConverter.IsLittleEndian)
- //{
- // Array.Reverse(data, beginPos, 4);
- //}
- float x;
- unsafe
- {
- fixed (byte* b = &Bytes[ReaderIndex])
- {
- #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
- if ((long)b % 4 == 0)
- {
- x = *(float*)b;
- }
- else
- {
- *((int*)&x) = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
- }
- #else
- x = *(float*)b;
- #endif
- }
- }
- ReaderIndex += 4;
- return x;
- }
- public void WriteDouble(double x)
- {
- EnsureWrite(8);
- unsafe
- {
- fixed (byte* b = &Bytes[WriterIndex])
- {
- #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
- if ((long)b % 8 == 0)
- {
- *(double*)b = x;
- }
- else
- {
- Copy8(b, (byte*)&x);
- }
- #else
- *(double*)b = x;
- #endif
- }
- //if (!BitConverter.IsLittleEndian)
- //{
- // Array.Reverse(data, endPos, 8);
- //}
- }
- WriterIndex += 8;
- }
- public double ReadDouble()
- {
- EnsureRead(8);
- //if (!BitConverter.IsLittleEndian)
- //{
- // Array.Reverse(data, beginPos, 8);
- //}
- double x;
- unsafe
- {
- fixed (byte* b = &Bytes[ReaderIndex])
- {
- #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
- if ((long)b % 8 == 0)
- {
- x = *(double*)b;
- }
- else
- {
- int low = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
- int high = (b[4]) | (b[5] << 8) | (b[6] << 16) | (b[7] << 24);
- *((long*)&x) = ((long)high << 32) | (uint)low;
- }
- #else
- x = *(double*)b;
- #endif
- }
- }
- ReaderIndex += 8;
- return x;
- }
- public void WriteSize(int n)
- {
- WriteUint((uint)n);
- }
- public int ReadSize()
- {
- return (int)ReadUint();
- }
- // marshal int
- // n -> (n << 1) ^ (n >> 31)
- // Read
- // (x >>> 1) ^ ((x << 31) >> 31)
- // (x >>> 1) ^ -(n&1)
- public void WriteSint(int x)
- {
- WriteUint(((uint)x << 1) ^ ((uint)x >> 31));
- }
- public int ReadSint()
- {
- uint x = ReadUint();
- return (int)((x >> 1) ^ ((x & 1) << 31));
- }
- // marshal long
- // n -> (n << 1) ^ (n >> 63)
- // Read
- // (x >>> 1) ^((x << 63) >> 63)
- // (x >>> 1) ^ -(n&1L)
- public void WriteSlong(long x)
- {
- WriteUlong(((ulong)x << 1) ^ ((ulong)x >> 63));
- }
- public long ReadSlong()
- {
- long x = ReadLong();
- return ((long)((ulong)x >> 1) ^ ((x & 1) << 63));
- }
- public void WriteString(string x)
- {
- var n = x != null ? Encoding.UTF8.GetByteCount(x) : 0;
- WriteSize(n);
- if (n > 0)
- {
- EnsureWrite(n);
- Encoding.UTF8.GetBytes(x, 0, x.Length, Bytes, WriterIndex);
- WriterIndex += n;
- }
- }
- // byte[], [start, end)
- public static Func<byte[], int, int, string> StringCacheFinder { get; set; }
- public string ReadString()
- {
- var n = ReadSize();
- if (n > 0)
- {
- EnsureRead(n);
- string s;
- if (StringCacheFinder == null)
- {
- s = Encoding.UTF8.GetString(Bytes, ReaderIndex, n);
- }
- else
- {
- // 只缓存比较小的字符串
- s = StringCacheFinder(Bytes, ReaderIndex, n);
- }
- ReaderIndex += n;
- return s;
- }
- else
- {
- return string.Empty;
- }
- }
- public void WriteBytes(byte[] x)
- {
- var n = x != null ? x.Length : 0;
- WriteSize(n);
- if (n > 0)
- {
- EnsureWrite(n);
- x.CopyTo(Bytes, WriterIndex);
- WriterIndex += n;
- }
- }
- public byte[] ReadBytes()
- {
- var n = ReadSize();
- if (n > 0)
- {
- EnsureRead(n);
- var x = new byte[n];
- Buffer.BlockCopy(Bytes, ReaderIndex, x, 0, n);
- ReaderIndex += n;
- return x;
- }
- else
- {
- return Array.Empty<byte>();
- }
- }
- // 以下是一些特殊类型
- public void WriteComplex(Complex x)
- {
- WriteDouble(x.Real);
- WriteDouble(x.Imaginary);
- }
- public Complex ReadComplex()
- {
- var x = ReadDouble();
- var y = ReadDouble();
- return new Complex(x, y);
- }
- public void WriteVector2(Vector2 x)
- {
- WriteFloat(x.X);
- WriteFloat(x.Y);
- }
- public Vector2 ReadVector2()
- {
- float x = ReadFloat();
- float y = ReadFloat();
- return new Vector2(x, y);
- }
- public void WriteVector3(Vector3 x)
- {
- WriteFloat(x.X);
- WriteFloat(x.Y);
- WriteFloat(x.Z);
- }
- public Vector3 ReadVector3()
- {
- float x = ReadFloat();
- float y = ReadFloat();
- float z = ReadFloat();
- return new Vector3(x, y, z);
- }
- public void WriteVector4(Vector4 x)
- {
- WriteFloat(x.X);
- WriteFloat(x.Y);
- WriteFloat(x.Z);
- WriteFloat(x.W);
- }
- public Vector4 ReadVector4()
- {
- float x = ReadFloat();
- float y = ReadFloat();
- float z = ReadFloat();
- float w = ReadFloat();
- return new Vector4(x, y, z, w);
- }
- public void WriteQuaternion(Quaternion x)
- {
- WriteFloat(x.X);
- WriteFloat(x.Y);
- WriteFloat(x.Z);
- WriteFloat(x.W);
- }
- public Quaternion ReadQuaternion()
- {
- float x = ReadFloat();
- float y = ReadFloat();
- float z = ReadFloat();
- float w = ReadFloat();
- return new Quaternion(x, y, z, w);
- }
- public void WriteMatrix4x4(Matrix4x4 x)
- {
- WriteFloat(x.M11);
- WriteFloat(x.M12);
- WriteFloat(x.M13);
- WriteFloat(x.M14);
- WriteFloat(x.M21);
- WriteFloat(x.M22);
- WriteFloat(x.M23);
- WriteFloat(x.M24);
- WriteFloat(x.M31);
- WriteFloat(x.M32);
- WriteFloat(x.M33);
- WriteFloat(x.M34);
- WriteFloat(x.M41);
- WriteFloat(x.M42);
- WriteFloat(x.M43);
- WriteFloat(x.M44);
- }
- public Matrix4x4 ReadMatrix4x4()
- {
- float m11 = ReadFloat();
- float m12 = ReadFloat();
- float m13 = ReadFloat();
- float m14 = ReadFloat();
- float m21 = ReadFloat();
- float m22 = ReadFloat();
- float m23 = ReadFloat();
- float m24 = ReadFloat();
- float m31 = ReadFloat();
- float m32 = ReadFloat();
- float m33 = ReadFloat();
- float m34 = ReadFloat();
- float m41 = ReadFloat();
- float m42 = ReadFloat();
- float m43 = ReadFloat();
- float m44 = ReadFloat();
- return new Matrix4x4(m11, m12, m13, m14,
- m21, m22, m23, m24,
- m31, m32, m33, m34,
- m41, m42, m43, m44);
- }
- internal void SkipBytes()
- {
- int n = ReadSize();
- EnsureRead(n);
- ReaderIndex += n;
- }
- public void WriteByteBufWithSize(ByteBuf o)
- {
- int n = o.Size;
- if (n > 0)
- {
- WriteSize(n);
- WriteBytesWithoutSize(o.Bytes, o.ReaderIndex, n);
- }
- else
- {
- WriteByte(0);
- }
- }
- public void WriteByteBufWithoutSize(ByteBuf o)
- {
- int n = o.Size;
- if (n > 0)
- {
- WriteBytesWithoutSize(o.Bytes, o.ReaderIndex, n);
- }
- }
- public bool TryReadByte(out byte x)
- {
- if (CanRead(1))
- {
- x = Bytes[ReaderIndex++];
- return true;
- }
- else
- {
- x = 0;
- return false;
- }
- }
- public EDeserializeError TryDeserializeInplaceByteBuf(int maxSize, ByteBuf inplaceTempBody)
- {
- //if (!CanRead(1)) { return EDeserializeError.NOT_ENOUGH; }
- int oldReadIndex = ReaderIndex;
- bool commit = false;
- try
- {
- int n;
- int h = Bytes[ReaderIndex];
- if (h < 0x80)
- {
- ReaderIndex++;
- n = h;
- }
- else if (h < 0xc0)
- {
- if (!CanRead(2)) { return EDeserializeError.NOT_ENOUGH; }
- n = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
- ReaderIndex += 2;
- }
- else if (h < 0xe0)
- {
- if (!CanRead(3)) { return EDeserializeError.NOT_ENOUGH; }
- n = ((h & 0x1f) << 16) | (Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- ReaderIndex += 3;
- }
- else if (h < 0xf0)
- {
- if (!CanRead(4)) { return EDeserializeError.NOT_ENOUGH; }
- n = ((h & 0x0f) << 24) | (Bytes[ReaderIndex + 1] << 16) | (Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
- ReaderIndex += 4;
- }
- else
- {
- return EDeserializeError.EXCEED_SIZE;
- }
- if (n > maxSize)
- {
- return EDeserializeError.EXCEED_SIZE;
- }
- if (Remaining < n)
- {
- return EDeserializeError.NOT_ENOUGH;
- }
- int inplaceReadIndex = ReaderIndex;
- ReaderIndex += n;
- inplaceTempBody.Replace(Bytes, inplaceReadIndex, ReaderIndex);
- commit = true;
- }
- finally
- {
- if (!commit)
- {
- ReaderIndex = oldReadIndex;
- }
- }
- return EDeserializeError.OK;
- }
- public void WriteRawTag(byte b1)
- {
- EnsureWrite(1);
- Bytes[WriterIndex++] = b1;
- }
- public void WriteRawTag(byte b1, byte b2)
- {
- EnsureWrite(2);
- Bytes[WriterIndex] = b1;
- Bytes[WriterIndex + 1] = b2;
- WriterIndex += 2;
- }
- public void WriteRawTag(byte b1, byte b2, byte b3)
- {
- EnsureWrite(3);
- Bytes[WriterIndex] = b1;
- Bytes[WriterIndex + 1] = b2;
- Bytes[WriterIndex + 2] = b3;
- WriterIndex += 3;
- }
- #region segment
- public void BeginWriteSegment(out int oldSize)
- {
- oldSize = Size;
- EnsureWrite(1);
- WriterIndex += 1;
- }
- public void EndWriteSegment(int oldSize)
- {
- int startPos = ReaderIndex + oldSize;
- int segmentSize = WriterIndex - startPos - 1;
- // 0 111 1111
- if (segmentSize < 0x80)
- {
- Bytes[startPos] = (byte)segmentSize;
- }
- else if (segmentSize < 0x4000) // 10 11 1111, -
- {
- EnsureWrite(1);
- Bytes[WriterIndex] = Bytes[startPos + 1];
- Bytes[startPos + 1] = (byte)segmentSize;
- Bytes[startPos] = (byte)((segmentSize >> 8) | 0x80);
- WriterIndex += 1;
- }
- else if (segmentSize < 0x200000) // 110 1 1111, -,-
- {
- EnsureWrite(2);
- Bytes[WriterIndex + 1] = Bytes[startPos + 2];
- Bytes[startPos + 2] = (byte)segmentSize;
- Bytes[WriterIndex] = Bytes[startPos + 1];
- Bytes[startPos + 1] = (byte)(segmentSize >> 8);
- Bytes[startPos] = (byte)((segmentSize >> 16) | 0xc0);
- WriterIndex += 2;
- }
- else if (segmentSize < 0x10000000) // 1110 1111,-,-,-
- {
- EnsureWrite(3);
- Bytes[WriterIndex + 2] = Bytes[startPos + 3];
- Bytes[startPos + 3] = (byte)segmentSize;
- Bytes[WriterIndex + 1] = Bytes[startPos + 2];
- Bytes[startPos + 2] = (byte)(segmentSize >> 8);
- Bytes[WriterIndex] = Bytes[startPos + 1];
- Bytes[startPos + 1] = (byte)(segmentSize >> 16);
- Bytes[startPos] = (byte)((segmentSize >> 24) | 0xe0);
- WriterIndex += 3;
- }
- else
- {
- throw new SerializationException("exceed max segment size");
- }
- }
- public void ReadSegment(out int startIndex, out int segmentSize)
- {
- EnsureRead(1);
- int h = Bytes[ReaderIndex++];
- startIndex = ReaderIndex;
- if (h < 0x80)
- {
- segmentSize = h;
- ReaderIndex += segmentSize;
- }
- else if (h < 0xc0)
- {
- EnsureRead(1);
- segmentSize = ((h & 0x3f) << 8) | Bytes[ReaderIndex];
- int endPos = ReaderIndex + segmentSize;
- Bytes[ReaderIndex] = Bytes[endPos];
- ReaderIndex += segmentSize + 1;
- }
- else if (h < 0xe0)
- {
- EnsureRead(2);
- segmentSize = ((h & 0x1f) << 16) | ((int)Bytes[ReaderIndex] << 8) | Bytes[ReaderIndex + 1];
- int endPos = ReaderIndex + segmentSize;
- Bytes[ReaderIndex] = Bytes[endPos];
- Bytes[ReaderIndex + 1] = Bytes[endPos + 1];
- ReaderIndex += segmentSize + 2;
- }
- else if (h < 0xf0)
- {
- EnsureRead(3);
- segmentSize = ((h & 0x0f) << 24) | ((int)Bytes[ReaderIndex] << 16) | ((int)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
- int endPos = ReaderIndex + segmentSize;
- Bytes[ReaderIndex] = Bytes[endPos];
- Bytes[ReaderIndex + 1] = Bytes[endPos + 1];
- Bytes[ReaderIndex + 2] = Bytes[endPos + 2];
- ReaderIndex += segmentSize + 3;
- }
- else
- {
- throw new SerializationException("exceed max size");
- }
- if (ReaderIndex > WriterIndex)
- {
- throw new SerializationException("segment data not enough");
- }
- }
- public void ReadSegment(ByteBuf buf)
- {
- ReadSegment(out int startPos, out var size);
- buf.Bytes = Bytes;
- buf.ReaderIndex = startPos;
- buf.WriterIndex = startPos + size;
- }
- public void EnterSegment(out SegmentSaveState saveState)
- {
- ReadSegment(out int startPos, out int size);
- saveState = new SegmentSaveState(ReaderIndex, WriterIndex);
- ReaderIndex = startPos;
- WriterIndex = startPos + size;
- }
- public void LeaveSegment(SegmentSaveState saveState)
- {
- ReaderIndex = saveState.ReaderIndex;
- WriterIndex = saveState.WriterIndex;
- }
- #endregion
- public override string ToString()
- {
- string[] datas = new string[WriterIndex - ReaderIndex];
- for (var i = ReaderIndex; i < WriterIndex; i++)
- {
- datas[i - ReaderIndex] = Bytes[i].ToString("X2");
- }
- return string.Join(".", datas);
- }
- public override bool Equals(object obj)
- {
- return (obj is ByteBuf other) && Equals(other);
- }
- public bool Equals(ByteBuf other)
- {
- if (other == null)
- {
- return false;
- }
- if (Size != other.Size)
- {
- return false;
- }
- for (int i = 0, n = Size; i < n; i++)
- {
- if (Bytes[ReaderIndex + i] != other.Bytes[other.ReaderIndex + i])
- {
- return false;
- }
- }
- return true;
- }
- public object Clone()
- {
- return new ByteBuf(CopyData());
- }
- public static ByteBuf FromString(string value)
- {
- var ss = value.Split(',');
- byte[] data = new byte[ss.Length];
- for (int i = 0; i < data.Length; i++)
- {
- data[i] = byte.Parse(ss[i]);
- }
- return new ByteBuf(data);
- }
- public override int GetHashCode()
- {
- int hash = 17;
- for (int i = ReaderIndex; i < WriterIndex; i++)
- {
- hash = hash * 23 + Bytes[i];
- }
- return hash;
- }
- public void Release()
- {
- _releaser?.Invoke(this);
- }
- #if SUPPORT_PUERTS_ARRAYBUF
- // -- add for puerts
- public Puerts.ArrayBuffer ReadArrayBuffer()
- {
- return new Puerts.ArrayBuffer(ReadBytes());
- }
- public void WriteArrayBuffer(Puerts.ArrayBuffer bytes)
- {
- WriteBytes(bytes.Bytes);
- }
- #endif
- }
- }
|