diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/LLFileTransfer.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/LindenUDP/LLFileTransfer.cs | 367 |
1 files changed, 0 insertions, 367 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLFileTransfer.cs b/OpenSim/Region/ClientStack/LindenUDP/LLFileTransfer.cs deleted file mode 100644 index 10e5a95..0000000 --- a/OpenSim/Region/ClientStack/LindenUDP/LLFileTransfer.cs +++ /dev/null | |||
@@ -1,367 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using OpenMetaverse; | ||
31 | using OpenSim.Framework; | ||
32 | |||
33 | namespace OpenSim.Region.ClientStack.LindenUDP | ||
34 | { | ||
35 | /// <summary> | ||
36 | /// A work in progress, to contain the SL specific file transfer code that is currently in various region modules | ||
37 | /// This file currently contains multiple classes that need to be split out into their own files. | ||
38 | /// </summary> | ||
39 | public class LLFileTransfer : IClientFileTransfer | ||
40 | { | ||
41 | protected IClientAPI m_clientAPI; | ||
42 | |||
43 | /// Dictionary of handlers for uploading files from client | ||
44 | /// TODO: Need to add cleanup code to remove handlers that have completed their upload | ||
45 | protected Dictionary<ulong, XferUploadHandler> m_uploadHandlers; | ||
46 | protected object m_uploadHandlersLock = new object(); | ||
47 | |||
48 | |||
49 | /// <summary> | ||
50 | /// Dictionary of files ready to be sent to clients | ||
51 | /// </summary> | ||
52 | protected static Dictionary<string, byte[]> m_files; | ||
53 | |||
54 | /// <summary> | ||
55 | /// Dictionary of Download Transfers in progess | ||
56 | /// </summary> | ||
57 | protected Dictionary<ulong, XferDownloadHandler> m_downloadHandlers = new Dictionary<ulong, XferDownloadHandler>(); | ||
58 | |||
59 | |||
60 | public LLFileTransfer(IClientAPI clientAPI) | ||
61 | { | ||
62 | m_uploadHandlers = new Dictionary<ulong, XferUploadHandler>(); | ||
63 | m_clientAPI = clientAPI; | ||
64 | |||
65 | m_clientAPI.OnXferReceive += XferReceive; | ||
66 | m_clientAPI.OnAbortXfer += AbortXferUploadHandler; | ||
67 | } | ||
68 | |||
69 | public void Close() | ||
70 | { | ||
71 | if (m_clientAPI != null) | ||
72 | { | ||
73 | m_clientAPI.OnXferReceive -= XferReceive; | ||
74 | m_clientAPI.OnAbortXfer -= AbortXferUploadHandler; | ||
75 | m_clientAPI = null; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | #region Upload Handling | ||
80 | |||
81 | public bool RequestUpload(string clientFileName, UploadComplete uploadCompleteCallback, UploadAborted abortCallback) | ||
82 | { | ||
83 | if ((String.IsNullOrEmpty(clientFileName)) || (uploadCompleteCallback == null)) | ||
84 | { | ||
85 | return false; | ||
86 | } | ||
87 | |||
88 | XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, clientFileName); | ||
89 | |||
90 | return StartUpload(uploader, uploadCompleteCallback, abortCallback); | ||
91 | } | ||
92 | |||
93 | public bool RequestUpload(UUID fileID, UploadComplete uploadCompleteCallback, UploadAborted abortCallback) | ||
94 | { | ||
95 | if ((fileID == UUID.Zero) || (uploadCompleteCallback == null)) | ||
96 | { | ||
97 | return false; | ||
98 | } | ||
99 | |||
100 | XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, fileID); | ||
101 | |||
102 | return StartUpload(uploader, uploadCompleteCallback, abortCallback); | ||
103 | } | ||
104 | |||
105 | private bool StartUpload(XferUploadHandler uploader, UploadComplete uploadCompleteCallback, UploadAborted abortCallback) | ||
106 | { | ||
107 | uploader.UploadDone += uploadCompleteCallback; | ||
108 | uploader.UploadDone += RemoveXferUploadHandler; | ||
109 | |||
110 | if (abortCallback != null) | ||
111 | { | ||
112 | uploader.UploadAborted += abortCallback; | ||
113 | } | ||
114 | |||
115 | lock (m_uploadHandlersLock) | ||
116 | { | ||
117 | if (!m_uploadHandlers.ContainsKey(uploader.XferID)) | ||
118 | { | ||
119 | m_uploadHandlers.Add(uploader.XferID, uploader); | ||
120 | uploader.RequestStartXfer(m_clientAPI); | ||
121 | return true; | ||
122 | } | ||
123 | else | ||
124 | { | ||
125 | // something went wrong with the xferID allocation | ||
126 | uploader.UploadDone -= uploadCompleteCallback; | ||
127 | uploader.UploadDone -= RemoveXferUploadHandler; | ||
128 | if (abortCallback != null) | ||
129 | { | ||
130 | uploader.UploadAborted -= abortCallback; | ||
131 | } | ||
132 | return false; | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | protected void AbortXferUploadHandler(IClientAPI remoteClient, ulong xferID) | ||
138 | { | ||
139 | lock (m_uploadHandlersLock) | ||
140 | { | ||
141 | if (m_uploadHandlers.ContainsKey(xferID)) | ||
142 | { | ||
143 | m_uploadHandlers[xferID].AbortUpload(remoteClient); | ||
144 | m_uploadHandlers.Remove(xferID); | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | protected void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data) | ||
150 | { | ||
151 | lock (m_uploadHandlersLock) | ||
152 | { | ||
153 | if (m_uploadHandlers.ContainsKey(xferID)) | ||
154 | { | ||
155 | m_uploadHandlers[xferID].XferReceive(remoteClient, xferID, packetID, data); | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | |||
160 | protected void RemoveXferUploadHandler(string filename, UUID fileID, ulong transferID, byte[] fileData, IClientAPI remoteClient) | ||
161 | { | ||
162 | |||
163 | } | ||
164 | #endregion | ||
165 | |||
166 | } | ||
167 | |||
168 | public class XferUploadHandler | ||
169 | { | ||
170 | private AssetBase m_asset; | ||
171 | |||
172 | public event UploadComplete UploadDone; | ||
173 | public event UploadAborted UploadAborted; | ||
174 | |||
175 | private sbyte type = 0; | ||
176 | |||
177 | public ulong mXferID; | ||
178 | private UploadComplete handlerUploadDone; | ||
179 | private UploadAborted handlerAbort; | ||
180 | |||
181 | private bool m_complete = false; | ||
182 | |||
183 | public bool UploadComplete | ||
184 | { | ||
185 | get { return m_complete; } | ||
186 | } | ||
187 | |||
188 | public XferUploadHandler(IClientAPI pRemoteClient, string pClientFilename) | ||
189 | { | ||
190 | Initialise(UUID.Zero, pClientFilename); | ||
191 | } | ||
192 | |||
193 | public XferUploadHandler(IClientAPI pRemoteClient, UUID fileID) | ||
194 | { | ||
195 | Initialise(fileID, String.Empty); | ||
196 | } | ||
197 | |||
198 | private void Initialise(UUID fileID, string fileName) | ||
199 | { | ||
200 | m_asset = new AssetBase(fileID, fileName, type, UUID.Zero.ToString()); | ||
201 | m_asset.Data = new byte[0]; | ||
202 | m_asset.Description = "empty"; | ||
203 | m_asset.Local = true; | ||
204 | m_asset.Temporary = true; | ||
205 | mXferID = Util.GetNextXferID(); | ||
206 | } | ||
207 | |||
208 | public ulong XferID | ||
209 | { | ||
210 | get { return mXferID; } | ||
211 | } | ||
212 | |||
213 | public void RequestStartXfer(IClientAPI pRemoteClient) | ||
214 | { | ||
215 | m_asset.Metadata.CreatorID = pRemoteClient.AgentId.ToString(); | ||
216 | |||
217 | if (!String.IsNullOrEmpty(m_asset.Name)) | ||
218 | { | ||
219 | pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, Utils.StringToBytes(m_asset.Name)); | ||
220 | } | ||
221 | else | ||
222 | { | ||
223 | pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, new byte[0]); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | /// <summary> | ||
228 | /// Process transfer data received from the client. | ||
229 | /// </summary> | ||
230 | /// <param name="xferID"></param> | ||
231 | /// <param name="packetID"></param> | ||
232 | /// <param name="data"></param> | ||
233 | public void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data) | ||
234 | { | ||
235 | if (mXferID == xferID) | ||
236 | { | ||
237 | if (m_asset.Data.Length > 1) | ||
238 | { | ||
239 | byte[] destinationArray = new byte[m_asset.Data.Length + data.Length]; | ||
240 | Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length); | ||
241 | Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length); | ||
242 | m_asset.Data = destinationArray; | ||
243 | } | ||
244 | else | ||
245 | { | ||
246 | byte[] buffer2 = new byte[data.Length - 4]; | ||
247 | Array.Copy(data, 4, buffer2, 0, data.Length - 4); | ||
248 | m_asset.Data = buffer2; | ||
249 | } | ||
250 | |||
251 | remoteClient.SendConfirmXfer(xferID, packetID); | ||
252 | |||
253 | if ((packetID & 0x80000000) != 0) | ||
254 | { | ||
255 | SendCompleteMessage(remoteClient); | ||
256 | |||
257 | } | ||
258 | } | ||
259 | } | ||
260 | |||
261 | protected void SendCompleteMessage(IClientAPI remoteClient) | ||
262 | { | ||
263 | m_complete = true; | ||
264 | handlerUploadDone = UploadDone; | ||
265 | if (handlerUploadDone != null) | ||
266 | { | ||
267 | handlerUploadDone(m_asset.Name, m_asset.FullID, mXferID, m_asset.Data, remoteClient); | ||
268 | } | ||
269 | } | ||
270 | |||
271 | public void AbortUpload(IClientAPI remoteClient) | ||
272 | { | ||
273 | handlerAbort = UploadAborted; | ||
274 | if (handlerAbort != null) | ||
275 | { | ||
276 | handlerAbort(m_asset.Name, m_asset.FullID, mXferID, remoteClient); | ||
277 | } | ||
278 | } | ||
279 | } | ||
280 | |||
281 | public class XferDownloadHandler | ||
282 | { | ||
283 | public IClientAPI Client; | ||
284 | private bool complete; | ||
285 | public byte[] Data = new byte[0]; | ||
286 | public int DataPointer = 0; | ||
287 | public string FileName = String.Empty; | ||
288 | public uint Packet = 0; | ||
289 | public uint Serial = 1; | ||
290 | public ulong XferID = 0; | ||
291 | |||
292 | public XferDownloadHandler(string fileName, byte[] data, ulong xferID, IClientAPI client) | ||
293 | { | ||
294 | FileName = fileName; | ||
295 | Data = data; | ||
296 | XferID = xferID; | ||
297 | Client = client; | ||
298 | } | ||
299 | |||
300 | public XferDownloadHandler() | ||
301 | { | ||
302 | } | ||
303 | |||
304 | /// <summary> | ||
305 | /// Start a transfer | ||
306 | /// </summary> | ||
307 | /// <returns>True if the transfer is complete, false if not</returns> | ||
308 | public bool StartSend() | ||
309 | { | ||
310 | if (Data.Length < 1000) | ||
311 | { | ||
312 | // for now (testing) we only support files under 1000 bytes | ||
313 | byte[] transferData = new byte[Data.Length + 4]; | ||
314 | Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
315 | Array.Copy(Data, 0, transferData, 4, Data.Length); | ||
316 | Client.SendXferPacket(XferID, 0 + 0x80000000, transferData); | ||
317 | |||
318 | complete = true; | ||
319 | } | ||
320 | else | ||
321 | { | ||
322 | byte[] transferData = new byte[1000 + 4]; | ||
323 | Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
324 | Array.Copy(Data, 0, transferData, 4, 1000); | ||
325 | Client.SendXferPacket(XferID, 0, transferData); | ||
326 | Packet++; | ||
327 | DataPointer = 1000; | ||
328 | } | ||
329 | |||
330 | return complete; | ||
331 | } | ||
332 | |||
333 | /// <summary> | ||
334 | /// Respond to an ack packet from the client | ||
335 | /// </summary> | ||
336 | /// <param name="packet"></param> | ||
337 | /// <returns>True if the transfer is complete, false otherwise</returns> | ||
338 | public bool AckPacket(uint packet) | ||
339 | { | ||
340 | if (!complete) | ||
341 | { | ||
342 | if ((Data.Length - DataPointer) > 1000) | ||
343 | { | ||
344 | byte[] transferData = new byte[1000]; | ||
345 | Array.Copy(Data, DataPointer, transferData, 0, 1000); | ||
346 | Client.SendXferPacket(XferID, Packet, transferData); | ||
347 | Packet++; | ||
348 | DataPointer += 1000; | ||
349 | } | ||
350 | else | ||
351 | { | ||
352 | byte[] transferData = new byte[Data.Length - DataPointer]; | ||
353 | Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer); | ||
354 | uint endPacket = Packet |= (uint)0x80000000; | ||
355 | Client.SendXferPacket(XferID, endPacket, transferData); | ||
356 | Packet++; | ||
357 | DataPointer += (Data.Length - DataPointer); | ||
358 | |||
359 | complete = true; | ||
360 | } | ||
361 | } | ||
362 | |||
363 | return complete; | ||
364 | } | ||
365 | } | ||
366 | |||
367 | } | ||