C # A class is used to compress data in a struct or class into "data packets" for network transmission.

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

Time: Source:Unknown Author:Admin Click:

135 timesI want to contributeHigh-quality ASP. NET space, perfect support for 1.0/2.0/3.5/MVC, etc.

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 byte [size];
Fixed (byte * pb = & bytes [0])
{
Marshal. structuretoptr (OBJ, new intptr (PB), true );
}
Return bytes;
}
/// <Summary>
/// Serialization
/// </Summary>
/// <Param name = "MSG"> structure information </param>
/// <Returns> </returns>
Public static byte [] serialize (Object OBJ)
{
Iformatter informatter = new binaryformatter ();
Memorystream STM = new memorystream ();
Informatter. serialize (STM, OBJ );

Byte [] buffer = STM. toarray ();
STM. Close ();
Return buffer;
}

/// <Summary>
/// Deserialization
/// </Summary>
/// <Param name = "buffer"> serial data </param>
/// <Returns> </returns>
Public static object deserialize (byte [] buffer)
{
Memorystream stream = new memorystream (buffer );
Binaryformatter outformatter = new binaryformatter ();
Object OBJ = outformatter. deserialize (Stream );
Stream. Close ();
Return OBJ;
}
}

// UDP command
//
// + ---- + ----- + ------- + ------ + ---------- +
// | Ver | cmd | CLR | atyp | DST. ADDR | DST. Port |
// + ---- + ----- + ------- + ------ + ---------- +
// | 1 | 1 | x '00' | 1 | variable | 2 |
// + ---- + ----- + ------- + ------ + ---------- +
Public struct udpcmd
{
Public byte ver;
Public byte cmd;
Public byte CLR;
Public byte atyp;
Public int dstaddr; // troublesome during conversion
Public ushort dstport;
}
// Response format
//
// + ---- + ----- + ------- + ------ + ---------- +
// | Ver | rep | CLR | atyp | BND. ADDR | BND. Port |
// + ---- + ----- + ------- + ------ + ---------- +
// | 1 | 1 | x '00' | 1 | variable | 2 |
// + ---- + ----- + ------- + ------ + ---------- +
Public struct udpresponse
{
Public byte ver;
Public byte rep;
Public byte CLR;
Public byte atyp;
Public int dstaddr; // troublesome during conversion
Public ushort dstport;
}
// Retain 2-byte 0 | indicates whether to reassemble the datagram into segments | Address type | target address to be sent out of the proxy | port of the remote target host | data to be transmitted through the proxy
//
// + ---- + ------ + ---------- +
// | CLR | frag | atyp | DST. ADDR | DST. Port | data |
// + ---- + ------ + ---------- +
// | 2 | 1 | 1 | variable | 2 | variable |
// + ---- + ------ + ---------- +
Public struct udppackagehead
{
Public ushort CLR;
Public byte frag;
Public byte atyp;
Public int dstaddr;
Public ushort dstport;
}
}

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.