public RenderResponse Packet_Render(RenderRequest req)
        {
            if (!_blender.IsVersionAvailable(req.Version))
            {
                return new RenderResponse()
                       {
                           TaskID  = req.TaskID,
                           Success = false,
                           Message = "Version not prepared.."
                       }
            }
            ;

            try
            {
                //Validate Settings
                string filePath = SessionData.GetFilePath(req.SessionID);

                if (filePath == null)
                {
                    return new RenderResponse()
                           {
                               TaskID  = req.TaskID,
                               Success = false,
                               Message = "Blend file was not available"
                           }
                }
                ;

                if (req.Settings == null)
                {
                    req.Settings = new Shared.RenderPacketModel();
                }
                if (req.Settings.Cores <= 0)
                {
                    req.Settings.Cores = Environment.ProcessorCount;
                }

                req.Settings.Cores = Math.Min(Environment.ProcessorCount, req.Settings.Cores);

                DateTime lastUpdate = DateTime.Now;

                List <string> exceptions = new List <string>();

                //Render
                string file = _blender.Render(req.Version, filePath,
                                              BlenderRenderSettings.FromRenderSettings(req.Settings),
                                              (process) =>
                {
                    process.OnBlenderStatus += (state) =>
                    {
                        if (DateTime.Now.Subtract(lastUpdate).TotalMilliseconds > UPDATE_TIMING_MS)
                        {
                            lastUpdate = DateTime.Now;
                            SendPacket(new RenderInfoResponse()
                            {
                                TaskID        = req.TaskID,
                                TilesFinished = state.TilesFinish,
                                TilesTotal    = state.TilesTotal,
                                Time          = state.Time,
                                TimeRemaining = state.TimeRemaining
                            });
                        }
                    };
                    process.OnBlenderException += (excp) => exceptions.Add(excp);
                });

                //Handle Result
                if (file == null || !File.Exists(file))
                {
                    if (exceptions.Count == 0)
                    {
                        return new RenderResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = "Missing Files?"
                               }
                    }
                    ;
                    else
                    {
                        return new RenderResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = string.Join(", ", exceptions)
                               }
                    };
                }
                else
                {
                    byte[] data = File.ReadAllBytes(file);

                    File.Delete(file);
                    return(new RenderResponse()
                    {
                        Success = true,
                        TaskID = req.TaskID,
                        Data = data
                    });
                };
            }
            catch (Exception ex)
            {
                return(new RenderResponse()
                {
                    TaskID = req.TaskID,
                    Success = false,
                    Message = "Exception:" + ex.Message
                });
            }
        }
        public RenderBatchResponse Packet_RenderBatch(RenderBatchRequest req)
        {
            if (!_blender.IsVersionAvailable(req.Version))
            {
                return new RenderBatchResponse()
                       {
                           TaskID  = req.TaskID,
                           Success = false,
                           Message = "Version not prepared.."
                       }
            }
            ;

            try
            {
                //Validate Settings
                string filePath = SessionData.GetFilePath(req.SessionID);

                if (filePath == null)
                {
                    return new RenderBatchResponse()
                           {
                               TaskID  = req.TaskID,
                               Success = false,
                               Message = "Blend file was not available"
                           }
                }
                ;

                for (int i = 0; i < req.Settings.Count; i++)
                {
                    Shared.RenderPacketModel settings = req.Settings[i];

                    if (settings == null)
                    {
                        settings = new Shared.RenderPacketModel();
                    }
                    if (settings.Cores <= 0)
                    {
                        settings.Cores = Environment.ProcessorCount;
                    }

                    settings.Cores = Math.Min(Environment.ProcessorCount, settings.Cores);
                }

                BlenderRenderSettings[] batch = req.Settings.Select(x => BlenderRenderSettings.FromRenderSettings(x)).ToArray();

                DateTime lastUpdate = DateTime.Now;

                List <string> exceptions = new List <string>();

                //Render
                List <string> files = _blender.RenderBatch(req.Version, filePath, batch,
                                                           (process) =>
                {
                    process.OnBlenderStatus += (status) =>
                    {
                        if (DateTime.Now.Subtract(lastUpdate).TotalMilliseconds > UPDATE_TIMING_MS)
                        {
                            lastUpdate = DateTime.Now;
                            SendPacket(new RenderInfoResponse()
                            {
                                TaskID        = req.TaskID,
                                TilesFinished = status.TilesFinish,
                                TilesTotal    = status.TilesTotal,
                                Time          = status.Time,
                                TimeRemaining = status.TimeRemaining
                            });
                        }
                    };
                    process.OnBlenderCompleteTask += (taskID) =>
                    {
                        BlenderRenderSettings settings = batch.FirstOrDefault(x => x.TaskID == taskID);
                        if (settings != null)
                        {
                            SendPacket(new RenderBatchResult()
                            {
                                Data    = File.ReadAllBytes(settings.Output),
                                Success = true,
                                TaskID  = settings.TaskID
                            });
                        }
                    };
                    process.OnBlenderException += (excp) => exceptions.Add(excp);
                });

                //Handle Result
                if (files == null || files.Count != req.Settings.Count)
                {
                    if (exceptions.Count == 0)
                    {
                        return new RenderBatchResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = "Missing Files?"
                               }
                    }
                    ;
                    else
                    {
                        return new RenderBatchResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = string.Join(", ", exceptions)
                               }
                    };
                }
                else
                {
                    //Cleanup
                    //string data = Convert.ToBase64String(File.ReadAllBytes(file));
                    foreach (string file in files)
                    {
                        File.Delete(file);
                    }
                    if (exceptions.Count > 0)
                    {
                        return(new RenderBatchResponse()
                        {
                            Success = false,
                            TaskID = req.TaskID,
                            SubTaskIDs = req.Settings.Select(X => X.TaskID).ToList(),
                            Message = string.Join(", ", exceptions)
                        });
                    }
                    else
                    {
                        return(new RenderBatchResponse()
                        {
                            Success = true,
                            TaskID = req.TaskID,
                            SubTaskIDs = req.Settings.Select(X => X.TaskID).ToList()
                        });
                    }
                };
            }
            catch (Exception ex)
            {
                return(new RenderBatchResponse()
                {
                    TaskID = req.TaskID,
                    Success = false,
                    Message = "Exception:" + ex.Message
                });
            }
        }