public static ushort Generate(byte[] data) { EIGen3CRC16 crc = new EIGen3CRC16(); crc.Update(data, 0, data.Length); return(crc.Value); }
public static ushort CRCMsg(byte[] msg, bool update = true) { EIGen3CRC16 crc = new EIGen3CRC16(); crc.Update(msg, 0, 62); if (update) { ByteUtil.ToBytesLE(crc.Value, msg, 62); } return(crc.Value); }
private async Task ISendMsg(byte[] msg, bool crc = true) { if (crc) { EIGen3CRC16.CRCMsg(msg); } m_log.Trace("<SND> '{0}'", BitConverter.ToString(msg).Replace('-', ' ')); byte[] buf = new byte[MsgBufSize]; if (MsgBufSize == 65 && msg.Length != 65) { Buffer.BlockCopy(msg, 0, buf, 1, 64); } else { msg.CopyTo(buf, 0); } await m_stream.WriteAsync(buf, 0, MsgBufSize); }
private async Task <IncomingMsgIDs> IReadMsg() { byte[] buf = new byte[MsgBufSize]; m_stream.ReadTimeout = Timeout.Infinite; await m_stream.ReadAsync(buf, 0, MsgBufSize); // Lop off that pesky leading zero... byte[] msg = new byte[64]; Buffer.BlockCopy(buf, MsgBufSize == 65 ? 1 : 0, msg, 0, 64); m_log.Trace("<RCV> '{0}'", BitConverter.ToString(msg).Replace('-', ' ')); // Check the CRC of the incoming message for kicks ushort softCRC = EIGen3CRC16.CRCMsg(msg, false); ushort rcvrCRC = ByteUtil.FromBytesLE(msg, 62); if (softCRC != rcvrCRC) { m_log.Fatal("Incoming message CRC fail [MINE:{0:X}] [THEIRS:{1:X}]", softCRC, rcvrCRC); return(IncomingMsgIDs.INVALID); } // Incoming message handlers... IncomingMsgIDs id = (IncomingMsgIDs)msg[0]; switch (id) { case IncomingMsgIDs.Acknowledgement: IProcessAck(msg); // FIXME: do something with return value??? break; case IncomingMsgIDs.Join: IProcessJoins(msg); break; } return(id); }