示例#1
0
        public override DataPacket ConvertToDataPacket()
        {
            using (MemoryStream sink = new MemoryStream ())
            {
                StreamHelper.WriteUInt16 (RequestIdentifier, sink);
                Write (sink);

                sink.Flush ();
                DataPacket packet = new DataPacket (true, Subsystem, sink.ToArray ());
                packet.PacketNr = _request.PacketIdentifier.PacketNr;

                return packet;
            }
        }
示例#2
0
        /// <summary>
        /// Converts this request to a default representation of a DataPacket
        /// </summary>
        /// <returns>
        /// A <see cref="DataPacket"/>
        /// </returns>
        public virtual DataPacket ConvertToDataPacket()
        {
            using(MemoryStream sink = new MemoryStream())
            {
                StreamHelper.WriteUInt16(RequestIdentifier, sink);
                Write(sink);

                sink.Flush();
                DataPacket packet = new DataPacket(false, Subsystem, sink.ToArray());
                return packet;
            }
        }
示例#3
0
            public bool TrySetResponsePacket(DataPacket packet)
            {
                lock(this)
                {
                    if(_packetExpired) return false;

                    _responsePacket = packet;
                    return true;
                }
            }
示例#4
0
        /// <summary>
        /// Is called when a new DataPacket from the remote end point receives
        /// </summary>
        private void OnRequestPacketReceived(DataPacket packet)
        {
            if(_configured == false)
                _configuredEvent.WaitOne();

            if(_subsystems.ContainsKey(packet.Subsystem))
            {
                _logger.DebugFormat("Redirecting packet to subsystem '{0}'", packet.Subsystem);
                _subsystems[packet.Subsystem].HandlePacket(packet);
            }
            else
            {
                 _logger.WarnFormat("Subsystem '{0}' has not been found", packet.Subsystem);
            }
        }
示例#5
0
        /// <summary>
        /// Receives packets from the connection stream.
        /// The classification of the packet (response or request packet) and
        /// the assignment to subsystems is done outside
        /// </summary>
        private void ReceiveProc()
        {
            try
            {
                while(!_disposed)
                {
                    if(_connection.Connected)
                    {
                        DataPacket packet = new DataPacket(_connection);

            #if DEBUG_PACKETTRANSMITTER
                        _logger.DebugFormat("Received DataPacket: {0}", packet);
            #endif
                        if(packet.IsResponse)
                            HandleResponsePackets(packet);
                        else
                            HandleRequestPackets(packet);

                    }
                    else
                        _receiveThreadWaitEvent.WaitOne(_maxReceiveThreadWaitTime);
                }
            }
            catch(DisconnectedException)
            {
                Dispose();
            }
        }
示例#6
0
 public PacketInfo(DataPacket packetToTransmit, bool awaitsResponse)
 {
     _packetToTransmit = packetToTransmit;
     _awaitsResponse = awaitsResponse;
 }
示例#7
0
 private void RaiseRequestPacketReceived(DataPacket dataPacket)
 {
     if(RequestPacketReceived != null)
         RequestPacketReceived.BeginInvoke(dataPacket, null, null);
 }
示例#8
0
 /// <summary>
 /// Trys to find the corresponding request packet, sets this packet as response
 /// and permits the transmitter of the origin request packet to proceed
 /// </summary>
 private void HandleResponsePackets(DataPacket dataPacket)
 {
     lock(_packetsWaitingForResponse)
     {
         if(_packetsWaitingForResponse.ContainsKey(dataPacket.PacketNr) == false)
         {
             //TODO: Log the packet, there is no corresponding request packet which waits
             //for a response
         }
         else
         {
             PacketInfo requestPacket = _packetsWaitingForResponse[dataPacket.PacketNr];
             requestPacket.ResponsePacket = dataPacket;
             _packetsWaitingForResponse.Remove(dataPacket.PacketNr);
             requestPacket.PacketResponseSync.Set();
         }
     }
 }
示例#9
0
 /// <summary>
 /// Handles packets that are not marked as response packets,
 /// they are simply passed to the outside of the PacketTransmitter
 /// to the target subsystem
 /// </summary>
 private void HandleRequestPackets(DataPacket dataPacket)
 {
     RaiseRequestPacketReceived(dataPacket);
 }
示例#10
0
 /// <summary>
 /// Transmits the specified packet to the connected host, and waits till a response 
 /// with the specified type is received
 /// </summary>
 /// <param name="packet">A <see cref="DataPacket"/> to transmit</param>
 /// <returns>Returns the specified response</returns>
 public DataPacket TransmitWithResponse(DataPacket packet)
 {
     PacketInfo packetToTransmit = new PacketInfo(packet, true);
     EnqueuePacket(packetToTransmit);
     packetToTransmit.PacketResponseSync.WaitOne();
     return packetToTransmit.ResponsePacket;
 }
示例#11
0
 /// <summary>
 /// Transmits the specified packet to the connected host.
 /// </summary>
 /// <param name="packet">A <see cref="DataPacket"/> to transmit</param>
 public void TransmitWithoutResponse(DataPacket packet)
 {
     PacketInfo packetToTransmit = new PacketInfo(packet, false);
     EnqueuePacket(packetToTransmit);
 }