Skip to content

Commit

Permalink
Memory usage greatly improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Az107 committed Aug 14, 2020
1 parent 4b2129e commit 2d2655a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Teleport/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace Teleport
{
class Server
{
public string filePath { get; set; }
public FileStream fileStream { get; set; }
public String FileName { get; set; }
public bool isAlive = false;
private TcpListener Listener { get; set; }
private byte[] fileBytes;
public string hash;
private bool lowMemoryMode = false;
public int Clients = 0;
private int row = -1;
const int chunkSize = 1024;




private async void ClientHandler(TcpClient client)
{
Expand All @@ -31,7 +31,13 @@ private async void ClientHandler(TcpClient client)
name = Encoding.UTF8.GetBytes($"{FileName}");
client.GetStream().Write(name);
client.GetStream().Flush();
client.GetStream().Write(fileBytes);
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
//here goes the progressbar
client.GetStream().Write(buffer);
}
client.Close();
Clients--;
updateCli();
Expand All @@ -42,10 +48,10 @@ private void updateCli()
if (row == -1) row = Console.CursorTop;
Console.CursorTop = row;
Console.CursorLeft = 0;
Console.Write(new String(' ',Console.WindowLeft));
Console.Write(new String(' ', Console.WindowLeft));
Console.CursorLeft = 0;
if (Clients == 0) Console.WriteLine("Waiting for Connections...");
else Console.WriteLine($"Active clients [{Clients}]");
else Console.WriteLine($"Active clients [{Clients}]");
}

public void Start()
Expand All @@ -58,7 +64,7 @@ public void Start()
clientTask.Wait();
Task clientTask2 = new Task(() => ClientHandler(clientTask.Result));
clientTask2.Start();

//clientTask.Start();
}

Expand All @@ -70,8 +76,7 @@ public Server(string file)
FileName = Path.GetFileName(file);
Listener = new TcpListener(1100);
isAlive = true;
filePath = file;
fileBytes = File.ReadAllBytes(file);
fileStream = File.OpenRead(file);
Console.WriteLine("OK");

}
Expand Down

0 comments on commit 2d2655a

Please sign in to comment.