Search Unity

Trying to send byte array to server yields error: Trying to send command for object without authorit

Discussion in 'Multiplayer' started by kenmarold, Nov 18, 2017.

  1. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    I have no idea why this is happening. I'm trying to send a byte array from my client to my server across a local network and I'm getting the error `Trying to send command for object without authority.` when multiple clients are connected to the server simultaneously. If I'm creating an object that's a serialized byte array, how do give it authority?

    The full error I'm getting is

    Trying to send command for object without authority.
    UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
    Network_Transmitter:CallCmdPrepareToReceiveBytes(Int32, Int32)
    <DoSendBytes>c__Iterator0:MoveNext() (at Assets/Scripts/Networking/Network_Transmitter.cs:46)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    Player_Data:prepareServerData() (at Assets/Scripts/Player/Player_Data.cs:77)
    Event_Manager:SendTextureData() (at Assets/Scripts/Event_Manager.cs:31)
    Game_UI:SendData() (at Assets/Scripts/Game_UI.cs:39)
    UnityEngine.EventSystems.EventSystem:Update()

    and the functions where the error is occurring is here:

    Code (CSharp):
    1.     [Client]
    2.         public IEnumerator DoSendBytes(int transmissionId, byte[] data)
    3.         {
    4.             Debug.Assert(!clientTransmissionIds.Contains(transmissionId));
    5.             Debug.Log(LOG_PREFIX + "SendBytesToClients processId=" + transmissionId + " | datasize=" + data.Length);
    6.  
    7.             //tell server that he is going to receive some data and tell him how much it will be.
    8.             CmdPrepareToReceiveBytes(transmissionId, data.Length);
    9.             yield return null;
    10.  
    11.             //begin transmission of data. send chunks of 'bufferSize' until completely transmitted.
    12.             clientTransmissionIds.Add(transmissionId);
    13.             TransmissionData dataToTransmit = new TransmissionData(data);
    14.             int bufferSize = defaultBufferSize;
    15.             while (dataToTransmit.curDataIndex < dataToTransmit.data.Length - 1)
    16.             {
    17.                 //determine the remaining amount of bytes, still need to be sent.
    18.                 int remaining = dataToTransmit.data.Length - dataToTransmit.curDataIndex;
    19.                 if (remaining < bufferSize)
    20.                     bufferSize = remaining;
    21.  
    22.                 //prepare the chunk of data which will be sent in this iteration
    23.                 byte[] buffer = new byte[bufferSize];
    24.                 System.Array.Copy(dataToTransmit.data, dataToTransmit.curDataIndex, buffer, 0, bufferSize);
    25.  
    26.                 //send the chunk
    27.                 CmdReceiveBytes(transmissionId, buffer);
    28.                 dataToTransmit.curDataIndex += bufferSize;
    29.  
    30.                 yield return null;
    31.  
    32.                 if (null != OnDataFragmentSent)
    33.                     OnDataFragmentSent.Invoke(transmissionId, buffer);
    34.             }
    35.  
    36.             //transmission complete.
    37.             clientTransmissionIds.Remove(transmissionId);
    38.  
    39.             if (null != OnDataComepletelySent)
    40.                 OnDataComepletelySent.Invoke(transmissionId, dataToTransmit.data);
    41.         }
    42.  
    43.         [Command]
    44.         private void CmdPrepareToReceiveBytes(int transmissionId, int expectedSize)
    45.         {
    46.             if (serverTransmissionData.ContainsKey(transmissionId))
    47.                 return;
    48.  
    49.             //prepare data array which will be filled chunk by chunk by the received data
    50.             TransmissionData receivingData = new TransmissionData(new byte[expectedSize]);
    51.             serverTransmissionData.Add(transmissionId, receivingData);
    52.         }
    53.  
    54.         //use reliable sequenced channel to ensure bytes are sent in correct order
    55.         [Command]
    56.         private void CmdReceiveBytes(int transmissionId, byte[] recBuffer)
    57.         {
    58.             //already completely received or not prepared?
    59.             if (!serverTransmissionData.ContainsKey(transmissionId))
    60.                 return;
    61.  
    62.             //copy received data into prepared array and remember current dataposition
    63.             TransmissionData dataToReceive = serverTransmissionData[transmissionId];
    64.             System.Array.Copy(recBuffer, 0, dataToReceive.data, dataToReceive.curDataIndex, recBuffer.Length);
    65.             dataToReceive.curDataIndex += recBuffer.Length;
    66.  
    67.             if (null != OnDataFragmentReceived)
    68.                 OnDataFragmentReceived(transmissionId, recBuffer);
    69.  
    70.             if (dataToReceive.curDataIndex < dataToReceive.data.Length - 1)
    71.                 //current data not completely received
    72.                 return;
    73.  
    74.             //current data completely received
    75.             Debug.Log(LOG_PREFIX + "Completely Received Data at transmissionId=" + transmissionId);
    76.             serverTransmissionData.Remove(transmissionId);
    77.  
    78.             if (null != OnDataCompletelyReceived)
    79.                 OnDataCompletelyReceived.Invoke(transmissionId, dataToReceive.data);
    80.         }
    The entire project can be found here: https://github.com/kenmarold/HLAPI-Prototype