public IEnumerator SendBytesToClientsRoutine(int transmissionId, byte[] data)
    {
        Debug.Assert(!serverTransmissionIds.Contains(transmissionId));
        Debug.Log(LOG_PREFIX + "SendBytesToClients processId=" + transmissionId + " | datasize=" + data.Length);

        //tell client that he is going to receive some data and tell him how much it will be.
        RpcPrepareToReceiveBytes(transmissionId, data.Length);
        yield return(null);

        //begin transmission of data. send chunks of 'bufferSize' until completely transmitted.
        serverTransmissionIds.Add(transmissionId);
        TransmissionData dataToTransmit = new TransmissionData(data);
        int bufferSize = defaultBufferSize;

        while (dataToTransmit.curDataIndex < dataToTransmit.data.Length - 1)
        {
            //determine the remaining amount of bytes, still need to be sent.
            int remaining = dataToTransmit.data.Length - dataToTransmit.curDataIndex;
            if (remaining < bufferSize)
            {
                bufferSize = remaining;
            }

            //prepare the chunk of data which will be sent in this iteration
            byte[] buffer = new byte[bufferSize];
            System.Array.Copy(dataToTransmit.data, dataToTransmit.curDataIndex, buffer, 0, bufferSize);

            //send the chunk
            RpcReceiveBytes(transmissionId, buffer);
            dataToTransmit.curDataIndex += bufferSize;

            yield return(null);

            if (null != OnDataFragmentSent)
            {
                OnDataFragmentSent.Invoke(transmissionId, buffer);
            }
        }

        //transmission complete.
        serverTransmissionIds.Remove(transmissionId);

        if (null != OnDataComepletelySent)
        {
            OnDataComepletelySent.Invoke(transmissionId, dataToTransmit.data);
        }
    }
Exemplo n.º 2
0
        public IEnumerator <float> SendBytesToClientsRoutine(int transmissionId, byte[] data, Vector3 centralCarPos)
        {
            SendToPeer(SenderPeer).ClientPrepareToRecieveTransmission(transmissionId, data.Length, centralCarPos);
            yield return(0f);

            serverTransmissionIds.Add(transmissionId);
            DataController.TransmissionData dataToTransmit = new DataController.TransmissionData(data);
            int bufferSize = DataController.Instance.defaultBufferSize;

            while (dataToTransmit.curDataIndex < dataToTransmit.data.Length - 1)
            {
                int remaining = dataToTransmit.data.Length - dataToTransmit.curDataIndex;

                if (remaining < bufferSize)
                {
                    bufferSize = remaining;
                }

                byte[] buffer = new byte[bufferSize];
                System.Array.Copy(dataToTransmit.data, dataToTransmit.curDataIndex, buffer, 0, bufferSize);

                SendToPeer(SenderPeer).ClientRecieveTransmission(transmissionId, buffer);
                dataToTransmit.curDataIndex += bufferSize;

                yield return(0f);

                if (null != OnDataFragmentSent)
                {
                    OnDataFragmentSent.Invoke(transmissionId, buffer);
                }
            }

            // Transmission complete.
            serverTransmissionIds.Remove(transmissionId);

            if (null != OnDataComepletelySent)
            {
                OnDataComepletelySent.Invoke(transmissionId, dataToTransmit.data);
            }
        }