Files
2026-07-10 15:25:05 +08:00

345 lines
11 KiB
C#

namespace LSServerService
{
using LSUtils;
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
public class PubUtils
{
public static readonly PubUtils Instance = new PubUtils();
private PubUtils()
{
}
public static byte[] BytesAddStruct(byte[] bytesHead, object structObj)
{
byte[] src = StructToBytes(structObj);
if ((bytesHead == null) || (bytesHead.Length <= 0))
{
return src;
}
int num = bytesHead.Length + src.Length;
byte[] dst = new byte[num];
Buffer.BlockCopy(bytesHead, 0, dst, 0, bytesHead.Length);
Buffer.BlockCopy(src, 0, dst, bytesHead.Length, src.Length);
return dst;
}
public static byte[] BytesAddStruct(object structObj, byte[] bytesTail)
{
byte[] src = StructToBytes(structObj);
if ((bytesTail == null) || (bytesTail.Length <= 0))
{
return src;
}
int num = src.Length + bytesTail.Length;
byte[] dst = new byte[num];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytesTail, 0, dst, src.Length, bytesTail.Length);
return dst;
}
public static byte[] BytesIntercept(byte[] SrcBytes, uint FixedLen)
{
byte[] dst = new byte[FixedLen];
int count = (SrcBytes.Length > FixedLen) ? ((int) FixedLen) : SrcBytes.Length;
Buffer.BlockCopy(SrcBytes, 0, dst, 0, count);
return dst;
}
public static string BytesToStr(byte[] SrcBytes)
{
return Encoding.Default.GetString(SrcBytes).Replace("\0", "");
}
public static string BytesToStr(byte[] SrcBytes, int index, int count)
{
return Encoding.Default.GetString(SrcBytes, index, count).Replace("\0", "");
}
public static object BytesToStruct(byte[] bytes, Type structType)
{
return BytesToStruct(bytes, structType, 0, 0);
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex)
{
return BytesToStruct(bytes, structType, startIndex, 0);
}
public static object BytesToStruct(byte[] bytes, Type structType, string ExceptField)
{
return BytesToStruct(bytes, structType, 0, 0, new string[] { ExceptField });
}
public static object BytesToStruct(byte[] bytes, Type structType, string[] ExceptFields)
{
return BytesToStruct(bytes, structType, 0, 0, ExceptFields);
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex, int decCount)
{
object obj2 = null;
int num = 1;
int num2 = 0;
int num3 = 0;
while (num > 0)
{
int cb = Marshal.SizeOf(structType);
IntPtr destination = Marshal.AllocHGlobal(cb);
try
{
num3 = 0;
Marshal.Copy(bytes, startIndex, destination, cb - decCount);
num3 = 1;
obj2 = Marshal.PtrToStructure(destination, structType);
num3 = 2;
}
catch (Exception exception)
{
WriteLog("PubUtils.BytesToStruct error is {0},size={1},startIndex={2},decCount={3},bError={4},LinePos={5},bytes is:\n{6}", new object[] { exception.Message.ToString(), cb, startIndex, decCount, num2, num3, bytes });
num++;
if (num2 > 2)
{
throw new Exception(exception.Message.ToString());
}
num2++;
}
finally
{
Marshal.FreeHGlobal(destination);
}
num--;
}
return obj2;
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex, string ExceptField)
{
return BytesToStruct(bytes, structType, startIndex, 0, new string[] { ExceptField });
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex, string[] ExceptFields)
{
return BytesToStruct(bytes, structType, startIndex, 0, ExceptFields);
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex, int decCount, string ExceptField)
{
return BytesToStruct(bytes, structType, startIndex, decCount, new string[] { ExceptField });
}
public static object BytesToStruct(byte[] bytes, Type structType, int startIndex, int decCount, string[] ExceptFields)
{
object obj3;
try
{
string str = "|";
for (int i = 0; i < ExceptFields.Length; i++)
{
str = str + ExceptFields[i] + "|";
}
int dstOffset = 0;
int num3 = Marshal.SizeOf(structType) - decCount;
byte[] buffer = new byte[num3];
object obj2 = BytesToStruct(buffer, structType);
FieldInfo[] fields = structType.GetFields();
foreach (FieldInfo info in fields)
{
int count = 0;
if (info.FieldType.IsArray)
{
Array array = (Array) info.GetValue(obj2);
count = array.Length;
}
else
{
count = Marshal.SizeOf(info.FieldType);
}
string str2 = "|" + info.Name + "|";
if (!str.Contains(str2))
{
Buffer.BlockCopy(bytes, startIndex, buffer, dstOffset, count);
startIndex += count;
}
dstOffset += count;
}
obj3 = BytesToStruct(buffer, structType);
}
catch (Exception exception)
{
WriteLog("PubUtils.BytesToStruct error: {0},bytes is :\n{1} \n,ExceptFields is \n{2}", new object[] { exception.Message.ToString(), bytes, ExceptFields });
throw new Exception(exception.Message.ToString());
}
return obj3;
}
public static byte[] ComposeTwoBytes(byte[] bytesHead, byte[] bytesTail)
{
int num = bytesHead.Length + bytesTail.Length;
byte[] dst = new byte[num];
Buffer.BlockCopy(bytesHead, 0, dst, 0, bytesHead.Length);
Buffer.BlockCopy(bytesTail, 0, dst, bytesHead.Length, bytesTail.Length);
return dst;
}
public static byte[] CopyArrayData(byte[] source, int startIndex, int length)
{
byte[] buffer = new byte[length];
for (int i = 0; i < length; i++)
{
buffer[i] = source[startIndex + i];
}
return buffer;
}
public static void CopyStructToBytes(object structObj, byte[] destBytes, int dstOffset)
{
int cb = Marshal.SizeOf(structObj);
IntPtr ptr = Marshal.AllocHGlobal(cb);
try
{
Marshal.StructureToPtr(structObj, ptr, false);
Marshal.Copy(ptr, destBytes, dstOffset, cb);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
}
public static IPAddress GetServerIP()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress address in hostEntry.AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
return address;
}
}
return null;
}
public static uint IPToLong(string strIP)
{
return (uint) IPAddress.Parse(strIP).Address;
}
public static string LongToIP(uint dwIP)
{
return new IPAddress((long) dwIP).ToString();
}
public static string ReadXmlStr(string SrcStr)
{
return SrcStr.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&apos;", "'").Replace("&quot;", "\"");
}
public static byte[] StrToBytes(string SrcString)
{
return Encoding.Default.GetBytes(SrcString);
}
public static byte[] StrToBytes(string SrcString, uint FixedLen)
{
return StrToBytes(SrcString.ToCharArray(), FixedLen);
}
public static byte[] StrToBytes(char[] SrcChar, uint FixedLen)
{
return BytesIntercept(Encoding.Default.GetBytes(SrcChar), FixedLen);
}
public static byte[] StructToBytes(object structObj)
{
byte[] buffer2;
int cb = Marshal.SizeOf(structObj);
IntPtr ptr = Marshal.AllocHGlobal(cb);
try
{
Marshal.StructureToPtr(structObj, ptr, false);
byte[] destination = new byte[cb];
Marshal.Copy(ptr, destination, 0, cb);
buffer2 = destination;
}
finally
{
Marshal.FreeHGlobal(ptr);
}
return buffer2;
}
public static byte[] StructToBytes(object structObj, int decCount)
{
byte[] buffer2;
int cb = Marshal.SizeOf(structObj);
IntPtr ptr = Marshal.AllocHGlobal(cb);
try
{
Marshal.StructureToPtr(structObj, ptr, false);
byte[] destination = new byte[cb - decCount];
Marshal.Copy(ptr, destination, 0, cb - decCount);
buffer2 = destination;
}
finally
{
Marshal.FreeHGlobal(ptr);
}
return buffer2;
}
public static void WriteLog(string ErrMsg, params object[] args)
{
LSWriteLog.WriteLog("sysError.log", ErrMsg, args);
Win32Exception exception = new Win32Exception();
LSWriteLog.WriteLog("sysError.log", "Win32Exception= {0}", new object[] { exception });
exception = null;
}
public static string WriteXmlStr(string SrcStr)
{
return SrcStr.Replace("<", "&lt;").Replace(">", "&gt;").Replace("&", "&amp;").Replace("'", "&apos;").Replace("\"", "&quot;");
}
public static IPAddress LocalFirstIPAddr
{
get
{
return Dns.Resolve(LocalMachineName).AddressList[0];
}
}
public static uint LocalFirstIPLong
{
get
{
return (uint) LocalFirstIPAddr.Address;
}
}
public static string LocalFirstIPStr
{
get
{
return LocalFirstIPAddr.Address.ToString();
}
}
public static string LocalMachineName
{
get
{
return Environment.MachineName;
}
}
}
}