Beispiel #1
0
    public static msg_cmd Decode_Json(byte[] package)
    {
        msg_cmd cmd = new msg_cmd();

        cmd.stype = package[0] | (package[1] << 8);
        cmd.ctype = package[2] | (package[3] << 8);
        cmd.body  = new byte[package.Length - 8];
        BytesWriter.Write_Byte_to_bytes(package, 8, cmd.body, 0);
        return(cmd);
    }
Beispiel #2
0
    public static byte[] Package(byte[] data)
    {
        int header_len = 2;
        int data_len   = data.Length + header_len;

        if (data.Length > 65535 - 2)
        {
            return(null);
        }
        byte[] package = new byte[data_len];
        BytesWriter.Write_short_to_bytes(package, 0, (short)data_len);
        BytesWriter.Write_Byte_to_bytes(data, 0, package, 2);
        return(package);
    }
Beispiel #3
0
    public static byte[] Encode_Json(int stype, int ctype, string json_str)
    {
        int Header_Len = 8;

        byte[] byte_array = null;
        if (json_str != null)
        {
            byte_array = System.Text.Encoding.UTF8.GetBytes(json_str);
        }
        byte[] Encode_Bytes = new byte[Header_Len + byte_array.Length];
        BytesWriter.Write_short_to_bytes(Encode_Bytes, 0, (short)stype);
        BytesWriter.Write_short_to_bytes(Encode_Bytes, 2, (short)ctype);
        BytesWriter.Write_Byte_to_bytes(byte_array, 0, Encode_Bytes, 8);
        return(Encode_Bytes);
    }
Beispiel #4
0
 public static byte[] UnPackage(byte[] data, out int header_len)
 {
     if (data.Length < 2)
     {
         header_len = 0;
         return(null);
     }
     header_len = data[0] | data[1] << 8;
     if (header_len > data.Length)//接收的数据不足
     {
         return(null);
     }
     byte[] package = new byte[header_len - 2];
     BytesWriter.Write_Byte_to_bytes(data, 2, package, 0);
     return(package);
 }
Beispiel #5
0
    public static byte[] Encode_Protobuf(int stype, int ctype, ProtoBuf.IExtensible body)
    {
        int Header_Len = 8;

        byte[] byte_array = null;
        int    body_len   = 0;

        if (body != null)
        {
            byte_array = Serialize(body);
            body_len   = byte_array.Length;
        }
        byte[] Encode_Bytes = new byte[Header_Len + body_len];
        BytesWriter.Write_short_to_bytes(Encode_Bytes, 0, (short)stype);
        BytesWriter.Write_short_to_bytes(Encode_Bytes, 2, (short)ctype);
        if (body != null)
        {
            BytesWriter.Write_Byte_to_bytes(byte_array, 0, Encode_Bytes, 8);
        }
        return(Encode_Bytes);
    }