Ejemplo n.º 1
0
    public static byte[] Encode(enPackageType type, byte[] body)
    {
        int length = HEADER_LENGTH;

        if (body != null)
        {
            length += body.Length;
        }

        byte[] buf = new byte[length];

        int index = 0;

        buf[index++] = Convert.ToByte(type);
        buf[index++] = Convert.ToByte(body.Length >> 16 & 0xFF);
        buf[index++] = Convert.ToByte(body.Length >> 8 & 0xFF);
        buf[index++] = Convert.ToByte(body.Length & 0xFF);

        while (index < length)
        {
            buf[index] = body[index - HEADER_LENGTH];
            index++;
        }

        return(buf);
    }
 internal void Send(enPackageType type)
 {
     if (this.state == enProtocolState.closed)
     {
         return;
     }
     transporter.Send(PackageProtocol.Encode(type));
 }
    //Send message use the transporter
    internal void Send(enPackageType type, byte[] body)
    {
        if (this.state == enProtocolState.closed)
        {
            return;
        }

        byte[] pkg = PackageProtocol.Encode(type, body);

        transporter.Send(pkg);
    }
    //Send system message, these message do not use messageProtocol
    internal void send(enPackageType type, MessageObject msg)
    {
        //This method only used to send system package
        if (type == enPackageType.Data)
        {
            return;
        }

        byte[] body = Encoding.UTF8.GetBytes(msg.ToString());

        Send(type, body);
    }
Ejemplo n.º 5
0
    public static Package Decode(byte[] buf)
    {
        enPackageType type = (enPackageType)buf[0];

        byte[] body = new byte[buf.Length - HEADER_LENGTH];

        for (int i = 0; i < body.Length; i++)
        {
            body[i] = buf[i + HEADER_LENGTH];
        }

        return(new Package(type, body));
    }
Ejemplo n.º 6
0
 public static byte[] Encode(enPackageType type)
 {
     return(new byte[] { Convert.ToByte(type), 0, 0, 0 });
 }
Ejemplo n.º 7
0
 public Package(enPackageType type, byte[] body)
 {
     this.type   = type;
     this.length = body.Length;
     this.body   = body;
 }