proto3 序列化过程实例

2018-08-23  本文已影响41人  JonorZhang

1. proto文件

enum ErrorCode {
    SUCCESS = 0;
    UNKNOWN = 1;
    INTERNAL = 2;
    UNSUPPORTED = 3;
    USER_CANCELLED = 4;
    NOT_FOUND = 5;
    INVALID = 6;
    BUSY = 7;
}

enum Command {
    NONE = 0;

    // ~~~~~Speech~~~~~
    // -> accessory
    PROVIDE_SPEECH = 10;
    NOTIFY_SPEECH_STATE = 14;

    // <- accessory
    START_SPEECH = 11;

    // <-> accessory
    STOP_SPEECH = 12;
    ENDPOINT_SPEECH = 13;

    // ~~~~~Device~~~~~
    // -> accessory
    GET_DEVICE_INFORMATION = 20;
    GET_DEVICE_CONFIGURATION = 21;
    OVERRIDE_ASSISTANT = 22;
    START_SETUP = 23;
    COMPLETE_SETUP = 24;
    NOTIFY_DEVICE_CONFIGURATION = 25;

    // ~~~~~Transport~~~~~
    // -> accessory
    UPGRADE_TRANSPORT = 30;
    SWITCH_TRANSPORT = 31;

    // ~~~~~Calling~~~~~
    // -> accessory
    FORWARD_AT_COMMAND = 40;
    // <- accessory
    INCOMING_CALL = 41;

    // ~~~~~System~~~~~
    // -> accessory
    SYNCHRONIZE_SETTINGS = 50;
    // <- accessory
    RESET_CONNECTION = 51;
    // <-> accessory
    KEEP_ALIVE = 55;

    // ~~~~~Media~~~~~
    // -> accessory
    ISSUE_MEDIA_CONTROL = 60;

    // ~~~~~State~~~~~
    // -> accessory
    GET_STATE = 100;
    SET_STATE = 101;

    // <- accessory
    SYNCHRONIZE_STATE = 102;

    // ~~~~~Central~~~~~
    // -> accessory
    GET_CENTRAL_INFORMATION = 103;
}

//This will be sent as a response to GetDeviceInformation.
message DeviceInformation {
    string serial_number = 1;
    string name = 2;
    repeated Transport supported_transports = 3;
    string device_type = 4;
}

message Response {
    ErrorCode error_code = 1;

    oneof payload {

        //As a response to GetDeviceInformation
        DeviceInformation device_information = 3;

        //As a response to GetState
        State state = 7;

        //As a response to UpgradeTransport
        ConnectionDetails connection_details = 8;

        //As a response to GetDeviceConfiguration
        DeviceConfiguration device_configuration = 10;


        //As a response to GetCentralInformation
        CentralInformation central_information = 13;

        //As a response to StartSpeech, StopSpeech, Endpointspeech.
        Dialog dialog = 14;

        //As a response to ProvideSpeech
        SpeechProvider speech_provider = 15;
    }
}

message ControlEnvelope {
    Command command = 1;

    oneof payload {
        //Response
        Response response = 9;

        //Speech
        ProvideSpeech provide_speech = 10;
        StartSpeech start_speech = 11;
        StopSpeech stop_speech = 12;
        EndpointSpeech endpoint_speech = 13;
        NotifySpeechState notify_speech_state = 14;

        //Device
        GetDeviceInformation get_device_information = 20;
        GetDeviceConfiguration get_device_configuration = 21;
        OverrideAssistant override_assistant = 22;
        StartSetup start_setup = 23;
        CompleteSetup complete_setup = 24;
        NotifyDeviceConfiguration notify_device_configuration = 25;

        //Transport
        UpgradeTransport upgrade_transport = 30;
        SwitchTransport switch_transport = 31;

        //Calling
        ForwardATCommand forward_at_command = 40;
        IncomingCall incoming_call = 41;

        //System
        SynchronizeSettings synchronize_settings = 50;
        ResetConnection reset_connection = 51;
        KeepAlive keep_alive = 55;

        //Media
        IssueMediaControl issue_media_control = 60;

        //State
        GetState get_state = 100;
        SetState set_state = 101;
        SynchronizeState synchronize_state = 102;

        //Central
        GetCentralInformation get_central_information = 103;
    }
}

2. 对象结构:

message ControlEnvelope {
    .command = GET_DEVICE_INFORMATION                   
    .response = Response {
        .error_code = SUCCESS
        .device_information = DeviceInformation {
            .serial_number = "ABCD"
            .name = "ABC"
            .device_type = "AB"
        }
    }
}

3. tag-value结构化:

{
    1 : 20
    9 : {
        1 : 0
        3 : {
            1 : "ABCD"
            2 : "ABC"
            4 : "AB"
        }
    }
}

4. 序列化:

T   00001 000        08H        
V   0 0010100        14H

T   01001 000        09H
L   0 0010011        13H

V 19{
    T   00001 000        08H
    V   0 0000000        00H

    T   00011 000        18H
    L   0 0001111        0FH
    V 15{
        T   00001 000        08H
        L   0 0000100        04H
        V   "ABCD"           41H 42H 43H 44H

        T   00010 000        10H
        L   0 0000011        03H
        V   "ABC"            41H 42H 43H

        T   00100 000        20H
        L   0 0000010        02H
        V   "AB"             41H 42H
    }
}

5. Binary:

08 14 09 13 08 00 18 0F 08 04 41 42 43 44 10 03 41 42 43 20 02 41 42

6.参考资料:

Protocol Buffer 序列化原理大揭秘
proto3官方文档

上一篇 下一篇

猜你喜欢

热点阅读