-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNetworkedClient.cs
More file actions
312 lines (287 loc) · 13.6 KB
/
NetworkedClient.cs
File metadata and controls
312 lines (287 loc) · 13.6 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
using Newtonsoft.Json;
using RoleplayingVoiceCore;
using System.IO.Compression;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Numerics;
namespace FFXIVLooseTextureCompiler.Networking {
public class NetworkedClient : IDisposable {
private bool disposedValue;
private bool connected;
private string id;
private string _ipAddress;
int sendAttempt = 0;
private int connectionAttempts;
private bool alreadySendingFiles;
public string Id { get => id; set => id = value; }
public bool Connected { get => connected; set => connected = value; }
public int Port { get { return 5105; } }
public event EventHandler OnSendFailed;
public event EventHandler<FailureMessage> OnConnectionFailed;
public NetworkedClient(string ipAddress) {
_ipAddress = ipAddress;
}
public void UpdateIPAddress(string ipAddress) {
_ipAddress = ipAddress;
}
public async Task<bool> SendFile(string sendID, string path) {
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
MemoryStream memory = new MemoryStream();
using (FileStream fileStream = new(path, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (BinaryWriter writer = new(memory)) {
writer.Write(sendID);
writer.Write(0);
writer.Write(fileStream.Length);
fileStream.CopyTo(writer.BaseStream);
writer.Write(60000);
writer.Flush();
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode != HttpStatusCode.OK) {
OnConnectionFailed?.Invoke(this, new FailureMessage() { Message = "Upload failed" });
}
return true;
}
}
}
} catch (Exception e) {
sendAttempt++;
if (sendAttempt <= 10) {
return await SendFile(sendID, path);
} else {
OnConnectionFailed?.Invoke(this, new FailureMessage() { Message = e.Message });
}
}
connectionAttempts = 0;
return false;
}
private void AuditPathContents(string path) {
if (Directory.Exists(path)) {
foreach (string file in Directory.GetFiles(path)) {
if (!file.EndsWith(".mp3") && !file.EndsWith(".ogg")) {
File.Delete(file);
}
}
}
}
public async Task<ushort> GetShort(string sendID) {
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
httpClient.Timeout = new TimeSpan(1, 0, 0);
MemoryStream memory = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memory);
writer.Write(sendID);
writer.Write(1);
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode == HttpStatusCode.OK) {
BinaryReader reader = new BinaryReader(await post.Content.ReadAsStreamAsync());
byte value = reader.ReadByte();
if (value != 0) {
long length = reader.ReadInt64();
return reader.ReadUInt16();
}
}
}
} catch {
}
return ushort.MaxValue - 1;
}
public async Task<bool> SendShort(string sendID, ushort shortValue) {
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
MemoryStream memory = new MemoryStream();
MemoryStream fileStream = new MemoryStream();
BinaryWriter shortWriter = new(fileStream);
shortWriter.Write(shortValue);
shortWriter.Flush();
fileStream.Position = 0;
using (BinaryWriter writer = new(memory)) {
writer.Write(sendID);
writer.Write(0);
writer.Write(fileStream.Length);
fileStream.CopyTo(writer.BaseStream);
writer.Write(5000);
writer.Flush();
fileStream.Flush();
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode != HttpStatusCode.OK) {
}
}
}
return true;
} catch {
}
connectionAttempts = 0;
return false;
}
public async Task<bool> SendZip(string sendID, string path) {
if (Path.Exists(path)) {
if (!alreadySendingFiles) {
alreadySendingFiles = true;
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
MemoryStream memory = new MemoryStream();
string zipPath = path + ".zip";
if (File.Exists(zipPath)) {
File.Delete(zipPath);
}
AuditPathContents(path);
ZipFile.CreateFromDirectory(path, zipPath);
using (FileStream fileStream = new(path + ".zip", FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (BinaryWriter writer = new(memory)) {
writer.Write(sendID);
writer.Write(0);
writer.Write(fileStream.Length);
fileStream.CopyTo(writer.BaseStream);
fileStream.Flush();
writer.Write(3600000);
writer.Flush();
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode != HttpStatusCode.OK) {
}
fileStream.Dispose();
}
}
File.Delete(zipPath);
return true;
}
} catch {
}
alreadySendingFiles = false;
} else {
alreadySendingFiles = false;
}
connectionAttempts = 0;
}
return false;
}
public async Task<KeyValuePair<Vector3, string>> GetFile(string sendID, string tempPath, string filename = "") {
Random random = new Random();
string path = Path.Combine(tempPath, (!string.IsNullOrEmpty(filename) ? filename : sendID) + ".mp3");
Vector3 position = new Vector3(-1, -1, -1);
Directory.CreateDirectory(tempPath);
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
MemoryStream memory = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memory);
writer.Write(sendID);
writer.Write(1);
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode == HttpStatusCode.OK) {
BinaryReader reader = new BinaryReader(await post.Content.ReadAsStreamAsync());
byte value = reader.ReadByte();
if (value != 0) {
position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
long length = reader.ReadInt64();
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) {
reader.BaseStream.CopyTo(fileStream);
}
}
}
}
} catch (Exception e) {
OnConnectionFailed?.Invoke(this, new FailureMessage() { Message = e.Message });
}
connectionAttempts = 0;
return new KeyValuePair<Vector3, string>(position, path);
}
public async Task<string> GetZip(string sendID, string tempPath) {
Random random = new Random();
string path = Path.Combine(tempPath, sendID + ".zip");
string zipDirectory = tempPath + @"\" + sendID;
try {
using (HttpClient httpClient = new HttpClient()) {
httpClient.BaseAddress = new Uri("http://" + _ipAddress + ":" + Port);
MemoryStream memory = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memory);
writer.Write(sendID);
writer.Write(1);
memory.Position = 0;
var post = await httpClient.PostAsync(httpClient.BaseAddress, new StreamContent(memory));
if (post.StatusCode == HttpStatusCode.OK) {
BinaryReader reader = new BinaryReader(await post.Content.ReadAsStreamAsync());
byte value = reader.ReadByte();
if (value != 0) {
long length = reader.ReadInt64();
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) {
CopyStream(reader.BaseStream, fileStream, (int)length);
}
Directory.CreateDirectory(tempPath);
try {
if (File.Exists(zipDirectory)) {
File.Delete(zipDirectory);
}
} catch {
}
ZipFile.ExtractToDirectory(path, zipDirectory, true);
AuditPathContents(zipDirectory);
if (File.Exists(path)) {
File.Delete(path);
}
}
}
}
} catch {
}
connectionAttempts = 0;
return zipDirectory;
}
protected virtual bool IsFileLocked(string path) {
try {
using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)) {
stream.Close();
}
} catch (IOException) {
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
//file is not locked
return false;
}
public static void CopyStream(Stream input, Stream output, int bytes) {
byte[] buffer = new byte[32768];
int read;
while (bytes > 0 &&
(read = input.Read(buffer, 0, Math.Min(buffer.Length, bytes))) > 0) {
output.Write(buffer, 0, read);
bytes -= read;
}
}
protected virtual void Dispose(bool disposing) {
if (!disposedValue) {
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~NetworkedClient()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
public class FailureMessage : EventArgs {
string _message;
public string Message { get => _message; set => _message = value; }
}
}