当前位置: 首页>>代码示例>>C#>>正文


C# SafePipeHandle.Dispose方法代码示例

本文整理汇总了C#中Microsoft.Win32.SafeHandles.SafePipeHandle.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# SafePipeHandle.Dispose方法的具体用法?C# SafePipeHandle.Dispose怎么用?C# SafePipeHandle.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Win32.SafeHandles.SafePipeHandle的用法示例。


在下文中一共展示了SafePipeHandle.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WaitForConnection

        public void WaitForConnection()
        {
            CheckConnectOperationsServer();
            if (State == PipeState.Connected)
            {
                throw new InvalidOperationException(SR.InvalidOperation_PipeAlreadyConnected);
            }

            // Binding to an existing path fails, so we need to remove anything left over at this location.
            // There's of course a race condition here, where it could be recreated by someone else between this
            // deletion and the bind below, in which case we'll simply let the bind fail and throw.
            Interop.Sys.Unlink(_path); // ignore any failures
            var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
            try
            {
                socket.Bind(new UnixDomainSocketEndPoint(_path));
                socket.Listen(1);

                Socket acceptedSocket = socket.Accept();
                SafePipeHandle serverHandle = new SafePipeHandle(acceptedSocket);
                try
                {
                    ConfigureSocket(acceptedSocket, serverHandle, _direction, _inBufferSize, _outBufferSize, _inheritability);
                }
                catch
                {
                    serverHandle.Dispose();
                    acceptedSocket.Dispose();
                    throw;
                }
                
                InitializeHandle(serverHandle, isExposed: false, isAsync: (_options & PipeOptions.Asynchronous) != 0);
                State = PipeState.Connected;
            }
            finally
            {
                // Bind will have created a file.  Now that the client is connected, it's no longer necessary, so get rid of it.
                Interop.Sys.Unlink(_path); // ignore any failures; worst case is we leave a tmp file

                // Clean up the listening socket
                socket.Dispose();
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:43,代码来源:NamedPipeServerStream.Unix.cs

示例2: TryConnect

        private bool TryConnect(int timeout, CancellationToken cancellationToken)
        {
            // timeout and cancellationToken aren't used as Connect will be very fast,
            // either succeeding immediately if the server is listening or failing
            // immediately if it isn't.  The only delay will be between the time the server
            // has called Bind and Listen, with the latter immediately following the former.
            var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
            SafePipeHandle clientHandle = null;
            try
            {
                socket.Connect(new UnixDomainSocketEndPoint(_normalizedPipePath));
                clientHandle = new SafePipeHandle(socket);
                ConfigureSocket(socket, clientHandle, _direction, 0, 0, _inheritability);
            }
            catch (SocketException e)
            {
                if (clientHandle != null)
                {
                    clientHandle.Dispose();
                }
                socket.Dispose();

                switch (e.SocketErrorCode)
                {
                    // Retryable errors
                    case SocketError.AddressAlreadyInUse:
                    case SocketError.AddressNotAvailable:
                    case SocketError.ConnectionRefused:
                        return false;

                    // Non-retryable errors
                    default:
                        throw;
                }
            }

            InitializeHandle(clientHandle, isExposed: false, isAsync: (_pipeOptions & PipeOptions.Asynchronous) != 0);
            State = PipeState.Connected;
            return true;
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:40,代码来源:NamedPipeClientStream.Unix.cs


注:本文中的Microsoft.Win32.SafeHandles.SafePipeHandle.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。