-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathReceiver.cs
64 lines (49 loc) · 1.72 KB
/
Receiver.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
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
{
static internal void Run(UdtSocket conn)
{
int ini = Environment.TickCount;
using (var netStream = new UdtNetworkStream(conn))
using (var writer = new BinaryWriter(netStream))
using (var reader = new BinaryReader(netStream))
{
string fileName = reader.ReadString();
long size = reader.ReadInt64();
byte[] buffer = new byte[4 * 1024 * 1024];
int i = 0;
ConsoleProgress.Draw(i++, 0, size, ini, Console.WindowWidth / 2);
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
long read = 0;
while (read < size)
{
int toRecv = reader.ReadInt32();
ReadFragment(reader, toRecv, buffer);
fileStream.Write(buffer, 0, toRecv);
read += toRecv;
writer.Write(true);
ConsoleProgress.Draw(i++, read, size, ini, Console.WindowWidth / 2);
}
}
}
}
static int ReadFragment(BinaryReader reader, int size, byte[] buffer)
{
int read = 0;
while (read < size)
{
read += reader.Read(buffer, read, size - read);
}
return read;
}
}
}