Ejemplo n.º 1
0
        public override async Task <Stream> AcceptAsync(CancellationToken token)
        {
            VerifyNotDisposed();

            using var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(token, _cancellation.Token);
            try
            {
                // Accept next client socket.
                var socket = await _socket.AcceptAsync(linkedSource.Token).ConfigureAwait(false);

                // Configure client socket based on transport type.
                OnAccept(socket);

                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
            catch (Exception)
            {
                // Keep server socket when getting any kind of cancel request.
                // Cancel happens when complete transport is about to disposed or caller
                // cancels out specific accept call, no need to recycle server socket.
                // In all other exception scenarios server socket will be re-created.
                if (!linkedSource.IsCancellationRequested)
                {
                    _socket = CreateNewSocketServer();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        internal override IpcSocket CreateNewSocketServer()
        {
            var socket = new IpcSocket(SocketType.Stream, ProtocolType.Tcp);

            if (_endPoint.DualMode)
            {
                socket.DualMode = _endPoint.DualMode;
            }
            socket.Bind(_endPoint);
            socket.Listen(_backlog);
            socket.LingerState.Enabled = false;
            OnCreateNewServer(socket.LocalEndPoint);
            return(socket);
        }