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 1 commit
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: 3 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace p2pcopy
{
class Program
{
public static string ReceiverRole = "receiver";
public static string SenderRole = "sender";
Copy link
Contributor

Choose a reason for hiding this comment

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

let's rather use an enum? and then use .ToString()


static void Main(string[] args)
{
CommandLineArguments cla = CommandLineArguments.Parse(args);
Expand Down
10 changes: 10 additions & 0 deletions Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
9 changes: 9 additions & 0 deletions Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down