Various utilities used in C# LibP2P.
PM> Install-Package LibP2P.Utilities
-
T[] Slice<T>(this T[] array, int offset, int? count = null)
Returns a slice of an array, from offset and count (or the remaining lengt).
-
T[] Append<T>(this T[] array, params T[] items)
Returns a new array containing the given array and all the items in sequential order.
-
int Copy<T>(this T[] src, T[] dst, int offset, int? count = null)
Copies
count
items fromsrc
at givenoffset
and returns the actual count of items copied.
-
byte[] Append(this byte[] bytes, params byte[][] arrays)
Returns a new byte array containing the given array and all the arrays in sequential order.
-
int Compare(this byte[] a, byte[] b)
Compare
a
tob
by length and content. -
byte[] XOR(this byte[] a, byte[] b)
Returns a new byte array of the xor'ed result of
a
andb
. -
byte[] ComputeHash(this byte[] bytes)
Returns the hash digest of the given bytes using the default algorithm (
SHA2_256
).
-
bool IsIPLoopback(this Multiaddress addr)
Is the given Multiaddress pointing to a loopback address? Catches both IPv4 and IPv6.
-
bool IsFDCostlyTransport(this Multiaddress addr)
Does the given Multiaddress contain a transport that require file descriptors, like sockets? Catches only TCP for now.
-
int Compare(this Multihash a, Multihash b)
Compares two Multihashes by length and content.
-
byte[] SerializeToBytes<T>(this T obj)
Serializes the given object to a byte array using Protocol Buffers. The given object must be a valid protobuf-net contract.
-
T Deserialize<T>(this byte[] bytes)
Deserializes the given byte array to an instance of
T
. The given type must be a valid protobuf-net contract type.
-
void Read(this ReaderWriterLockSlim rwls, Action action, int timeout = Timeout.Infinite)
Aqcuire a read lock with optional timeout.
-
T Read<T>(this ReaderWriterLockSlim rwls, Func<T> func, int timeout = TImeout.Infinite)
Acquire a read lock and return a value with optional timeout.
-
void Write(this ReaderWriterLockSlim rwls, Action action, int timeout = Timeout.Infinite)
Acquire a write lock with optional timeout.
-
T Write<T>(this ReaderWriterLockSlim rwls, Func<T> func, int timeout = TImeout.Infinite)
Acquire a write lock and return a value with optional timeout.
-
void Lock(this SemaphoreSlim sl, Action action)
Acquire a lock and perform an action.
-
Task LockAsync(this SemaphoreSlim sl, Action action, CancellationToken cancellationToken
Acquire a lock and perform an action asynchronously.
-
T Lock<T>(this SemaphoreSim sl, Func<T> func)
Acquire a lock and return a value.
-
Task<T> LockAsync<T>(this SemaphoreSlim sl, Func<T> func, CancellationToken cancellationToken)
Acquire a lock and return a value asynchronously.
-
IReader AsReader(this Stream stream)
Get an
IReader
from aStream
. -
IWriter AsWriter(this Stream stream)
Get an
IWriter
from aStream
. -
Stream AsSystemStream(this IReader)
-
Stream AsSystemStream(this IWriter)
-
Stream AsSystemStream(this ISeeker)
-
Stream AsSystemStream(this IReadWriter)
-
Stream AsSystemStream(this IReadWriteSeeker)
-
Stream AsSystemStream(this IReadWriteCloser)
Get a
Stream
from a LibP2P interface reader/writer/seeker/etc. -
int CopyTo(this IReader reader, IWriter writer, int bufferSize = 4096)
Copy everything from reader to writer with a given buffer size. Returns bytes copied.
-
Task<int> CopyToAsync(this IReader reader, IWriter writer, int bufferSize = 4096, CancellationToken cancellationToken = default(CancellationToken))
Copy everything from reader to writer with a given buffer size asynchronously. Returns bytes copied.
-
int ReadFull(this IReader reader, byte[] buffer, int offset = 0, int count = -1)
Read from reader until given count is reached or the size of the buffer minus offset.
-
Task<int> ReadFullAsync(this IReader reader, byte[] buffer, int offset = 0, int count = -1, CancellationToken cancellationToken = default(CancellationToken))
Read from reader until given count is reached or the size of the buffer minus offset asynchronously.
-
Tuple<T, T> Swap<T>(this Tuple<T, T> tuple)
Swaps the values of a tuple.
-
ConcurrentList<T> : IList<T>, IDisposable
IList
with built inReaderWriterLockSlim
.
-
abstract class SyncMutex : IDisposable
Base class for usage with one
ReaderWriterLockSlim
.
Captain: @tabrath.
Contributions welcome. Please check out the issues.
Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.
Small note: If editing the README, please conform to the standard-readme specification.
MIT � 2016 Trond Br�then