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 ProcessAccept(SocketAsyncEventArgs acceptEventArg)
        {
            try
            {
                WaitHandle.WaitAll(_resetEvent);
                _resetEvent[0].Set();
                Socket clientSocket = acceptEventArg.AcceptSocket;
                if (clientSocket == null)
                {
                    acceptEventArg.AcceptSocket = null;
                    StartAccept(acceptEventArg);
                    return;
                }
                SocketAsyncEventArgs receiveEventArgs = _readWritePool.Pop();
                receiveEventArgs.AcceptSocket = acceptEventArg.AcceptSocket;
                DataToken    token        = (DataToken)receiveEventArgs.UserToken;
                SocketObject socketObject = new SocketObject(Guid.NewGuid(), acceptEventArg.AcceptSocket);
                socketObject.Init();
                socketObject.sessionPool    = _sessionPool;
                socketObject.LastAccessTime = DateTime.Now;
                token.Socket = socketObject;

#if DEBUG
                Console.WriteLine("{0}>>{1} connect success", DateTime.Now.ToLongTimeString(), token.RemoteAddress);
#endif
                _sessionPool.Put(socketObject);
                _saeaProxy.StartReceive(receiveEventArgs);
                _acceptPool.Push(acceptEventArg);

                if (ConnectCompleted != null)
                {
                    ConnectCompleted.BeginInvoke(new SocketProcessEventArgs()
                    {
                        Socket = socketObject
                    }, null, null);
                }
            }
            catch (SocketException ex)
            {
                DataToken token = acceptEventArg.UserToken as DataToken;
                TraceLog.WriteError("Listener Error when processing data received from {0}:\r\n{1}", acceptEventArg.RemoteEndPoint, ex);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Listener error:{0}", ex.ToString());
            }

            // Accept the next connection request.
            acceptEventArg.AcceptSocket = null;
            StartAccept(acceptEventArg);
        }
Exemplo n.º 4
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.º 5
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.º 6
0
        private void StartReceive()
        {
            socketObject.Init();
            socketObject.LastAccessTime = DateTime.Now;
            if (ConnectCompleted != null)
            {
                ConnectCompleted.BeginInvoke(new SocketProcessEventArgs()
                {
                    Socket = socketObject
                }, null, null);
            }
            _saeaReceive = _readWritePool.Pop();
            _saeaReceive.AcceptSocket = socketObject.Connection;
            ((DataToken)_saeaReceive.UserToken).Socket = socketObject;

            _saeaSend = _readWritePool.Pop();
            _saeaSend.AcceptSocket = socketObject.Connection;
            ((DataToken)_saeaSend.UserToken).Socket = socketObject;

            _saeaProxy.StartReceive(_saeaReceive);
        }
Exemplo n.º 7
0
 private void Socket__ConnectCompleted(object sender, TcpCompletedEventArgs e)
 {
     ConnectCompleted?.Invoke(sender, e);
 }