bool IController.QueueOutput(MessageRaw raw) { SendRawBuffer(raw.m_header); if (raw.m_payload != null) { SendRawBuffer(raw.m_payload); } return(true); }
public async Task <bool> QueueOutputAsync(MessageRaw raw) { await SendRawBufferAsync(raw.m_header, new CancellationToken()).ConfigureAwait(false); if (raw.m_payload != null) { await SendRawBufferAsync(raw.m_payload, new CancellationTokenSource().Token).ConfigureAwait(false); } return(true); }
bool _WP.IControllerHostRemote.ProcessMessage(byte[] header, byte[] payload, bool fReply) { _WP.MessageRaw raw = new _WP.MessageRaw(); raw.m_header = header; raw.m_payload = payload; _WP.MessageBase bs = new _WP.MessageBase(); _WP.IncomingMessage msg = new _WP.IncomingMessage(this, raw, bs); bs.m_header = new _WP.Packet(); new _WP.Converter(this.ParentIControllerRemote.Capabilities).Deserialize(bs.m_header, raw.m_header); return(m_owner.ProcessMessage(msg, fReply)); }
bool _WP.IController.QueueOutput(_WP.MessageRaw raw) { _WP.Packet pkt = new _WP.Packet(); new _WP.Converter(this.ParentIController.Capabilities).Deserialize(pkt, raw.m_header); if (m_requests.Count > c_ForcedPurgeLimit) { Purge(c_ForcedPurgeThreshold); } m_requests[pkt.m_seq] = DateTime.Now; return(this.ParentIController.QueueOutput(raw)); }
internal void InitializeForSend(IController parent, Converter converter, uint cmd, uint flags, object payload) { Packet header = parent.NewPacket(); header.m_cmd = cmd; header.m_flags = flags; m_parent = parent; m_raw = new MessageRaw(); m_base = new MessageBase(); m_base.m_header = header; m_base.m_payload = payload; if (payload != null) { m_raw.m_payload = converter.Serialize(payload); m_base.m_header.m_size = (uint)m_raw.m_payload.Length; m_base.m_header.m_crcData = CRC.ComputeCRC(m_raw.m_payload, 0); } }
public IncomingMessage(IController parent, MessageRaw raw, MessageBase messageBase) { m_parent = parent; m_raw = raw; m_base = messageBase; }
/// <summary> /// Essential Rx method. Drives state machine by reading data and processing it. This works in /// conjunction with NotificationThreadWorker [Tx]. /// </summary> internal void Process() { int count; int bytesRead; try { switch (m_state) { case ReceiveState.Initialize: m_rawPos = 0; m_base = new MessageBase(); m_base.m_header = new Packet(); m_raw = new MessageRaw(); m_raw.m_header = m_parent.CreateConverter().Serialize(m_base.m_header); m_state = ReceiveState.WaitingForHeader; goto case ReceiveState.WaitingForHeader; case ReceiveState.WaitingForHeader: count = m_raw.m_header.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_header, m_rawPos, count); m_rawPos += bytesRead; while (m_rawPos > 0) { int flag_Debugger = ValidSignature(m_parent.marker_Debugger); int flag_Packet = ValidSignature(m_parent.marker_Packet); if (flag_Debugger == 1 || flag_Packet == 1) { m_state = ReceiveState.ReadingHeader; goto case ReceiveState.ReadingHeader; } if (flag_Debugger == 0 || flag_Packet == 0) { break; // Partial match. } m_parent.App.SpuriousCharacters(m_raw.m_header, 0, 1); Array.Copy(m_raw.m_header, 1, m_raw.m_header, 0, --m_rawPos); } break; case ReceiveState.ReadingHeader: count = m_raw.m_header.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_header, m_rawPos, count); m_rawPos += bytesRead; if (bytesRead != count) { break; } m_state = ReceiveState.CompleteHeader; goto case ReceiveState.CompleteHeader; case ReceiveState.CompleteHeader: try { m_parent.CreateConverter().Deserialize(m_base.m_header, m_raw.m_header); if (VerifyHeader() == true) { bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; m_base.DumpHeader("Receiving"); if (m_base.m_header.m_size != 0) { m_raw.m_payload = new byte[m_base.m_header.m_size]; //reuse m_rawPos for position in header to read. m_rawPos = 0; m_state = ReceiveState.ReadingPayload; goto case ReceiveState.ReadingPayload; } else { m_state = ReceiveState.CompletePayload; goto case ReceiveState.CompletePayload; } } } catch (ThreadAbortException) { throw; } catch (Exception e) { Console.WriteLine("Fault at payload deserialization:\n\n{0}", e.ToString()); } m_state = ReceiveState.Initialize; if ((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { IncomingMessage.ReplyBadPacket(m_parent, Flags.c_BadHeader); } break; case ReceiveState.ReadingPayload: count = m_raw.m_payload.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_payload, m_rawPos, count); m_rawPos += bytesRead; if (bytesRead != count) { break; } m_state = ReceiveState.CompletePayload; goto case ReceiveState.CompletePayload; case ReceiveState.CompletePayload: if (VerifyPayload() == true) { try { bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; if ((m_base.m_header.m_flags & Flags.c_NACK) != 0) { m_raw.m_payload = null; } m_parent.App.ProcessMessage(this.GetCompleteMessage(), fReply); m_state = ReceiveState.Initialize; return; } catch (ThreadAbortException) { throw; } catch (Exception e) { Console.WriteLine("Fault at payload deserialization:\n\n{0}", e.ToString()); } } m_state = ReceiveState.Initialize; if ((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { IncomingMessage.ReplyBadPacket(m_parent, Flags.c_BadPayload); } break; } } catch { m_state = ReceiveState.Initialize; throw; } }
bool _WP.IControllerHostRemote.ProcessMessage( byte[] header, byte[] payload, bool fReply ) { _WP.MessageRaw raw = new _WP.MessageRaw (); raw.m_header = header; raw.m_payload = payload; _WP.MessageBase bs = new _WP.MessageBase(); _WP.IncomingMessage msg = new _WP.IncomingMessage( this, raw, bs ); bs.m_header = new _WP.Packet(); new _WP.Converter( this.ParentIControllerRemote.Capabilities ).Deserialize( bs.m_header, raw.m_header ); return m_owner.ProcessMessage( msg, fReply ); }
internal void InitializeForSend( IController parent, Converter converter, uint cmd, uint flags, object payload ) { Packet header = parent.NewPacket(); header.m_cmd = cmd; header.m_flags = flags; m_parent = parent; m_raw = new MessageRaw (); m_base = new MessageBase(); m_base.m_header = header; m_base.m_payload = payload; if(payload != null) { m_raw.m_payload = converter.Serialize( payload ); m_base.m_header.m_size = (uint)m_raw.m_payload.Length; m_base.m_header.m_crcData = CRC.ComputeCRC( m_raw.m_payload, 0 ); } }
public IncomingMessage( IController parent, MessageRaw raw, MessageBase messageBase) { m_parent = parent; m_raw = raw; m_base = messageBase; }
bool IController.QueueOutput(MessageRaw raw) { SendRawBuffer(raw.m_header); if (raw.m_payload != null) SendRawBuffer(raw.m_payload); return true; }
/// <summary> /// Essential Rx method. Drives state machine by reading data and processing it. This works in /// conjunction with NotificationThreadWorker [Tx]. /// </summary> internal void Process() { int count; int bytesRead; try { switch(m_state) { case ReceiveState.Initialize: m_rawPos = 0; m_base = new MessageBase(); m_base.m_header = new Packet(); m_raw = new MessageRaw (); m_raw .m_header = m_parent.CreateConverter().Serialize( m_base.m_header ); m_state = ReceiveState.WaitingForHeader; goto case ReceiveState.WaitingForHeader; case ReceiveState.WaitingForHeader: count = m_raw.m_header.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_header, m_rawPos, count); m_rawPos += bytesRead; while(m_rawPos > 0) { int flag_Debugger = ValidSignature( m_parent.marker_Debugger ); int flag_Packet = ValidSignature( m_parent.marker_Packet ); if(flag_Debugger == 1 || flag_Packet == 1) { m_state = ReceiveState.ReadingHeader; goto case ReceiveState.ReadingHeader; } if(flag_Debugger == 0 || flag_Packet == 0) { break; // Partial match. } m_parent.App.SpuriousCharacters( m_raw.m_header, 0, 1 ); Array.Copy( m_raw.m_header, 1, m_raw.m_header, 0, --m_rawPos ); } break; case ReceiveState.ReadingHeader: count = m_raw.m_header.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_header, m_rawPos, count); m_rawPos += bytesRead; if (bytesRead != count) break; m_state = ReceiveState.CompleteHeader; goto case ReceiveState.CompleteHeader; case ReceiveState.CompleteHeader: try { m_parent.CreateConverter().Deserialize( m_base.m_header, m_raw.m_header ); if(VerifyHeader() == true) { bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; m_base.DumpHeader( "Receiving" ); if(m_base.m_header.m_size != 0) { m_raw.m_payload = new byte[m_base.m_header.m_size]; //reuse m_rawPos for position in header to read. m_rawPos = 0; m_state = ReceiveState.ReadingPayload; goto case ReceiveState.ReadingPayload; } else { m_state = ReceiveState.CompletePayload; goto case ReceiveState.CompletePayload; } } } catch(ThreadAbortException) { throw; } catch(Exception e) { Console.WriteLine( "Fault at payload deserialization:\n\n{0}", e.ToString() ); } m_state = ReceiveState.Initialize; if((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { IncomingMessage.ReplyBadPacket( m_parent, Flags.c_BadHeader ); } break; case ReceiveState.ReadingPayload: count = m_raw.m_payload.Length - m_rawPos; bytesRead = m_parent.Read(m_raw.m_payload, m_rawPos, count); m_rawPos += bytesRead; if (bytesRead != count) break; m_state = ReceiveState.CompletePayload; goto case ReceiveState.CompletePayload; case ReceiveState.CompletePayload: if(VerifyPayload() == true) { try { bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; if((m_base.m_header.m_flags & Flags.c_NACK) != 0) { m_raw.m_payload = null; } m_parent.App.ProcessMessage( this.GetCompleteMessage(), fReply ); m_state = ReceiveState.Initialize; return; } catch(ThreadAbortException) { throw; } catch(Exception e) { Console.WriteLine( "Fault at payload deserialization:\n\n{0}", e.ToString() ); } } m_state = ReceiveState.Initialize; if((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { IncomingMessage.ReplyBadPacket( m_parent, Flags.c_BadPayload ); } break; } } catch { m_state = ReceiveState.Initialize; throw; } }
bool _WP.IController.QueueOutput(_WP.MessageRaw raw) { return(m_ctrl.QueueOutput(raw)); }
bool _WP.IController.QueueOutput(_WP.MessageRaw raw) { return(this.ParentIControllerRemote.QueueOutput(raw)); }
/// <summary> /// Essential Rx method. Drives state machine by reading data and processing it. This works in /// conjunction with NotificationThreadWorker [Tx]. /// </summary> internal async Task <IncomingMessage> ProcessAsync(CancellationToken cancellationToken) { int count; int bytesRead; try { switch (m_state) { case ReceiveState.Initialize: if (cancellationToken.IsCancellationRequested) { // cancellation requested return(null); } m_rawPos = 0; m_base = new MessageBase(); m_base.m_header = new Packet(); m_raw = new MessageRaw(); m_raw.m_header = m_parent.CreateConverter().Serialize(m_base.m_header); m_state = ReceiveState.WaitingForHeader; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.WaitingForHeader; case ReceiveState.WaitingForHeader: count = m_raw.m_header.Length - m_rawPos; //Debug.WriteLine("WaitingForHeader"); // need to have a timeout to cancel the read task otherwise it may end up waiting forever for this to return // because we have an external cancellation token and the above timeout cancellation token, need to combine both bytesRead = await m_parent.ReadBufferAsync(m_raw.m_header, m_rawPos, count, request.waitRetryTimeout, cancellationToken.AddTimeout(request.waitRetryTimeout)).ConfigureAwait(false); m_rawPos += bytesRead; // sanity check if (bytesRead != 32) { // doesn't look like a header, better restart m_state = ReceiveState.Initialize; goto case ReceiveState.Initialize; } while (m_rawPos > 0) { int flag_Debugger = ValidSignature(marker_Debugger); int flag_Packet = ValidSignature(marker_Packet); if (flag_Debugger == 1 || flag_Packet == 1) { m_state = ReceiveState.ReadingHeader; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.ReadingHeader; } if (flag_Debugger == 0 || flag_Packet == 0) { break; // Partial match. } m_parent.App.SpuriousCharacters(m_raw.m_header, 0, 1); Array.Copy(m_raw.m_header, 1, m_raw.m_header, 0, --m_rawPos); } break; case ReceiveState.ReadingHeader: count = m_raw.m_header.Length - m_rawPos; //Debug.WriteLine("ReadingHeader"); // need to have a timeout to cancel the read task otherwise it may end up waiting forever for this to return // because we have an external cancellation token and the above timeout cancellation token, need to combine both bytesRead = await m_parent.ReadBufferAsync(m_raw.m_header, m_rawPos, count, request.waitRetryTimeout, cancellationToken.AddTimeout(request.waitRetryTimeout)).ConfigureAwait(false); m_rawPos += bytesRead; if (bytesRead != count) { break; } m_state = ReceiveState.CompleteHeader; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.CompleteHeader; //break; case ReceiveState.CompleteHeader: try { //Debug.WriteLine("CompleteHeader"); m_parent.CreateConverter().Deserialize(m_base.m_header, m_raw.m_header); if (VerifyHeader() == true) { //Debug.WriteLine("CompleteHeader, header OK"); bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; DebuggerEventSource.Log.WireProtocolRxHeader(m_base.m_header.m_crcHeader, m_base.m_header.m_crcData, m_base.m_header.m_cmd, m_base.m_header.m_flags, m_base.m_header.m_seq, m_base.m_header.m_seqReply, m_base.m_header.m_size); if (m_base.m_header.m_size != 0) { m_raw.m_payload = new byte[m_base.m_header.m_size]; //reuse m_rawPos for position in header to read. m_rawPos = 0; m_state = ReceiveState.ReadingPayload; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.ReadingPayload; } else { m_state = ReceiveState.CompletePayload; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.CompletePayload; } } //Debug.WriteLine("CompleteHeader, header not valid"); } //catch (ThreadAbortException) //{ // throw; //} catch (Exception e) { Debug.WriteLine("Fault at payload deserialization:\n\n{0}", e.ToString()); } m_state = ReceiveState.Initialize; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); if ((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { // FIXME // evaluate the purpose of this reply back to the NETMF device, the TinyCRL doesn't seem to have to handle this. In the end it looks like this does have any real purpose and will only be wasting CPU. //await IncomingMessage.ReplyBadPacketAsync(m_parent, Flags.c_BadHeader).ConfigureAwait(false); return(null); } break; case ReceiveState.ReadingPayload: count = m_raw.m_payload.Length - m_rawPos; //Debug.WriteLine("ReadingPayload"); // need to have a timeout to cancel the read task otherwise it may end up waiting forever for this to return // because we have an external cancellation token and the above timeout cancellation token, need to combine both bytesRead = await m_parent.ReadBufferAsync(m_raw.m_payload, m_rawPos, count, request.waitRetryTimeout, cancellationToken.AddTimeout(request.waitRetryTimeout)).ConfigureAwait(false); m_rawPos += bytesRead; if (bytesRead != count) { break; } m_state = ReceiveState.CompletePayload; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); goto case ReceiveState.CompletePayload; case ReceiveState.CompletePayload: //Debug.WriteLine("CompletePayload"); if (VerifyPayload() == true) { //Debug.WriteLine("CompletePayload payload OK"); try { bool fReply = (m_base.m_header.m_flags & Flags.c_Reply) != 0; if ((m_base.m_header.m_flags & Flags.c_NACK) != 0) { m_raw.m_payload = null; } if (await ProcessMessage(this.GetCompleteMessage(), fReply).ConfigureAwait(false)) { DebuggerEventSource.Log.WireProtocolReceiveState(m_state); //Debug.WriteLine("*** leaving reassembler"); return(this.GetCompleteMessage()); } else { // this is not the message we were waiting // FIXME } //m_parent.App.ProcessMessage(this.GetCompleteMessage(), fReply); //m_state = ReceiveState.Initialize; //return; } //catch (ThreadAbortException) //{ // throw; //} catch (Exception e) { Debug.WriteLine("Fault at payload deserialization:\n\n{0}", e.ToString()); } } else { Debug.WriteLine("CompletePayload payload not valid"); } m_state = ReceiveState.Initialize; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); if ((m_base.m_header.m_flags & Flags.c_NonCritical) == 0) { // FIXME // evaluate the purpose of this reply back to the NETMF device, the TinyCRL doesn't seem to have to handle this. In the end it looks like this does have any real purpose and will only be wasting CPU. await IncomingMessage.ReplyBadPacketAsync(m_parent, Flags.c_BadPayload).ConfigureAwait(false); return(null); } break; } } catch { m_state = ReceiveState.Initialize; DebuggerEventSource.Log.WireProtocolReceiveState(m_state); //Debug.WriteLine("*** leaving reassembler"); throw; } //Debug.WriteLine("*** leaving reassembler"); return(null); }