// Constructors public ChunkComponent(long total, int size, int max) { Chunks = (int)(total / size); if ((total % size) > 0) { Chunks++; } Max = max; Remaining = Chunks; _chunks = new Deque <JsonChunk>(Chunks); _data = new Deque <JsonTransfer>(max); lock (_chunks) { for (int i = 0; i < Chunks; ++i) { long offset = (long)i * (long)size; int bytes = ((offset + size) > total) ? (int)(total - offset) : size; Debug.Assert(bytes > 0); JsonChunk jsonChunk = new JsonChunk() { Offset = offset, Size = bytes }; PushChunk(jsonChunk); } } }
public JsonLayer(Layer l) { List <JsonChunk> list = new List <JsonChunk>(); for (int i = 0; i < l.Resolution; i++) { for (int j = 0; j < l.Resolution; j++) { for (int k = 0; k < l.Resolution; k++) { if (l.chunks[i, j, k].voxels.Initialized) { JsonChunk jsonChunk = new JsonChunk(l.chunks[i, j, k]); if (jsonChunk.Values != null && jsonChunk.Values.Length != 0) { list.Add(jsonChunk); } } } } } Chunks = list.ToArray(); Name = l.name; Rotation = new JsonQuaternion(l.transform.rotation); Position = new JsonVector3(l.transform.position); Scale = new JsonVector3(l.transform.localScale); Size = l.Size; Smoothness = l.Smoothness; Metallic = l.Metallic; Resolution = l.Resolution; ChunkResolution = l.ChunkResolution; RenderType = (int)l.RenderType; }
public HttpCode Execute(string jsonId, FileComponent file, string clientId = null) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonChunk jsonChunk = new JsonChunk() { Size = file.Size }; JsonClient jsonClient = (clientId != null) ? new JsonClient(clientId) : null; JsonDownloadRequestMessage jsonRequestMessage = new JsonDownloadRequestMessage(jsonId) { Chunk = jsonChunk, Client = jsonClient }; JsonFile jsonFile = (JsonFile)file; JsonDownloadRequestData jsonRequestData = new JsonDownloadRequestData() { File = jsonFile }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonDownloadResponseMessage jsonResponseMessage = JsonDownloadResponseMessage.Parse(jsonResponse.Message); Chunk = jsonResponseMessage.Chunk; Id = jsonResponseMessage.Id; JsonDownloadResponseData jsonResponseData = JsonDownloadResponseData.Parse(Group.Decrypt(jsonResponse.Data)); if (jsonResponseData != null) { Data = jsonResponseData.Data; } #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } return(httpResponse.Code); }
public JsonChunk Execute(string jsonId, byte[] jsonData, JsonChunk jsonChunk) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonUploadRequestMessage jsonRequestMessage = new JsonUploadRequestMessage(jsonId) { Chunk = jsonChunk }; JsonUploadRequestData jsonRequestData = new JsonUploadRequestData() { Data = jsonData }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonUploadResponseMessage jsonResponseMessage = JsonUploadResponseMessage.Parse(jsonResponse.Message); jsonChunk = jsonResponseMessage.Chunk; #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif if (jsonResponseMessage.Delay > 0) { Thread.Sleep(TimeSpan.FromMilliseconds(jsonResponseMessage.Delay)); } } return(jsonChunk); }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonDownloadRequestMessage jsonRequestMessage = JsonDownloadRequestMessage.Parse(jsonRequest.Message); JsonChunk jsonChunk = jsonRequestMessage.Chunk; string jsonId = jsonRequestMessage.Id; JsonDownloadRequestData jsonRequestData = JsonDownloadRequestData.Parse(Group.Decrypt(jsonRequest.Data)); JsonFile jsonFile = jsonRequestData.File; // Data FileComponent file = FileMap.Get(jsonFile.Id); if (file == null) { channel.SendNotFound(); return; } // Controller //UploadData uploadData = new UploadData(fileData, Client) { Id = jsonId, Chunk = jsonChunk }; //OnUpload(uploadData); // Response JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(); JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage); HttpResponse httpResponse = new HttpResponse() { Data = Session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG jsonRequest.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif }
public HttpCode Execute(string jsonId, string jsonData, JsonChunk jsonChunk) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonDownloadRequestMessage jsonRequestMessage = new JsonDownloadRequestMessage(jsonId) { Chunk = jsonChunk }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, jsonData); HttpRequest httpRequest = new HttpRequest() { Data = Session.Encrypt(jsonRequest) }; // Response HttpResponse httpResponse; lock (Socket) { channel.Send(httpRequest); channel.Receive(out httpResponse); } Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } return(Code); }
// Index public void PushChunk(JsonChunk chunk) { lock (_chunks) _chunks.Push(chunk); }
public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonAction jsonAction = JsonAction.None; JsonDownloadRequestMessage jsonRequestMessage = JsonDownloadRequestMessage.Parse(jsonRequest.Message); string jsonId = jsonRequestMessage.Id; if (jsonId != null) { // Data Entity entity = TransferMap.Get(jsonId); if (entity == null) { channel.SendNotFound(); return(jsonAction); } ChunkComponent transfer = entity.Get <ChunkComponent>(); if (transfer == null) { channel.SendNotFound(); return(jsonAction); } JsonTransfer jsonTransfer = transfer.PopData(); if (!transfer.Finished) { jsonAction = JsonAction.Request; entity.Update(); } else { TransferMap.Remove(jsonId); } string jsonData = null; JsonChunk jsonChunk = null; if (jsonTransfer != null) { jsonData = jsonTransfer.Data; jsonChunk = jsonTransfer.Chunk; } // Response JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId) { Chunk = jsonChunk }; JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, jsonData); HttpResponse httpResponse = new HttpResponse() { Data = session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } else { // Data Entity entity = session.Owner; SearchListComponent search = entity.Get <SearchListComponent>(); JsonClient jsonClient = jsonRequestMessage.Client; // Request if (jsonClient == null && search.Empty) { channel.SendNotFound(); return(jsonAction); } // Response jsonId = SecurityUtil.CreateKeyString(); JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId); JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage); HttpResponse httpResponse = new HttpResponse() { Data = session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif // Data entity = new Entity(jsonId); EntityIdleComponent idle = new EntityIdleComponent(); entity.Add(idle); JsonChunk jsonChunk = jsonRequestMessage.Chunk; ChunkComponent transfer = new ChunkComponent(jsonChunk.Size, Options.ChunkSize, Options.MaxChunks); entity.Add(transfer); TransferMap.Add(entity); // Command string jsonData = jsonRequest.Data; jsonChunk = transfer.PopChunk(); if (jsonClient == null) { foreach (Entity e in search) { CommandState state = new CommandState() { Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e }; Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread)) { Priority = ThreadPriority.BelowNormal, IsBackground = true }; thread.Start(state); } } else { Entity e = ClientMap.Get(jsonClient.Id); if (e == null) { channel.SendNotFound(); return(jsonAction); } CommandState state = new CommandState() { Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e }; Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread)) { Priority = ThreadPriority.BelowNormal, IsBackground = true }; thread.Start(state); } jsonAction = JsonAction.Request; } return(jsonAction); }
public static Chunk jsonToChunk(string jsonData) { JsonChunk myDeserializedClass = JsonConvert.DeserializeObject <JsonChunk>(jsonData); return(myDeserializedClass.toChunk()); }
public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonUploadRequestMessage jsonRequestMessage = JsonUploadRequestMessage.Parse(jsonRequest.Message); // Data string jsonId = jsonRequestMessage.Id; Entity entity = TransferMap.Get(jsonId); if (entity == null) { channel.SendNotFound(); return(JsonAction.None); } JsonChunk jsonChunk = jsonRequestMessage.Chunk; ChunkComponent transfer = entity.Get <ChunkComponent>(); if (jsonChunk != null) { JsonTransfer jsonTransfer = new JsonTransfer() { Chunk = jsonChunk, Data = jsonRequest.Data }; transfer.PushData(jsonTransfer); } if (!transfer.HasChunk()) { channel.SendNotFound(); return(JsonAction.None); } int delay = 0; jsonChunk = null; if (transfer.HasDataCapacity()) { jsonChunk = transfer.PopChunk(); } else { delay = (int)DemonTimeout.File.TotalMilliseconds; } // Response JsonUploadResponseMessage jsonResponseMessage = new JsonUploadResponseMessage(jsonId) { Chunk = jsonChunk, Delay = delay }; JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage); HttpResponse httpResponse = new HttpResponse() { Data = session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG jsonRequest.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif return(JsonAction.Request); }
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; } }