Search Unity

Binary Protocol Specification?

Discussion in 'Multiplayer' started by ZimM, Jul 24, 2016.

  1. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
  2. barrywu

    barrywu

    Joined:
    Jul 13, 2016
    Posts:
    2
    Yes, Do you have a UNet Protocol Sepcification? I have used wireshark to capture UNET.pcap,I want to reverse engineering UNET Protocol, but it's too hard!
     
  3. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Any update on this?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    @aabramychev (LLAPI dev)
     
  5. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    ping me on the week after next week. Hope will have time to publish :(
     
  6. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Ping.
     
    TwoTen likes this.
  7. W0nderful

    W0nderful

    Joined:
    Jun 16, 2017
    Posts:
    1
  8. zcxv

    zcxv

    Joined:
    Nov 16, 2013
    Posts:
    1
  9. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
  10. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Code (CSharp):
    1. //all 16 and 32 bit data are network ordered
    2. //message length occupy 1 byte length if first bit == 0 or 2 bytes if it equal 1:
    3. int SetLength(char* buffer, uint16_t messageLength)
    4. {
    5.     int messageLengthSize = (messageLength > 0x7F) ? 2 : 1;
    6.     if (messageLengthSize == 1)
    7.         {
    8.             *buffer = (uint8_t)messageLength;
    9.         }
    10.         else
    11.         {
    12.             Assert(messageLength < 0x7FFF);
    13.             *buffer = (messageLength >> 8) | 0x80;
    14.             *(buffer + 1) = (uint8_t)messageLength & 0xFF;
    15.         }
    16.         return messageLengthSize;
    17. }
    18.  
    19. //packets:
    20. //       header:
    21.     struct PacketBaseHeader
    22.     {
    23.         uint16_t connectionId;  //if 0 it will be system packet
    24.     };
    25.    
    26. //    user packet headers:
    27.     struct NetPacketHeader : public PacketBaseHeader
    28.     {
    29.         uint16_t packetId;
    30.         uint16_t sessionId;   //never changed and always in network order
    31.     };
    32. //    if there are reliable channels
    33.     struct PacketAcksXX  //XX == 32, 64, 96, 128
    34.     {
    35.         uint16_t ackMessageId;
    36.         uint32_t acks[x]; //x == 1,2,3,4        
    37.     };
    38.    
    39. //    system packets:
    40.      struct LocalDiscoveryPacket : public PacketBaseHeader //note that structure of broad cast packet is different from system!
    41.     {
    42.         uint8_t  requestType;
    43.         uint16_t clientPort;
    44.         uint8_t  guid[kUnetGUIDLength]; //kUnetGUIDLength == 36
    45.         uint32_t version;
    46.         uint32_t subversrion;
    47.     };
    48.    
    49.     //helper for other structures described below
    50.     struct SystemPacket : public PacketBaseHeader
    51.     {
    52.         uint8_t             requestType;
    53.         uint16_t            packetId;
    54.         uint16_t            sessionId;         //never changed
    55.         uint16_t            localConnectionId;
    56.         uint16_t            remoteConnectionId;
    57.     };
    58.    
    59.     struct ConnectPacket : public SystemPacket
    60.     {
    61.         uint32_t lib_version;
    62.         uint32_t crc;
    63.     };
    64.     struct DisconnectPacket : public SystemPacket
    65.     {
    66.         uint32_t lib_version;
    67.         uint8_t reason;
    68.     };
    69.     struct PingPacket : public SystemPacket
    70.     {
    71.         uint32_t sentPingTime;       //timestamp when this ping was sent
    72.         uint32_t ackPingTime;        //when we receive last ping from remote connection
    73.         uint32_t localTimeCorrection;//how many ms has been spent between time when we receive ackPingTime and time when we send this ping
    74.         uint8_t  intDropRate;        //how many packet (in % from 255) was dropped due lack storage in buffers
    75.         uint8_t  extDropRate;        //how many packet (in % from 255) was lost or delivered out of order due network conditions
    76.         uint16_t remoteSessionId;   //ping packet serves as connection request acknowledging, to avoid scenario where wrong ping will be ack connection, both session should be taken into account
    77.     };
    78.    
    79.     //Messages commons structure
    80.     //{message} == {channelId (uint8_t)|length(1 or 2 bytes)|message header|payload}
    81.     //special channels:
    82.     //reliable channelId == 255
    83.     //message == 255|length|NMReliableHeader|{message}{message}{message}
    84.     //and all messages lost its NMReliableHeader from message headers
    85.     //combined message - messages from one channel combined to one combined message and shared the same header
    86.     //channelId == 254
    87.     //message == 254|OriginChannelId{uint8_t)|length|{message in format: length|payload -- no header no channel number}
    88.    
    89.     //headers:
    90.     //helpers:
    91.     struct NMReliableHeader  //reliable header
    92.     {
    93.         uint16_t messageId;
    94.     };
    95.     struct NetMessageOrderedHeader  //this header applied to sequenced and state update qos
    96.     {
    97.         uint8_t orderedMessageId;
    98.     };
    99.     struct NetMessageFragmentedHeader //this header applied to fragmented messages
    100.     {
    101.         uint8_t  fragmentedMessageId;  //id of fragmented message    
    102.         uint8_t  fragmentIdx;          //id of fragment
    103.         uint8_t  fragmentAmnt;         // fragments total
    104.     };
    105.     //all reliable messages will be combined to one, except AllCost qos, so if channel has alacost host message = channelId|)|length(1 or 2 bytes)|NMReliableHeader|payload
    106.    
    107.    
     
    zcxv, TwoTen and HiddenMonk like this.
  11. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @ProinterRage it was for you. You can ping me directly if you will have a questions :0) (I guess you will :))
     
    zcxv likes this.
  12. marcedavi

    marcedavi

    Joined:
    Jan 2, 2020
    Posts:
    1
    Is this still up to date or has it changed?