FileClientBase.cs
9.46 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
using HslCommunication.Core.Net;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace HslCommunication.Enthernet
{
/// <summary>
/// 文件传输客户端基类,提供上传,下载,删除的基础服务
/// </summary>
public abstract class FileClientBase : NetworkXBase
{
#region Private Member
private IPEndPoint m_ipEndPoint = null;
#endregion
#region Public Member
/// <summary>
/// 服务器端的文件管理引擎终结点
/// </summary>
public IPEndPoint ServerIpEndPoint
{
get { return m_ipEndPoint; }
set { m_ipEndPoint = value; }
}
/// <summary>
/// 获取或设置连接的超时时间,默认10秒
/// </summary>
public int ConnectTimeOut { get; set; } = 10000;
#endregion
#region Private Method
/// <summary>
/// 发送三个文件分类到服务器端
/// </summary>
/// <param name="socket">套接字对象</param>
/// <param name="factory">一级分类</param>
/// <param name="group">二级分类</param>
/// <param name="id">三级分类</param>
/// <returns>是否成功的结果对象</returns>
protected OperateResult SendFactoryGroupId(
Socket socket,
string factory,
string group,
string id
)
{
OperateResult factoryResult = SendStringAndCheckReceive(socket, 1, factory);
if (!factoryResult.IsSuccess)
{
return factoryResult;
}
OperateResult groupResult = SendStringAndCheckReceive(socket, 2, group);
if (!groupResult.IsSuccess)
{
return groupResult;
}
OperateResult idResult = SendStringAndCheckReceive(socket, 3, id);
if (!idResult.IsSuccess)
{
return idResult;
}
return OperateResult.CreateSuccessResult(); ;
}
#endregion
#region Delete File
/// <summary>
/// 删除服务器上的文件
/// </summary>
/// <param name="fileName">文件的名称</param>
/// <param name="factory">一级分类</param>
/// <param name="group">二级分类</param>
/// <param name="id">三级分类</param>
/// <returns>是否成功的结果对象</returns>
protected OperateResult DeleteFileBase(string fileName, string factory, string group, string id)
{
// connect server
OperateResult<Socket> socketResult = CreateSocketAndConnect(ServerIpEndPoint, ConnectTimeOut);
if (!socketResult.IsSuccess) return socketResult;
// 发送操作指令
OperateResult sendString = SendStringAndCheckReceive(socketResult.Content, HslProtocol.ProtocolFileDelete, fileName);
if (!sendString.IsSuccess) return sendString;
// 发送文件名以及三级分类信息
OperateResult sendFileInfo = SendFactoryGroupId(socketResult.Content, factory, group, id);
if (!sendFileInfo.IsSuccess) return sendFileInfo;
// 接收服务器操作结果
OperateResult<int, string> receiveBack = ReceiveStringContentFromSocket(socketResult.Content);
if (!receiveBack.IsSuccess) return receiveBack;
OperateResult result = new OperateResult();
if (receiveBack.Content1 == 1) result.IsSuccess = true;
result.Message = receiveBack.Message;
socketResult.Content?.Close();
return result;
}
#endregion
#region Download File
/// <summary>
/// 基础下载信息
/// </summary>
/// <param name="factory">一级分类</param>
/// <param name="group">二级分类</param>
/// <param name="id">三级分类</param>
/// <param name="fileName">服务器的文件名称</param>
/// <param name="processReport">下载的进度报告</param>
/// <param name="source">数据源信息,决定最终存储到哪里去</param>
/// <returns>是否成功的结果对象</returns>
protected OperateResult DownloadFileBase(
string factory,
string group,
string id,
string fileName,
Action<long, long> processReport,
object source
)
{
// connect server
OperateResult<Socket> socketResult = CreateSocketAndConnect(ServerIpEndPoint, ConnectTimeOut);
if (!socketResult.IsSuccess) return socketResult;
// 发送操作指令
OperateResult sendString = SendStringAndCheckReceive(socketResult.Content, HslProtocol.ProtocolFileDownload, fileName);
if (!sendString.IsSuccess) return sendString;
// 发送三级分类
OperateResult sendClass = SendFactoryGroupId(socketResult.Content, factory, group, id);
if (!sendClass.IsSuccess) return sendClass;
// 根据数据源分析
if (source is string fileSaveName)
{
OperateResult result = ReceiveFileFromSocket(socketResult.Content, fileSaveName, processReport);
if (!result.IsSuccess) return result;
}
else if (source is Stream stream)
{
OperateResult result = ReceiveFileFromSocket(socketResult.Content, stream, processReport);
if (!result.IsSuccess)
{
return result;
}
}
else
{
socketResult.Content?.Close();
LogNet?.WriteError(ToString(), StringResources.Language.NotSupportedDataType);
return new OperateResult(StringResources.Language.NotSupportedDataType);
}
socketResult.Content?.Close();
return OperateResult.CreateSuccessResult();
}
#endregion
#region Upload File
/// <summary>
/// 上传文件给服务器
/// </summary>
/// <param name="source">数据源,可以是文件名,也可以是数据流</param>
/// <param name="serverName">在服务器保存的文件名,不包含驱动器路径</param>
/// <param name="factory">一级分类</param>
/// <param name="group">二级分类</param>
/// <param name="id">三级分类</param>
/// <param name="fileTag">文件的描述</param>
/// <param name="fileUpload">文件的上传人</param>
/// <param name="processReport">汇报进度</param>
/// <returns>是否成功的结果对象</returns>
protected OperateResult UploadFileBase(
object source,
string serverName,
string factory,
string group,
string id,
string fileTag,
string fileUpload,
Action<long, long> processReport)
{
// HslReadWriteLock readWriteLock = new HslReadWriteLock( );
// 创建套接字并连接服务器
OperateResult<Socket> socketResult = CreateSocketAndConnect(ServerIpEndPoint, ConnectTimeOut);
if (!socketResult.IsSuccess) return socketResult;
// 上传操作暗号的文件名
OperateResult sendString = SendStringAndCheckReceive(socketResult.Content, HslProtocol.ProtocolFileUpload, serverName);
if (!sendString.IsSuccess) return sendString;
// 发送三级分类
OperateResult sendClass = SendFactoryGroupId(socketResult.Content, factory, group, id);
if (!sendClass.IsSuccess) return sendClass;
// 判断数据源格式
if (source is string fileName)
{
OperateResult result = SendFileAndCheckReceive(socketResult.Content, fileName, serverName, fileTag, fileUpload, processReport);
if (!result.IsSuccess)
{
return result;
}
}
else if (source is Stream stream)
{
OperateResult result = SendFileAndCheckReceive(socketResult.Content, stream, serverName, fileTag, fileUpload, processReport);
if (!result.IsSuccess)
{
return result;
}
}
else
{
socketResult.Content?.Close();
LogNet?.WriteError(ToString(), StringResources.Language.DataSourseFormatError);
return new OperateResult(StringResources.Language.DataSourseFormatError);
}
// 确认服务器文件保存状态
OperateResult<int, string> resultCheck = ReceiveStringContentFromSocket(socketResult.Content);
if (!resultCheck.IsSuccess) return resultCheck;
if (resultCheck.Content1 == 1)
{
return OperateResult.CreateSuccessResult();
}
else
{
return new OperateResult(StringResources.Language.ServerFileCheckFailed);
}
}
#endregion
#region Object Override
/// <summary>
/// 获取本对象的字符串表示形式
/// </summary>
/// <returns>字符串信息</returns>
public override string ToString()
{
return "FileClientBase";
}
#endregion
}
}