Search Unity

Help Wanted Updating from MLAPI missing features

Discussion in 'Netcode for GameObjects' started by BruceKristelijn, Nov 10, 2021.

  1. BruceKristelijn

    BruceKristelijn

    Joined:
    Apr 28, 2017
    Posts:
    47
    Hey, so I the following script in my project:
    Code (CSharp):
    1.     private MlapiCommsNetwork _network;
    2.  
    3.     public MlapiServer(MlapiCommsNetwork network)
    4.     {
    5.         _network = network;
    6.     }
    7.     public override void Connect()
    8.     {
    9.         // Register receiving packets on the server from the client
    10.         CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToServer", (senderClientId, stream) =>
    11.         {
    12.             var client = new MlapiConn();
    13.             client.clientId = senderClientId;
    14.  
    15.             Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
    16.             Byte[] buffer = new Byte[length];
    17.             stream.Read(buffer, 0, length);
    18.  
    19.             base.NetworkReceivedPacket(client, new ArraySegment<byte>(buffer));
    20.         });
    21.         base.Connect();
    22.     }
    23.  
    24.     public override void Disconnect()
    25.     {
    26.         base.Disconnect();
    27.     }
    28.  
    29.     protected override void ReadMessages()
    30.     {
    31.  
    32.     }
    33.  
    34.     protected override void SendReliable(MlapiConn destination, ArraySegment<byte> packet)
    35.     {
    36.         using (PooledNetworkBuffer stream = PooledNetworkBuffer.Get())
    37.         {
    38.             using (PooledNetworkWriter writer = PooledNetworkWriter.Get(stream))
    39.             {
    40.                 var count = packet.Count;
    41.                 var offset = packet.Offset;
    42.                 for (var i = 0; i < count; i++)
    43.                 {
    44.                     writer.WriteByte(packet.Array[i + offset]);
    45.                 }
    46.                 if (NetworkManager.Singleton.LocalClientId == destination.clientId)
    47.                 {
    48.                     // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues
    49.                     _network.client.NetworkReceivedPacket(packet);
    50.                 }
    51.                 else
    52.                 {
    53.                     CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, MLAPI.Transports.NetworkChannel.AnimationUpdate);
    54.                 }
    55.             }
    56.         }
    57.     }
    58.  
    59.     protected override void SendUnreliable(MlapiConn destination, ArraySegment<byte> packet)
    60.     {
    61.         using (PooledNetworkBuffer stream = PooledNetworkBuffer.Get())
    62.         {
    63.             using (PooledNetworkWriter writer = PooledNetworkWriter.Get(stream))
    64.             {
    65.                 var count = packet.Count;
    66.                 var offset = packet.Offset;
    67.                 for (var i = 0; i < count; i++)
    68.                 {
    69.                     writer.WriteByte(packet.Array[i + offset]);
    70.                 }
    71.                 if (NetworkManager.Singleton.LocalClientId == destination.clientId)
    72.                 {
    73.                     // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues
    74.                     _network.client.NetworkReceivedPacket(packet);
    75.                 }
    76.                 else
    77.                 {
    78.                     CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, MLAPI.Transports.NetworkChannel.TimeSync);
    79.                 }
    80.             }
    81.         }
    82.  
    83.     }
    84.  
    85.     public void ClientDisconnected()
    86.     {
    87.  
    88.     }
    89.  
    But after updating I am missing PooledNetworkBuffer, PooledNetworkWriter and the read function. Is this still possible in the com.unity.netcode.gameobjects package?
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    223
    The pooled buffer/writer/reader have been replaced with FastBufferWriter/Reader.
     
  3. BruceKristelijn

    BruceKristelijn

    Joined:
    Apr 28, 2017
    Posts:
    47
    Thankyou, however I am having some issues regarding the reading and writing like I did in the script above. Can you maybe give me a push in the right direction?
     
  4. BruceKristelijn

    BruceKristelijn

    Joined:
    Apr 28, 2017
    Posts:
    47
    Ok so I just did this:
    I turned this:
    Code (CSharp):
    1.             using (PooledNetworkBuffer stream = PooledNetworkBuffer.Get())
    2.             {
    3.                 using (PooledNetworkWriter writer = PooledNetworkWriter.Get(stream))
    4.                 {
    5.                     for (var i = 0; i < packet.Count; i++)
    6.                     {
    7.                         writer.WriteByte(packet.Array[i + packet.Offset]);
    8.                     }
    9.                     CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, stream, MLAPI.Transports.NetworkChannel.TimeSync);
    10.                 }
    11.             }
    Into this:
    Code (CSharp):
    1.             using (FastBufferWriter writer = new FastBufferWriter())
    2.             {
    3.                 for (var i = 0; i < packet.Count; i++)
    4.                 {
    5.                     writer.WriteByte(packet.Array[i + packet.Offset]);
    6.                 }
    7.                 NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Unreliable);
    8.             }
     
unityunity