diff --git a/Program.cs b/Program.cs index 30ce55c..87c935a 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; @@ -475,7 +475,6 @@ static P2pEndPoint GetExternalEndPoint(Socket socket) return null; } - static int SleepTime(DateTime now) { List seconds = new List() {10, 20, 30, 40, 50, 60}; diff --git a/Receiver.cs b/Receiver.cs index 7a1ef20..f49cc76 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,6 +16,17 @@ 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 opposite role + writer.Write(myRole.ToString()); + string role = reader.ReadString(); + + if (role == myRole.ToString()) + { + Console.Error.WriteLine("Peers can't have the same role."); + Environment.Exit(1); + return; + } + string fileName = reader.ReadString(); long size = reader.ReadInt64(); 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 d052f3b..3f6b5da 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,6 +17,17 @@ 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 opposite role + writer.Write(myRole.ToString()); + string role = reader.ReadString(); + + if (role == myRole.ToString()) + { + Console.Error.WriteLine("Peers can't have the same role."); + Environment.Exit(1); + return; + } + long fileSize = new FileInfo(file).Length; writer.Write(Path.GetFileName(file)); 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