void OnAcceptInnerTransport(TransportAsyncCallbackArgs innerArgs)
        {
            Fx.Assert(innerArgs.Exception == null, "Should not be called with an exception.");
            Fx.Assert(innerArgs.Transport != null, "Should be called with a transport.");
            Utils.Trace(TraceLevel.Info, "{0}: Tls listener accepted an inner transport {1}", this, innerArgs.Transport);

            try
            {
                // upgrade transport
                innerArgs.Transport = new TlsTransport(innerArgs.Transport, this.transportSettings);
                IAsyncResult result = innerArgs.Transport.BeginOpen(
                    innerArgs.Transport.DefaultOpenTimeout, 
                    this.onTransportOpened,
                    innerArgs);
                if (result.CompletedSynchronously)
                {
                    this.HandleTransportOpened(result);
                    return;
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                innerArgs.Transport.TryClose(exception);
            }
        }
        void HandleInnerTransportConnected(TransportAsyncCallbackArgs innerArgs)
        {
            this.callbackArgs.CompletedSynchronously = innerArgs.CompletedSynchronously;
            if (innerArgs.Exception != null)
            {
                this.callbackArgs.Exception = innerArgs.Exception;
                this.Complete();
            }
            else
            {
                Fx.Assert(innerArgs.Transport != null, "must have a valid inner transport");
                // upgrade transport
                this.callbackArgs.Transport = new TlsTransport(innerArgs.Transport, this.transportSettings);
                try
                {
                    IAsyncResult result = this.callbackArgs.Transport.BeginOpen(this.timeoutHelper.RemainingTime(), onTransportOpened, this);
                    if (result.CompletedSynchronously)
                    {
                        this.HandleTransportOpened(result);
                        this.Complete();
                    }
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }

                    this.callbackArgs.Exception = exception;
                    this.Complete();
                }
            }
        }
Example #3
0
 protected void OnTransportAccepted(TransportAsyncCallbackArgs args)
 {
     if (args.CompletedSynchronously)
     {
         ActionItem.Schedule(this.notifyAccept, args);
     }
     else
     {
         this.NotifyAccept(args);
     }
 }
Example #4
0
 public sealed override bool WriteAsync(TransportAsyncCallbackArgs args)
 {
     IAsyncResult result = this.sslStream.BeginWrite(args.Buffer, args.Offset, args.Count, this.onWriteComplete, args);
     if (result.CompletedSynchronously)
     {
         this.HandleWriteComplete(result);
         return false;
     }
     else
     {
         return true;
     }
 }
Example #5
0
 public sealed override bool ReadAsync(TransportAsyncCallbackArgs args)
 {
     Fx.Assert(args.Buffer != null, "must have buffer to read");
     IAsyncResult result = this.sslStream.BeginRead(args.Buffer, args.Offset, args.Count, this.onReadComplete, args);
     if (result.CompletedSynchronously)
     {
         this.HandleReadComplete(result);
         return false;
     }
     else
     {
         return true;
     }
 }
        void OnHandleTransportComplete(TransportAsyncCallbackArgs args)
        {
            args.SetBuffer(null, 0, 0);
            args.CompletedCallback = null;

            if (args.Exception != null)
            {
                args.Transport.TryClose(args.Exception);
            }
            else
            {
                this.OnTransportAccepted(args);
            }
        }
Example #7
0
        public sealed override bool ReadAsync(TransportAsyncCallbackArgs args)
        {
            Fx.Assert(args.Buffer != null, "must have buffer(s) to read");
            Fx.Assert(args.CompletedCallback != null, "must have a valid callback");

            this.receiveEventArgs.SetBuffer(args.Buffer, args.Offset, args.Count);
            this.receiveEventArgs.UserToken = args;
            if (!this.socket.ReceiveAsync(this.receiveEventArgs))
            {
                this.OperationComplete(this.receiveEventArgs, true);
                return false;
            }

            return true;
        }
Example #8
0
        public void WriteFrame(Performative command, bool needReply)
        {
            try
            {
                Frame frame = new Frame(FrameType.Sasl, 0, command);

                TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
                args.SetBuffer(frame.Buffer);
                args.CompletedCallback = this.onWriteFrameComplete;
                args.UserToken = needReply;
                this.writer.WriteBuffer(args);
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
        public override bool ConnectAsync(TimeSpan timeout, TransportAsyncCallbackArgs callbackArgs)
        {
            Utils.Trace(TraceLevel.Info, "{0}: Tls initiator start connecting...", this);
            this.callbackArgs = callbackArgs;
            this.timeoutHelper = new TimeoutHelper(timeout);
            TransportInitiator innerInitiator = this.transportSettings.InnerTransportSettings.CreateInitiator();

            TransportAsyncCallbackArgs innerArgs = new TransportAsyncCallbackArgs();
            innerArgs.CompletedCallback = OnInnerTransportConnected;
            innerArgs.UserToken = this;
            if (innerInitiator.ConnectAsync(timeout, innerArgs))
            {
                // pending
                return true;
            }
            else
            {
                this.HandleInnerTransportConnected(innerArgs);
                return !this.callbackArgs.CompletedSynchronously;
            }
        }
        public override bool ConnectAsync(TimeSpan timeout, TransportAsyncCallbackArgs callbackArgs)
        {
            // TODO: set socket connect timeout to timeout
            this.callbackArgs = callbackArgs;

            // TODO: IPv6 support for DnsEndPoint
            EndPoint endPoint = this.transportSettings.EndPoint;
            AddressFamily addressFamily = endPoint is DnsEndPoint ? AddressFamily.InterNetwork : endPoint.AddressFamily;
            Socket socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
            SocketAsyncEventArgs connectEventArgs = new SocketAsyncEventArgs();
            connectEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnectComplete);
            connectEventArgs.AcceptSocket = socket;
            connectEventArgs.RemoteEndPoint = endPoint;
            connectEventArgs.UserToken = this;
            if (socket.ConnectAsync(connectEventArgs))
            {
                return true;
            }
            else
            {
                this.Complete(connectEventArgs, true);
                return false;
            }
        }
Example #11
0
 void OnWriteFrameComplete(TransportAsyncCallbackArgs args)
 {
     if (args.Exception != null)
     {
         this.HandleException(args.Exception);
     }
     else
     {
         bool readFrame = (bool)args.UserToken;
         if (readFrame)
         {
             this.ReadFrame();
         }
     }
 }
Example #12
0
        void NotifyAccept(object state)
        {
            TransportAsyncCallbackArgs args = (TransportAsyncCallbackArgs)state;

            this.acceptCallback(args);
        }
Example #13
0
        void OnAcceptTransport(TransportAsyncCallbackArgs args)
        {
            Fx.Assert(args.Exception == null, "Should not be failed.");
            Fx.Assert(args.Transport != null, "Should have a transport");

            AmqpConnectionSettings settings = this.connectionSettings.Clone();
            settings.OnOpenCallback = this.OnReceiveConnectionOpen;
            AmqpConnection connection = null;
            try
            {
                connection = this.CreateConnection(
                    args.Transport, 
                    (ProtocolHeader)args.UserToken, 
                    false, 
                    this.amqpSettings.Clone(), 
                    settings);
                connection.BeginOpen(connection.DefaultOpenTimeout, this.onConnectionOpenComplete, connection);
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }

                if (connection != null)
                {
                    connection.TryClose(ExceptionHelper.ToAmqpException(ex));
                }
            }
        }
Example #14
0
 public abstract bool WriteAsync(TransportAsyncCallbackArgs args);
 static void OnInnerTransportConnected(TransportAsyncCallbackArgs innerArgs)
 {
     TlsTransportInitiator thisPtr = (TlsTransportInitiator)innerArgs.UserToken;
     thisPtr.HandleInnerTransportConnected(innerArgs);
 }
Example #16
0
 static void OnTransport(TransportAsyncCallbackArgs args)
 {
     OpenContainerAsyncResult thisPtr = (OpenContainerAsyncResult)args.UserToken;
     AmqpSettings settings = new AmqpSettings();
     TransportProvider provider = new AmqpTransportProvider();
     provider.Versions.Add(new AmqpVersion(1, 0, 0));
     settings.TransportProviders.Add(provider);
     thisPtr.connection = new AmqpConnection(args.Transport, settings, new AmqpConnectionSettings() { ContainerId = thisPtr.parent.id });
     thisPtr.connection.BeginOpen(TimeSpan.MaxValue, thisPtr.PrepareAsyncCompletion(onConnectionOpen), thisPtr);
 }
Example #17
0
            bool HandleWriteComplete(TransportAsyncCallbackArgs args)
            {
                bool done = true;
                Exception exception = null;
                if (args.Exception != null)
                {
                    exception = args.Exception;
                }
                else if (args.BytesTransfered == 0)
                {
                    exception = new ObjectDisposedException(this.transport.ToString());
                }
                else if (args.BytesTransfered < args.Count)
                {
                    args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered);
                    done = false;
                }

                TransportAsyncCallbackArgs innerArgs = (TransportAsyncCallbackArgs)args.UserToken;
                if (done && innerArgs.CompletedCallback != null)
                {
                    innerArgs.Exception = exception;
                    innerArgs.BytesTransfered = innerArgs.Count;
                    innerArgs.CompletedCallback(innerArgs);
                }

                return done;
            }
Example #18
0
 void OnWriteComplete(TransportAsyncCallbackArgs args)
 {
     if (!this.HandleWriteComplete(args) && !args.CompletedSynchronously)
     {
         this.Write(args);
     }
 }
Example #19
0
            void OnAcceptTransport(TransportAsyncCallbackArgs args)
            {
                this.listener.Close();

                if (args.Exception != null)
                {
                    this.Complete(false, args.Exception);
                    return;
                }

                this.OnTransport(args.Transport);
            }
Example #20
0
            void OnEstablishTransport(TransportAsyncCallbackArgs args)
            {
                if (args.Exception != null)
                {
                    this.Complete(false, args.Exception);
                    return;
                }

                this.OnTransport(args.Transport);
            }
Example #21
0
 protected override void Start(TransportSettings transportSettings)
 {
     this.initiator = transportSettings.CreateInitiator();
     TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
     args.CompletedCallback = this.OnEstablishTransport;
     if (!initiator.ConnectAsync(this.TimeoutHelper.RemainingTime(), args))
     {
         this.OnEstablishTransport(args);
     }
 }
Example #22
0
 public abstract bool ReadAsync(TransportAsyncCallbackArgs args);
Example #23
0
            public OpenContainerAsyncResult(Container parent, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.parent = parent;

                int port = this.parent.addressUri.Port;
                if (port == -1)
                {
                    port = AmqpConstants.DefaultPort;
                }

                TcpTransportSettings tcpSettings = new TcpTransportSettings();
                tcpSettings.TcpBacklog = 20;
                tcpSettings.TcpBufferSize = 4096;
                tcpSettings.SetEndPoint(this.parent.addressUri.Host, port, false);
                TransportSettings transportSettings = tcpSettings;

                TransportInitiator initiator = transportSettings.CreateInitiator();
                TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
                args.CompletedCallback = onTransport;
                args.UserToken = this;
                if (!initiator.ConnectAsync(TimeSpan.MaxValue, args))
                {
                    OnTransport(args);
                }
            }
Example #24
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
            {
                e.Dispose();
                this.TryClose(new SocketException((int)e.SocketError));
                return false;
            }
        }
Example #25
0
            void OnAcceptTransport(TransportAsyncCallbackArgs args)
            {
                if (args.Exception != null)
                {
                    return;
                }

                AmqpConnection connection = new AmqpConnection(args.Transport, this.settings, new AmqpConnectionSettings() { ContainerId = this.container.id });
                connection.Closed += new EventHandler(connection_Closed);
                lock (this.syncRoot)
                {
                    this.connections.Add(connection);
                }

                connection.Open();
            }
Example #26
0
            void Write(TransportAsyncCallbackArgs args)
            {
                try
                {
                    while (true)
                    {
                        if (this.transport.WriteAsync(args))
                        {
                            break;
                        }

                        if (this.HandleWriteComplete(args))
                        {
                            break;
                        }
                    }
                }
                catch(Exception exception)
                {
                    args.Exception = exception;
                    this.HandleWriteComplete(args);
                }
            }
Example #27
0
 public WriteAsyncResult(TransportBase transport)
 {
     this.transport = transport;
     this.args = new TransportAsyncCallbackArgs();
     this.args.CompletedCallback = OnWriteComplete;
     this.args.UserToken = this;
 }
Example #28
0
 static void OnWriteComplete(TransportAsyncCallbackArgs args)
 {
     WriteAsyncResult thisPtr = (WriteAsyncResult)args.UserToken;
     thisPtr.completedSynchronously = args.CompletedSynchronously;
     thisPtr.Complete();
 }
Example #29
0
 public ReadAsyncResult(TransportBase transport)
 {
     this.transport = transport;
     this.readBuffer = new byte[AmqpConstants.AsyncBufferSize];
     this.readArgs = new TransportAsyncCallbackArgs();
     this.readArgs.SetBuffer(this.readBuffer, 0, this.readBuffer.Length);
     this.readArgs.CompletedCallback = OnReadComplete;
     this.readArgs.UserToken = this;
 }
Example #30
0
            public AsyncReader(
                AsyncIO parent,
                int readBufferSize,
                Action<ByteBuffer> receiveBufferHandler)
            {
                this.parent = parent;
                this.receiveBufferHandler = receiveBufferHandler;

                this.readAsyncEventArgs = new TransportAsyncCallbackArgs();
                this.readAsyncEventArgs.CompletedCallback = this.OnBufferReadComplete;
                this.readBuffer = ByteBuffer.Wrap(new byte[readBufferSize]);
            }
Example #31
0
 static void OnReadComplete(TransportAsyncCallbackArgs args)
 {
     ReadAsyncResult thisPtr = (ReadAsyncResult)args.UserToken;
     thisPtr.bufferOffset = 0;
     thisPtr.bufferEnd = args.BytesTransfered;
     thisPtr.completedSynchronously = args.CompletedSynchronously;
     thisPtr.Complete();
 }
Example #32
0
 private void HandleReadComplete(TransportAsyncCallbackArgs args, bool fromCache, bool completedSynchronously)
 {
     if (this.receiveEventArgs.SocketError != SocketError.Success)
     {
         args.Exception = new SocketException((int)this.receiveEventArgs.SocketError);
     }
     else
     {
         try
         {
             int num   = (fromCache ? this.receiveEventArgs.ReadBuffer.Length : this.receiveEventArgs.BytesTransferred);
             int count = num;
             if (num > 0)
             {
                 if (!this.receiveEventArgs.IsSegment)
                 {
                     ByteBuffer readBuffer = this.receiveEventArgs.ReadBuffer;
                     if (readBuffer != null)
                     {
                         if (!fromCache)
                         {
                             readBuffer.Append(num);
                         }
                         if (num > args.Count)
                         {
                             Buffer.BlockCopy(readBuffer.Buffer, readBuffer.Offset, args.Buffer, args.Offset, args.Count);
                             count = args.Count;
                             readBuffer.Complete(args.Count);
                         }
                         else
                         {
                             Buffer.BlockCopy(readBuffer.Buffer, readBuffer.Offset, args.Buffer, args.Offset, num);
                             readBuffer.Reset();
                         }
                     }
                     else
                     {
                         count = 0;
                     }
                 }
                 else
                 {
                     Buffer.BlockCopy(this.receiveEventArgs.Buffer, this.receiveEventArgs.Offset, args.Buffer, args.Offset, num);
                     ArraySegment <byte> nums = new ArraySegment <byte>(this.receiveEventArgs.Buffer, this.receiveEventArgs.Offset, this.receiveEventArgs.Count);
                     TcpTransport.SmallBufferPool.ReturnBuffer(nums);
                 }
             }
             args.BytesTransfered = count;
             args.Exception       = null;
         }
         catch (Exception exception1)
         {
             Exception exception = exception1;
             if (Fx.IsFatal(exception))
             {
                 throw;
             }
             args.Exception = exception;
         }
     }
     args.CompletedSynchronously = completedSynchronously;
     try
     {
         this.receiveEventArgs.Reset();
     }
     catch (ObjectDisposedException objectDisposedException)
     {
         args.Exception = objectDisposedException;
     }
     if (!completedSynchronously)
     {
         args.CompletedCallback(args);
     }
 }