mirror of
https://github.com/islehorse/HISP.git
synced 2025-04-21 20:25:51 +12:00
Add Multiple transports system, begin work on WebSockets
This commit is contained in:
parent
e869a23463
commit
e74f66a439
22 changed files with 3752 additions and 3301 deletions
|
@ -1,61 +1,84 @@
|
|||
using HISP.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace HISP.Util
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
// Thanks Stackoverflow (https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array)
|
||||
private static int getHexVal(char hex)
|
||||
{
|
||||
int val = (int)hex;
|
||||
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
||||
}
|
||||
public static byte[] StringToByteArray(string hex)
|
||||
{
|
||||
if (hex.Length % 2 == 1)
|
||||
throw new ArgumentException("The binary key cannot have an odd number of digits");
|
||||
|
||||
byte[] arr = new byte[hex.Length >> 1];
|
||||
|
||||
for (int i = 0; i < hex.Length >> 1; ++i)
|
||||
{
|
||||
arr[i] = (byte)((getHexVal(hex[i << 1]) << 4) + (getHexVal(hex[(i << 1) + 1])));
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static double PointsToDistance(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
|
||||
}
|
||||
public static string CapitalizeFirstLetter(string str)
|
||||
{
|
||||
char firstChar = char.ToUpper(str[0]);
|
||||
return firstChar + str.Substring(1);
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace HISP.Util
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
// Thanks Stackoverflow (https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array)
|
||||
private static int getHexVal(char hex)
|
||||
{
|
||||
int val = (int)hex;
|
||||
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
||||
}
|
||||
public static byte[] StringToByteArray(string hex)
|
||||
{
|
||||
if (hex.Length % 2 == 1)
|
||||
throw new ArgumentException("The binary key cannot have an odd number of digits");
|
||||
|
||||
byte[] arr = new byte[hex.Length >> 1];
|
||||
|
||||
for (int i = 0; i < hex.Length >> 1; ++i)
|
||||
{
|
||||
arr[i] = (byte)((getHexVal(hex[i << 1]) << 4) + (getHexVal(hex[(i << 1) + 1])));
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static double PointsToDistance(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
|
||||
}
|
||||
public static string CapitalizeFirstLetter(string str)
|
||||
{
|
||||
char firstChar = char.ToUpper(str[0]);
|
||||
return firstChar + str.Substring(1);
|
||||
}
|
||||
public static int GetMonthsBetweenTwoDateTimes(DateTime from, DateTime to)
|
||||
{
|
||||
if (from > to) return GetMonthsBetweenTwoDateTimes(to, from);
|
||||
int monthDiff = Math.Abs((to.Year * 12 + (to.Month)) - (from.Year * 12 + (from.Month)));
|
||||
return monthDiff;
|
||||
|
||||
}
|
||||
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
|
||||
{
|
||||
// Unix timestamp is seconds past epoch
|
||||
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
|
||||
public static void ByteArrayToByteList(byte[] byteArray, List<byte> byteList)
|
||||
{
|
||||
byteList.AddRange(byteArray.ToList());
|
||||
}
|
||||
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
|
||||
{
|
||||
// Unix timestamp is seconds past epoch
|
||||
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
|
||||
public static bool ByteArrayStartsWith(byte[] byteArray, byte[] searchValue)
|
||||
{
|
||||
if (byteArray.Length < searchValue.Length) return false;
|
||||
|
||||
byte[] buffer = new byte[searchValue.Length];
|
||||
Array.ConstrainedCopy(byteArray, 0, buffer, 0, searchValue.Length);
|
||||
|
||||
return buffer.SequenceEqual(searchValue);
|
||||
}
|
||||
|
||||
public static bool ByteArrayEndsWith(byte[] byteArray, byte[] searchValue)
|
||||
{
|
||||
if (searchValue.Length > byteArray.Length) return false;
|
||||
|
||||
byte[] buffer = new byte[searchValue.Length];
|
||||
Array.ConstrainedCopy(byteArray, (byteArray.Length - searchValue.Length), buffer, 0, searchValue.Length);
|
||||
|
||||
return buffer.SequenceEqual(searchValue);
|
||||
}
|
||||
|
||||
public static void ByteArrayToByteList(byte[] byteArray, List<byte> byteList)
|
||||
{
|
||||
byteList.AddRange(byteArray.ToList());
|
||||
}
|
||||
|
||||
public static string RandomString(string allowedCharacters)
|
||||
{
|
||||
int length = GameServer.RandomNumberGenerator.Next(7, 16);
|
||||
|
@ -64,7 +87,7 @@ namespace HISP.Util
|
|||
str += allowedCharacters[GameServer.RandomNumberGenerator.Next(0, allowedCharacters.Length - 1)];
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
public static string ReverseString(string str)
|
||||
{
|
||||
char[] charArray = str.ToCharArray();
|
||||
|
@ -72,6 +95,14 @@ namespace HISP.Util
|
|||
string newStr = new string(charArray);
|
||||
|
||||
return newStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetIp(EndPoint ep)
|
||||
{
|
||||
string endPointIp = ep.ToString();
|
||||
if (endPointIp.Contains(":"))
|
||||
endPointIp = endPointIp.Substring(0, endPointIp.IndexOf(":"));
|
||||
return endPointIp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue