Exemplo n.º 1
0
 private static void OnData(WebRTCPeer peer, byte[] obj)
 {
     if (obj.Length < IntPtr.Size)
     {
         var pieceNum = BitConverter.ToInt32(obj, 0);
         //logger.LogDebug($"{Name}: data channel ({_dataChannel.label}:{_dataChannel.id}): {pieceNum}.");
         logger.LogDebug($"{peer._peerName}: Data channel receive: {pieceNum}, length {obj.Length}.");
     }
     else
     {
         var packet = BytesToStructure <Message>(obj);
         logger.LogDebug($"{peer._peerName}: Data channel receive: {packet.QueueName} Num: {packet.Num}.");
     }
 }
Exemplo n.º 2
0
        private static async Task RunCommand()
        {
            CancellationTokenSource exitCts = new CancellationTokenSource();

            AddConsoleLogger();

            var peerA = new WebRTCPeer("PeerA", "dcx");

            peerA.OnData = OnData;
            var peerB = new WebRTCPeer("PeerB", "dcy");

            peerB.OnData = OnData;

            // Exchange the SDP offer/answers. ICE Host candidates are included in the SDP.
            var offer = peerA.PeerConnection.createOffer(null);
            await peerA.PeerConnection.setLocalDescription(offer);

            if (peerB.PeerConnection.setRemoteDescription(offer) != SetDescriptionResultEnum.OK)
            {
                throw new ApplicationException("Couldn't set remote description.");
            }
            var answer = peerB.PeerConnection.createAnswer(null);
            await peerB.PeerConnection.setLocalDescription(answer);

            if (peerA.PeerConnection.setRemoteDescription(answer) != SetDescriptionResultEnum.OK)
            {
                throw new ApplicationException("Couldn't set remote description.");
            }

            // Wait for the peers to connect. Should take <1s if the peers are on the same host.
            while (peerA.PeerConnection.connectionState != RTCPeerConnectionState.connected &&
                   peerB.PeerConnection.connectionState != RTCPeerConnectionState.connected)
            {
                Console.WriteLine("Waiting for WebRTC peers to connect...");
                await Task.Delay(1000);
            }

            var taskList = new List <Task>();

            taskList.Add(Task.Run(async() =>
            {
                string sendLabel = "dcx";

                while (!peerA.IsDataChannelReady(sendLabel))
                {
                    Console.WriteLine($"Waiting 1s for data channel {sendLabel} to open.");
                    await Task.Delay(1000);
                }

                for (int i = 0; i < 100; i++)
                {
                    try
                    {
                        Console.WriteLine($"Data channel send {i} on {sendLabel}.");

                        var num = BitConverter.GetBytes(i);
                        await peerA.SendAsync(sendLabel, num).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ClientA:" + ex.ToString());
                    }
                }
            }));

            string[] queueNames = new string[] { "ThreadA", "ThreadB", "ThreadC" };

            foreach (var queueName in queueNames)
            {
                var name = queueName;
                taskList.Add(Task.Run(async() =>
                {
                    string sendLabel = "dcx";

                    while (!peerA.IsDataChannelReady(sendLabel))
                    {
                        Console.WriteLine($"Waiting 1s for data channel {sendLabel} {name} to open.");
                        await Task.Delay(1000);
                    }

                    for (int i = 0; i < 100; i++)
                    {
                        try
                        {
                            Console.WriteLine($"Data channel send {i} on {sendLabel} {name}.");

                            var packetNum       = new Message();
                            packetNum.Num       = i;
                            packetNum.QueueName = $"{peerA._peerName} {sendLabel} {name}";
                            packetNum.Data      = new byte[64000];
                            await peerA.SendAsync(sendLabel, packetNum.ToData()).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ClientA:" + ex.ToString());
                        }
                    }
                }));
            }

            taskList.Add(Task.Run(async() =>
            {
                string sendLabel = "dcx";

                while (!peerA.IsDataChannelReady(sendLabel))
                {
                    Console.WriteLine($"Waiting 1s for data channel {sendLabel} to open.");
                    await Task.Delay(1000);
                }

                for (int i = 100; i < 200; i++)
                {
                    try
                    {
                        Console.WriteLine($"Data channel send {i} on {sendLabel}.");

                        var num = BitConverter.GetBytes(i);
                        await peerA.SendAsync(sendLabel, num).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ClientA:" + ex.ToString());
                    }
                }
            }));

            await Task.WhenAll(taskList.ToArray());
        }