diff options
Diffstat (limited to 'OpenSim/Region/Environment/XferModule.cs')
-rw-r--r-- | OpenSim/Region/Environment/XferModule.cs | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/XferModule.cs b/OpenSim/Region/Environment/XferModule.cs new file mode 100644 index 0000000..beb72120 --- /dev/null +++ b/OpenSim/Region/Environment/XferModule.cs | |||
@@ -0,0 +1,175 @@ | |||
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 | using OpenSim.Region.Environment.Scenes; | ||
9 | using OpenSim.Region.Environment.Interfaces; | ||
10 | |||
11 | namespace OpenSim.Region.Environment | ||
12 | { | ||
13 | public class XferModule : IRegionModule | ||
14 | { | ||
15 | public Dictionary<string, byte[]> NewFiles = new Dictionary<string, byte[]>(); | ||
16 | public Dictionary<ulong, XferDownLoad> Transfers = new Dictionary<ulong, XferDownLoad>(); | ||
17 | |||
18 | private Scene m_scene; | ||
19 | |||
20 | public XferModule() | ||
21 | { | ||
22 | |||
23 | } | ||
24 | |||
25 | public void Initialise(Scene scene) | ||
26 | { | ||
27 | m_scene = scene; | ||
28 | m_scene.EventManager.OnNewClient += NewClient; | ||
29 | |||
30 | m_scene.RegisterAPIMethod("API_AddXferFile", new ModuleAPIMethod<bool, string, byte[]>(this.AddNewFile)); | ||
31 | } | ||
32 | |||
33 | public void PostInitialise() | ||
34 | { | ||
35 | |||
36 | } | ||
37 | |||
38 | public void CloseDown() | ||
39 | { | ||
40 | |||
41 | } | ||
42 | |||
43 | public string GetName() | ||
44 | { | ||
45 | return "XferModule"; | ||
46 | } | ||
47 | |||
48 | public void NewClient(IClientAPI client) | ||
49 | { | ||
50 | client.OnRequestXfer += RequestXfer; | ||
51 | client.OnConfirmXfer += AckPacket; | ||
52 | } | ||
53 | |||
54 | /// <summary> | ||
55 | /// | ||
56 | /// </summary> | ||
57 | /// <param name="remoteClient"></param> | ||
58 | /// <param name="xferID"></param> | ||
59 | /// <param name="fileName"></param> | ||
60 | public void RequestXfer(IClientAPI remoteClient, ulong xferID, string fileName) | ||
61 | { | ||
62 | lock (NewFiles) | ||
63 | { | ||
64 | if (NewFiles.ContainsKey(fileName)) | ||
65 | { | ||
66 | if (!Transfers.ContainsKey(xferID)) | ||
67 | { | ||
68 | byte[] fileData = NewFiles[fileName]; | ||
69 | XferDownLoad transaction = new XferDownLoad(fileName, fileData, xferID, remoteClient); | ||
70 | Transfers.Add(xferID, transaction); | ||
71 | NewFiles.Remove(fileName); | ||
72 | transaction.StartSend(); | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | |||
78 | public void AckPacket(IClientAPI remoteClient, ulong xferID, uint packet) | ||
79 | { | ||
80 | if (this.Transfers.ContainsKey(xferID)) | ||
81 | { | ||
82 | Transfers[xferID].AckPacket(packet); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | public bool AddNewFile(string fileName, byte[] data) | ||
87 | { | ||
88 | lock (NewFiles) | ||
89 | { | ||
90 | if (NewFiles.ContainsKey(fileName)) | ||
91 | { | ||
92 | NewFiles[fileName] = data; | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | NewFiles.Add(fileName, data); | ||
97 | } | ||
98 | } | ||
99 | return true; | ||
100 | } | ||
101 | |||
102 | |||
103 | public class XferDownLoad | ||
104 | { | ||
105 | public byte[] Data = new byte[0]; | ||
106 | public string FileName = ""; | ||
107 | public ulong XferID = 0; | ||
108 | public int DataPointer = 0; | ||
109 | public uint Packet = 0; | ||
110 | public IClientAPI Client; | ||
111 | public uint Serial = 1; | ||
112 | private bool complete = false; | ||
113 | |||
114 | public XferDownLoad(string fileName, byte[] data, ulong xferID, IClientAPI client) | ||
115 | { | ||
116 | FileName = fileName; | ||
117 | Data = data; | ||
118 | XferID = xferID; | ||
119 | Client = client; | ||
120 | } | ||
121 | |||
122 | public XferDownLoad() | ||
123 | { | ||
124 | |||
125 | } | ||
126 | |||
127 | public void StartSend() | ||
128 | { | ||
129 | if (Data.Length < 1000) | ||
130 | { | ||
131 | // for now (testing ) we only support files under 1000 bytes | ||
132 | byte[] transferData = new byte[Data.Length + 4]; | ||
133 | Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
134 | Array.Copy(Data, 0, transferData, 4, Data.Length); | ||
135 | Client.SendXferPacket(XferID, 0 + 0x80000000, transferData); | ||
136 | complete = true; | ||
137 | } | ||
138 | else | ||
139 | { | ||
140 | byte[] transferData = new byte[1000 +4]; | ||
141 | Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4); | ||
142 | Array.Copy(Data, 0, transferData, 4, 1000); | ||
143 | Client.SendXferPacket(XferID, 0 , transferData); | ||
144 | Packet++; | ||
145 | DataPointer = 1000; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | public void AckPacket(uint packet) | ||
150 | { | ||
151 | if (!complete) | ||
152 | { | ||
153 | if ((Data.Length - DataPointer) > 1000) | ||
154 | { | ||
155 | byte[] transferData = new byte[1000]; | ||
156 | Array.Copy(Data, DataPointer, transferData, 0, 1000); | ||
157 | Client.SendXferPacket(XferID, Packet, transferData); | ||
158 | Packet++; | ||
159 | DataPointer += 1000; | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | byte[] transferData = new byte[Data.Length - DataPointer]; | ||
164 | Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer); | ||
165 | uint endPacket = Packet |= (uint)0x80000000; | ||
166 | Client.SendXferPacket(XferID, endPacket, transferData); | ||
167 | Packet++; | ||
168 | DataPointer += (Data.Length - DataPointer); | ||
169 | complete = true; | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | } | ||