private void ConnectionOnMessageReceived(object o, byte[] data) { byte[] msg; try { msg = _protocolHelper.DecryptData(data); } catch (Exception e) { AddLog("Message decrypt error!\nException: " + e); return; } var res = _protocol.Parse(msg, out _index, out _resultData); if (res != ProtocolResult.Ack && res != ProtocolResult.None && res != ProtocolResult.AckAck) { AddLog("NACK: " + res); } else if (res == ProtocolResult.Ack) { var cmd = _protocol.CreateAck(); Connection.Send(_protocolHelper.EncryptMessage(cmd)); } }
public void ReadFile() { if (!string.IsNullOrEmpty(SelectedFile)) { var cmd = _protocol.CreateReadFile(SelectedFile); var enc = _protocolHelper.EncryptMessage(cmd); var ackEnc = Connection.SendReceive(enc); if (ackEnc == null) { AddLog("No anwser"); return; } byte idxCheck = 0; List <byte> memory = new List <byte>(); while (true) { var ack = _protocolHelper.DecryptData(ackEnc); var result = _protocol.Parse(ack, out _index, out _resultData); if (result == ProtocolResult.Ack) { if (idxCheck != _index) { AddLog("Read File index error!"); break; } idxCheck++; memory.AddRange(_resultData); cmd = _protocol.CreateAck(); enc = _protocolHelper.EncryptMessage(cmd); ackEnc = Connection.SendReceive(enc); if (ackEnc == null) { AddLog("No anwser"); break; } } else if (result == ProtocolResult.FileEnd) { cmd = _protocol.CreateAck(); enc = _protocolHelper.EncryptMessage(cmd); Connection.Send(enc); var sfd = new SaveFileDialog(); sfd.FileName = SelectedFile; if (sfd.ShowDialog() == true) { File.WriteAllBytes(sfd.FileName, memory.ToArray()); } break; } else { AddLog("Read File failed -> " + result); break; } } } }