public void ViewIO() { SparseMemoryStream memStream = new SparseMemoryStream(); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); SparseStream altView = tss.OpenView(); // Check positions are independant tss.Position = 100; Assert.Equal(0, altView.Position); Assert.Equal(100, tss.Position); // Check I/O is synchronous byte[] buffer = new byte[200]; tss.WriteByte(99); altView.Read(buffer, 0, 200); Assert.Equal(99, buffer[100]); // Check positions are updated correctly Assert.Equal(200, altView.Position); Assert.Equal(101, tss.Position); }
// Methods #region FileSend void FileSend() { //This is our progress percent, between 0 and 1 double progressPercentage = 0; //Initialise stream with 1MB byte[] buffer = new byte[1024 * 1024]; ThreadSafeStream dataToSend = new ThreadSafeStream(new System.IO.MemoryStream(buffer)); long totalBytesSent = 0; //We will send in chunks of 50KB int sendInChunksOfNBytes = 50 * 1024; //Get the connection to the target Connection connection = TCPConnection.GetConnection(new ConnectionInfo(this.serverIp, this.serverPort)); do { //Determine the total number of bytes to send //We need to watch out for the end of the buffer when we send less than sendInChunksOfNBytes long bytesToSend = (buffer.Length - totalBytesSent > sendInChunksOfNBytes ? sendInChunksOfNBytes : buffer.Length - totalBytesSent); StreamSendWrapper streamWrapper = new StreamSendWrapper(dataToSend, totalBytesSent, bytesToSend); connection.SendObject("PartitionedSend", streamWrapper); totalBytesSent += bytesToSend; progressPercentage = ((double)totalBytesSent / buffer.Length); }while (totalBytesSent < buffer.Length); }
/// <summary> /// 傳送檔案 /// </summary> /// <param name="Token">權杖</param> /// <param name="IPaddress">IP位址</param> /// <param name="Port">Port號</param> /// <param name="FilePath">檔案路徑</param> public void FileFunction(string Token, string IPaddress, int Port, string FilePath) { try { FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); DataTable dataTable = new DataTable(); bool IsFirst = true; string strline = null; while ((strline = streamReader.ReadLine()) != null) { if (IsFirst) { try { ThreadSafeStream threadSafeStream = new ThreadSafeStream(fileStream); StreamSendWrapper sendWrapper = new StreamSendWrapper(threadSafeStream, 0, fileStream.Length); NetworkComms.SendObject(Token, IPaddress, Port, sendWrapper); fileStream.Close(); } catch (ArgumentException ex) { Log.Error("網路通訊異常請檢查", ex); ErrorStr = "網路通訊異常請檢查"; } catch (ConnectionSetupException ex) { Log.Error("網路通訊異常請檢查", ex); ErrorStr = "網路通訊異常請檢查"; } catch (ConnectionSendTimeoutException ex) { Log.Error("接收端程式異常", ex); ErrorStr = "接收端程式異常"; } catch (Exception ex) { Log.Error(ex, "傳送檔案失敗"); } } break; } } catch (FileNotFoundException ex) { Log.Error("搜尋不到或未有檔案產生", ex); ErrorStr = "搜尋不到或未有檔案產生"; } catch (ObjectDisposedException ex) { Log.Error("檔案刪除後未有檔案產生", ex); ErrorStr = "檔案刪除後未有檔案產生"; } catch (DirectoryNotFoundException ex) { Log.Error("找不到檔案路徑", ex); ErrorStr = "找不到檔案路徑"; } }
public void CanRead() { SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite); ThreadSafeStream tss = new ThreadSafeStream(memStream); Assert.Equal(true, tss.CanRead); memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.Write); tss = new ThreadSafeStream(memStream); Assert.Equal(false, tss.CanRead); }
void OpenFileSend() { long totalBytesSent = 0; //Get the connection to the target Connection connection = TCPConnection.GetConnection(new ConnectionInfo(this.serverIp, this.serverPort)); OpenFileDialog fd = new OpenFileDialog(); if (fd.ShowDialog() == DialogResult.OK) { string filePath = fd.FileName; var fileName = System.IO.Path.GetFileName(filePath); using (var file = new FileStream(filePath, FileMode.Open)) { using (var safeStream = new ThreadSafeStream(file)) { var sendChunkSizeBytes = (long)(file.Length / 20.0) + 1; //Limit send chunk size to 500MB const long maxChunkSizeBytes = 500L * 1024L * 1024L; if (sendChunkSizeBytes > maxChunkSizeBytes) { sendChunkSizeBytes = maxChunkSizeBytes; } totalBytesSent = 0; do { //Check the number of bytes to send as the last one may be smaller var bytesToSend = (totalBytesSent + sendChunkSizeBytes < file.Length ? sendChunkSizeBytes : file.Length - totalBytesSent); //Wrap the threadSafeStream in a StreamSendWrapper so that we can get NetworkComms.Net //to only send part of the stream. using (var streamWrapper = new StreamSendWrapper(safeStream, totalBytesSent, bytesToSend)) { //We want to record the packetSequenceNumber long packetSequenceNumber; connection.SendObject("PartitionedSend", streamWrapper); //Send the select data //connection.SendObject("PartialFileData", streamWrapper, NetworkComms.DefaultSendReceiveOptions, out packetSequenceNumber); //Send the associated SendInfo for this send so that the remote can correctly rebuild the data //connection.SendObject("PartialFileDataInfo", new SendInfo(fileName, file.Length, totalBytesSent, packetSequenceNumber)); totalBytesSent += bytesToSend; } } while (totalBytesSent < file.Length); } } } }
/// <summary> /// Create a new stream wrapper /// </summary> /// <param name="stream">The underlying stream</param> /// <param name="start">The start position from where to read data</param> /// <param name="length">The length to read</param> public StreamSendWrapper(ThreadSafeStream stream, long start, long length) { if (start < 0) { throw new Exception("Provided start value cannot be less than 0."); } if (length < 0) { throw new Exception("Provided length value cannot be less than 0."); } this.ThreadSafeStream = stream; this.Start = start; this.Length = length; }
public void Seek() { SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); tss.Seek(10, SeekOrigin.Begin); Assert.Equal(10, tss.Position); tss.Seek(10, SeekOrigin.Current); Assert.Equal(20, tss.Position); tss.Seek(-10, SeekOrigin.End); Assert.Equal(1014, tss.Position); }
public void DisposeView() { SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); SparseStream altView = tss.OpenView(); altView.Dispose(); tss.ReadByte(); SparseStream altView2 = tss.OpenView(); altView2.ReadByte(); }
public void ChangeLengthFails() { SparseMemoryStream memStream = new SparseMemoryStream(); memStream.SetLength(2); ThreadSafeStream tss = new ThreadSafeStream(memStream); Assert.Equal(2, tss.Length); try { tss.SetLength(10); Assert.True(false, "SetLength should fail"); } catch (NotSupportedException) { } }
public void Dispose_StopsView() { SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); SparseStream altView = tss.OpenView(); tss.Dispose(); try { altView.ReadByte(); Assert.True(false, "Disposed stream didn't stop view"); } catch (ObjectDisposedException) { } }
/// <summary> /// Return the MD5 for the specific part of the stream only. /// </summary> /// <returns></returns> public string MD5CheckSum() { using (MemoryStream ms = new MemoryStream()) { ThreadSafeStream.CopyTo(ms, Start, Length, 8000); #if NETFX_CORE var alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5); var buffer = (new Windows.Storage.Streams.DataReader(ms.AsInputStream())).ReadBuffer((uint)ms.Length); var hashedData = alg.HashData(buffer); return(Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashedData).Replace("-", "")); #else #if WINDOWS_PHONE using (var md5 = new Tools.MD5Managed()) { #else using (var md5 = System.Security.Cryptography.MD5.Create()) { #endif return(BitConverter.ToString(md5.ComputeHash(ms)).Replace("-", "")); } #endif } }
public void Extents() { SparseMemoryStream memStream = new SparseMemoryStream(new SparseMemoryBuffer(1), FileAccess.ReadWrite); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); SparseStream altView = tss.OpenView(); tss.Position = 100; tss.WriteByte(99); List <StreamExtent> extents = new List <StreamExtent>(altView.Extents); Assert.Equal(1, extents.Count); Assert.Equal(100, extents[0].Start); Assert.Equal(1, extents[0].Length); extents = new List <StreamExtent>(altView.GetExtentsInRange(10, 300)); Assert.Equal(1, extents.Count); Assert.Equal(100, extents[0].Start); Assert.Equal(1, extents[0].Length); }
/// <summary> /// Dispose the internal ThreadSafeStream /// </summary> public void Dispose() { ThreadSafeStream.Dispose(); }
/// <summary> /// Create a new stream wrapper and set Start and Length to encompass the entire Stream /// </summary> /// <param name="stream">The underlying stream</param> public StreamSendWrapper(ThreadSafeStream stream) { this.ThreadSafeStream = stream; this.Start = 0; this.Length = stream.Length; }
public void OpenView() { ThreadSafeStream tss = new ThreadSafeStream(SparseStream.FromStream(Stream.Null, Ownership.None)); SparseStream view = tss.OpenView(); }
/// <summary> /// Create a new stream wrapper /// </summary> /// <param name="stream">The underlying stream</param> /// <param name="start">The start position from where to read data</param> /// <param name="length">The length to read</param> public StreamSendWrapper(ThreadSafeStream stream, long start, long length) { if (start < 0) throw new Exception("Provided start value cannot be less than 0."); if (length < 0) throw new Exception("Provided length value cannot be less than 0."); this.ThreadSafeStream = stream; this.Start = start; this.Length = length; }