Exemplo n.º 1
0
 private static void SendCallback(this AckState ackState, IAsyncResult asyncResult)
 {
     ackState.AsyncResult = asyncResult;
     ackState.TryAndLog(
         ack =>
     {
         var bytesSent = ack.Handler.EndSend(ack.AsyncResult);
         Log.Success(
             "Sent {0} bytes from {1} connected to {2}",
             bytesSent,
             ack.Handler.LocalEndPoint,
             ack.Handler.RemoteEndPoint
             );
     }
         );
 }
Exemplo n.º 2
0
        private static void BeginSend(this AckState ackState)
        {
            ackState.TryAndLog(
                ack =>
            {
                AckQueueDictionary.AddOrUpdate(
                    ack.RemoteEndPoint,
                    key => ack.Queue,
                    (key, current) =>
                {
                    current.CompleteAdding();
                    return(ack.Queue);
                }
                    );
                while (!ack.Queue.IsCompleted)
                {
                    var command = ack.Queue.Take();
                    Command.Command ackCommand;
                    try
                    {
                        switch (command.Operation)
                        {
                        case Operation.Connect:
                            ackCommand = GetAckCommand(
                                command,
                                value: 1
                                );
                            break;

                        case Operation.WiringPiSetupPiFace:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.WiringPiSetupPiFace()
                                );
                            break;

                        case Operation.PullUpDnControl:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.PullUpDnControl(command.Pin, command.Value)
                                );
                            break;

                        case Operation.DigitalWrite:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.DigitalWrite(command.Pin, command.Value)
                                );
                            break;

                        case Operation.DigitalRead:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.DigitalRead(command.Pin)
                                );
                            break;

                        case Operation.MonitorInput:
                            var monitor = MonitorInputDictionary.AddOrUpdate(
                                ackState.RemoteEndPoint,
                                key =>
                            {
                                var value          = new Command.Command[8];
                                value[command.Pin] = command;
                                return(value);
                            },
                                (key, value) =>
                            {
                                value[command.Pin] = command;
                                return(value);
                            }
                                );
                            var result = monitor[command.Pin];
                            ackCommand = GetAckCommand(
                                command,
                                value: (result.Id == command.Id && command.Value == result.Value)
                                                   ? 1
                                                   : 0
                                );
                            if (PiFace.InputChanged == null)
                            {
                                PiFace.InputChanged = MonitorInputChanged;
                            }
                            break;

                        case Operation.MonitorInputChanged:
                            ackCommand = command;
                            break;

                        // ReSharper disable RedundantCaseLabel
                        case Operation.None:
                        default:
                            // ReSharper restore RedundantCaseLabel
                            ackCommand = GetAckCommand(
                                command,
                                operation: Operation.None,
                                value: PiFace.DigitalRead(command.Pin)
                                );
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.LogException();
                        ackCommand = GetAckCommand(
                            command,
                            value: -1
                            );
                    }

                    var ackCommandBytes = ackCommand.ToArray();
                    Log.Info(
                        "Sending {0} bytes from {1} connected to {2}",
                        ackCommandBytes.Length,
                        ack.Handler.LocalEndPoint,
                        ack.Handler.RemoteEndPoint
                        );
                    ackState.Handler.BeginSend(
                        ackCommandBytes,
                        0,
                        ackCommandBytes.Length,
                        0,
                        ackState.SendCallback,
                        null
                        );
                }
            }
                );
        }