Esempio n. 1
0
        public async Task <IResponse> WriteAsync(Stream stream, bool returnResponse = false, bool flush = true, CancellationToken token = default(CancellationToken))
        {
            if (returnResponse)
            {
                //subscribe to IObserveable
                PacketRouter router = new PacketRouter();
                Subscribe(router);

                //write the packet to the stream
                //await base.WriteAsync(stream, flush, token);
                await WriteAsync(stream, flush, token);

                var ts = TimeSpan.FromSeconds(Timeout);
                //wait for the response
                _isResponseReceived = new ValueWatcher <bool>(GetResponseReceivedValue, IsValueAcceptable);
                await Task.Run(() => _isResponseReceived.Wait(ts), token);

                //Unsubscribe when the correct type of packet has been received or it has timed out
                Unsubscribe();
            }
            else
            {
                //we dont need the return packet so we will just write to the stream and exit
                //await base.WriteAsync(stream, flush, token);
                await WriteAsync(stream, flush, token);
            }
            //return the response packet
            return(ResponseData);
        }
Esempio n. 2
0
        internal static async void PacketReceived(Packet packet, Stream stream)
        {
            //Handle Direct return messages : it is possible this should be placed in the packet class!
            if (packet.Header == PacketHeaderType.CommandStationOperationResponse && packet.Payload[0] == Convert.ToByte(PacketIdentifier.CommandStationOperationResponse.TransferErrors))
            {
                var ackResp = new AckRespMessage();
                //if (XpressNetConstants.UseAsyncWrite)
                //{
                //    //ackResp.WriteAsync(stream);
                //}
                //else
                //{
                await ackResp.WriteAsync(stream);

                //}
            }

            //Handle Broadcast Messages

            if (packet.Header == PacketHeaderType.EmergencyStopAll)
            {
                var bmr = new BroadcastMessageReceivedEventArgs
                {
                    ResponseType = BroadcastType.EmergencyStop,
                    ReceivedTime = DateTime.Now
                };
                OnBroadcastMessageReceived(bmr);
            }
            else if (packet.Header == PacketHeaderType.CommandStationOperationResponse && packet.Payload[0] == Convert.ToByte(PacketIdentifier.CommandStationOperationResponse.NormalOperationsResumedBroadcast))
            {
                var bmr = new BroadcastMessageReceivedEventArgs
                {
                    ResponseType = BroadcastType.NormalOperationResumed,
                    ReceivedTime = DateTime.Now
                };
                OnBroadcastMessageReceived(bmr);
            }
            else if (packet.Header == PacketHeaderType.CommandStationOperationResponse && packet.Payload[0] == Convert.ToByte(PacketIdentifier.CommandStationOperationResponse.TrackPowerOffBroadcast))
            {
                var bmr = new BroadcastMessageReceivedEventArgs
                {
                    ResponseType = BroadcastType.TrackPowerOff,
                    ReceivedTime = DateTime.Now
                };
                OnBroadcastMessageReceived(bmr);
            }
            else if (packet.Header == PacketHeaderType.CommandStationOperationResponse && packet.Payload[0] == Convert.ToByte(PacketIdentifier.CommandStationOperationResponse.ServiceModeEntryBroadcast))
            {
                var bmr = new BroadcastMessageReceivedEventArgs
                {
                    ResponseType = BroadcastType.ServiceModeEntry,
                    ReceivedTime = DateTime.Now
                };
                OnBroadcastMessageReceived(bmr);
            }
            else if (packet.Header == PacketHeaderType.AccessoryDecoderInformation)
            {
                var bmr = new BroadcastMessageReceivedEventArgs
                {
                    ResponseType = BroadcastType.AccessoryFeedback,
                    ReceivedTime = DateTime.Now
                };
                OnBroadcastMessageReceived(bmr);
            }

            var pump = new PacketRouter();

            pump.DisseminatePacket(packet);
        }