public bool Upload(string path) { TransferState state = TransferState.NAK; FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); long bytesRemaining = fileStream.Length; Comms.com.EventMode = false; Comms.com.PurgeQueue(); ClearPayload(); if (TransferInitalPacket(path, fileStream) == TransferState.ACK) { do { packetNum++; if (bytesRemaining < 1024) { ClearPayload(); } bytes = fileStream.Read(payload, 0, 1024); bytesRemaining -= bytes; DataTransferred?.Invoke(fileStream.Length, fileStream.Length - bytesRemaining); state = TransferPacket(bytes <= 128 ? 128 : 1024); } while (bytesRemaining > 0 && state == TransferState.ACK); if (state == TransferState.ACK) { hdr[0] = EOT; Comms.com.WriteBytes(hdr, 1); } } Thread.Sleep(100); Comms.com.PurgeQueue(); Comms.com.EventMode = true; return(state == TransferState.ACK); }
protected virtual void OnDataTransferred(int bytesTransferred) { DataTransferred?.Invoke(this, new DataTransferredArgs(bytesTransferred)); }