Esempio n. 1
0
        public byte WriteBytes(IBytes bytes)
        {
            var buffer = bytes.ToArray();

            _port?.Write(buffer, 0, buffer.Length);
            return((byte)(_port?.ReadByte() ?? 0));
        }
Esempio n. 2
0
 public static byte[] CopyTo(this IBytes bytes, byte[] array)
 {
     if (bytes.ByteCount > 0)
     {
         Marshal.Copy(bytes.Ptr, array, 0, Math.Min(bytes.ByteCount, array.Length));
     }
     return(array);
 }
Esempio n. 3
0
 public static byte[] CopyTo(this IBytes bytes, byte[] array, int offset, int size)
 {
     if (bytes.ByteCount > 0)
     {
         Marshal.Copy(bytes.Ptr, array, offset, Math.Min(bytes.ByteCount, size));
     }
     return(array);
 }
Esempio n. 4
0
 /// <summary>
 /// Writes all data from the buffer to the file.
 /// </summary>
 public static void WriteAllBytes(string filePath, IBytes bytes)
 {
     using (var hFile = OpenForWriting(filePath))
     {
         hFile.Write(bytes);
         hFile.Flush();
     }
 }
Esempio n. 5
0
 public static IBytes CopyTo(this byte[] array, IBytes bytes)
 {
     if (bytes.ByteCount > 0)
     {
         Marshal.Copy(array, 0, bytes.Ptr, Math.Min(bytes.ByteCount, array.Length));
     }
     return(bytes);
 }
Esempio n. 6
0
 /// <summary>
 /// A cell which exists in memory.
 /// Note: This memory cell lives only as long as this object lives.
 /// If you create two cells with the same path using this ctor,
 /// they will not have the same content.
 /// </summary>
 public RamCell(string path, IBytes content) : this(
         new Live <string>(() => path),
         new Solid <IMnemonic>(() =>
 {
     var mem = new RamMnemonic();
     mem.Contents().UpdateBytes(path, content.AsBytes());
     return(mem);
 })
         )
 { }
Esempio n. 7
0
 /// <summary>
 /// Returns true if the two buffers are the same.
 /// </summary>
 public static bool SequenceEqual(this IBytes self, IBytes other)
 {
     // TODO: consider casting to a Vector<T>() https://docs.microsoft.com/en-us/dotnet/api/system.numerics.vector-1.-ctor?view=netcore-2.2#System_Numerics_Vector_1__ctor_System_Span__0__
     // Look at https://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net/1445405#1445405
     // Compare to PInvoke
     if (self.ByteCount != other.ByteCount)
     {
         return(false);
     }
     return(self.ToSpan <byte>().SequenceEqual(other.ToSpan <byte>()));
 }
 /// <summary>
 /// A Response containing the given status, reason and a body from the given bytes.
 /// </summary>
 public Of(int status, string reason, IBytes body, params IMapInput[] extraParts) : base(() =>
                                                                                         new Response.Of(
                                                                                             new Status(status),
                                                                                             new Reason(reason),
                                                                                             new Body(body),
                                                                                             new Parts.Joined(extraParts)
                                                                                             ),
                                                                                         live: false
                                                                                         )
 {
 }
Esempio n. 9
0
 /// <summary>
 /// Adds a body to a request.
 /// </summary>
 public Body(IBytes content) : this(
         new MapInputOf(
             new KvpOf(KEY, () =>
                       new TextOf(
                           new BytesBase64(content)
                           ).AsString()
                       )
             )
         )
 {
 }
Esempio n. 10
0
        public AttributeBytes(IBytes bytes, AttributeDescriptor descriptor)
        {
            Bytes      = bytes;
            Descriptor = descriptor;

            // Should never happen, but just in case
            if (Bytes.ByteCount % Descriptor.ItemSize != 0)
            {
                throw new Exception("Number of items does not divide by item size properly");
            }

            Count = Bytes.ByteCount / Descriptor.ItemSize;
        }
Esempio n. 11
0
 public static IBytes CopyTo(this IBytes self, IBytes other)
 {
     Util.MemoryCopy(self.Ptr, other.Ptr, Math.Min(self.ByteCount, other.ByteCount));
     return(other);
 }
Esempio n. 12
0
 public static bool Overlapping(this IBytes self, IBytes other)
 {
     return(self.Contains(other.Ptr) || self.Contains(other.End()) || other.Contains(self.Ptr) || self.Contains(self.End()));
 }
Esempio n. 13
0
 public static IntPtr End(this IBytes self)
 {
     return(self.Ptr + self.ByteCount);
 }
Esempio n. 14
0
        public static bool Contains(this IBytes self, IntPtr ptr)
        {
            var dist = self.Ptr.Distance(ptr);

            return(dist >= 0 && dist < self.ByteCount);
        }
Esempio n. 15
0
 /// <summary>
 /// Provides access to raw memory as an unmanaged memory stream.
 /// https://docs.microsoft.com/en-us/dotnet/api/system.io.unmanagedmemorystream?view=netframework-4.7.2
 /// </summary>
 public static UnmanagedMemoryStream ToMemoryStream(this IBytes self)
 {
     return(new UnmanagedMemoryStream(self.Ptr.ToBytePtr(), self.ByteCount));
 }
Esempio n. 16
0
 /// <summary>
 /// Initializes a new instance of <see cref="BytesAsOutput"/>.
 /// </summary>
 /// <param name="source">The source bytes.</param>
 public BytesAsOutput(IBytes source) : this(source.Bytes())
 {
 }
Esempio n. 17
0
 public BytesText(IBytes source, Encoding encoding)
 {
     _source   = source;
     _encoding = encoding;
 }
Esempio n. 18
0
 /// <summary>
 /// Initializes a new instance of <see cref="AsyncOutput"/>.
 /// </summary>
 /// <param name="from">Bytes to read from.</param>
 /// <param name="encoding">The input encoding.</param>
 /// <param name="to">AsyncOutput to write to.</param>
 public AsyncOutput(IBytes from, Encoding encoding, IOutput to) : this(new BytesText(from, encoding), to)
 {
 }
Esempio n. 19
0
 /// <summary>
 /// Read data from the file to the buffer. Assumes enough space in the buffer.
 /// </summary>
 public static SafeFileHandle Read(this SafeFileHandle hFile, IBytes bytes)
 {
     return(Read(hFile, bytes.Ptr, bytes.ByteCount));
 }
Esempio n. 20
0
 /// <summary>
 /// Writes all data from the buffer to the file.
 /// </summary>
 public static SafeFileHandle Write(this SafeFileHandle hFile, IBytes bytes)
 {
     return(Write(hFile, bytes.Ptr, bytes.ByteCount));
 }
Esempio n. 21
0
 /// <summary>
 /// Writes raw bytes to the stream by creating a memory stream around it.
 /// </summary>
 public static void Write(this BinaryWriter self, IBytes bytes)
 {
     self.Write(bytes.ToBytes());
 }
Esempio n. 22
0
 /// <summary>
 /// Initializes a new instance of <see cref="AsyncOutput"/>.
 /// </summary>
 /// <param name="from">Bytes to read from.</param>
 /// <param name="to">AsyncOutput to write to.</param>
 public AsyncOutput(IBytes from, IOutput to) : this(new ByteInput(from), to)
 {
 }
Esempio n. 23
0
 public static IAttribute ToAttribute(this IBytes bytes, AttributeDescriptor desc)
 => new AttributeBytes(bytes, desc);
Esempio n. 24
0
 // TODO: all of these copies make me die a little bit inside
 public static G3D Create(IBytes bytes)
 => Create(bytes.ToBytes());
 /// <summary>
 /// A 200/OK Response containing a body from the given bytes.
 /// </summary>
 public Of(IBytes body, params IMapInput[] extraParts) : this(200, "OK", body, extraParts)
 {
 }
Esempio n. 26
0
 public static T[] ToStructs <T>(this IBytes bytes) where T : struct
 {
     return(bytes.ToSpan <T>().ToArray());
 }
Esempio n. 27
0
 /// <summary>
 /// Initializes a new instance of <see cref="ByteInput"/>.
 /// </summary>
 /// <param name="source">Source <see cref="IBytes"/>.</param>
 public ByteInput(IBytes source)
 {
     _source = source;
 }
Esempio n. 28
0
 public static byte[] ToBytes(this IBytes bytes)
 {
     return(bytes.CopyTo(new byte[bytes.ByteCount]));
 }
Esempio n. 29
0
 public static unsafe Span <T> ToSpan <T>(this IBytes span) where T : struct
 {
     return(new Span <T>((void *)span.Ptr, span.ByteCount / typeof(T).SizeOf()));
 }
Esempio n. 30
0
 public UnsafeFloatArray(IBytes bytes)
     : this(bytes.Ptr.ToPointer(), bytes.ByteCount)
 {
 }