Exemplo n.º 1
0
        private void ConnectToServerFinished(IAsyncResult ar)
        {
            var    result  = (AsyncResult)ar;
            var    connect = (Func <string, string>)result.AsyncDelegate;
            string error   = connect.EndInvoke(ar);

            ConnectCompleted?.Invoke(error, result.AsyncState.ToString());
        }
Exemplo n.º 2
0
 public void ConnectToServerAsync(string url)
 {
     if (url == "https://" + Utils.ValidServer)
     {
         ConnectCompleted?.Invoke(null, url);
         ConnectedServer = url;
     }
     else
     {
         ConnectCompleted?.Invoke("Couldn't connect to server", url);
     }
 }
Exemplo n.º 3
0
        private void TryConnect(IAsyncResult ar)
        {
            try
            {
                Socket.EndConnect(ar);
                ConnectCompleted?.Invoke(this, new TcpCompletedEventArgs(ar.AsyncState));
            }
            catch (Exception ex)
            {
                var eventArgs = new TcpCompletedEventArgs(ex);
                eventArgs.Error = true;

                ConnectCompleted?.Invoke(this, eventArgs);
            }
        }
Exemplo n.º 4
0
        private void EndConnect(IAsyncResult result)
        {
            SocketAsyncState state = (SocketAsyncState)result.AsyncState;

            try
            {
                Socket.EndConnect(result);
            }
            catch (Exception ex)
            {
                //出现异常,连接失败。
                state.Completed = true;
                //判断是否为异步,异步则引发事件
                if (state.IsAsync)
                {
                    ConnectFailed?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Connect, ex.Message));
                }
                return;
            }

            //连接成功。
            //创建Socket网络流
            Stream = new NetworkStream(Socket);
            if (IsUseAuthenticate)
            {
                NegotiateStream negotiate = new NegotiateStream(Stream);
                negotiate.AuthenticateAsClient();
                while (!negotiate.IsMutuallyAuthenticated)
                {
                    Thread.Sleep(10);
                }
            }
            //连接完成
            state.Completed = true;
            if (state.IsAsync)
            {
                ConnectCompleted?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Connect));
            }

            //开始接收数据
            Handler.BeginReceive(Stream, EndReceive, state);
        }
Exemplo n.º 5
0
 private void Socket__ConnectCompleted(object sender, TcpCompletedEventArgs e)
 {
     ConnectCompleted?.Invoke(sender, e);
 }