Initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System.IO;
|
||||
|
||||
namespace WebDesktopBackend.Extentions {
|
||||
public static class DirectoryInfoExtentions {
|
||||
/// <summary>
|
||||
/// Returns the size of the Directory (from: https://stackoverflow.com/a/32364847)
|
||||
/// </summary>
|
||||
/// <param name="directoryInfo">The Directory</param>
|
||||
/// <param name="recursive">When set to true, the Functions includes also Subdirectories</param>
|
||||
/// <returns>The size of the Directory</returns>
|
||||
public static long GetDirectorySize(this DirectoryInfo directoryInfo, bool recursive = true) {
|
||||
var startDirectorySize = default(long);
|
||||
if (directoryInfo == null || !directoryInfo.Exists)
|
||||
return startDirectorySize; //Return 0 while Directory does not exist.
|
||||
|
||||
//Add size of files in the Current Directory to main size.
|
||||
foreach (var fileInfo in directoryInfo.GetFiles())
|
||||
System.Threading.Interlocked.Add(ref startDirectorySize, fileInfo.Length);
|
||||
|
||||
if (recursive) //Loop on Sub Direcotries in the Current Directory and Calculate it's files size.
|
||||
System.Threading.Tasks.Parallel.ForEach(directoryInfo.GetDirectories(), (subDirectory) =>
|
||||
System.Threading.Interlocked.Add(ref startDirectorySize, GetDirectorySize(subDirectory, recursive)));
|
||||
|
||||
return startDirectorySize; //Return full Size of this Directory.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WebDesktopBackend.Extentions {
|
||||
public static class WebSocketExtentions {
|
||||
|
||||
public static async Task SendMessage(this WebSocket socket, string message, Encoding encoding = null) {
|
||||
encoding ??= Encoding.Default;
|
||||
await socket.SendAsync(new ArraySegment<byte>(encoding.GetBytes(message)), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
public static async Task<string> RecieveMessage(this WebSocket socket, Encoding encoding = null) {
|
||||
encoding ??= Encoding.Default;
|
||||
byte[] buffer = new byte[1024 * 4];
|
||||
var result = await socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
return encoding.GetString(new ArraySegment<byte>(buffer, 0, result.Count));
|
||||
}
|
||||
|
||||
public static async Task<WebSocket> ConnectAsync(this ClientWebSocket socket, Uri endpoint) {
|
||||
await socket.ConnectAsync(endpoint, CancellationToken.None);
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static CancellationTokenSource AddMessageEventHandler(this WebSocket socket, Action<string> handler) {
|
||||
var source = new CancellationTokenSource();
|
||||
|
||||
Task.Run(async () => {
|
||||
while (!socket.CloseStatus.HasValue) {
|
||||
string msg = await socket.RecieveMessage();
|
||||
handler.Invoke(msg);
|
||||
}
|
||||
}, source.Token);
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user