Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peers now transmit their role before sending file #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.IO;
using System;
using System;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Net;
Expand Down Expand Up @@ -475,7 +475,6 @@ static P2pEndPoint GetExternalEndPoint(Socket socket)
return null;
}


static int SleepTime(DateTime now)
{
List<int> seconds = new List<int>() {10, 20, 30, 40, 50, 60};
Expand Down
17 changes: 13 additions & 4 deletions Receiver.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you exit the function like this, this won't translate into a non-zero exitcode for the program

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call directly Environment.Exit? I know it doesn't seem very clean, but it would be more correct.

}

string fileName = reader.ReadString();
long size = reader.ReadInt64();

Expand Down
8 changes: 8 additions & 0 deletions Roles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace p2pcopy
{
enum Roles
{
Receiver,
Sender,
}
}
13 changes: 13 additions & 0 deletions Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion p2pcopy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Receiver.cs" />
<Compile Include="Roles.cs" />
<Compile Include="Sender.cs" />
<Compile Include="SizeConverter.cs" />
<Compile Include="StunClient.cs" />
Expand All @@ -64,4 +65,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>