C # A self-written class used to compress data in a struct or class into "data packets" for Network Transmission

Source: Internet
Author: User

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 converts the struct types of byte, ushort, int, and string members, and the string type is only a 4-byte IP address.
/// Example:
/// Login login = new Login ();
/// Login. Flage = 1;
/// Login. PassWord = "login ";
/// 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>
/// Convert a member into a byte array
/// </Summary>
/// <Param name = "thetype"> type, such as jy. P2PBLL. Login </param>
/// <Param name = "obj"> this type of object </param>
/// <Returns> A byte array containing formats </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); // convert it to the byte sequence of the Network
UShort. CopyTo (buffer, bp );
Bp + = 2;
}
If (a = n)
{
Byte [] bint = BitConverter. GetBytes (int) fi. GetValue (obj); // It is not converted for the moment because it is used to hold an IP address
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>
/// Obtain thetype type objects
/// </Summary>
/// <Param name = "thetype"> type, such as jy. P2PBLL. Login </param>
/// <Param name = "data"> an array of bytes in a format </param>
/// <Returns> thetype type object </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.