private void ClientSocket_OnMessage(object sender, MessageEventArgs e)
        {
            if (!e.IsBinary)
            {
                return;
            }
            SimplePacket packet = StreamUtils.ReadPacket(e.RawData);

            lock (keepAliveSw)
                if (keepAliveSw.IsRunning)
                {
                    keepAliveSw.Restart();
                }
            switch (packet.Type)
            {
            case PacketType.TaskInput:
                if (Status != Packets.WorkerStatus.None)
                {
                    Logger.Log("Bad state");
                    clientSocket.Close();
                    return;
                }
                clientSocket.Send(new Packets.WorkerInfo()
                {
                    OkPart = 0,
                    Status = Packets.WorkerStatus.Working,
                    System = Platform
                }.GetPacket());
                Status = Packets.WorkerStatus.Working;
                StartProcessing(packet.Data);
                break;

            case PacketType.Signal:
                Packets.Signal signal = new Packets.Signal(packet);
                switch (signal.Type)
                {
                case Packets.SignalEnum.Abort:
                    workerThread.Abort();
                    while (!(workerThread.ThreadState == System.Threading.ThreadState.Stopped || workerThread.ThreadState == System.Threading.ThreadState.Unstarted || workerThread.ThreadState == System.Threading.ThreadState.Aborted))
                    {
                        Thread.Sleep(1);
                    }
                    Logger.Log("Aborted");
                    clientSocket.Send(new Packets.Signal()
                    {
                        Data = new byte[0],
                        Type = Packets.SignalEnum.Abort
                    }.GetPacket().GetBytes());
                    Status = Packets.WorkerStatus.None;
                    break;
                }
                break;
            }
            return;
        }
Example #2
0
 public static void WritePacket(Stream stream, SimplePacket packet)
 {
     WriteShort(stream, (short)packet.Type);
     stream.Write(packet.Data, 0, packet.Data.Length);
 }
        private void DoWork(object inputObj)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            logger.Debug("Started working");
            byte[] input = (byte[])inputObj;
            //CHANGE LATER ------->     \/
            IMainVoid mainVoid;

            try
            {
                Logger.Log($"Loading class {config["mainvoid"].Value<string>()}");
                object controllerInstance = Activator.CreateInstance(Type.GetType(config["mainvoid"].Value <string>()));
                mainVoid = (IMainVoid)controllerInstance;
                Logger.Log($"Class {config["mainvoid"].Value<string>()} loaded successfully");
            }
            catch (Exception e)
            {
                Logger.Log($"Can't load class {config["mainvoid"].Value<string>()}; Exception: {e}; Loading default class");
                mainVoid = new MainVoid();
            }
            mainVoid.Setup(config);
            //CHANGE LATER ------->     /\
            try
            {
                byte[] dataBytes = new SimplePacket()
                {
                    Type = PacketType.TaskOutput,
                    Data = mainVoid.DoIt(input, (x) =>
                    {
                        try
                        {
                            if (sw.ElapsedMilliseconds < 750)
                            {
                                return;
                            }
                            sw.Restart();
                            if (x < 0 || x > 1)
                            {
                                return;
                            }
                            if (!clientSocket.IsAlive)
                            {
                                return;
                            }
                            clientSocket.Send(new Packets.WorkerInfo()
                            {
                                OkPart = x,
                                Status = Packets.WorkerStatus.Working,
                                System = Platform
                            }.GetPacket());
                        }
                        catch
                        {
                        }
                    })
                }.GetBytes();
                Status = Packets.WorkerStatus.None;
                if (clientSocket.IsAlive)
                {
                    clientSocket.Send(dataBytes);
                }
            }
            catch (Exception e)
            {
                if (e is ThreadAbortException)
                {
                    Logger.Log($"Thread aborted: {e.Message}");
                }
                else
                {
                    try
                    {
                        if (clientSocket.IsAlive)
                        {
                            clientSocket.Send(new SimplePacket()
                            {
                                Type = PacketType.TaskOutput,
                                Data = Encoding.UTF8.GetBytes($"Exception: {e}")
                            }.GetBytes());
                        }
                    }
                    catch
                    {
                        Logger.Log($"Nice error: {e}");
                    }
                }
            }
            Status = Packets.WorkerStatus.None;
            logger.Debug("End working");
        }