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; } }
/// <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; } }
public bool TrySetResponsePacket(DataPacket packet) { lock(this) { if(_packetExpired) return false; _responsePacket = packet; return true; } }
/// <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); } }
/// <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(); } }
public PacketInfo(DataPacket packetToTransmit, bool awaitsResponse) { _packetToTransmit = packetToTransmit; _awaitsResponse = awaitsResponse; }
private void RaiseRequestPacketReceived(DataPacket dataPacket) { if(RequestPacketReceived != null) RequestPacketReceived.BeginInvoke(dataPacket, null, null); }
/// <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(); } } }
/// <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); }
/// <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; }
/// <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); }