private static void OnAtomicWriteFileRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, bool is_stream, BacnetObjectId object_id, int position, uint block_count, byte[][] blocks, int[] counts, BacnetMaxSegments max_segments) { lock (m_lockObject) { try { if (object_id.Type != BacnetObjectTypes.OBJECT_FILE) { throw new Exception("File Reading on non file objects ... bah!"); } else if (object_id.Instance != 0) { throw new Exception("Don't know this file"); } //this is a test file for performance measuring //don't do anything with the content //adjust size though int filesize = m_storage.ReadPropertyValue(object_id, BacnetPropertyIds.PROP_FILE_SIZE); int new_filesize = position + counts[0]; if (new_filesize > filesize) { m_storage.WritePropertyValue(object_id, BacnetPropertyIds.PROP_FILE_SIZE, new_filesize); } if (counts[0] == 0) { m_storage.WritePropertyValue(object_id, BacnetPropertyIds.PROP_FILE_SIZE, 0); //clear file } //send confirm HandleSegmentationResponse(sender, adr, invoke_id, max_segments, (seg) => { sender.WriteFileResponse(adr, invoke_id, seg, position); }); } catch (Exception) { sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_WRITE_FILE, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER); } } }
// Here something could be done to avoid a to big fill to be written on the disk private static void handler_OnAtomicWriteFileRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, bool is_stream, BacnetObjectId object_id, int position, uint block_count, byte[][] blocks, int[] counts, BacnetMaxSegments max_segments) { lock (device) { BaCSharpObject File = device.FindBacnetObject(object_id); if (File is BacnetFile) { try { BacnetFile f = (BacnetFile)File; if (f.PROP_READ_ONLY == false) { int currentposition = position; for (int i = 0; i < block_count; i++) { f.WriteFileBlock(blocks[i], currentposition, counts[i]); currentposition += counts[i]; } sender.WriteFileResponse(adr, invoke_id, sender.GetSegmentBuffer(max_segments), position); } else { sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_WRITE_FILE, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED); } } catch (Exception) { sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_WRITE_FILE, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER); } } else { sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_WRITE_FILE, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER); } } }