コード例 #1
0
 public MultiServerClient(MasterHandler handler, ClientInfo info, WebSocketSession session)
 {
     this.handler = handler;
     this.info    = info;
     this.session = session;
     timeoutTimer = new Timer(x =>
     {
         if (status == ClientWorkerStatus.Connecting)
         {
             session.Close(SuperSocket.SocketBase.CloseReason.TimeOut);
         }
     }, null, 2000, Timeout.Infinite);
     keepAliveSw    = new Stopwatch();
     keepAliveTimer = new Timer(x =>
     {
         if (!session.Connected)
         {
             return;
         }
         lock (keepAliveSw)
         {
             if (!keepAliveSw.IsRunning)
             {
                 keepAliveSw.Start();
             }
             if (keepAliveSw.ElapsedMilliseconds > 5000)
             {
                 handler.Logger.Log($"Client {session.SessionID} is disconnected with idle of {keepAliveSw.ElapsedMilliseconds}");
                 keepAliveSw.Stop();
                 handler.RemoveClient(session.SessionID);
                 session.Close();
                 status = ClientWorkerStatus.None;
                 keepAliveTimer.Change(Timeout.Infinite, Timeout.Infinite);
                 return;
             }
         }
         SendPacket(new SimplePacket()
         {
             Data = new byte[0],
             Type = PacketType.Nop
         });
     }, null, 0, 1000);
 }
コード例 #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;
            }
        }