using System;
using System.Reflection;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
namespace CSProxy
{
/// <summary>
/// CSProxyTrans 轉換有byte,ushort,int 和 string 類型成員的結構體,並且string 類型只是4字節的IP地址。
/// 例:
/// Login login=new Login();
/// login.Flage=1;
/// login.PassWord="zzy";
/// login.State=1;
/// login.UserID="zhaozhonglei";
///
/// byte[] buffer=Trans.ToBytes("jy.P2PBLL.Login",login);
/// Login login2=(Login)Trans.ToStruct("jy.P2PBLL.Login",buffer);
/// </summary>
public class CSProxyTrans
{
/// <summary>
/// 將成員轉化為字節數組
/// </summary>
/// <param name="thetype">類型的完全限定名,如jy.P2PBLL.Login</param>
/// <param name="obj">該類型的對象</param>
/// <returns>含有個格式的字節數組</returns>
public static byte[] ToBytes(string thetype,object obj)
{
Type type=Type.GetType(thetype);
FieldInfo [] infos=type.GetFields();
byte[] buffer=new byte[10240];
int bp=0;
foreach(FieldInfo fi in infos)
{
string a=fi.FieldType.ToString();
string b=typeof(byte).ToString();
string us=typeof(ushort).ToString();
string n=typeof(int).ToString();
string s=typeof(string).ToString();
if(a == b)
{
buffer[bp]=(byte)fi.GetValue(obj);
bp++;
}
if(a == us)
{
byte[] uShort=BitConverter.GetBytes(IPAddress.HostToNetworkOrder((ushort)fi.GetValue(obj)));//轉化成網絡字節順序
uShort.CopyTo(buffer,bp);
bp+=2;
}
if(a == n)
{
byte[] bint=BitConverter.GetBytes((int)fi.GetValue(obj));//暫時不轉,因用來盛放一個 IP
bint.CopyTo(buffer,bp);
bp+=4;
}
if(a == s)
{
object O= fi.GetValue(obj);
string str=(string)O;
if(O!=null)
{
byte[] bstring=System.Text.Encoding.Unicode.GetBytes(str);
int len=bstring.Length;
byte[] bint=BitConverter.GetBytes(len);
bint.CopyTo(buffer,bp);
bp+=4;
bstring.CopyTo(buffer,bp);
bp+=len;
}
else
{
byte[] bint=BitConverter.GetBytes(0);
bint.CopyTo(buffer,bp);
bp+=4;
}
}
}
byte[] data=new byte[bp];
Array.Copy(buffer,0,data,0,bp);
return data;
}
/// <summary>
/// 得到thetype類型的對象
/// </summary>
/// <param name="thetype">類型的完全限定名,如jy.P2PBLL.Login</param>
/// <param name="data">含有個格式的字節數組</param>
/// <returns>thetype類型的對象</returns>
public static object ToStruct(string thetype,byte [] data)
{
Type type=Type.GetType(thetype);
FieldInfo [] infos=type.GetFields();
object obj=Activator.CreateInstance(type);
int bp=0;
foreach(FieldInfo fi in infos)
{
string a=fi.FieldType.ToString();
string b=typeof(byte).ToString();
string us=typeof(ushort).ToString();
string n=typeof(int).ToString();
string s=typeof(string).ToString();
if(a == b)
{
byte bval=data[bp];
fi.SetValue(obj,bval);
bp+=1;
}
if(a == us)
{
ushort be=BitConverter.ToUInt16(data,bp);
ushort sval=(ushort)IPAddress.NetworkToHostOrder((short)be);
fi.SetValue(obj,sval);
bp+=2;
}
if(a == n)
{
int val=BitConverter.ToInt32(data,bp);
fi.SetValue(obj,val);
bp+=4;
}
if(a == s)
{
int len=BitConverter.ToInt32(data,bp);
bp+=4;
if(len!=0)
{
string val=System.Text.Encoding.Unicode.GetString(data,bp,len);
fi.SetValue(obj,val);
bp+=len;
}
else
{
string val="";
fi.SetValue(obj,val);
}
}
}
return obj;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static unsafe byte[] ToBytes(object obj)
{
int size = Marshal.SizeOf(obj);
byte[] bytes = new b