protected void HandleServerError(MessageHandler messageHandler) { Log.Info(this, $"Handle message {nameof(HandleServerError)}"); var message = messageHandler.ReadMessage <ServerErrorMessage>(); //OnServerError(message); ServerErrorEvent?.Invoke(this, (message.errorCode, message.errorMessage)); }
void HandleWebSocketFrame(IChannelHandlerContext ctx, IByteBufferHolder frame) { switch (frame) { case CloseWebSocketFrame _: _handShaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain()); ConsoleHelper.ConDepServerWriteLine($"连接{ctx.Channel.Id}已断开"); return; case PingWebSocketFrame _: ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain())); return; case PongWebSocketFrame _: ctx.WriteAsync(new PingWebSocketFrame((IByteBuffer)frame.Content.Retain())); return; case TextWebSocketFrame _: try { string commandString = frame.Content.ReadString(frame.Content.WriterIndex, Encoding.UTF8); var command = commandString.JsonToObject <Command>(); if (_webSocketConfig.NotShowCommandBlackList == null || _webSocketConfig.NotShowCommandBlackList.Length == 0 || !_webSocketConfig.NotShowCommandBlackList.Contains(command.HandlerName)) { ConsoleHelper.ConDepServerWriteLine(command.HandlerName); } if (CanLoginSuccess(command)) { Task.Run(async() => await SendCommand(ctx, commandString, command)); } else { Task.Run(async() => { var @event = new ServerErrorEvent { Status = 401, Message = "未登录" }; await ctx.Channel.SendJsonEventAsync(@event); }); } } catch (Exception ex) { ConsoleHelper.ConDepServerErrorWriteLine(ex); _logger.LogCritical(ex, ex.Message); } return; case BinaryWebSocketFrame binaryFrame: ctx.WriteAndFlushAsync(binaryFrame.Retain()); break; } }
public override async Task ExcuteAsync(IChannelHandlerContext ctx, object commandData) { try { UploadPartCommand command = GetCommand(commandData); await _uploadPoolService.LoadBuffer(ctx.Channel, command); } catch (InvalidOperationException ex) { var @event = new ServerErrorEvent(ex); await ctx.Channel.SendJsonEventAsync(@event); } }
/// <summary> /// 更新APP文件 /// </summary> /// <param name="path"></param> /// <param name="channel"></param> /// <returns></returns> private async Task UpdateAppFileAsync(string path, IChannel channel) { var appManager = ApplicationData.GetService <IAppManager>(); string tempPath = $"{workingDirectory}Temp/{StringManager.GetRandomStrByGuid()}"; DirectoryInfo tempDirectoryInfo = null; if (!Directory.Exists(tempPath)) { tempDirectoryInfo = Directory.CreateDirectory(tempPath); } if (tempDirectoryInfo == null) { tempDirectoryInfo = new DirectoryInfo(tempPath); } var cmdManager = new CmdManager(); await cmdManager.RunCmdCommandsAsync($"unrar x -o+ -y {path} {tempPath}"); DirectoryInfo[] directoryInfos = tempDirectoryInfo.GetDirectories(); string[] paths = directoryInfos.Select(m => m.Name).ToArray(); appManager.StopAppByPaths(paths); foreach (DirectoryInfo directoryInfo in directoryInfos) { try { string dirPath = $"{workingDirectory}{directoryInfo.Name}"; CopyDirectory(directoryInfo, dirPath); } catch (Exception ex) { var @event = new ServerErrorEvent { Status = 500, Message = ex.Message }; await channel.SendJsonEventAsync(@event); } finally { directoryInfo.Delete(true); } } tempDirectoryInfo.Delete(true); }
/// <summary> /// 更新APP文件 /// </summary> /// <param name="path"></param> /// <param name="channel"></param> /// <returns></returns> private async Task UpdateWebFileAsync(string path, IChannel channel) { var fileService = ApplicationData.GetService <IWebFileService>(); string tempPath = $"{AppDomain.CurrentDomain.BaseDirectory}Temp/{StringManager.GetRandomStrByGuid()}"; DirectoryInfo tempDirectoryInfo = null; if (!Directory.Exists(tempPath)) { tempDirectoryInfo = Directory.CreateDirectory(tempPath); } if (tempDirectoryInfo == null) { tempDirectoryInfo = new DirectoryInfo(tempPath); } var cmdManager = new CmdManager(); await cmdManager.RunCmdCommandsAsync($"unrar x -o+ -y {path} {tempPath}"); DirectoryInfo[] directoryInfos = tempDirectoryInfo.GetDirectories(); foreach (DirectoryInfo directoryInfo in directoryInfos) { try { string targetPath = $"{workingDirectory}{directoryInfo.Name}"; CopyDirectory(directoryInfo, targetPath); fileService.InitServices(); } catch (Exception ex) { var @event = new ServerErrorEvent { Status = 500, Message = ex.Message }; await channel.SendJsonEventAsync(@event); } finally { directoryInfo.Delete(true); } } tempDirectoryInfo.Delete(true); }
/// <summary> /// 发送命令 /// </summary> /// <param name="ctx"></param> /// <param name="commandString"></param> /// <param name="command"></param> /// <returns></returns> private async Task SendCommand(IChannelHandlerContext ctx, string commandString, Command command) { try { await _commandBus.SendAsync(ctx, commandString, command); } catch (InvalidOperationException ex) { var @event = new ServerErrorEvent { Message = ex.Message }; await ctx.Channel.SendJsonEventAsync(@event); } catch (Exception ex) { var @event = new ServerErrorEvent(); await ctx.Channel.SendJsonEventAsync(@event); ConsoleHelper.ConDepServerErrorWriteLine(ex); _logger.LogCritical(ex, ex.Message); } }