Skip to content

Commit

Permalink
CLI capabilites moved from Server class
Browse files Browse the repository at this point in the history
to Program class
  • Loading branch information
Az107 committed Nov 20, 2020
1 parent 3f951e5 commit 3022a11
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 70 deletions.
44 changes: 0 additions & 44 deletions .github/workflows/dotnet-core.yml

This file was deleted.

17 changes: 16 additions & 1 deletion Teleport/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ namespace teleport
{
class Program
{
private static int row = -1;
private static Server server;
private static void updateCli(String clientAddress)
{
//Progressbar pb = new Progressbar("c1", fileSize);
if (row == -1) row = Console.CursorTop;
Console.CursorTop = row;
Console.CursorLeft = 0;
Console.Write(new String(' ', Console.WindowLeft));
Console.CursorLeft = 0;
if (server.Clients == 0) Console.WriteLine("Waiting for Connections...");
else Console.WriteLine($"Active clients [{server.Clients}]");
}
static void Main(string[] args)
{
if (args.Length is 0 or >2){
Expand All @@ -19,7 +32,9 @@ static void Main(string[] args)
}
else
{
Server server = new Server(args[0]);
updateCli("");
server = new Server(args[0]);
server.ClientConnectedEvent += updateCli;
server.Start();
}

Expand Down
2 changes: 1 addition & 1 deletion Teleport/Reciver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Teleport
{
class Reciver
public class Reciver
{
string filename { get; set; }

Expand Down
67 changes: 43 additions & 24 deletions Teleport/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,39 @@
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Threading;

namespace Teleport
{
class Server
public class Server
{

public Exception NoFileException;
public delegate void NewClientd(String ip);
public event NewClientd ClientConnectedEvent;
public event NewClientd ClientDisconnectedEvent;




public String filePath { get; set; }
public String FileName { get; set; }
public bool isAlive = false;
private TcpListener Listener { get; set; }
private long fileSize = -1;
public string hash;
public int Clients = 0;
private int row = -1;

const int chunkSize = 1024 * 1024;
private int Port = 1100;


private CancellationTokenSource cts = new CancellationTokenSource();

private async void ClientHandler(TcpClient client)
{
ClientConnectedEvent?.Invoke(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
FileStream fileStream = File.OpenRead(filePath);
Progressbar pb = new Progressbar("c1", fileSize);
pb.Start();
Clients++;
updateCli();
byte[] name = new byte[1024];
name = Encoding.UTF8.GetBytes($"{FileName}");
client.GetStream().Write(name);
Expand All @@ -41,51 +49,62 @@ private async void ClientHandler(TcpClient client)
{
totalBytesReaded += bytesRead;
client.GetStream().Write(buffer);
pb.Change(totalBytesReaded);
Array.Clear(buffer, 0, buffer.Length);

}
client.Close();
fileStream.Close();
Clients--;
updateCli();
ClientDisconnectedEvent?.Invoke(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
GC.Collect();
GC.WaitForPendingFinalizers();
}

private void updateCli()
{
if (row == -1) row = Console.CursorTop;
Console.CursorTop = row;
Console.CursorLeft = 0;
Console.Write(new String(' ', Console.WindowLeft));
Console.CursorLeft = 0;
if (Clients == 0) Console.WriteLine("Waiting for Connections...");
else Console.WriteLine($"Active clients [{Clients}]");

public void Stop(){
isAlive = false;
while(Clients != 0){
Thread.Sleep(100);
}
cts.Cancel();
}

public void ForceStop(){
isAlive = false;
cts.Cancel(false);
}

public void StartInThread(){
new Thread(new ThreadStart(Start)).Start();
}
public void Start()
{
if (string.IsNullOrEmpty(filePath)) throw NoFileException;
isAlive = true;
Listener.Start();
updateCli();
while (isAlive)
{
Task<TcpClient> clientTask = Listener.AcceptTcpClientAsync();
clientTask.Wait();
Task clientTask2 = new Task(() => ClientHandler(clientTask.Result));
clientTask.Wait(cts.Token);
Task clientTask2 = new Task(() => ClientHandler(clientTask.Result),cts.Token);
clientTask2.Start();

}

}

public Server(string file)
{
public void AddFile(String file){
filePath = file;
FileName = Path.GetFileName(file);
fileSize = (new FileInfo(file)).Length;
Listener = new TcpListener(IPAddress.Any, Port);
isAlive = true;

}


public Server(string file)
{
AddFile(file);
Listener = new TcpListener(Port);

}
}
Expand Down

0 comments on commit 3022a11

Please sign in to comment.