1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Net.Sockets;
5
using System.Threading.Tasks;
6
using Libplanet.Stun;
7

8
namespace Libplanet.Net
9
{
10
    public class IceServer
11
    {
12
        public IceServer(
13
            IEnumerable<string> urls,
14
            string username = null,
15
            string credential = null)
16 1
            : this(urls.Select(u => new Uri(u)), username, credential)
17 1
        {
18 1
        }
19

20 1
        public IceServer(
21 1
            IEnumerable<Uri> urls,
22 1
            string username = null,
23 1
            string credential = null)
24 1
        {
25 1
            Urls = urls;
26 1
            Username = username;
27 1
            Credential = credential;
28 1
        }
29

30 1
        public IEnumerable<Uri> Urls { get; }
31

32 1
        public string Username { get; }
33

34 1
        public string Credential { get; }
35

36
        internal static async Task<TurnClient> CreateTurnClient(
37
            IEnumerable<IceServer> iceServers)
38 1
        {
39 1
            foreach (IceServer server in iceServers)
40 1
            {
41 1
                foreach (Uri url in server.Urls)
42 1
                {
43 1
                    if (url.Scheme != "turn")
44 1
                    {
45 1
                        throw new ArgumentException($"{url} isn't valid TURN url.");
46
                    }
47

48
                    try
49 1
                    {
50 1
                        int port = url.IsDefaultPort
51 1
                            ? TurnClient.TurnDefaultPort
52 1
                            : url.Port;
53 1
                        var turnClient = new TurnClient(
54 1
                            url.Host,
55 1
                            server.Username,
56 1
                            server.Credential,
57 1
                            port);
58

59
                        // Check connectability
60 1
                        await turnClient.GetMappedAddressAsync();
61

62 1
                        return turnClient;
63
                    }
64 0
                    catch (ArgumentException)
65 0
                    {
66 0
                    }
67 1
                    catch (SocketException)
68 1
                    {
69 1
                    }
70 1
                }
71 1
            }
72

73 1
            throw new IceServerException("Can't find suitable server.");
74 1
        }
75
    }
76
}

Read our documentation on viewing source code .

Loading