From 0fbd72377de679a06fe3b32fa8a6ebaedca57075 Mon Sep 17 00:00:00 2001 From: Bruno Teixeira Freitas Date: Sat, 30 Jan 2021 15:57:52 +0000 Subject: [PATCH 1/4] Peers now transmit their role before sending file --- Program.cs | 3 +++ Receiver.cs | 10 ++++++++++ Sender.cs | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/Program.cs b/Program.cs index 30ce55c..9bfbf18 100644 --- a/Program.cs +++ b/Program.cs @@ -9,6 +9,9 @@ namespace p2pcopy { class Program { + public static string ReceiverRole = "receiver"; + public static string SenderRole = "sender"; + static void Main(string[] args) { CommandLineArguments cla = CommandLineArguments.Parse(args); diff --git a/Receiver.cs b/Receiver.cs index 7a1ef20..0fb4e7b 100644 --- a/Receiver.cs +++ b/Receiver.cs @@ -18,6 +18,16 @@ static internal void Run(UdtSocket conn) using (var writer = new BinaryWriter(netStream)) using (var reader = new BinaryReader(netStream)) { + // transmit your role and check if connected peer has the correct role + writer.Write(Program.ReceiverRole); + string role = reader.ReadString(); + + if (role == Program.ReceiverRole) + { + Console.Error.WriteLine("Peers can't have the same role."); + return; + } + string fileName = reader.ReadString(); long size = reader.ReadInt64(); diff --git a/Sender.cs b/Sender.cs index d052f3b..d7c4284 100644 --- a/Sender.cs +++ b/Sender.cs @@ -15,6 +15,15 @@ static internal void Run(UdtSocket conn, string file, bool bVerbose) using (var reader = new BinaryReader(netStream)) using (var fileReader = new FileStream(file, FileMode.Open, FileAccess.Read)) { + // transmit your role and check if connected peer has the correct role + writer.Write(Program.SenderRole); + string role = reader.ReadString(); + if (role == Program.SenderRole) + { + Console.Error.WriteLine("Peers can't have the same role."); + return; + } + long fileSize = new FileInfo(file).Length; writer.Write(Path.GetFileName(file)); From d9bb6f22c5e5fea12a3e80ef306b18fc89014c9d Mon Sep 17 00:00:00 2001 From: Bruno Teixeira Freitas Date: Wed, 3 Feb 2021 10:50:12 +0000 Subject: [PATCH 2/4] Switch role transmission to enum --- Program.cs | 8 ++------ Receiver.cs | 12 +++++------- Roles.cs | 8 ++++++++ Sender.cs | 9 ++++++--- p2pcopy.csproj | 3 ++- 5 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 Roles.cs diff --git a/Program.cs b/Program.cs index 9bfbf18..31666ac 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,5 @@ using System.IO; -using System; +using System; using System.Net.Sockets; using System.Collections.Generic; using System.Net; @@ -9,9 +9,6 @@ namespace p2pcopy { class Program { - public static string ReceiverRole = "receiver"; - public static string SenderRole = "sender"; - static void Main(string[] args) { CommandLineArguments cla = CommandLineArguments.Parse(args); @@ -478,10 +475,9 @@ static P2pEndPoint GetExternalEndPoint(Socket socket) return null; } - static int SleepTime(DateTime now) { - List seconds = new List() {10, 20, 30, 40, 50, 60}; + List seconds = new List() { 10, 20, 30, 40, 50, 60 }; int next = seconds.Find(x => x > now.Second); diff --git a/Receiver.cs b/Receiver.cs index 0fb4e7b..47a0f1a 100644 --- a/Receiver.cs +++ b/Receiver.cs @@ -1,15 +1,13 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using UdtSharp; namespace p2pcopy { static class Receiver { + const Roles myRole = Roles.Receiver; + static internal void Run(UdtSocket conn) { int ini = Environment.TickCount; @@ -18,11 +16,11 @@ static internal void Run(UdtSocket conn) using (var writer = new BinaryWriter(netStream)) using (var reader = new BinaryReader(netStream)) { - // transmit your role and check if connected peer has the correct role - writer.Write(Program.ReceiverRole); + // transmit your role and check if connected peer has the opposite role + writer.Write(myRole.ToString()); string role = reader.ReadString(); - if (role == Program.ReceiverRole) + if (role == myRole.ToString()) { Console.Error.WriteLine("Peers can't have the same role."); return; diff --git a/Roles.cs b/Roles.cs new file mode 100644 index 0000000..f7ab3a7 --- /dev/null +++ b/Roles.cs @@ -0,0 +1,8 @@ +namespace p2pcopy +{ + enum Roles + { + Receiver, + Sender, + } +} diff --git a/Sender.cs b/Sender.cs index d7c4284..edc1f76 100644 --- a/Sender.cs +++ b/Sender.cs @@ -6,6 +6,8 @@ namespace p2pcopy { static class Sender { + const Roles myRole = Roles.Sender; + static internal void Run(UdtSocket conn, string file, bool bVerbose) { int ini = Environment.TickCount; @@ -15,10 +17,11 @@ static internal void Run(UdtSocket conn, string file, bool bVerbose) using (var reader = new BinaryReader(netStream)) using (var fileReader = new FileStream(file, FileMode.Open, FileAccess.Read)) { - // transmit your role and check if connected peer has the correct role - writer.Write(Program.SenderRole); + // transmit your role and check if connected peer has the opposite role + writer.Write(myRole.ToString()); string role = reader.ReadString(); - if (role == Program.SenderRole) + + if (role == myRole.ToString()) { Console.Error.WriteLine("Peers can't have the same role."); return; diff --git a/p2pcopy.csproj b/p2pcopy.csproj index fc14775..6d062e4 100644 --- a/p2pcopy.csproj +++ b/p2pcopy.csproj @@ -45,6 +45,7 @@ + @@ -64,4 +65,4 @@ --> - + \ No newline at end of file From 656d596d28fd2e53898b8226b18f8a3434499637 Mon Sep 17 00:00:00 2001 From: Bruno Teixeira Freitas Date: Fri, 5 Feb 2021 16:37:22 +0000 Subject: [PATCH 3/4] Revert spacing --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 31666ac..87c935a 100644 --- a/Program.cs +++ b/Program.cs @@ -477,7 +477,7 @@ static P2pEndPoint GetExternalEndPoint(Socket socket) static int SleepTime(DateTime now) { - List seconds = new List() { 10, 20, 30, 40, 50, 60 }; + List seconds = new List() {10, 20, 30, 40, 50, 60}; int next = seconds.Find(x => x > now.Second); From cd2323f617cec5b630baadfa1c70aa964a7cfaeb Mon Sep 17 00:00:00 2001 From: Bruno Teixeira Freitas Date: Mon, 8 Feb 2021 11:44:08 +0000 Subject: [PATCH 4/4] Added Environment.Exit when role check is invalid --- Receiver.cs | 1 + Sender.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/Receiver.cs b/Receiver.cs index 47a0f1a..f49cc76 100644 --- a/Receiver.cs +++ b/Receiver.cs @@ -23,6 +23,7 @@ static internal void Run(UdtSocket conn) if (role == myRole.ToString()) { Console.Error.WriteLine("Peers can't have the same role."); + Environment.Exit(1); return; } diff --git a/Sender.cs b/Sender.cs index edc1f76..3f6b5da 100644 --- a/Sender.cs +++ b/Sender.cs @@ -24,6 +24,7 @@ static internal void Run(UdtSocket conn, string file, bool bVerbose) if (role == myRole.ToString()) { Console.Error.WriteLine("Peers can't have the same role."); + Environment.Exit(1); return; }