123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- namespace UdPunching
- {
- public static class ExtensionMethods
- {
- private static readonly DateTime UnixTimeStampOrig = new DateTime(1970, 1, 1);
- public static bool IpEndPointEqualsTo(this EndPoint me, EndPoint another)
- {
- if (me is IPEndPoint ipMe && another is IPEndPoint ipAnother)
- return ipMe.Port == ipAnother.Port && ipMe.Address.Equals(ipAnother.Address);
- return false;
- }
- public static int ToUnixTimeStamp(this DateTime me)
- {
- return (int)me.ToUniversalTime().Subtract(UnixTimeStampOrig).TotalSeconds;
- }
- public static int ReadUnixTimeStamp(this byte[] buf, int index, out DateTime timeStamp)
- {
- var i32 = buf.ToLeInt32(index);
- timeStamp = UnixTimeStampOrig.AddSeconds(i32).ToLocalTime();
- return 4;
- }
- public static int ReadIpEndPoint(this byte[] buf, int index, out IPEndPoint ipEndPoint)
- {
- var strAddress = buf.GetAsciiString(15, index);
- var port = buf.GetLeInt16(index + 15);
- ipEndPoint = new IPEndPoint(IPAddress.Parse(strAddress.Trim()), port);
- return 17;
- }
- public static IPEndPoint ParseToIpEndPointV4(this string str)
- {
- var parts = str.Split(':');
- if (2 != parts.Length) throw new ArgumentException("invalid addr port parts");
- if (IPAddress.TryParse(parts[0], out var addr) && int.TryParse(parts[1], out var port)) return new IPEndPoint(addr, port);
- throw new FormatException("parse fail");
- }
- //--------- underlying -------------
- public static byte[] ToLeBytes(this int value)
- {
- return new[]
- {
- (byte)(value & 0xFF),
- (byte)((value>>8 ) & 0xFF),
- (byte)((value>>16) & 0xFF),
- (byte)((value>>24) & 0xFF),
- };
- }
- public static int ToLeInt32(this byte[] buf, int index)
- {
- return buf[index]
- | (buf[index + 1] << 8)
- | (buf[index + 2] << 16)
- | (buf[index + 3] << 24)
- ;
- }
- public static byte[] ToLe16Bytes(this int value)
- {
- return new[]
- {
- (byte)(value & 0xFF),
- (byte)((value>>8 ) & 0xFF),
- };
- }
- public static void ToLe16Bytes(this int value, byte[] buf, int index = 0)
- {
- buf[index] = (byte)(value & 0xFF);
- buf[index + 1] = (byte)((value >> 8) & 0xFF);
- }
- public static int GetLeInt16(this byte[] buf, int index)
- {
- return buf[index]
- | buf[index + 1] << 8
- ;
- }
- public static string ToPaddingString(this IPAddress ipAddress)
- {
- if (ipAddress.AddressFamily != AddressFamily.InterNetwork) throw new NotSupportedException("Only ipv4");
- return ipAddress.ToString().PadRight(15, ' ');
- }
- public static byte[] ToAsciiBytes(this string str) => Encoding.ASCII.GetBytes(str);
- public static void ToAsciiBytes(this string str, byte[] buf, int index = 0) => Encoding.ASCII.GetBytes(str, 0, str.Length, buf, index);
- public static string GetAsciiString(this byte[] buf, int count, int index = 0) => Encoding.ASCII.GetString(buf.Skip(index).Take(count).ToArray());
- }
- }
|