AdvancedFileServer.cs
11.2 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace HslCommunication.Enthernet
{
/// <summary>
/// 文件管理类服务器,负责服务器所有分类文件的管理,特点是不支持文件附加数据,但是支持直接访问文件名
/// </summary>
/// <remarks>
/// 本文件的服务器不支持存储文件携带的额外信息,是直接将文件存放在服务器指定目录下的,文件名不更改,特点是服务器查看方便。
/// </remarks>
/// <example>
/// 以下的示例来自Demo项目,创建了一个简单的服务器对象。
/// <code lang="cs" source="TestProject\FileNetServer\FormFileServer.cs" region="Advanced Server" title="AdvancedFileServer示例" />
/// </example>
public class AdvancedFileServer : HslCommunication.Core.Net.NetworkFileServerBase
{
#region Constructor
/// <summary>
/// 实例化一个对象
/// </summary>
public AdvancedFileServer()
{
}
#endregion
#region Override Method
/// <summary>
/// 当接收到了新的请求的时候执行的操作
/// </summary>
/// <param name="socket">异步对象</param>
/// <param name="endPoint">终结点</param>
protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
{
OperateResult result = new OperateResult();
// 获取ip地址
string IpAddress = ((IPEndPoint)(socket.RemoteEndPoint)).Address.ToString();
// 接收操作信息
OperateResult infoResult = ReceiveInformationHead(
socket,
out int customer,
out string fileName,
out string Factory,
out string Group,
out string Identify);
if (!infoResult.IsSuccess)
{
Console.WriteLine(infoResult.ToMessageShowString());
return;
}
string relativeName = ReturnRelativeFileName(Factory, Group, Identify, fileName);
// 操作分流
if (customer == HslProtocol.ProtocolFileDownload)
{
string fullFileName = ReturnAbsoluteFileName(Factory, Group, Identify, fileName);
// 发送文件数据
OperateResult sendFile = SendFileAndCheckReceive(socket, fullFileName, fileName, "", "");
if (!sendFile.IsSuccess)
{
LogNet?.WriteError(ToString(), $"{StringResources.Language.FileDownloadFailed}:{relativeName} ip:{IpAddress} reason:{sendFile.Message}");
return;
}
else
{
socket?.Close();
LogNet?.WriteInfo(ToString(), StringResources.Language.FileDownloadSuccess + ":" + relativeName);
}
}
else if (customer == HslProtocol.ProtocolFileUpload)
{
string tempFileName = FilesDirectoryPathTemp + "\\" + CreateRandomFileName();
string fullFileName = ReturnAbsoluteFileName(Factory, Group, Identify, fileName);
// 上传文件
CheckFolderAndCreate();
// 创建新的文件夹
try
{
FileInfo info = new FileInfo(fullFileName);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
}
catch (Exception ex)
{
LogNet?.WriteException(ToString(), StringResources.Language.FilePathCreateFailed + fullFileName, ex);
socket?.Close();
return;
}
OperateResult receiveFile = ReceiveFileFromSocketAndMoveFile(
socket, // 网络套接字
tempFileName, // 临时保存文件路径
fullFileName, // 最终保存文件路径
out string FileName, // 文件名称,从客户端上传到服务器时,为上传人
out long FileSize,
out string FileTag,
out string FileUpload
);
if (receiveFile.IsSuccess)
{
socket?.Close();
LogNet?.WriteInfo(ToString(), StringResources.Language.FileUploadSuccess + ":" + relativeName);
}
else
{
LogNet?.WriteInfo(ToString(), StringResources.Language.FileUploadFailed + ":" + relativeName + " " + StringResources.Language.TextDescription + receiveFile.Message);
}
}
else if (customer == HslProtocol.ProtocolFileDelete)
{
string fullFileName = ReturnAbsoluteFileName(Factory, Group, Identify, fileName);
bool deleteResult = DeleteFileByName(fullFileName);
// 回发消息
if (SendStringAndCheckReceive(
socket, // 网络套接字
deleteResult ? 1 : 0, // 是否移动成功
deleteResult ? StringResources.Language.FileDeleteSuccess : StringResources.Language.FileDeleteFailed
).IsSuccess)
{
socket?.Close();
}
if (deleteResult) LogNet?.WriteInfo(ToString(), StringResources.Language.FileDeleteSuccess + ":" + relativeName);
}
else if (customer == HslProtocol.ProtocolFileDirectoryFiles)
{
List<GroupFileItem> fileNames = new List<GroupFileItem>();
foreach (var m in GetDirectoryFiles(Factory, Group, Identify))
{
FileInfo fileInfo = new FileInfo(m);
fileNames.Add(new GroupFileItem()
{
FileName = fileInfo.Name,
FileSize = fileInfo.Length,
});
}
Newtonsoft.Json.Linq.JArray jArray = Newtonsoft.Json.Linq.JArray.FromObject(fileNames.ToArray());
if (SendStringAndCheckReceive(
socket,
HslProtocol.ProtocolFileDirectoryFiles,
jArray.ToString()).IsSuccess)
{
socket?.Close();
}
}
else if (customer == HslProtocol.ProtocolFileDirectories)
{
List<string> folders = new List<string>();
foreach (var m in GetDirectories(Factory, Group, Identify))
{
DirectoryInfo directory = new DirectoryInfo(m);
folders.Add(directory.Name);
}
Newtonsoft.Json.Linq.JArray jArray = Newtonsoft.Json.Linq.JArray.FromObject(folders.ToArray());
if (SendStringAndCheckReceive(
socket,
HslProtocol.ProtocolFileDirectoryFiles,
jArray.ToString()).IsSuccess)
{
socket?.Close();
}
}
else
{
socket?.Close();
}
}
/// <summary>
/// 初始化数据
/// </summary>
protected override void StartInitialization()
{
if (string.IsNullOrEmpty(FilesDirectoryPathTemp))
{
throw new ArgumentNullException("FilesDirectoryPathTemp", "No saved path is specified");
}
base.StartInitialization();
}
/// <summary>
/// 检查文件夹
/// </summary>
protected override void CheckFolderAndCreate()
{
if (!Directory.Exists(FilesDirectoryPathTemp))
{
Directory.CreateDirectory(FilesDirectoryPathTemp);
}
base.CheckFolderAndCreate();
}
/// <summary>
/// 从网络套接字接收文件并移动到目标的文件夹中,如果结果异常,则结束通讯
/// </summary>
/// <param name="socket"></param>
/// <param name="savename"></param>
/// <param name="fileNameNew"></param>
/// <param name="filename"></param>
/// <param name="size"></param>
/// <param name="filetag"></param>
/// <param name="fileupload"></param>
/// <returns></returns>
private OperateResult ReceiveFileFromSocketAndMoveFile(
Socket socket,
string savename,
string fileNameNew,
out string filename,
out long size,
out string filetag,
out string fileupload
)
{
// 先接收文件
OperateResult<FileBaseInfo> fileInfo = ReceiveFileFromSocket(socket, savename, null);
if (!fileInfo.IsSuccess)
{
DeleteFileByName(savename);
filename = null;
size = 0;
filetag = null;
fileupload = null;
return fileInfo;
}
filename = fileInfo.Content.Name;
size = fileInfo.Content.Size;
filetag = fileInfo.Content.Tag;
fileupload = fileInfo.Content.Upload;
// 标记移动文件,失败尝试三次
int customer = 0;
int times = 0;
while (times < 3)
{
times++;
if (MoveFileToNewFile(savename, fileNameNew))
{
customer = 1;
break;
}
else
{
Thread.Sleep(500);
}
}
if (customer == 0)
{
DeleteFileByName(savename);
}
// 回发消息
OperateResult sendString = SendStringAndCheckReceive(socket, customer, "success");
return sendString;
}
#endregion
#region Public Method
/// <summary>
/// 用于接收上传文件时的临时文件夹,临时文件使用结束后会被删除
/// </summary>
public string FilesDirectoryPathTemp
{
get { return m_FilesDirectoryPathTemp; }
set { m_FilesDirectoryPathTemp = PreprocessFolderName(value); }
}
#endregion
#region Private Member
private string m_FilesDirectoryPathTemp = null;
#endregion
#region Object Override
/// <summary>
/// 获取本对象的字符串标识形式
/// </summary>
/// <returns>字符串对象</returns>
public override string ToString()
{
return "AdvancedFileServer";
}
#endregion
}
}