diff --git a/.github/workflows/dotnet-core.yml b/.github/workflows/dotnet-core.yml deleted file mode 100644 index e4f0382..0000000 --- a/.github/workflows/dotnet-core.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: .NET Core - -on: - push: - tags: - - 'v*' - - - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Set env - run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF#refs/*/} - - name: Setup .NET Core - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 5.0.100 - - name: Install dependencies - run: dotnet restore - - name: Build - run: dotnet publish -c Release - - name: Create Release - id: create_release - uses: actions/create-release@v1.1.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - body: automatic release - - name: Upload Release - uses: actions/upload-release-asset@v1.0.2 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./Teleport/bin/Release/net5.0/win-x64/publish/Teleport.exe - asset_name: Teleport.exe - asset_content_type: application diff --git a/Teleport/Program.cs b/Teleport/Program.cs index f3729ce..9ad0de8 100644 --- a/Teleport/Program.cs +++ b/Teleport/Program.cs @@ -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){ @@ -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(); } diff --git a/Teleport/Reciver.cs b/Teleport/Reciver.cs index 0d290ea..664e9e9 100644 --- a/Teleport/Reciver.cs +++ b/Teleport/Reciver.cs @@ -7,7 +7,7 @@ namespace Teleport { - class Reciver + public class Reciver { string filename { get; set; } diff --git a/Teleport/Server.cs b/Teleport/Server.cs index 2d1e49d..cad389d 100644 --- a/Teleport/Server.cs +++ b/Teleport/Server.cs @@ -5,11 +5,21 @@ 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; @@ -17,19 +27,17 @@ class Server 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); @@ -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 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); } }