private void ExecuteThread(object obj) { try { CommandState state = (CommandState)obj; Entity entity = state.Entity; TunnelComponent tunnel = entity.Get <TunnelComponent>(); DownloadRequestCommand command = new DownloadRequestCommand(entity, tunnel.Connection); command.Execute(state.Id, state.Data, state.Chunk); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void DownloadState() { try { // Command FileComponent file = Owner.Get <FileComponent>(); DownloadRequestCommand command = new DownloadRequestCommand(Entity, Connection); HttpCode code = command.Execute(Id, file, ClientId); if (code == HttpCode.Ok) { byte[] data = command.Data; if (data != null) { // Data JsonChunk jsonChunk = command.Chunk; Debug.Assert(data.Length == jsonChunk.Size); while (true) { try { FileUtil.Write(file.Path, data, jsonChunk.Offset); break; } catch (IOException ex) { Console.WriteLine(ex.Message); Status = DemonStatus.Warning; Thread.Sleep(DemonTimeout.File); } } TransferComponent transfer = Owner.Get <TransferComponent>(); transfer.Size += jsonChunk.Size; if (transfer.Done) { State = DemonState.Shutdown; Status = DemonStatus.Success; } } else { if (Id == null) { FileUtil.Create(file.Path, file.Size); if (code == HttpCode.Ok) { // Data Id = command.Id; if (!Paused && !Cancelled) { Status = DemonStatus.Success; } } } else { // TODO: Implement infinite time-out? // Sleep(DemonTimeout.Seconds); } } } else { State = DemonState.Shutdown; } } catch (Exception ex) { Console.WriteLine(ex.Message); State = DemonState.Shutdown; Status = DemonStatus.Error; } }