diff options
Diffstat (limited to 'OpenSim/Region/Environment/XferManager.cs')
-rw-r--r-- | OpenSim/Region/Environment/XferManager.cs | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/XferManager.cs b/OpenSim/Region/Environment/XferManager.cs new file mode 100644 index 0000000..7a8e762 --- /dev/null +++ b/OpenSim/Region/Environment/XferManager.cs | |||
@@ -0,0 +1,142 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | |||
5 | using libsecondlife; | ||
6 | using OpenSim.Framework.Interfaces; | ||
7 | using OpenSim.Framework.Utilities; | ||
8 | |||
9 | namespace OpenSim.Region.Environment | ||
10 | { | ||
11 | public class XferManager | ||
12 | { | ||
13 | public Dictionary<string, byte[]> NewFiles = new Dictionary<string, byte[]>(); | ||
14 | public Dictionary<ulong, XferDownLoad> Transfers = new Dictionary<ulong, XferDownLoad>(); | ||
15 | |||
16 | public XferManager() | ||
17 | { | ||
18 | |||
19 | } | ||
20 | |||
21 | /// <summary> | ||
22 | /// | ||
23 | /// </summary> | ||
24 | /// <param name="remoteClient"></param> | ||
25 | /// <param name="xferID"></param> | ||
26 | /// <param name="fileName"></param> | ||
27 | public void RequestXfer(IClientAPI remoteClient, ulong xferID, string fileName) | ||
28 | { | ||
29 | Console.WriteLine("xfer request for " + fileName); | ||
30 | lock (NewFiles) | ||
31 | { | ||
32 | if (NewFiles.ContainsKey(fileName)) | ||
33 | { | ||
34 | if (!Transfers.ContainsKey(xferID)) | ||
35 | { | ||
36 | byte[] fileData = NewFiles[fileName]; | ||
37 | XferDownLoad transaction = new XferDownLoad(fileName, fileData, xferID, remoteClient); | ||
38 | Transfers.Add(xferID, transaction); | ||
39 | NewFiles.Remove(fileName); | ||
40 | transaction.StartSend(); | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | public void AckPacket(IClientAPI remoteClient, ulong xferID, uint packet) | ||
47 | { | ||
48 | if (this.Transfers.ContainsKey(xferID)) | ||
49 | { | ||
50 | Transfers[xferID].AckPacket(packet); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | public void AddNewFile(string fileName, byte[] data) | ||
55 | { | ||
56 | lock (NewFiles) | ||
57 | { | ||
58 | if (NewFiles.ContainsKey(fileName)) | ||
59 | { | ||
60 | NewFiles[fileName] = data; | ||
61 | } | ||
62 | else | ||
63 | { | ||
64 | NewFiles.Add(fileName, data); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | public class XferDownLoad | ||
70 | { | ||
71 | public byte[] Data = new byte[0]; | ||
72 | public string FileName = ""; | ||
73 | public ulong XferID = 0; | ||
74 | public int DataPointer = 0; | ||
75 | public uint Packet = 0; | ||
76 | public IClientAPI Client; | ||
77 | public uint Serial = 1; | ||
78 | private bool complete = false; | ||
79 | |||
80 | public XferDownLoad(string fileName, byte[] data, ulong xferID, IClientAPI client) | ||
81 | { | ||
82 | FileName = fileName; | ||
83 | Data = data; | ||
84 | XferID = xferID; | ||
85 | Client = client; | ||
86 | } | ||
87 | |||
88 | public XferDownLoad() | ||
89 | { | ||
90 | |||
91 | } | ||
92 | |||
93 | public void StartSend() | ||
94 | { | ||
95 | if (Data.Length < 1000) | ||
96 | { | ||
97 | // for now (testing ) we only support files under 1000 bytes | ||
98 | byte[] transferData = new byte[Data.Length + 4]; | ||
99 | Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
100 | Array.Copy(Data, 0, transferData, 4, Data.Length); | ||
101 | Client.SendXferPacket(XferID, 0 + 0x80000000, transferData); | ||
102 | complete = true; | ||
103 | Console.WriteLine("xfer is under 1000 bytes"); | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | byte[] transferData = new byte[1000 +4]; | ||
108 | Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
109 | Array.Copy(Data, 0, transferData, 4, 1000); | ||
110 | Client.SendXferPacket(XferID, 0 , transferData); | ||
111 | Packet++; | ||
112 | DataPointer = 1000; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | public void AckPacket(uint packet) | ||
117 | { | ||
118 | if (!complete) | ||
119 | { | ||
120 | if ((Data.Length - DataPointer) > 1000) | ||
121 | { | ||
122 | byte[] transferData = new byte[1000]; | ||
123 | Array.Copy(Data, DataPointer, transferData, 0, 1000); | ||
124 | Client.SendXferPacket(XferID, Packet, transferData); | ||
125 | Packet++; | ||
126 | DataPointer += 1000; | ||
127 | } | ||
128 | else | ||
129 | { | ||
130 | byte[] transferData = new byte[Data.Length - DataPointer]; | ||
131 | Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer); | ||
132 | uint endPacket = Packet |= (uint)0x80000000; | ||
133 | Client.SendXferPacket(XferID, endPacket, transferData); | ||
134 | Packet++; | ||
135 | DataPointer += (Data.Length - DataPointer); | ||
136 | complete = true; | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | } | ||