示例#1
0
        /// <summary>
        /// Asynchronously creates a <see cref="SecureUdpConnection"/> with the given parent <see cref="TcpConnection"/>.
        /// </summary>
        /// <param name="tcpConnection">The <see cref="TcpConnection"/> via which to connect the <see cref="SecureUdpConnection"/>.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation with the promise of a tuple holding the created
        /// <see cref="SecureUdpConnection"/> and <see cref="ConnectionResult"/> on completion.
        /// </returns>
        /// <exception cref="ArgumentException">The given <see cref="TcpConnection"/> isn't connected.</exception>
        public static async Task <Tuple <UdpConnection, ConnectionResult> > CreateSecureUdpConnectionAsync(TcpConnection tcpConnection, RSAPair rsaPair)
        {
            UdpConnection           udpConnection     = null;
            ConnectionResult        connectionResult  = ConnectionResult.Connected;
            CancellationTokenSource cancellationToken = new CancellationTokenSource();

            cancellationToken.CancelAfter(CONNECTION_TIMEOUT);
            if (tcpConnection == null || !tcpConnection.IsAlive)
            {
                return(new Tuple <UdpConnection, ConnectionResult>(udpConnection, ConnectionResult.TCPConnectionNotAlive));
            }
            tcpConnection.EstablishUdpConnection((localEndPoint, RemoteEndPoint) => udpConnection = new SecureUdpConnection(new UdpClient(localEndPoint), RemoteEndPoint, rsaPair));
            while (udpConnection == null && !cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(25);
            }
            if (udpConnection == null && cancellationToken.IsCancellationRequested)
            {
                connectionResult = ConnectionResult.Timeout;
            }
            return(new Tuple <UdpConnection, ConnectionResult>(udpConnection, connectionResult));
        }
        /// <summary>
        /// Creates a new instance of a udp connection async.
        /// </summary>
        /// <param name="tcpConnection">The tcp connection to establish the udp connection.</param>
        /// <returns>Task&lt;UdpConnection&gt;.</returns>
        /// <exception cref="ArgumentException">TcpConnection is not connected to the endpoint.</exception>
        public static async Task <Tuple <UdpConnection, ConnectionResult> > CreateUdpConnectionAsync(TcpConnection tcpConnection)
        {
            UdpConnection           udpConnection     = null;
            ConnectionResult        connectionResult  = ConnectionResult.Connected;
            CancellationTokenSource cancellationToken = new CancellationTokenSource();

            cancellationToken.CancelAfter(CONNECTION_TIMEOUT);
            if (tcpConnection == null || !tcpConnection.IsAlive)
            {
                throw new ArgumentException("TcpConnection is not connected to the endpoint.");
            }
            tcpConnection.EstablishUdpConnection((localEndPoint, RemoteEndPoint) => udpConnection = new UdpConnection(new UdpClient(localEndPoint), RemoteEndPoint));
            while (udpConnection == null && !cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(25);
            }
            if (udpConnection == null && cancellationToken.IsCancellationRequested)
            {
                connectionResult = ConnectionResult.Timeout;
            }
            return(new Tuple <UdpConnection, ConnectionResult>(udpConnection, connectionResult));
        }
 /// <summary>
 /// Creates a new instance of a udp connection async.
 /// </summary>
 /// <param name="tcpConnection">The tcp connection to establish the udp connection.</param>
 /// <returns>Task&lt;UdpConnection&gt;.</returns>
 /// <exception cref="ArgumentException">TcpConnection is not connected to the endpoint.</exception>
 public static async Task<Tuple<UdpConnection, ConnectionResult>> CreateUdpConnectionAsync(TcpConnection tcpConnection)
 {
     UdpConnection udpConnection = null;
     ConnectionResult connectionResult = ConnectionResult.Connected;
     CancellationTokenSource cancellationToken = new CancellationTokenSource();
     cancellationToken.CancelAfter(CONNECTION_TIMEOUT);
     if (tcpConnection == null || !tcpConnection.IsAlive) throw new ArgumentException("TcpConnection is not connected to the endpoint.");
     tcpConnection.EstablishUdpConnection((localEndPoint, RemoteEndPoint) => udpConnection = new UdpConnection(new UdpClient(localEndPoint), RemoteEndPoint));
     while (udpConnection == null && !cancellationToken.IsCancellationRequested) await Task.Delay(25);
     if (udpConnection == null && cancellationToken.IsCancellationRequested) connectionResult = ConnectionResult.Timeout;
     return new Tuple<UdpConnection, ConnectionResult>(udpConnection, connectionResult);
 }