Inheritance: TransportBase
Esempio n. 1
0
        void Complete(SocketAsyncEventArgs e, bool completeSynchronously)
        {
            TransportBase transport = null;
            Exception exception = null;
            if (e.SocketError != SocketError.Success)
            {
                exception = new SocketException((int)e.SocketError);
                if (e.AcceptSocket != null)
                {
                    e.AcceptSocket.Dispose();
                }
            }
            else
            {
                try
                {
                    Fx.Assert(e.ConnectSocket != null, "Must have a valid socket accepted.");
                    e.ConnectSocket.NoDelay = true;
                    transport = new TcpTransport(e.ConnectSocket, this.transportSettings);
                    transport.Open();
                }
                catch (Exception exp)
                {
                    if (Fx.IsFatal(exp))
                    {
                        throw;
                    }

                    exception = exp;
                    if (transport != null)
                    {
                        transport.SafeClose();
                    }
                    transport = null;
                }
            }

            e.Dispose();
            this.callbackArgs.CompletedSynchronously = completeSynchronously;
            this.callbackArgs.Exception = exception;
            this.callbackArgs.Transport = transport;

            if (!completeSynchronously)
            {
                this.callbackArgs.CompletedCallback(this.callbackArgs);
            }
        }
        public override bool ConnectAsync(TimeSpan timeout, TransportAsyncCallbackArgs callbackArgs)
        {
            var streamSocket = new Windows.Networking.Sockets.StreamSocket();
            var addr = this.transportSettings.Host;

            this.callbackArgs = callbackArgs;

            var connectTask = streamSocket.ConnectAsync(new Windows.Networking.HostName(addr), this.transportSettings.Port.ToString(), Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket).AsTask();
            connectTask.ContinueWith(_ =>
            {
                TransportBase transport = null;
                Exception exception = null;

                try
                {
                    transport = new TcpTransport(streamSocket, this.transportSettings);
                    transport.Open();
                }
                catch (Exception exp)
                {
                    if (Fx.IsFatal(exp))
                    {
                        throw;
                    }

                    exception = exp;
                    if (transport != null)
                    {
                        transport.SafeClose();
                    }
                    transport = null;
                }

                var completeSynchronously = false;
                this.callbackArgs.CompletedSynchronously = completeSynchronously;
                this.callbackArgs.Exception = exception;
                this.callbackArgs.Transport = transport;

                if (!completeSynchronously)
                {
                    this.callbackArgs.CompletedCallback(this.callbackArgs);
                }
            });
            return true;
        }
Esempio n. 3
0
        bool HandleAcceptComplete(SocketAsyncEventArgs e, bool completedSynchronously)
        {
            if (e.SocketError == SocketError.Success)
            {
                TcpTransport transport = new TcpTransport(e.AcceptSocket, this.transportSettings);
                transport.Open();

                TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
                args.Transport = transport;
                args.CompletedSynchronously = completedSynchronously;
                this.OnTransportAccepted(args);
                return true;
            }
            else
            {
                bool shouldRetry = this.ShouldRetryAccept(e.SocketError);
                if (!shouldRetry)
                {
                    e.Dispose();
                    this.SafeClose(new SocketException((int)e.SocketError));
                }

                return shouldRetry;
            }
        }