public void SendPacketLength() { if (bootLoaderStatus == BootLoaderStatus.SendLength) { lengthOfFileArr = BitConverter.GetBytes(lengthOfFile); NlengthOfFileArr = BitConverter.GetBytes(lengthOfFile ^ 0xffffff); //3 Bytes PackitLengthOfFile[0] = Convert.ToByte('*'); PackitLengthOfFile[1] = lengthOfFileArr[2]; //MSB of lengthOfFile PackitLengthOfFile[2] = lengthOfFileArr[1]; PackitLengthOfFile[3] = lengthOfFileArr[0]; PackitLengthOfFile[4] = NlengthOfFileArr[2]; //MSB of NlengthOfFileArr PackitLengthOfFile[5] = NlengthOfFileArr[1]; PackitLengthOfFile[6] = NlengthOfFileArr[0]; CRC32Value = calculateCRCBuffer(0xFFFFFFFF, PackitLengthOfFile, 1, 6); CRC32Arr = BitConverter.GetBytes(CRC32Value); NCRC32Arr = BitConverter.GetBytes(CRC32Value ^ 0xffffffff); //4Bytes PackitLengthOfFile[7] = CRC32Arr[3]; //MSB of CRC32Arr PackitLengthOfFile[8] = CRC32Arr[2]; PackitLengthOfFile[9] = CRC32Arr[1]; PackitLengthOfFile[10] = CRC32Arr[0]; PackitLengthOfFile[11] = NCRC32Arr[3];//MSB of NCRC32Arr PackitLengthOfFile[12] = NCRC32Arr[2]; PackitLengthOfFile[13] = NCRC32Arr[1]; PackitLengthOfFile[14] = NCRC32Arr[0]; time = DateTime.Now; txtstatus.Invoke(new MethodInvoker(delegate() { txtstatus.Text += String.Format("\r\n{0}:- Send Packet Length {1} bytes", time.ToLongTimeString(), lengthOfFile); })); serialPortManager.SendMessage(PackitLengthOfFile); } }
private static void HandleReceiveFileRequest(SerialPortManager serialPortManager) { OperationContext.Request.ContentStream = new MemoryStream(OperationContext.Request.Message.ContentBytes); ReceiveFileResponseMessage response = new ReceiveFileResponseMessage { Content = new ReceiveFileResponseContent() { Success = true } }; if (OperationContext.Request.Message.SequenceNumber == 1) { string jsonContent = Encoding.UTF8.GetString( OperationContext.Request.Message.ContentBytes); ReceiveFileRequestMetadata receiveFileRequestMetadata = JsonConvert.DeserializeObject <ReceiveFileRequestMetadata>(jsonContent); string folderPath = ConfigurationManager.AppSettings["FolderPath"]; string filePath = Path.Combine(folderPath, receiveFileRequestMetadata.Filename); if (!File.Exists(filePath)) { response.Content.Success = false; response.Content.Error = "File not found"; //serialPortManager.SendMessage(response); //return; } else { response.FilePath = filePath; } } serialPortManager.SendMessage(response); }
private static void HandleListFilesRequest(SerialPortManager serialPortManager) { GetFilesResponseMessage response = new GetFilesResponseMessage(); serialPortManager.SendMessage(response); }
private static void HandleSendFileRequest(SerialPortManager serialPortManager) { OperationContext.Request.ContentStream = new MemoryStream(OperationContext.Request.Message.ContentBytes); SendFileResponseMessage response = new SendFileResponseMessage { Content = new SendFileResponseContent() { Success = true } }; using (OperationContext.Request.ContentStream) { if (OperationContext.Request.Message.SequenceNumber == 1) { byte[] preambleLengthBytes = new byte[sizeof(int)]; OperationContext.Request.ContentStream.Read( preambleLengthBytes, 0, preambleLengthBytes.Length); int preambleLength = BitConverter.ToInt32(preambleLengthBytes, 0); byte[] preambleBytes = new byte[preambleLength]; OperationContext.Request.ContentStream.Read( preambleBytes, 0, preambleLength); string jsonContent = Encoding.UTF8.GetString(preambleBytes); sendFileRequestMetadata = JsonConvert.DeserializeObject <SendFileRequestMetadata>(jsonContent); string folderPath = ConfigurationManager.AppSettings["FolderPath"]; string filePath = Path.Combine(folderPath, sendFileRequestMetadata.Filename); if (File.Exists(filePath) && !sendFileRequestMetadata.Overwrite) { // TODO: This needs to be returned to the caller //throw new Exception("File exists"); response.Content.Success = false; response.Content.Error = "File exists at destination"; serialPortManager.SendMessage(response); return; } // This is the first message, so we need to create the response stream sendFileRequestFileStream = new FileStream( filePath, FileMode.Create, FileAccess.Write); // Allocate a new buffer to read whatever if remaining in this message byte[] initialFileBuffer = new byte[OperationContext.Request.ContentStream.Length]; int bytesRead = OperationContext.Request.ContentStream.Read( initialFileBuffer, 0, initialFileBuffer.Length); sendFileRequestFileStream.Write(initialFileBuffer, 0, bytesRead); } else { byte[] buffer = new byte[OperationContext.Request.ContentStream.Length]; OperationContext.Request.ContentStream.Read(buffer, 0, buffer.Length); sendFileRequestFileStream.Write(buffer, 0, buffer.Length); } if (OperationContext.Request.Message.SequenceNumber == OperationContext.Request.Message.SequenceLength) { sendFileRequestFileStream.Close(); } } serialPortManager.SendMessage(response); }