public static unsafe ByteSpan Pin(this Span <byte> source, out GCHandle handle) { handle = GCHandle.Alloc(source._array, GCHandleType.Pinned); var pinned = handle.AddrOfPinnedObject() + source._index; var byteSpan = new ByteSpan(((byte *)pinned.ToPointer()), source._length); return(byteSpan); }
internal unsafe ByteSpan BorrowDisposableByteSpan() { var handle = GCHandle.Alloc(_array, GCHandleType.Pinned); var pinned = handle.AddrOfPinnedObject() + _index; var byteSpan = new ByteSpan(((byte *)pinned.ToPointer()), _length, handle); return(byteSpan); }
public bool TryCopyTo(ByteSpan buffer) { if (Length > buffer.Length) { return(false); } BufferInternal.MemoryCopy(UnsafeBuffer, buffer.UnsafeBuffer, buffer.Length, Length); return(true); }
protected override HttpServerBuffer CreateResponse(HttpRequestLine requestLine, ByteSpan headerAndBody) { var api = Apis.Map(requestLine); switch (api) { case Api.GetTime: return CreateResponseForGetTime(); default: return CreateResponseFor404(requestLine, headerAndBody); } }
public static int Invoke(SOCKET socket, ByteSpan buffer) { // Actually we should get WSARecvCall from a pool var instance = new WSARecvCall(); instance._socketFlags = 0; instance._WSABuffer.buffer = new IntPtr(buffer.UnsafeBuffer); instance._WSABuffer.len = (ulong)buffer.Length; return instance.Invoke(socket); }
// TODO: .ctor(char) // TODO: .ctor(Utf8EncodedCodePoint) public unsafe Utf16LittleEndianEncodedCodePoint(ByteSpan buffer) : this() { fixed (byte* encodedData = &_byte0) { if (buffer.Length == 1 || buffer.Length == 3) throw new ArgumentException("buffer"); if (!buffer.TryCopyTo(encodedData, 4)) throw new ArgumentException("buffer"); } }
public static bool TryDecodeCodePoint(ByteSpan buffer, out UnicodeCodePoint codePoint, out int encodedBytes) { if (buffer.Length < 2) { codePoint = default(UnicodeCodePoint); encodedBytes = default(int); // buffer too small return false; } uint codePointValue = (uint)buffer[0] | ((uint)buffer[1] << 8); encodedBytes = 2; // Notice: This is any surrogate, not only high surrogate bool isSurrogate = codePointValue >= SpecConstants.HighSurrogateFirstCodePoint && codePointValue <= SpecConstants.LowSurrogateLastCodePoint; if (isSurrogate) { isSurrogate = codePointValue <= SpecConstants.HighSurrogateLastCodePoint; if (!isSurrogate || buffer.Length < 4) { codePoint = default(UnicodeCodePoint); encodedBytes = default(int); // invalid high surrogate or buffer too small return false; } unchecked { codePointValue -= SpecConstants.HighSurrogateFirstCodePoint; encodedBytes += 2; } // high surrogate contains 10 first bits of the code point codePointValue <<= 10; uint lowSurrogate = (uint)buffer[2] | ((uint)buffer[3] << 8); if (lowSurrogate < SpecConstants.LowSurrogateFirstCodePoint || lowSurrogate > SpecConstants.LowSurrogateLastCodePoint) { codePoint = default(UnicodeCodePoint); encodedBytes = default(int); // invalid low surrogate character return false; } unchecked { lowSurrogate -= SpecConstants.LowSurrogateFirstCodePoint; } codePointValue |= lowSurrogate; } codePoint = (UnicodeCodePoint)codePointValue; return true; }
internal int Receive(ByteSpan buffer) { unsafe { IntPtr ptr = new IntPtr(buffer.UnsafeBuffer); int bytesReceived = SocketImports.recv(Handle, ptr, buffer.Length, 0); if (bytesReceived < 0) { var error = SocketImports.WSAGetLastError(); throw new Exception(String.Format("receive failed with {0}", error)); } return bytesReceived; } }
public GivenAnHttpHeaders() { ByteSpan headers; var bytes = new UTF8Encoding().GetBytes(HeadersString); unsafe { fixed (byte* buffer = bytes) { headers = new ByteSpan(buffer, bytes.Length); } } _httpHeaders = new HttpHeaders(headers); }
public static bool TryEncodeCodePoint(UnicodeCodePoint codePoint, ByteSpan buffer, out int encodedBytes) { if ((uint)codePoint > 0x10FFFF) { encodedBytes = default(int); return false; } // is Surrogate? encodedBytes = ((uint)codePoint >= 0x10000) ? 4 : 2; if (buffer.Length < encodedBytes) { codePoint = default(UnicodeCodePoint); encodedBytes = default(int); // buffer too small return false; } if (encodedBytes == 2) { unchecked { buffer[0] = (byte)((uint)codePoint); buffer[1] = (byte)((uint)codePoint >> 8); } } else { unchecked { uint highSurrogate = ((uint)codePoint >> 10) + 0xD800; uint lowSurrogate = ((uint)codePoint & MaskLow10Bits) + 0xDC00; buffer[0] = (byte)highSurrogate; buffer[1] = (byte)(highSurrogate >> 8); buffer[2] = (byte)lowSurrogate; buffer[3] = (byte)(lowSurrogate >> 8); } } return true; }
public static void Listen(this TcpClient connection, Action<HttpRequestLine> request) { NetworkStream stream = connection.GetStream(); var buffer = BufferPool.Shared.RentBuffer(1024); while (true) { var bytesRead = stream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } Console.WriteLine("Read {0} bytes, Payload: {1}", bytesRead, Encoding.ASCII.GetString(buffer, 0, bytesRead)); unsafe { fixed (byte* pBuffer = buffer) { var bufferSpan = new ByteSpan(pBuffer, bytesRead); HttpRequestLine requestLine; if (!HttpRequestParser.TryParseRequestLine(bufferSpan, out requestLine)) { Console.WriteLine("request could not be parsed"); } request(requestLine); } } if (bytesRead < buffer.Length) { break; } } BufferPool.Shared.ReturnBuffer(ref buffer); connection.Dispose(); }
public unsafe void CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] arrayManaged) { fixed (byte* pinnedArray = arrayManaged) { byte* array = pinnedArray; // new byte[0] is internally null if (array == null && arrayManaged.Length == 0) { array = (byte*)123; } ByteSpan span = new ByteSpan(array, arrayManaged.Length); Assert.Equal(arrayManaged.Length, span.Length); Assert.NotSame(arrayManaged, span.CreateArray()); Assert.True(span.ReferenceEquals(span)); Assert.True(span.Equals(span)); Assert.True(span.Equals((object)span)); Assert.Equal(span.GetHashCode(), span.GetHashCode()); Assert.False(span.Equals(arrayManaged)); ByteSpan.Enumerator it = span.GetEnumerator(); for (int i = 0; i < span.Length; i++) { Assert.True(it.MoveNext()); Assert.Equal(array[i], it.Current); Assert.Equal(array[i], span.Slice(i)[0]); array[i] = unchecked((byte)(array[i] + 1)); Assert.Equal(array[i], it.Current); Assert.Equal(array[i], span.Slice(i)[0]); var slice = span.Slice(i); slice[0] = (unchecked((byte)(array[i] + 1))); Assert.Equal(array[i], it.Current); Assert.Equal(array[i], span.Slice(i)[0]); } Assert.False(it.MoveNext()); it.Reset(); for (int i = 0; i < span.Length; i++) { Assert.True(it.MoveNext()); Assert.Equal(array[i], it.Current); } Assert.False(it.MoveNext()); { ByteSpan sameSpan = new ByteSpan(array, arrayManaged.Length); Assert.Equal(span.GetHashCode(), sameSpan.GetHashCode()); Assert.True(span.ReferenceEquals(sameSpan)); Assert.True(span.Equals(sameSpan)); Assert.True(span.Equals((object)sameSpan)); } { ByteSpan structCopy = span; Assert.Equal(span.GetHashCode(), structCopy.GetHashCode()); Assert.True(span.ReferenceEquals(structCopy)); Assert.True(span.Equals(structCopy)); Assert.True(span.Equals((object)structCopy)); } { byte[] differentArrayManaged = new byte[arrayManaged.Length * 2]; for (int i = 0; i < arrayManaged.Length; i++) { differentArrayManaged[i] = unchecked((byte)(array[i] + 1)); differentArrayManaged[arrayManaged.Length + i] = array[i]; } fixed (byte* differentArray = differentArrayManaged) { { ByteSpan equivalentSpan = new ByteSpan(differentArray + arrayManaged.Length, arrayManaged.Length); Assert.Equal(span.GetHashCode(), equivalentSpan.GetHashCode()); Assert.False(span.ReferenceEquals(equivalentSpan)); Assert.True(span.Equals(equivalentSpan)); Assert.True(span.Equals((object)equivalentSpan)); if (equivalentSpan.Length > 0) { ByteSpan similarSpan = equivalentSpan.Slice(0, equivalentSpan.Length - 1); Assert.False(span.ReferenceEquals(similarSpan)); Assert.False(span.Equals(similarSpan)); Assert.False(span.Equals((object)similarSpan)); } } { ByteSpan differentSpan = new ByteSpan(differentArray, arrayManaged.Length); Assert.False(span.ReferenceEquals(differentSpan)); // This can be simplified although it is harder to understand after simplification if (arrayManaged.Length == 0) { Assert.True(span.Equals(differentSpan)); Assert.True(span.Equals((object)differentSpan)); } else { Assert.False(span.Equals(differentSpan)); Assert.False(span.Equals((object)differentSpan)); } } } } } }
public unsafe void SpanOfByteEqualOtherSpanOfByte() { byte[] managedBytes1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // ^span1^ // ^span4^ byte[] managedBytes2 = new byte[] { 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; // ^span2^ ^span3^ // ^span5^ fixed (byte* bytes1 = managedBytes1) fixed (byte* bytes2 = managedBytes2) { ByteSpan spanOfAllBytes1 = new ByteSpan(bytes1, managedBytes1.Length); ByteSpan spanOfAllBytes2 = new ByteSpan(bytes2, managedBytes2.Length); ByteSpan span1 = spanOfAllBytes1.Slice(1, 3); ByteSpan span2 = spanOfAllBytes2.Slice(1, 3); ByteSpan span3 = spanOfAllBytes2.Slice(4).Slice(1, 3); ByteSpan span4 = spanOfAllBytes1.Slice(2, 3); ByteSpan span5 = spanOfAllBytes2.Slice(2, 3); Assert.Equal(span1, span1); Assert.Equal(span1, span2); Assert.Equal(span1, span3); Assert.NotEqual(span1, span4); Assert.NotEqual(span1, span5); Assert.Equal(span2, span1); Assert.Equal(span2, span2); Assert.Equal(span2, span3); Assert.NotEqual(span2, span4); Assert.NotEqual(span2, span5); Assert.Equal(span3, span1); Assert.Equal(span3, span2); Assert.Equal(span3, span3); Assert.NotEqual(span3, span4); Assert.NotEqual(span3, span5); Assert.NotEqual(span4, span1); Assert.NotEqual(span4, span2); Assert.NotEqual(span4, span3); Assert.Equal(span4, span4); Assert.NotEqual(span4, span5); Assert.NotEqual(span5, span1); Assert.NotEqual(span5, span2); Assert.NotEqual(span5, span3); Assert.NotEqual(span5, span4); Assert.Equal(span5, span5); } }
public unsafe void SpanOfByteCopyToAnotherSpanOfByteTwoDifferentBuffersValidCases(byte[] expectedManaged, byte[] aManaged, int aidx, int acount, byte[] bManaged, int bidx, int bcount) { fixed (byte* expected = expectedManaged) fixed (byte* a = aManaged) fixed (byte* b = bManaged) { ByteSpan spanA = new ByteSpan(a + aidx, acount); ByteSpan spanB = new ByteSpan(b + bidx, bcount); if (expectedManaged != null) { Assert.True(spanA.TryCopyTo(spanB)); Assert.Equal(expectedManaged, bManaged); ByteSpan spanExpected = new ByteSpan(expected, expectedManaged.Length); ByteSpan spanBAll = new ByteSpan(b, bManaged.Length); Assert.Equal(spanExpected, spanBAll); } else { Assert.False(spanA.TryCopyTo(spanB)); } } }
public unsafe void DefaultSpanNullSpanEmptySpanEqualsTo() { byte[] managedBytes1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // ^span1^ fixed (byte* bytes1 = managedBytes1) { ByteSpan allBytes1 = new ByteSpan(bytes1, managedBytes1.Length); ByteSpan span1 = allBytes1.Slice(1, 3); ByteSpan defaultSpan = default(ByteSpan); Assert.NotEqual(defaultSpan, span1); Assert.NotEqual(span1, defaultSpan); byte[] emptyArrayManaged = new byte[0]; fixed (byte* emptyArray = emptyArrayManaged) { ByteSpan emptySpan = new ByteSpan(emptyArray, emptyArrayManaged.Length); Assert.Equal(emptySpan, emptySpan); Assert.NotEqual(emptySpan, span1); Assert.NotEqual(span1, emptySpan); Assert.Equal(emptySpan, defaultSpan); Assert.Equal(defaultSpan, emptySpan); } // TODO: Not so sure if this should be forbidden //byte[] nullBytes = null; //ByteSpan nullSpan = new ByteSpan(nullBytes, 0); //Assert.Equal(nullSpan, nullSpan); //Assert.Equal(nullSpan, defaultSpan); //Assert.Equal(nullSpan, emptySpan); //Assert.Equal(defaultSpan, nullSpan); //Assert.Equal(emptySpan, nullSpan); //Assert.NotEqual(nullSpan, span1); //Assert.NotEqual(span1, nullSpan); } }
internal static void FreeBuffer(ByteSpan buffer) { _pool.Return(buffer); }
internal void Dispose() { unsafe { var readSlice = new ByteSpan((byte*)Buffer, (int)Length); FreeBuffer(readSlice); } }
public void Setup() { var bytes = new UTF8Encoding().GetBytes(_headersString); unsafe { fixed (byte* buffer = bytes) { _headers = new ByteSpan(buffer, bytes.Length); } } _httpHeaders = new HttpHeaders(_headers); }
/// <summary> /// Demonstrate the usage of WSARecv even though this method is sync /// </summary> internal int ReceiveWithAsyncAPI(ByteSpan buffer) { unsafe { return WSARecvCall.Invoke(Handle, buffer); } }
public bool TryCopyTo(ByteSpan buffer) { if (Length > buffer.Length) return false; BufferInternal.MemoryCopy(UnsafeBuffer, buffer.UnsafeBuffer, buffer.Length, Length); return true; }
int _position; // The current position. public Enumerator(ByteSpan slice) { _slice = slice; _position = -1; }
public void Dispose() { _slice = default(ByteSpan); _position = -1; }