forked from psantosl/p2pcopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender.cs
65 lines (49 loc) · 2 KB
/
Sender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.IO;
using UdtSharp;
namespace p2pcopy
{
static class Sender
{
static internal void Run(UdtSocket conn, string file, bool bVerbose)
{
int ini = Environment.TickCount;
using (var netStream = new UdtNetworkStream(conn))
using (var writer = new BinaryWriter(netStream))
using (var reader = new BinaryReader(netStream))
using (var fileReader = new FileStream(file, FileMode.Open, FileAccess.Read))
{
long fileSize = new FileInfo(file).Length;
writer.Write(Path.GetFileName(file));
writer.Write(fileSize);
byte[] buffer = new byte[512 * 1024];
long pos = 0;
int i = 0;
ConsoleProgress.Draw(i++, pos, fileSize, ini, Console.WindowWidth / 3);
while (pos < fileSize)
{
int toSend = buffer.Length < (fileSize - pos)
? buffer.Length
: (int)(fileSize - pos);
fileReader.Read(buffer, 0, toSend);
int iteration = Environment.TickCount;
writer.Write(toSend);
conn.Send(buffer, 0, toSend);
if (!reader.ReadBoolean())
{
Console.WriteLine("Error in transmission");
return;
}
pos += toSend;
ConsoleProgress.Draw(i++, pos, fileSize, ini, Console.WindowWidth / 3);
if (bVerbose)
{
Console.WriteLine();
Console.WriteLine("Current: {0} / s",
SizeConverter.ConvertToSizeString(toSend / (Environment.TickCount - iteration) * 1000));
}
}
}
}
}
}