Пример #1
0
 public void SendPacket(SimplePacket packet)
 {
     byte[] data = packet.GetBytes();
     session.Send(data, 0, data.Length);
 }
Пример #2
0
        public void OnMessage(byte[] data)
        {
            SimplePacket packet = StreamUtils.ReadPacket(data);

            lock (keepAliveSw)
                if (keepAliveSw.IsRunning)
                {
                    keepAliveSw.Restart();
                }
            switch (packet.Type)
            {
            case PacketType.WorkerInfo:
                if (Status == ClientWorkerStatus.Connecting)
                {
                    return;
                }
                Packets.WorkerInfo info = new Packets.WorkerInfo(packet);
                System = info.System;
                switch (info.Status)
                {
                case Packets.WorkerStatus.Error:
                    LastProgress = 0;
                    errorCallback?.Invoke(ErrorType.WorkerError);
                    errorCallback          = null;
                    progressChangeCallback = null;
                    workCallback           = null;
                    break;

                case Packets.WorkerStatus.Working:
                    LastProgress = info.OkPart;
                    status       = ClientWorkerStatus.Working;
                    progressChangeCallback?.Invoke(info.OkPart);
                    break;
                }
                break;

            case PacketType.TaskOutput:
                if (status != ClientWorkerStatus.Working)
                {
                    return;
                }
                LastProgress = 0;
                workCallback?.Invoke(packet.Data);
                errorCallback          = null;
                progressChangeCallback = null;
                workCallback           = null;
                break;

            case PacketType.WorkerAuth:
                if (status != ClientWorkerStatus.Connecting)
                {
                    return;
                }
                Packets.WorkerAuth response = new Packets.WorkerAuth(packet);
                if (!response.CheckPassword(Encoding.UTF8.GetBytes(this.info.Config["password"].Value <string>())))
                {
                    handler.RemoveClient(session.SessionID);
                    session.Close();
                    return;
                }
                status = ClientWorkerStatus.None;
                break;

            case PacketType.Signal:
                Packets.Signal signal = new Packets.Signal(packet);
                if (signal.Type == Packets.SignalEnum.Abort)
                {
                    if (status != ClientWorkerStatus.Working)
                    {
                        return;
                    }
                    LastProgress = 0;
                    errorCallback?.Invoke(ErrorType.TaskAbort);
                    errorCallback          = null;
                    progressChangeCallback = null;
                    workCallback           = null;
                }
                break;
            }
        }
Пример #3
0
 public static void WritePacket(Stream stream, SimplePacket packet)
 {
     WriteShort(stream, (short)packet.Type);
     stream.Write(packet.Data, 0, packet.Data.Length);
 }