Example #1
0
        /// <summary>
        /// Initiates the connection to the app
        /// </summary>
        /// <returns>An async Task</returns>
        /// <exception cref="Exception">Thrown if the connection is refused</exception>
        public async Task Connect()
        {
            if (SocketId != null)
            {
                return;
            }
            ConnectCompletionSource = new TaskCompletionSource <bool>();
            uint nonce = Packet.WatchNonce(async pk =>
            {
                await Task.Run(() =>
                {
                    TcpCSSocketControl control = new TcpCSSocketControl(pk);
                    SocketId = control.SocketId;
                    EP.Connections.Add(IdToString(control.SocketId), this);
                    Requestor = true;
                    Stream    = new NRStream(this);
                    if (control.CheckFlag(TcpCSSocketControl.Close))
                    {
                        ConnectCompletionSource.SetResult(false);
                    }
                });
            });

            EP.TransmitRaw(new TcpCOpenSocket(InstanceId, nonce));
            bool success = await ConnectCompletionSource.Task;

            if (!success)
            {
                await Close(false);

                throw new Exception("Connect to distant end was refused.");
            }
        }
Example #2
0
 /// <summary>
 /// Accepts the connection
 /// </summary>
 public async Task Accept()
 {
     await Task.Run(() =>
     {
         if ((Requestor && !Loopback) || Open)
         {
             return;
         }
         Stream = new NRStream(this);
         TcpCSSocketControl control = new TcpCSSocketControl(SocketId, TcpCSSocketControl.AcceptConnection);
         EP.TCPConnection.Stream.Write(control.Build());
         Open = true;
     });
 }