Exemplo n.º 1
0
 void ExitOnPipe(GameObject player)
 {
     if (!myAudio.isPlaying && playTimes == 0)
     {
         myAudio.Play();
         playTimes++;
     }
     if (player && Mathf.Abs(player.transform.position.y - PositionA.position.y) > 0.1f)
     {
         player.transform.position += Vector3.up * Time.deltaTime;
         player.GetComponentInChildren <CircleCollider2D>().isTrigger = true;
         player.GetComponent <Player>().bInThePipe = true;
     }
     else
     {
         myState = PipeState.Idle;
         player.GetComponent <Player>().bInThePipe           = false;
         player.GetComponent <Player>().bStopInput           = false;
         player.GetComponent <CapsuleCollider2D>().isTrigger = false;
         player.GetComponent <Rigidbody2D>().gravityScale    = 3.0f;
         player.GetComponent <SpriteRenderer>().sortingOrder = 0;
         playTimes = 0;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NamedPipeClient" /> class.
        /// </summary>
        /// <param name="description">The client description.</param>
        /// <param name="server">The server.</param>
        /// <param name="onReceive">The action to call on receipt of a message.</param>
        /// <param name="token">The token.</param>
        private NamedPipeClient(
            [NotNull] string description,
            [NotNull] NamedPipeServerInfo server,
            [NotNull] Action <Message> onReceive,
            CancellationToken token = default(CancellationToken))
        {
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (onReceive == null)
            {
                throw new ArgumentNullException("onReceive");
            }

            _server = server;

            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken disposeToken = _cancellationTokenSource.Token;

            _clientTask = Task.Run(
                async() =>
            {
                try
                {
                    using (ITokenSource tokenSource = token.CreateLinked(disposeToken))
                        using (
                            OverlappingPipeClientStream stream = new OverlappingPipeClientStream(
                                _server.Host,
                                _server.FullName,
                                PipeTransmissionMode.Message))
                        {
                            _state = PipeState.Open;

                            token = tokenSource.Token;

                            // We need to support cancelling the connect.
                            await stream.Connect(token).ConfigureAwait(false);

                            ConnectResponse connectResponse       = null;
                            DisconnectResponse disconnectResponse = null;

                            if (!token.IsCancellationRequested)
                            {
                                // Set the stream.
                                _stream = stream;
                                _state  = PipeState.AwaitingConnect;

                                // Kick off a connect request, but don't wait for it's result as we're the task that will receive it!
                                ConnectRequest connectRequest = new ConnectRequest(description);
                                await stream.WriteAsync(connectRequest.Serialize(), token).ConfigureAwait(false);

                                // Keep going as long as we're connected.
                                try
                                {
                                    while (stream.IsConnected &&
                                           !disposeToken.IsCancellationRequested)
                                    {
                                        // Read data in.
                                        byte[] data = await stream.ReadAsync(disposeToken).ConfigureAwait(false);

                                        if (data == null)
                                        {
                                            break;
                                        }

                                        // Deserialize the incoming message.
                                        Message message = Message.Deserialize(data);

                                        if (connectResponse == null)
                                        {
                                            // We require a connect response to start
                                            connectResponse = message as ConnectResponse;
                                            if (connectResponse == null ||
                                                connectResponse.ID != connectRequest.ID)
                                            {
                                                break;
                                            }

                                            _state       = PipeState.Connected;
                                            _serviceName = connectResponse.ServiceName;

                                            Log.Add(
                                                LoggingLevel.Notification,
                                                () => ClientResources.Not_NamedPipeClient_Connection,
                                                connectResponse.ServiceName);

                                            TaskCompletionSource <NamedPipeClient> ccs =
                                                Interlocked.Exchange(ref _connectionCompletionSource, null);
                                            if (ccs != null)
                                            {
                                                ccs.TrySetResult(this);
                                            }

                                            // Observer the message.
                                            onReceive(message);
                                            continue;
                                        }

                                        // Check for disconnect, we don't observe the message until the disconnect is complete.
                                        disconnectResponse = message as DisconnectResponse;
                                        if (disconnectResponse != null)
                                        {
                                            break;
                                        }

                                        // Observe the message.
                                        onReceive(message);

                                        Response response = message as Response;
                                        if (response == null)
                                        {
                                            continue;
                                        }

                                        ConnectedCommand connectedCommand;

                                        // Check for cancellation responses.
                                        CommandCancelResponse cancelResponse = response as CommandCancelResponse;
                                        if (cancelResponse != null)
                                        {
                                            // Cancel the associated request
                                            if (_commandRequests.TryGetValue(
                                                    cancelResponse.CancelledCommandId,
                                                    out connectedCommand))
                                            {
                                                // ReSharper disable once PossibleNullReferenceException
                                                connectedCommand.Cancel(cancelResponse);
                                            }
                                        }

                                        // And fall through to complete the response...

                                        // Find command the response is related to, and notify it of the response.
                                        if (!_commandRequests.TryGetValue(response.ID, out connectedCommand))
                                        {
                                            continue;
                                        }

                                        Debug.Assert(connectedCommand != null);
                                        if (connectedCommand.Received(response))
                                        {
                                            _commandRequests.TryRemove(response.ID, out connectedCommand);
                                        }
                                    }
                                }
                                catch (TaskCanceledException)
                                {
                                }
                            }

                            // If we're still connected, and we haven't received a disconnect response, try to send a disconnect request.
                            if (stream.IsConnected &&
                                disconnectResponse == null)
                            {
                                CancellationTokenSource cts = token.IsCancellationRequested
                                    ? new CancellationTokenSource(500)
                                    : null;
                                try
                                {
                                    CancellationToken t = cts != null
                                        ? cts.Token
                                        : token;

                                    // Try to send disconnect request.
                                    // ReSharper disable once PossibleNullReferenceException
                                    await Send(new DisconnectRequest(), t)
                                    .ToTask(t)
                                    .ConfigureAwait(false);
                                }
                                catch (TaskCanceledException)
                                {
                                }
                                finally
                                {
                                    if (cts != null)
                                    {
                                        cts.Dispose();
                                    }
                                }
                            }

                            // Remove the stream.
                            _stream      = null;
                            _state       = PipeState.Closed;
                            _serviceName = null;

                            // If we had a disconnect message observe it now that the disconnect has been actioned,
                            // this prevents the receiver thinking the connection is still active.
                            if (disconnectResponse != null)
                            {
                                onReceive(disconnectResponse);
                                ConnectedCommand connectedCommand;
                                if (_commandRequests.TryGetValue(disconnectResponse.ID, out connectedCommand))
                                {
                                    Debug.Assert(connectedCommand != null);
                                    if (connectedCommand.Received(disconnectResponse))
                                    {
                                        _commandRequests.TryRemove(disconnectResponse.ID, out connectedCommand);
                                    }
                                }
                            }
                        }
                }
                catch (IOException ioe)
                {
                    if (!token.IsCancellationRequested)
                    {
                        // Common exception caused by sudden disconnect, lower level
                        Log.Add(
                            ioe,
                            LoggingLevel.Information,
                            () => ClientResources.Err_NamedPipeClient_Failed);
                    }
                }
                catch (Exception exception)
                {
                    TaskCanceledException tce = exception as TaskCanceledException;
                    TaskCompletionSource <NamedPipeClient> ccs = Interlocked.Exchange(
                        ref _connectionCompletionSource,
                        null);
                    if (ccs != null)
                    {
                        if (tce != null)
                        {
                            ccs.TrySetCanceled();
                        }
                        else
                        {
                            ccs.TrySetException(exception);
                        }
                    }

                    // We only log if this wasn't a cancellation exception.
                    if (tce == null &&
                        !token.IsCancellationRequested)
                    {
                        Log.Add(
                            exception,
                            LoggingLevel.Error,
                            () => ClientResources.Err_NamedPipeClient_Failed);
                    }
                }
                finally
                {
                    Dispose();
                }
            },
                disposeToken);
        }
Exemplo n.º 3
0
        private void Init(PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
        {
            Debug.Assert(direction >= PipeDirection.In && direction <= PipeDirection.InOut, "invalid pipe direction");
            Debug.Assert(transmissionMode >= PipeTransmissionMode.Byte && transmissionMode <= PipeTransmissionMode.Message, "transmissionMode is out of range");
            Debug.Assert(outBufferSize >= 0, "outBufferSize is negative");

            // always defaults to this until overridden
            _readMode = transmissionMode;
            _transmissionMode = transmissionMode;

            _pipeDirection = direction;

            if ((_pipeDirection & PipeDirection.In) != 0)
            {
                _canRead = true;
            }
            if ((_pipeDirection & PipeDirection.Out) != 0)
            {
                _canWrite = true;
            }

            _outBufferSize = outBufferSize;

            // This should always default to true
            _isMessageComplete = true;

            _state = PipeState.WaitingToConnect;
            _streamAsyncHelper = new StreamAsyncHelper(this);
        }
 /// <summary>
 /// Create TransSetNamedPipeState request packet for client to set the state of named pipe. 
 /// </summary>
 /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
 /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
 /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
 /// <param name = "flags">
 /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
 /// </param>
 /// <param name = "flags2">
 /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
 /// various client and server capabilities. 
 /// </param>
 /// <param name = "fileId">the valid file id to operation on, response by server. </param>
 /// <param name = "transactOptions">
 /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
 /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
 /// client MAY set either or both of the following bit flags 
 /// </param>
 /// <param name = "timeOut">
 /// The maximum amount of time in milliseconds to wait for the operation to complete. The client SHOULD set  
 /// this to 0 to indicate that no time-out is given. If the operation does not complete within the specified  
 /// time, the server MAY abort the request and send a failure response. 
 /// </param>
 /// <param name = "pipeState">
 /// This field contains the value that defines the state being set on the pipe. Any combination of the  
 /// following flags MUST be valid for the set operation. All other flags are considered unused and SHOULD be  
 /// set to 0 when this message is sent. The server MUST ignore the unused bits when the message is received 
 /// </param>
 /// <returns>a set named pipe state request packet. </returns>
 private SmbTransSetNmpipeStateRequestPacket CreateTransSetNamedPipeStateRequest(
     ushort messageId,
     ushort sessionUid,
     ushort treeId,
     SmbHeader_Flags_Values flags,
     SmbHeader_Flags2_Values flags2,
     ushort fileId,
     TransSmbParametersFlags transactOptions,
     uint timeOut,
     PipeState pipeState)
 {
     return new SmbTransSetNmpipeStateRequestPacket(
         this.cifsClient.CreateTransSetNmpipeStateRequest(
         messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2,
         this.capability.MaxParameterCount, this.capability.MaxDataCount, this.capability.MaxSetupCount,
         transactOptions, timeOut, "", fileId, pipeState));
 }
            /// <summary>
            /// Starts this instance.
            /// </summary>
            public void Start()
            {
                CancellationTokenSource cts = _cancellationTokenSource;
                if (cts == null)
                    return;

                CancellationToken token = cts.Token;
                _serverTask = Task.Run(
                    async () =>
                    {
                        // The disconnect GUID can be set by a disconnect request if the client requests it.
                        Guid disconnectGuid = Guid.Empty;

                        // Create pipe access rule to allow everyone to connect - may want to change this later
                        try
                        {
                            // Create pipe stream.
                            using (OverlappingPipeServerStream stream = new OverlappingPipeServerStream(
                                _server.Name,
                                _server.MaximumConnections,
                                PipeTransmissionMode.Message,
                                InBufferSize,
                                OutBufferSize,
                                _server._pipeSecurity))
                            {
                                _state = PipeState.Open;

                                // Wait for connection
                                await stream.Connect(token).ConfigureAwait(false);

                                if (!token.IsCancellationRequested)
                                {
                                    // Connect this connection to the service.
                                    _connectionGuid = _server.Service.Connect(this);

                                    if (_connectionGuid != Guid.Empty)
                                    {
                                        ConnectRequest connectRequest = null;

                                        // Set the stream.
                                        _stream = stream;

                                        _server.Add();
                                        _state = PipeState.AwaitingConnect;

                                        try
                                        {
                                            // Keep going as long as we're connected.
                                            while (stream.IsConnected &&
                                                   !token.IsCancellationRequested &&
                                                   _server.Service.State != ServiceControllerStatus.Stopped &&
                                                   _connectionGuid != Guid.Empty)
                                            {
                                                // Read data in.
                                                byte[] data = await stream.ReadAsync(token).ConfigureAwait(false);
                                                if (data == null ||
                                                    token.IsCancellationRequested ||
                                                    _server.Service.State == ServiceControllerStatus.Stopped ||
                                                    _connectionGuid == Guid.Empty)
                                                    break;

                                                // Deserialize the incoming message.
                                                Message message = Message.Deserialize(data);

                                                Request request = message as Request;

                                                // We only accept requests, anything else is a protocol error and so we must disconnect.
                                                if (request == null)
                                                    break;

                                                if (connectRequest == null)
                                                {
                                                    // We require a connect request to start
                                                    connectRequest = request as ConnectRequest;
                                                    if (connectRequest == null)
                                                        break;

                                                    _state = PipeState.Connected;
                                                    _connectionDescription = connectRequest.Description;
                                                    Log.Add(
                                                        LoggingLevel.Notification,
                                                        () => ServiceResources.Not_NamedPipeConnection_Connection,
                                                        _connectionDescription);

                                                    await Send(
                                                        new ConnectResponse(request.ID, _server.Service.ServiceName),
                                                        token)
                                                        .ConfigureAwait(false);
                                                    continue;
                                                }

                                                CommandRequest commandRequest = request as CommandRequest;
                                                if (commandRequest != null)
                                                {
                                                    if (!string.IsNullOrWhiteSpace(commandRequest.CommandLine))
                                                        _commands.TryAdd(
                                                            commandRequest.ID,
                                                            new ConnectedCommand(
                                                                _connectionGuid,
                                                                _server.Service,
                                                                this,
                                                                commandRequest,
                                                                token));
                                                    continue;
                                                }

                                                CommandCancelRequest commandCancelRequest =
                                                    request as CommandCancelRequest;
                                                if (commandCancelRequest != null)
                                                {
                                                    ConnectedCommand cancelled;
                                                    if (_commands.TryRemove(
                                                        commandCancelRequest.CancelCommandId,
                                                        out cancelled))
                                                        // ReSharper disable once PossibleNullReferenceException
                                                        cancelled.Cancel(commandCancelRequest);
                                                    continue;
                                                }

                                                DisconnectRequest disconnectRequest = request as DisconnectRequest;
                                                if (disconnectRequest != null)
                                                {
                                                    // Set the guid for disconnect.
                                                    disconnectGuid = disconnectRequest.ID;
                                                    break;
                                                }
                                            }
                                        }
                                        catch (TaskCanceledException)
                                        {
                                        }

                                        if (stream.IsConnected)
                                            try
                                            {
                                                // Try to send disconnect response.
                                                using (CancellationTokenSource dts = Constants.FireAndForgetTokenSource)
                                                    await Send(
                                                        new DisconnectResponse(disconnectGuid),
                                                        dts.Token)
                                                        .ConfigureAwait(false);
                                            }
                                            catch (TaskCanceledException)
                                            {
                                            }
                                    }
                                }

                                // Remove the stream.
                                _stream = null;
                                _state = PipeState.Closed;
                            }
                        }
                        catch (TaskCanceledException)
                        {
                            // Don't log cancellation.
                        }
                        catch (IOException ioe)
                        {
                            if (!token.IsCancellationRequested)
                                // Common exception caused by sudden disconnect, lower level
                                Log.Add(
                                    ioe,
                                    LoggingLevel.Information,
                                    () => ServiceResources.Err_NamedPipeConnection_Failed);
                        }
                        catch (Exception exception)
                        {
                            if (!token.IsCancellationRequested)
                                Log.Add(
                                    exception,
                                    LoggingLevel.Error,
                                    () => ServiceResources.Err_NamedPipeConnection_Failed);
                        }
                        finally
                        {
                            Dispose();
                        }
                    },
                    token);
            }
        /// <summary>
        /// Create TRANS_QUERY_NMPIPE_STATE Response 
        /// </summary>
        /// <param name="connection">the connection identified the client</param>
        /// <param name = "pipeState">indicate the state of the named pipe </param>
        /// <returns>The SmbTransQueryNmpipeStateResponsePacket </returns>
        /// <exception cref="ArgumentNullException">connection must not be null</exception>
        public virtual SmbTransQueryNmpipeStateResponsePacket CreateTransQueryNmpipeStateResponse(
            SmbServerConnection connection,
            PipeState pipeState)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            SmbTransQueryNmpipeStateResponsePacket packet = new SmbTransQueryNmpipeStateResponsePacket();

            // get the request packet
            SmbPacket request = connection.GetRequestPacket(connection.MessageId);

            // create smb packet header
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(
                SmbCommand.SMB_COM_TRANSACTION,
                connection.ProcessId, connection.MessageId, request.SmbHeader.Uid, request.SmbHeader.Tid,
                (SmbFlags)connection.Capability.Flag, (SmbFlags2)connection.Capability.Flags2);

            // update smb parameters
            SMB_COM_TRANSACTION_SuccessResponse_SMB_Parameters smbParameters = packet.SmbParameters;

            smbParameters.Setup = new ushort[0];

            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION_SuccessResponse_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // update smb data
            SMB_COM_TRANSACTION_SuccessResponse_SMB_Data smbData = packet.SmbData;

            // update trans param
            TRANS_QUERY_NMPIPE_STATE_Response_Trans_Parameters transParameters = packet.TransParameters;

            transParameters.NMPipeStatus = (SMB_NMPIPE_STATUS)pipeState;

            // store the parameters and data to packet.
            packet.TransParameters = transParameters;
            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;

            packet.UpdateCountAndOffset();

            return packet;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _state = PipeState.Closed;
            _serviceName = null;

            CancellationTokenSource cts = Interlocked.Exchange(ref _cancellationTokenSource, null);
            if (cts != null)
            {
                cts.Cancel();
                cts.Dispose();
            }

            TaskCompletionSource<NamedPipeClient> ccs = Interlocked.Exchange(ref _connectionCompletionSource, null);
            if (ccs != null)
                ccs.TrySetCanceled();

            _clientTask = null;
            _state = PipeState.Closed;
            _serviceName = null;

            foreach (Guid id in _commandRequests.Keys.ToArray())
            {
                ConnectedCommand cc;
                if (_commandRequests.TryRemove(id, out cc))
                {
                    Debug.Assert(cc != null);
                    cc.Dispose();
                }
            }
        }
Exemplo n.º 8
0
 internal virtual void ChangeState(PipeState newState)
 {
     _state = newState;
 }
Exemplo n.º 9
0
 public void StopProductPipe()
 {
     pipeState = PipeState.OFF;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NamedPipeClient" /> class.
        /// </summary>
        /// <param name="description">The client description.</param>
        /// <param name="server">The server.</param>
        /// <param name="onReceive">The action to call on receipt of a message.</param>
        /// <param name="token">The token.</param>
        private NamedPipeClient(
            [NotNull] string description,
            [NotNull] NamedPipeServerInfo server,
            [NotNull] Action<Message> onReceive,
            CancellationToken token = default(CancellationToken))
        {
            if (description == null) throw new ArgumentNullException("description");
            if (server == null) throw new ArgumentNullException("server");
            if (onReceive == null) throw new ArgumentNullException("onReceive");

            _server = server;

            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken disposeToken = _cancellationTokenSource.Token;

            _clientTask = Task.Run(
                async () =>
                {
                    try
                    {
                        using (ITokenSource tokenSource = token.CreateLinked(disposeToken))
                        using (
                            OverlappingPipeClientStream stream = new OverlappingPipeClientStream(
                                _server.Host,
                                _server.FullName,
                                PipeTransmissionMode.Message))
                        {
                            _state = PipeState.Open;

                            token = tokenSource.Token;

                            // We need to support cancelling the connect.
                            await stream.Connect(token).ConfigureAwait(false);

                            ConnectResponse connectResponse = null;
                            DisconnectResponse disconnectResponse = null;

                            if (!token.IsCancellationRequested)
                            {
                                // Set the stream.
                                _stream = stream;
                                _state = PipeState.AwaitingConnect;

                                // Kick off a connect request, but don't wait for it's result as we're the task that will receive it!
                                ConnectRequest connectRequest = new ConnectRequest(description);
                                await stream.WriteAsync(connectRequest.Serialize(), token).ConfigureAwait(false);

                                // Keep going as long as we're connected.
                                try
                                {
                                    while (stream.IsConnected &&
                                           !disposeToken.IsCancellationRequested)
                                    {
                                        // Read data in.
                                        byte[] data = await stream.ReadAsync(disposeToken).ConfigureAwait(false);

                                        if (data == null)
                                            break;

                                        // Deserialize the incoming message.
                                        Message message = Message.Deserialize(data);

                                        if (connectResponse == null)
                                        {
                                            // We require a connect response to start
                                            connectResponse = message as ConnectResponse;
                                            if (connectResponse == null ||
                                                connectResponse.ID != connectRequest.ID)
                                                break;

                                            _state = PipeState.Connected;
                                            _serviceName = connectResponse.ServiceName;

                                            Log.Add(
                                                LoggingLevel.Notification,
                                                () => ClientResources.Not_NamedPipeClient_Connection,
                                                connectResponse.ServiceName);

                                            TaskCompletionSource<NamedPipeClient> ccs =
                                                Interlocked.Exchange(ref _connectionCompletionSource, null);
                                            if (ccs != null)
                                                ccs.TrySetResult(this);

                                            // Observer the message.
                                            onReceive(message);
                                            continue;
                                        }

                                        // Check for disconnect, we don't observe the message until the disconnect is complete.
                                        disconnectResponse = message as DisconnectResponse;
                                        if (disconnectResponse != null)
                                            break;

                                        // Observe the message.
                                        onReceive(message);

                                        Response response = message as Response;
                                        if (response == null)
                                            continue;

                                        ConnectedCommand connectedCommand;

                                        // Check for cancellation responses.
                                        CommandCancelResponse cancelResponse = response as CommandCancelResponse;
                                        if (cancelResponse != null)
                                            // Cancel the associated request
                                            if (_commandRequests.TryGetValue(
                                                cancelResponse.CancelledCommandId,
                                                out connectedCommand))
                                                // ReSharper disable once PossibleNullReferenceException
                                                connectedCommand.Cancel(cancelResponse);

                                        // And fall through to complete the response...

                                        // Find command the response is related to, and notify it of the response.
                                        if (!_commandRequests.TryGetValue(response.ID, out connectedCommand))
                                            continue;

                                        Debug.Assert(connectedCommand != null);
                                        if (connectedCommand.Received(response))
                                            _commandRequests.TryRemove(response.ID, out connectedCommand);
                                    }
                                }
                                catch (TaskCanceledException)
                                {
                                }
                            }

                            // If we're still connected, and we haven't received a disconnect response, try to send a disconnect request.
                            if (stream.IsConnected &&
                                disconnectResponse == null)
                            {
                                CancellationTokenSource cts = token.IsCancellationRequested
                                    ? new CancellationTokenSource(500)
                                    : null;
                                try
                                {
                                    CancellationToken t = cts != null
                                        ? cts.Token
                                        : token;

                                    // Try to send disconnect request.
                                    // ReSharper disable once PossibleNullReferenceException
                                    await Send(new DisconnectRequest(), t)
                                        .ToTask(t)
                                        .ConfigureAwait(false);
                                }
                                catch (TaskCanceledException)
                                {
                                }
                                finally
                                {
                                    if (cts != null)
                                        cts.Dispose();
                                }
                            }

                            // Remove the stream.
                            _stream = null;
                            _state = PipeState.Closed;
                            _serviceName = null;

                            // If we had a disconnect message observe it now that the disconnect has been actioned,
                            // this prevents the receiver thinking the connection is still active.
                            if (disconnectResponse != null)
                            {
                                onReceive(disconnectResponse);
                                ConnectedCommand connectedCommand;
                                if (_commandRequests.TryGetValue(disconnectResponse.ID, out connectedCommand))
                                {
                                    Debug.Assert(connectedCommand != null);
                                    if (connectedCommand.Received(disconnectResponse))
                                        _commandRequests.TryRemove(disconnectResponse.ID, out connectedCommand);
                                }
                            }
                        }
                    }
                    catch (IOException ioe)
                    {
                        if (!token.IsCancellationRequested)
                            // Common exception caused by sudden disconnect, lower level
                            Log.Add(
                                ioe,
                                LoggingLevel.Information,
                                () => ClientResources.Err_NamedPipeClient_Failed);
                    }
                    catch (Exception exception)
                    {
                        TaskCanceledException tce = exception as TaskCanceledException;
                        TaskCompletionSource<NamedPipeClient> ccs = Interlocked.Exchange(
                            ref _connectionCompletionSource,
                            null);
                        if (ccs != null)
                            if (tce != null)
                                ccs.TrySetCanceled();
                            else
                                ccs.TrySetException(exception);

                        // We only log if this wasn't a cancellation exception.
                        if (tce == null &&
                            !token.IsCancellationRequested)
                            Log.Add(
                                exception,
                                LoggingLevel.Error,
                                () => ClientResources.Err_NamedPipeClient_Failed);
                    }
                    finally
                    {
                        Dispose();
                    }
                },
                disposeToken);
        }
Exemplo n.º 11
0
 public void StartProductPipe()
 {
     pipeState = PipeState.ON;
     StartCoroutine(NewPipe());
 }
Exemplo n.º 12
0
        public static String GetStateName(PipeState state)
        {
            string name = string.Empty;

            if ((state & PipeState.Returned) == PipeState.Returned)
            {
                if ((state & PipeState.ReturnValid) == PipeState.ReturnValid)
                {
                    return "退料合格";
                }
                if ((state & PipeState.ReturnInvalid) == PipeState.ReturnInvalid)
                {
                    return "退料不合格";
                }
            }

            if ((state & PipeState.SentingOut) == PipeState.SentingOut)
            {
                return "发料中";
            }

            if ((state & PipeState.SentOut) == PipeState.SentOut)
            {
                return "已发出";
            }

            if ((state & PipeState.CheckValid) == PipeState.CheckValid)
            {
                name = "常检合格";
            }
            else if ((state & PipeState.CheckInvalid) == PipeState.CheckInvalid)
            {
                name = "检测不合格";
            }
            else if ((state & PipeState.NormalChecking) == PipeState.NormalChecking)
            {
                name = "常检中";
            }

            if ((state & PipeState.Transfering) == PipeState.Transfering)
            {
                name += (name.Length == 0) ? "" : ",";
                name += "调度中";
            }

            if (name.Length > 0)
            {
                if ((state & PipeState.IncomeRecordDone) == PipeState.IncomeRecordDone)
                {
                    name += ",已收料";
                }
                else if ((state & PipeState.IncomeRecordDoing) == PipeState.IncomeRecordDoing)
                {
                    name += ",收料中";
                }
                else
                {
                    name += ",未收料";
                }

                return name;
            }

            if ((state & PipeState.Storing) == PipeState.Storing)
            {
                return "存储中";
            }

            if ((state & PipeState.IncomeRecordDone) == PipeState.IncomeRecordDone)
            {
                return "已收料";
            }

            if ((state & PipeState.IncomeRecordDoing) == PipeState.IncomeRecordDoing)
            {
                name = "收料中";
            }

            if ((state & PipeState.BusinessChecked) == PipeState.BusinessChecked)
            {
                name += (name.Length == 0) ? "" : ",";
                name += "商检完成";
            }
            else if ((state & PipeState.BusinessChecking) == PipeState.BusinessChecking)
            {
                name += (name.Length == 0) ? "" : ",";
                name += "商检中";
            }

            if (name.Length > 0)
            {
                return name;
            }

            if ((state & PipeState.Received) == PipeState.Received)
            {
                name = "已送达";
            }
            else
            {
                name = "未知";
            }

            return name;
        }
Exemplo n.º 13
0
        public static PipeState GetMaxStateIn(PipeState state)
        {
            if ((state & PipeState.SentOut) == PipeState.SentOut)
            {
                return PipeState.SentOut;
            }

            if ((state & PipeState.CheckInvalid) == PipeState.CheckInvalid)
            {
                return PipeState.CheckInvalid;
            }

            if ((state & PipeState.CheckValid) == PipeState.CheckValid)
            {
                return PipeState.CheckValid;
            }

            if ((state & PipeState.NormalChecking) == PipeState.NormalChecking)
            {
                return PipeState.NormalChecking;
            }

            if ((state & PipeState.Transfering) == PipeState.Transfering)
            {
                return PipeState.Transfering;
            }

            if ((state & PipeState.Storing) == PipeState.Storing)
            {
                return PipeState.Storing;
            }
            if ((state & PipeState.IncomeRecordDone) == PipeState.IncomeRecordDone)
            {
                return PipeState.IncomeRecordDone;
            }

            if ((state & PipeState.IncomeRecordDoing) == PipeState.IncomeRecordDoing)
            {
                return PipeState.IncomeRecordDoing;
            }

            if ((state & PipeState.BusinessChecked) == PipeState.BusinessChecked)
            {
                return PipeState.BusinessChecked;
            }

            if ((state & PipeState.BusinessChecking) == PipeState.BusinessChecking)
            {
                return PipeState.BusinessChecking;
            }

            if ((state & PipeState.Received) == PipeState.Received)
            {
                return PipeState.Received;
            }

            return PipeState.NotGiven;
        }
        /// <summary>
        /// to create a TransSetNmpipeState request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="smbParametersflags">A set of bit flags that alter the behavior of the requested
        /// operation</param>
        /// <param name="timeout">The value of this field MUST be the maximum number of milliseconds the server SHOULD
        /// wait for completion of the transaction before generating a timeout and returning a response to the
        /// client. </param>
        /// <param name="name">The pathname of the mailslot or named pipe to which the transaction subcommand applies
        /// or a client supplied identifier that provides a name for the transaction.</param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="pipeState">This field contains the value that defines the state being set on the pipe</param>
        /// <returns>a TransSetNmpipeState request packet</returns>
        public SmbTransSetNmpipeStateRequestPacket CreateTransSetNmpipeStateRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            ushort maxParameterCount,
            ushort maxDataCount,
            byte maxSetupCount,
            TransSmbParametersFlags smbParametersflags,
            uint timeout,
            string name,
            ushort fid,
            PipeState pipeState)
        {
            if (name == null)
            {
                name = string.Empty;
            }

            SmbTransSetNmpipeStateRequestPacket packet = new SmbTransSetNmpipeStateRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION,
                messageId, uid, treeId, flags, flags2);

            // Set Smb Parameters
            SMB_COM_TRANSACTION_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.Flags = smbParametersflags;
            smbParameters.Timeout = timeout;
            smbParameters.SetupCount = 2; // the correct count in word of the Setup is always 2.
            smbParameters.Setup = new ushort[smbParameters.SetupCount];
            smbParameters.Setup[0] = (ushort)TransSubCommand.TRANS_SET_NMPIPE_STATE;
            smbParameters.Setup[1] = fid;
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb Data
            SMB_COM_TRANSACTION_Request_SMB_Data smbData = new SMB_COM_TRANSACTION_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans_Parameters
            TRANS_SET_NMPIPE_STATE_RequestTransParameters transParameters =
                new TRANS_SET_NMPIPE_STATE_RequestTransParameters();
            transParameters.PipeState = pipeState;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.TransParameters = transParameters;
            packet.UpdateCountAndOffset();

            return packet;
        }
Exemplo n.º 15
0
            /// <summary>
            /// Starts this instance.
            /// </summary>
            public void Start()
            {
                CancellationTokenSource cts = _cancellationTokenSource;

                if (cts == null)
                {
                    return;
                }

                CancellationToken token = cts.Token;

                _serverTask = Task.Run(
                    async() =>
                {
                    // The disconnect GUID can be set by a disconnect request if the client requests it.
                    Guid disconnectGuid = Guid.Empty;

                    // Create pipe access rule to allow everyone to connect - may want to change this later
                    try
                    {
                        // Create pipe stream.
                        using (OverlappingPipeServerStream stream = new OverlappingPipeServerStream(
                                   _server.Name,
                                   _server.MaximumConnections,
                                   PipeTransmissionMode.Message,
                                   InBufferSize,
                                   OutBufferSize,
                                   _server._pipeSecurity))
                        {
                            _state = PipeState.Open;

                            // Wait for connection
                            await stream.Connect(token).ConfigureAwait(false);

                            if (!token.IsCancellationRequested)
                            {
                                // Connect this connection to the service.
                                _connectionGuid = _server.Service.Connect(this);

                                if (_connectionGuid != Guid.Empty)
                                {
                                    ConnectRequest connectRequest = null;

                                    // Set the stream.
                                    _stream = stream;

                                    _server.Add();
                                    _state = PipeState.AwaitingConnect;

                                    try
                                    {
                                        // Keep going as long as we're connected.
                                        while (stream.IsConnected &&
                                               !token.IsCancellationRequested &&
                                               _server.Service.State != ServiceControllerStatus.Stopped &&
                                               _connectionGuid != Guid.Empty)
                                        {
                                            // Read data in.
                                            byte[] data = await stream.ReadAsync(token).ConfigureAwait(false);
                                            if (data == null ||
                                                token.IsCancellationRequested ||
                                                _server.Service.State == ServiceControllerStatus.Stopped ||
                                                _connectionGuid == Guid.Empty)
                                            {
                                                break;
                                            }

                                            // Deserialize the incoming message.
                                            Message message = Message.Deserialize(data);

                                            Request request = message as Request;

                                            // We only accept requests, anything else is a protocol error and so we must disconnect.
                                            if (request == null)
                                            {
                                                break;
                                            }

                                            if (connectRequest == null)
                                            {
                                                // We require a connect request to start
                                                connectRequest = request as ConnectRequest;
                                                if (connectRequest == null)
                                                {
                                                    break;
                                                }

                                                _state = PipeState.Connected;
                                                _connectionDescription = connectRequest.Description;
                                                Log.Add(
                                                    LoggingLevel.Notification,
                                                    () => ServiceResources.Not_NamedPipeConnection_Connection,
                                                    _connectionDescription);

                                                await Send(
                                                    new ConnectResponse(request.ID, _server.Service.ServiceName),
                                                    token)
                                                .ConfigureAwait(false);
                                                continue;
                                            }

                                            CommandRequest commandRequest = request as CommandRequest;
                                            if (commandRequest != null)
                                            {
                                                if (!string.IsNullOrWhiteSpace(commandRequest.CommandLine))
                                                {
                                                    _commands.TryAdd(
                                                        commandRequest.ID,
                                                        new ConnectedCommand(
                                                            _connectionGuid,
                                                            _server.Service,
                                                            this,
                                                            commandRequest,
                                                            token));
                                                }
                                                continue;
                                            }

                                            CommandCancelRequest commandCancelRequest =
                                                request as CommandCancelRequest;
                                            if (commandCancelRequest != null)
                                            {
                                                ConnectedCommand cancelled;
                                                if (_commands.TryRemove(
                                                        commandCancelRequest.CancelCommandId,
                                                        out cancelled))
                                                {
                                                    // ReSharper disable once PossibleNullReferenceException
                                                    cancelled.Cancel(commandCancelRequest);
                                                }
                                                continue;
                                            }

                                            DisconnectRequest disconnectRequest = request as DisconnectRequest;
                                            if (disconnectRequest != null)
                                            {
                                                // Set the guid for disconnect.
                                                disconnectGuid = disconnectRequest.ID;
                                                break;
                                            }
                                        }
                                    }
                                    catch (TaskCanceledException)
                                    {
                                    }

                                    if (stream.IsConnected)
                                    {
                                        try
                                        {
                                            // Try to send disconnect response.
                                            using (CancellationTokenSource dts = Constants.FireAndForgetTokenSource)
                                                await Send(
                                                    new DisconnectResponse(disconnectGuid),
                                                    dts.Token)
                                                .ConfigureAwait(false);
                                        }
                                        catch (TaskCanceledException)
                                        {
                                        }
                                    }
                                }
                            }

                            // Remove the stream.
                            _stream = null;
                            _state  = PipeState.Closed;
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // Don't log cancellation.
                    }
                    catch (IOException ioe)
                    {
                        if (!token.IsCancellationRequested)
                        {
                            // Common exception caused by sudden disconnect, lower level
                            Log.Add(
                                ioe,
                                LoggingLevel.Information,
                                () => ServiceResources.Err_NamedPipeConnection_Failed);
                        }
                    }
                    catch (Exception exception)
                    {
                        if (!token.IsCancellationRequested)
                        {
                            Log.Add(
                                exception,
                                LoggingLevel.Error,
                                () => ServiceResources.Err_NamedPipeConnection_Failed);
                        }
                    }
                    finally
                    {
                        Dispose();
                    }
                },
                    token);
            }
        /// <summary>
        /// to create a TransSetNmpipeState request packet.
        /// </summary>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="name">The pathname of the mailslot or named pipe to which the transaction subcommand applies
        /// or a client supplied identifier that provides a name for the transaction.</param>
        /// <param name="pipeState">This field contains the value that defines the state being set on the pipe</param>
        /// <returns>a TransSetNmpipeState request packet</returns>
        /// <exception cref="System.NullReferenceException">There is no connection in context. </exception>
        public SmbTransSetNmpipeStateRequestPacket CreateTransSetNmpipeStateRequest(
            ushort uid,
            ushort treeId,
            string name,
            ushort fid,
            PipeState pipeState)
        {
            // This field SHOULD be set to 0x0000.
            ushort maxParameterCount = 0x0000;
            // This field SHOULD be set to 0x0000.
            ushort maxDataCount = 0x0000;
            // This field SHOULD be 0x00.
            byte maxSetupCount = 0x00;

            return this.CreateTransSetNmpipeStateRequest(this.Context.GetMessageId(this.connectionId),
                uid, treeId, this.defaultParameters.Flag, this.defaultParameters.Flag2, maxParameterCount,
                maxDataCount, maxSetupCount, this.defaultParameters.TransSmbParametersFlags,
                this.defaultParameters.Timeout, name, fid, pipeState);
        }
Exemplo n.º 17
0
        internal void WinIOError(int errorCode)
        {
            if (errorCode == Interop.ERROR_BROKEN_PIPE ||
                errorCode == Interop.ERROR_PIPE_NOT_CONNECTED ||
                errorCode == Interop.ERROR_NO_DATA
                )
            {
                // Other side has broken the connection
                _state = PipeState.Broken;
                throw new IOException(SR.IO_PipeBroken, Win32Marshal.MakeHRFromErrorCode(errorCode));
            }
            else if (errorCode == Interop.ERROR_HANDLE_EOF)
            {
                throw __Error.GetEndOfFile();
            }
            else
            {
                // For invalid handles, detect the error and mark our handle
                // as invalid to give slightly better error messages.  Also
                // help ensure we avoid handle recycling bugs.
                if (errorCode == Interop.ERROR_INVALID_HANDLE)
                {
                    _handle.SetHandleAsInvalid();
                    _state = PipeState.Broken;
                }

                throw Win32Marshal.GetExceptionForWin32Error(errorCode);
            }
        }
Exemplo n.º 18
0
        internal void WinIOError(int errorCode) {
            
            if (errorCode == UnsafeNativeMethods.ERROR_BROKEN_PIPE ||
                errorCode == UnsafeNativeMethods.ERROR_PIPE_NOT_CONNECTED ||
                errorCode == UnsafeNativeMethods.ERROR_NO_DATA
                ) {
                // Other side has broken the connection
                m_state = PipeState.Broken;
                throw new IOException(SR.GetString(SR.IO_IO_PipeBroken), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));

            }
            else if (errorCode == UnsafeNativeMethods.ERROR_HANDLE_EOF) {
                __Error.EndOfFile();
            }
            else {
                // For invalid handles, detect the error and mark our handle
                // as invalid to give slightly better error messages.  Also
                // help ensure we avoid handle recycling bugs.
                if (errorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE) {
                    m_handle.SetHandleAsInvalid();
                    m_state = PipeState.Broken;
                }

                __Error.WinIOError(errorCode, String.Empty);
            }
        }
Exemplo n.º 19
0
        private int mCurId;         //current id

        public PipeDelCommand(PipeState state, Path path)
        {
            mState = state;
            mPath  = path;
        }
Exemplo n.º 20
0
            /// <summary>
            /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
            /// </summary>
            public void Dispose()
            {
                _state = PipeState.Closed;
                // This is a safe race condition, we can tell the service we're disconnected as many times as we want.
                Guid cg = _connectionGuid;
                if (cg != Guid.Empty)
                {
                    _connectionGuid = Guid.Empty;
                    _server.Service.Disconnect(cg);
                    Log.Add(
                        LoggingLevel.Notification,
                        () => ServiceResources.Not_NamedPipeConnection_Disconnected,
                        _connectionDescription);
                }

                CancellationTokenSource cts = Interlocked.Exchange(ref _cancellationTokenSource, null);
                if (cts != null)
                {
                    cts.Cancel();
                    cts.Dispose();
                }

                _serverTask = null;
                NamedPipeServer server = Interlocked.Exchange(ref _server, null);
                if (server != null)
                    server.Remove(this);
            }
 protected override void Dispose(bool disposing)
 {
     try
     {
         if ((this.m_handle != null) && !this.m_handle.IsClosed)
         {
             this.m_handle.Dispose();
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
     this.m_state = PipeState.Closed;
 }
Exemplo n.º 22
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                // Nothing will be done differently based on whether we are 
                // disposing vs. finalizing.  
                if (_handle != null && !_handle.IsClosed)
                {
                    _handle.Dispose();
                }
            }
            finally
            {
                base.Dispose(disposing);
            }

            _state = PipeState.Closed;
        }
 private void Init(PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
 {
     this.m_readMode = transmissionMode;
     this.m_transmissionMode = transmissionMode;
     this.m_pipeDirection = direction;
     if ((this.m_pipeDirection & PipeDirection.In) != ((PipeDirection) 0))
     {
         this.m_canRead = true;
     }
     if ((this.m_pipeDirection & PipeDirection.Out) != ((PipeDirection) 0))
     {
         this.m_canWrite = true;
     }
     this.m_outBufferSize = outBufferSize;
     this.m_isMessageComplete = true;
     this.m_state = PipeState.WaitingToConnect;
 }
 /// <summary>
 /// Create TransSetNamedPipeState request packet for client to set the state of named pipe. 
 /// </summary>
 /// <param name = "fileId">the valid file id to operation on, response by server. </param>
 /// <param name = "transactOptions">
 /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
 /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
 /// client MAY set either or both of the following bit flags 
 /// </param>
 /// <param name = "pipeState">
 /// This field contains the value that defines the state being set on the pipe. Any combination of the  
 /// following flags MUST be valid for the set operation. All other flags are considered unused and SHOULD be  
 /// set to 0 when this message is sent. The server MUST ignore the unused bits when the message is received 
 /// </param>
 /// <returns>a set named pipe state request packet. </returns>
 public virtual SmbTransSetNmpipeStateRequestPacket CreateTransSetNamedPipeStateRequest(
     ushort fileId,
     TransSmbParametersFlags transactOptions,
     PipeState pipeState)
 {
     return CreateTransSetNamedPipeStateRequest(
         this.MessageId,
         this.GetSessionIdByFileId(fileId), this.GetTreeIdByFileId(fileId), this.capability.Flag,
         this.capability.Flags2, fileId, transactOptions, this.capability.Timeout, pipeState);
 }
 internal void WinIOError(int errorCode)
 {
     if (((errorCode == 0x6d) || (errorCode == 0xe9)) || (errorCode == 0xe8))
     {
         this.m_state = PipeState.Broken;
         throw new IOException(System.SR.GetString("IO_IO_PipeBroken"), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));
     }
     if (errorCode == 0x26)
     {
         System.IO.__Error.EndOfFile();
     }
     else
     {
         if (errorCode == 6)
         {
             this.m_handle.SetHandleAsInvalid();
             this.m_state = PipeState.Broken;
         }
         System.IO.__Error.WinIOError(errorCode, string.Empty);
     }
 }
        public TransactionSetNamedPipeStateRequest(byte[] setup, byte[] parameters) : base()
        {
            FID = LittleEndianConverter.ToUInt16(setup, 2);

            PipeState = (PipeState)LittleEndianConverter.ToUInt16(parameters, 0);
        }
Exemplo n.º 27
0
 public Pipe(Vector2 location) : base(location)
 {
     BlockState = new PipeState(this);
 }