예제 #1
0
        /// <summary>
        /// The HandleConnect method is invoked by an asynchronous
        /// connection attempt to inform the connector that it has
        /// been completed. The connector will take the appropriate
        /// action depending on the resulting state of the request.
        /// </summary>
        /// <param name="ar">
        /// The .NET asynchronous result details.
        /// </param>
        private void HandleConnect(IAsyncResult ar)
        {
            // REC: Retrieve the original IPC state information
            // that was provided to the asynchronous operation:
            IpcState ipcState = ar.AsyncState as IpcState;

            if (ipcState != null)
            {
                try
                {
                    ipcState.IpcSocket.EndConnect(ar);
                    // REC: Dispatch the connect success event to the
                    // subscribers so that they can determine whether
                    // or not the connection should go forward.
                    EventHandler <VfxTcpConnectorEventArgs> tmpDispatch = EventDispatch;
                    if (tmpDispatch != null)
                    {
                        VfxTcpConnectorEventArgs connectArgs = new VfxTcpConnectorEventArgs();
                        connectArgs.EventType     = VfxTcpConnectorEventTypes.Event_Connect_Success;
                        connectArgs.EventSocket   = ipcState.IpcSocket;
                        connectArgs.EventEndpoint = ipcState.IpcEndpoint;
                        tmpDispatch(this, connectArgs);

                        // REC: Check the cancellation flag to determine if
                        // any subscriber wants the connection canceled:
                        if (connectArgs.EventCancel == true)
                        {
                            ipcState.IpcSocket.Close();
                            return;
                        }
                    }


                    // REC: Create a new instance of VfxTcpModule that
                    // will be used for processing IO over the socket:
                    VfxTcpModule tcpModule = new VfxTcpModule();
                    tcpModule.Init(ipcState.IpcSocket, this.RxBuffering);

                    // REC: Pass the initialized module to the instance
                    // of the VFX IPC session that is associated with the
                    // connection request:
                    ipcState.IpcSession.Init(tcpModule);
                }
                catch (System.Exception x)
                {
                    EventHandler <VfxTcpConnectorEventArgs> tmpDispatch = EventDispatch;
                    if (tmpDispatch != null)
                    {
                        VfxTcpConnectorEventArgs tmpArgs = new VfxTcpConnectorEventArgs();
                        tmpArgs.EventType      = VfxTcpConnectorEventTypes.Event_Connect_Failure;
                        tmpArgs.EventEndpoint  = ipcState.IpcEndpoint;
                        tmpArgs.EventException = x;
                        tmpDispatch(this, tmpArgs);
                    }
                }
            }
        }
예제 #2
0
        private void CompleteAccept(IAsyncResult ar)
        {
            IoContext context = ar.AsyncState as IoContext;

            if (context != null)
            {
                try
                {
                    Socket peer = context.IpcSocket.EndAccept(ar);

                    // REC: Locate the factory associated with this endpoint
                    // and create the appropriate type of session instance:
                    if (_mapFactories.ContainsKey(context.IpcEndPoint))
                    {
                        IPEndPoint peerEP = peer.RemoteEndPoint as IPEndPoint;
                        if (ValidatePeer(peerEP))
                        {
                            VfxTcpModule tcpModule = new VfxTcpModule();
                            tcpModule.Init(peer, this.RxBuffering);

                            // REC: Create a new instance of the session handler that
                            // is configured for the endpoint and associated the
                            // client IO stream with it:
                            IVfxIpcSession session = _mapFactories[context.IpcEndPoint].CreateSession();
                            session.Init(tcpModule);
                        }
                        else
                        {
                            peer.Close();
                        }
                    }

                    // REC: Initiate another asynchronous connection:
                    context.IpcSocket.BeginAccept(CompleteAccept, context);
                }
                catch (System.ObjectDisposedException)
                {
                    // REC: This exception gets thrown when an asynchronous
                    // accept fails due to the socket it was initiated from
                    // being closed. This is part of the shutdown procedure
                    // and should just be caught and ignored.
                }
            }
        }