forked from psantosl/p2pcopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sender.cs
166 lines (136 loc) · 6.67 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PseudoTcp;
using System.Threading;
using System.Collections;
namespace p2pcopy
{
static class Sender
{
static internal void Run(PseudoTcpSocket conn, string file, bool bVerbose)
{
int ini = Environment.TickCount;
Queue notifyClockQueue = new Queue();
using (FileStream fileReader = new FileStream(file, FileMode.Open, FileAccess.Read))
{
long fileSize = new FileInfo(file).Length;
byte[] ackBuffer = new byte[1];
PLog.DEBUG ("Sending filename {0}", Path.GetFileName (file));
byte[] fileNameBytes = Encoding.UTF8.GetBytes (Path.GetFileName (file));
byte[] fileNameBytesLength = BitConverter.GetBytes (fileNameBytes.Length);
PLog.DEBUG ("Length of file name in bytes={0}", fileNameBytesLength);
SyncPseudoTcpSocket.Send(conn, fileNameBytesLength, (uint)fileNameBytesLength.Length);
SyncPseudoTcpSocket.Recv (conn, ackBuffer, 1);
PLog.DEBUG ("Sent filename length, ack={0}", ackBuffer [0]);
SyncPseudoTcpSocket.Send(conn, fileNameBytes, (uint)fileNameBytes.Length);
SyncPseudoTcpSocket.Recv (conn, ackBuffer, 1);
PLog.DEBUG ("Sent filename bytes {0}={1}, ack={2}",
fileNameBytes, BitConverter.ToString(fileNameBytes), ackBuffer[0]);
SyncPseudoTcpSocket.Send(conn, BitConverter.GetBytes(fileSize), 8);
SyncPseudoTcpSocket.Recv (conn, ackBuffer, 1);
PLog.DEBUG ("Sent file size, ack={0}", ackBuffer [0]);
byte[] buffer = new byte[512 * 1024];
long pos = 0;
int i = 0;
ConsoleProgress.Draw(i++, pos, fileSize, ini, Console.WindowWidth / 3);
while (pos < fileSize)
{
ProcessNotifyClockQueue (conn, notifyClockQueue);
int toSend = buffer.Length < (fileSize - pos)
? buffer.Length
: (int)(fileSize - pos);
PLog.DEBUG ("File reading: pos={0}, fileSize={1}, toSend={2}", pos, fileSize, toSend);
fileReader.Read(buffer, 0, toSend);
int iteration = Environment.TickCount;
int sent;
int totalSent = 0;
int fragmentSize = toSend;
while (totalSent < toSend) {
sent = SendFragment (conn, buffer, fragmentSize, notifyClockQueue);
totalSent += sent;
PLog.DEBUG ("totalSent={0} sent={1} fragmentSize={2}",totalSent, sent, fragmentSize);
if (sent < fragmentSize) {
byte[] buffer2 = new byte[fragmentSize-sent];
Buffer.BlockCopy(buffer, sent, buffer2, 0, buffer2.Length);
fragmentSize = buffer2.Length;
buffer = buffer2;
}
}
PLog.DEBUG("finished sending toSend={0}", toSend);
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));
//Console.WriteLine("BandwidthMbps {0} mbps.", conn.GetPerformanceInfo().Probe.BandwidthMbps);
//Console.WriteLine("RoundtripTime {0}.", conn.GetPerformanceInfo().Probe.RoundtripTime);
//Console.WriteLine("SendMbps {0}.", conn.GetPerformanceInfo().Local.SendMbps);
//Console.WriteLine("ReceiveMbps {0}.", conn.GetPerformanceInfo().Local.ReceiveMbps);
}
}
}
Console.WriteLine ();
while (conn.priv.sbuf.data_length != 0)
{
Console.WriteLine ("Waiting for buffered data to finish sending...");
for (int i = 0; i < 20; i++)
{
conn.NotifyClock();
Thread.Sleep (50);
}
}
Console.WriteLine ();
Console.WriteLine ("Done!");
}
static void ProcessNotifyClockQueue(PseudoTcpSocket conn, Queue notifyClockQueue)
{
PLog.DEBUG ("Entering ProcessNotifyClockQueue with queue size={0}", notifyClockQueue.Count);
if (notifyClockQueue.Count != 0) {
PLog.DEBUG ("...and head timestamp={0}, current time={1}", notifyClockQueue.Peek (), Environment.TickCount);
}
if (notifyClockQueue.Count == 0)
{
UdpCallbacks.AdjustClock (conn, notifyClockQueue);
return;
}
bool keepChecking = true;
while (keepChecking && notifyClockQueue.Count > 0)
{
int iTimestamp = (int)notifyClockQueue.Peek();
if (Environment.TickCount > iTimestamp)
{
SyncPseudoTcpSocket.NotifyClock (conn);
notifyClockQueue.Dequeue ();
}
else
{
keepChecking = false;
}
}
}
static int SendFragment(PseudoTcpSocket conn, byte[] buffer, int fragmentSize, Queue notifyClockQueue)
{
int sent;
do {
PLog.DEBUG ("Trying to send {0} bytes", fragmentSize);
sent = SyncPseudoTcpSocket.Send (conn, buffer, (uint)fragmentSize);
if (sent==-1) {
PLog.DEBUG("sent==-1 so processing notifyClockQueue");
ProcessNotifyClockQueue(conn, notifyClockQueue);
}
else {
UdpCallbacks.AdjustClock(conn, notifyClockQueue);
}
UdpCallbacks.PollingSleep (sent, -1);
} while (sent == -1);
PLog.DEBUG ("Tried sending fragment sized {0} with result {1}", fragmentSize, sent);
return sent;
}
}
}