-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCPFileTransfer.cs
340 lines (322 loc) · 13.3 KB
/
TCPFileTransfer.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace UChat
{
/// <summary>
/// 文件传输。
/// </summary>
public partial class TCPFileTransfer
{
/// <summary>
/// 表示文件传输任务完成结果。
/// </summary>
#region
public enum TaskCompletionStatus
{
/// <summary>
/// 已经被启动了
/// </summary>
AlreadyRun,
/// <summary>
/// 发送成功
/// </summary>
Success,
/// <summary>
/// 被我方取消
/// </summary>
HostCancel,
/// <summary>
/// 被对方取消
/// </summary>
OppositeCancel,
/// <summary>
/// 未知错误
/// </summary>
Error
}
#endregion
/// <summary>
/// 在端口 50024 上发送文件。
/// </summary>
public class FileSender
{
/// <summary>
/// 在端口 50024 上发送文件。
/// </summary>
public FileSender()//构造函数
{
}
/// <summary>
/// 在端口 50024 上发送文件。
/// </summary>
/// <param name="filePath">文件的路径</param>
/// <param name="fileByte">文件字节数</param>
/// <param name="receiverIP">接收者的 IP</param>
public FileSender(string filePath, long fileByte, string receiverIP)//构造函数
{
SetParameters(filePath, fileByte, receiverIP);
}
/// <summary>
/// 设置必要的参数。
/// </summary>
/// <param name="filePath">文件保存的路径</param>
/// <param name="fileByte">文件字节数</param>
/// <param name="receiverIP">文件接受者的 IP</param>
public void SetParameters(string filePath, long fileByte, string receiverIP)
{
FilePath = filePath;
FileByte = fileByte;
RemoteIP = receiverIP;
}
/// <summary>
/// 是否已经启动
/// </summary>
bool isAlreadyStart = false;
/// <summary>
/// 数据片缓存
/// </summary>
byte[] fragmentBuffer = new byte[14000];
/// <summary>
/// 发送的文件路径。
/// </summary>
string FilePath { get; set; }
long FileByte { get; set; }
string RemoteIP { get; set; }
/// <summary>
/// 指示对方是否结束。
/// </summary>
bool CancelByOpposite = false;
/// <summary>
/// 指示我方是否结束。
/// </summary>
bool CancelByHost = false;
/// <summary>
/// 已经发送的百分比。范围是 0~100。
/// </summary>
public int Percentage { get; private set; } = 0;
/// <summary>
/// 监听控制信息
/// </summary>
TcpListener signalListener = new TcpListener(IPAddress.Any, 50023);
/// <summary>
/// 处理收到的取消控制消息。
/// </summary>
/// <param name="iar"></param>
public void DoAcceptTcpClient(IAsyncResult iar)
{
//还原原始的TcpListner对象
TcpListener listener = (TcpListener)iar.AsyncState;
string message = "";
try
{
//完成连接的动作,并返回新的TcpClient
TcpClient client = listener.EndAcceptTcpClient(iar);
using (client)
{
byte[] buffer = new byte[client.ReceiveBufferSize];//缓冲字节数组
NetworkStream clientStream = client.GetStream();
using (clientStream)
{
clientStream.Read(buffer, 0, buffer.Length);
message = Encoding.UTF8.GetString(buffer).Trim('\0');//收到的信息
clientStream.Close();
}
client.Close();
if (message == "CANCEL")//对面发来的取消请求
{
CancelByOpposite = true;
}
}
}
catch //(Exception e)
{
//MessageBox.Show(e.ToString());
}
finally
{
listener.Stop();
}
}
/// <summary>
/// 开始发送文件。返回一个 TaskCompletionStatus 值,指示是文件传输结果。
/// </summary>
/// <param name="percentage">将文件传输任务进度以 0-100 的数字传递出来到这个数。</param>
/// <returns>返回一个值,指示完成文件传输的结果。</returns>
public TaskCompletionStatus Start(ref int percentage)
{
if (isAlreadyStart == false)//没有启动
{
isAlreadyStart = true;
TCP tCP = new TCP();
TcpListener cancelSignalListener = new TcpListener(IPAddress.Any, 50020);
cancelSignalListener.Start();
//异步接受连接请求
cancelSignalListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClient), cancelSignalListener);
int fragmentBufferLength = fragmentBuffer.Length;
long totalindex = FileByte / fragmentBufferLength;//文件的总块数
CommonFoundations.FileTransferTempData.TotalBlocks = totalindex;
long index = 0;
//int max = 0;
signalListener.Start();//开始监听控制信息;
TcpClient signalClient = signalListener.AcceptTcpClient();//接收到控制信号连接请求,即允许建立文件传输连接
NetworkStream signalStream = signalClient.GetStream();
Thread.Sleep(50);//等待一点时间,让对面建立监听
TcpClient client = new TcpClient(RemoteIP, 50024);//初始化一个对象,并连接到目标主机(因此不需要 connect 方法)
client.NoDelay = true;//禁用 Nagle 算法,使数据包立即发出
try
{
FileStream fStream = File.OpenRead(FilePath);//打开文件流
NetworkStream sendStream = client.GetStream();
using (fStream)
{
using (client)
{
using (sendStream)
{
while ((fragmentBufferLength = fStream.Read(fragmentBuffer, 0, fragmentBufferLength)) > 0)//blockBufferLength为0时,即文件流到达结尾
{
sendStream.Write(fragmentBuffer, 0, fragmentBufferLength);
sendStream.Flush();
CommonFoundations.FileTransferTempData.CurrentBlocks = index;
BlockConfirmationReceive(ref signalStream, signalClient.ReceiveBufferSize); //这是一个阻塞方法,只有收到确认后才继续传下一个数据片
index++;
Percentage = (int)(((double)index / totalindex) * 100);//计算传输百分比
percentage = Percentage;
//中断传输
#region
if (CommonFoundations.FileTransferTempData.CancelFTR == true)
{
CancelByHost = true;
}
if (CancelByHost == true || CancelByOpposite == true)
{
if (CancelByHost == true)//自己结束的
{
tCP.TCPMessageSender(RemoteIP, "CANCEL", 50020);//发送取消消息监听
return TaskCompletionStatus.HostCancel;
}
if (CancelByOpposite == true)//对面结束的,表明已经收到了 取消消息
{
return TaskCompletionStatus.OppositeCancel;
}
}
#endregion
}
}
}
}
}
//catch
#region
catch (ArgumentNullException e)
{
MessageBox.Show(e.ToString());
}
catch (ArgumentOutOfRangeException e)
{
MessageBox.Show(e.ToString());
}
catch (ArgumentException e)
{
MessageBox.Show(e.ToString());
}
catch (NotSupportedException e)
{
MessageBox.Show(e.ToString());
}
catch (FileNotFoundException e)///File.OpenRead
{
MessageBox.Show(e.ToString());
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
}
catch (ObjectDisposedException e)
{
MessageBox.Show(e.ToString());
}
catch (InvalidOperationException e)
{
MessageBox.Show(e.ToString());
}
catch (SocketException e)
{
MessageBox.Show(e.ToString());
}
catch (Exception e)
{
MessageBox.Show("未知错误!\r\n" + e.ToString());
}
#endregion
finally//收尾工作
{
cancelSignalListener.Stop();//结束取消控制信息监听器
signalStream.Dispose();
signalStream.Close();
signalClient.Close();
client.Close();//关闭客户端
signalListener.Stop();
}
isAlreadyStart = false;
return TaskCompletionStatus.Success;//完美结束
}
else
{
return TaskCompletionStatus.AlreadyRun;
}
}
/// <summary>
/// 停止发送文件。
/// </summary>
public void Abort()
{
if (isAlreadyStart == true)//在运行
{
CancelByHost = true;
isAlreadyStart = false;
}
else
{
}
}
/// <summary>
/// 如果收到对方发送的"已经处理好之前发送的 2M 块"信息,返回给上一层。在 50023 端口通讯。
/// </summary>
/// <returns></returns>
private bool BlockConfirmationReceive(ref NetworkStream signalStream, int receiveBufferSize)
{
while (true)
{
try
{
string message = "";
byte[] buffer = new byte[receiveBufferSize];//缓冲字节数组
signalStream.Read(buffer, 0, buffer.Length);
message = Encoding.UTF8.GetString(buffer).Trim('\0');//收到的信息
if (message == "OK")
{
return false;
}
if (message == "OVER")
{
return true;
}
else
{
return true;
}
}
catch
{
}
}
}
}
}
}