Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

WebRTC DataChannel asynchronous send/receive

Discussion in 'Unity Render Streaming' started by SpindizzyGames, Feb 16, 2023.

  1. SpindizzyGames

    SpindizzyGames

    Joined:
    Jun 29, 2017
    Posts:
    108
    I am in the process of substituting existing System.Net.Socket code with WebRTC code.

    System.Net.Socket has asynchronous Send and Receive functions:

    Can Unity's WebRTC DataChannel send and receive data asynchronously? If so, How would it be done? Can WebRTC's class AsyncOperationBase be used here?

    System.Net.Socket example:

    Socket.SendToAsync(args);
    Socket.ReceiveFromAsync(args);

    TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
    IPAddress address = Socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any;
    IPEndPoint remote = new IPEndPoint(address, 0);
    args.SetBuffer(buffer, 0, buffer.Length);
    args.RemoteEndPoint = remote;
    args.UserToken = tcs;

    // Read next packet to the buffer
    bool pending = Socket.ReceiveFromAsync(args);
    if (pending) {
    await tcs.Task;
    } else {
    tcs.SetCanceled();
    }
     
  2. kazuki_unity729

    kazuki_unity729

    Unity Technologies

    Joined:
    Aug 2, 2018
    Posts:
    803
    We haven't provided the asyncnous read/write feature for DataChannel as you expected. What amount of data are you using with DataChannel?