public static Packet ReadFrom(Stream stream)
        {
            byte[] header = new byte[HeaderSize];
            FillBuffer(stream, header);

            var packet = new Packet(header);
            byte[] payload = FillBuffer(stream, packet.Payload);

            return packet;
        }
 public Packet Copy()
 {
     Packet copy = new Packet(UID, PayloadSize, FunctionID, SequenceNumber, ResponseExpected, ErrorCode);
     Array.Copy(Payload, copy.Payload, PayloadSize);
     return copy;
 }
 public void HandleRequest(Packet packet)
 {
     RequestQueue.Enqueue(packet);
 }
 protected void OnResponse(Packet response)
 {
     var handler = Response;
     if (handler != null)
     {
         handler(this, new ResponseEventArgs(response));
     }
 }
 private void TryRouteAndDispatchRequest(Packet packet)
 {
     if (packet.UID.Equals(BroadcastUID))
     {
         foreach (Device device in Devices.Values)
         {
             DispatchRequest(packet, device);
         }
     }
     else
     {
         Device device;
         if (Devices.TryGetValue(packet.UID, out device))
         {
             DispatchRequest(packet, device);
         }
         else
         {
             Console.WriteLine("No Device for UID {0}", packet.UID);
         }
     }
 }
 public ResponseEventArgs(Packet response)
 {
     Response = response;
 }
 private void RouteAndDispatchRequest(Packet packet)
 {
     try
     {
         TryRouteAndDispatchRequest(packet);
     }
     catch (Exception e)
     {
         Console.WriteLine("An unexpected and unhandled error occured while handling a request:");
         Console.WriteLine(e);
     }
 }
 private void DispatchRequest(Packet packet, Device device)
 {
     Packet response = device.HandleRequest(packet.Copy());
     if (response != null)
     {
         OnResponse(response);
     }
 }
 public void SendPacket(Packet packet)
 {
     SinkOperation(packet);
 }