Esempio n. 1
0
        private void WebSocketServer_NewMessageReceived(WebSocketSession session, string value)
        {
            try
            {
                if (value.Length > 16384)
                {
                    return;
                }
                WebSocketPackets.Packet packet = new WebSocketPackets.Packet();
                packet.DeserializeJson(value);
                switch (packet.PacketType)
                {
                case WebSocketPackets.ControlPacketType.AuthRequest:
                    WebSocketPackets.AuthPacket authPacket = new WebSocketPackets.AuthPacket();
                    authPacket.DeserializeJson(packet.JsonData);
                    PermissionLevel level, newLevel;
                    newLevel = dataController.GetPermissionByCode(Encoding.UTF8.GetString(authPacket.PasswordData));
                    if (newLevel == PermissionLevel.WrongPassword)
                    {
                        session.Send(new WebSocketPackets.InfoPacket()
                        {
                            Level = newLevel
                        }.SerializeJson());
                    }
                    else
                    {
                        while (!clients.TryRemove(session.SessionID, out level))
                        {
                            Thread.Sleep(1);
                        }
                        while (!clients.TryAdd(session.SessionID, newLevel))
                        {
                            Thread.Sleep(1);
                        }
                        session.Send(new WebSocketPackets.InfoPacket()
                        {
                            Level = newLevel
                        }.SerializeJson());
                        if (newLevel >= PermissionLevel.Workers)
                        {
                            session.Send(new WebSocketPackets.GeneralInfoPacket()
                            {
                                TasksCount  = dataController.TaskQueueGetCount(),
                                WorkerCount = workers.Count
                            }.SerializeJson());
                            session.Send(new WebSocketPackets.StartCPanelPacket()
                            {
                                Workers = workers.Values.Select(x => new WebSocketPackets.WorkerInfo()
                                {
                                    WorkerProgress = x.LastProgress,
                                    WorkerId       = x.GetId(),
                                    WorkerStatus   = x.Status,
                                    WorkerSystem   = x.System,
                                    CurrentTask    = (newLevel < PermissionLevel.WorkersTasks ? null : (dataController.CurrentTasksGetByKey(x.GetId()) != null ? new WebSocketPackets.TaskInfo(dataController.CurrentTasksGetByKey(x.GetId()), WebSocketPackets.TaskStatus.Processing) : null))
                                }).ToArray(),
                                ReadyTasks   = dataController.ReadyTasksGetAll().Select(x => new WebSocketPackets.TaskInfo(x, WebSocketPackets.TaskStatus.Ready)).ToArray(),
                                InQueueTasks = dataController.TaskQueueGetAll().Select(x => new WebSocketPackets.TaskInfo(x, WebSocketPackets.TaskStatus.InQueue)).ToArray()
                            }.SerializeJson());
                        }
                    }
                    break;

                case WebSocketPackets.ControlPacketType.NewTask:
                    if (clients[session.SessionID] >= PermissionLevel.CreateTasks)
                    {
                        WebSocketPackets.TaskInputPacket inputPacket = new WebSocketPackets.TaskInputPacket();
                        inputPacket.DeserializeJson(packet.JsonData);
                        if (inputPacket.Data.Length > 65536)
                        {
                            break;
                        }
                        SendTask(inputPacket.Data);
                    }
                    break;

                case WebSocketPackets.ControlPacketType.AbortWorker:
                    WebSocketPackets.AbortWorkerPacket tPacket = new WebSocketPackets.AbortWorkerPacket();
                    tPacket.DeserializeJson(packet.JsonData);
                    if (!workers.ContainsKey(tPacket.WorkerId))
                    {
                        break;
                    }
                    workers[tPacket.WorkerId].SendPacket(new Packets.Signal()
                    {
                        Data = new byte[0],
                        Type = Packets.SignalEnum.Abort
                    }.GetPacket());
                    break;

                case WebSocketPackets.ControlPacketType.RemoveTask:
                    WebSocketPackets.RemoveTask removeTaskPacket = new WebSocketPackets.RemoveTask();
                    removeTaskPacket.DeserializeJson(packet.JsonData);
                    dataController.ReadyTasksRemove(removeTaskPacket.TaskId);
                    dataController.TaskQueueRemove(removeTaskPacket.TaskId);
                    SendToWebSocket(new WebSocketPackets.CPanelTaskChanged()
                    {
                        TaskEventType = WebSocketPackets.TaskEventType.TaskRemoved,
                        Task          = new WebSocketPackets.TaskInfo(new Task()
                        {
                            Id = removeTaskPacket.TaskId
                        }, WebSocketPackets.TaskStatus.Processing)
                    });
                    SendToWebSocket(new WebSocketPackets.GeneralInfoPacket()
                    {
                        TasksCount  = dataController.TaskQueueGetCount(),
                        WorkerCount = workers.Count
                    });
                    break;
                }
            }
            catch (Exception e)
            {
                Logger.Debug($"Exception on WS packet: {e}");
            }
        }