/// <summary> /// 初始化会话信息 /// </summary> /// <param name="readBuffer">请求的缓存数据</param> /// <param name="offset">起始位置</param> /// <param name="length">长度</param> /// <returns>组包后转化成DataSyncRequestInfo对象</returns> protected override SocketRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length) { if (length <= CommandRuleProvider.BytesLength) { return(NullRequestInfo); } var startMarkLength = BeginMark.Length; var endMarkLength = EndMark.Length; var requestBody = new byte[readBuffer.Length - offset - endMarkLength - startMarkLength - CommandRuleProvider.BytesLength]; Array.Copy(readBuffer, offset + startMarkLength + CommandRuleProvider.BytesLength, requestBody, 0, requestBody.Length); var entry = readBuffer[offset + startMarkLength]; var cmdCode = new byte[CommandRuleProvider.BytesLength - 1]; Array.Copy(readBuffer, offset + startMarkLength + 1, cmdCode, 0, cmdCode.Length); var routeRule = new CommandRule() { Command = cmdCode, Entry = entry }; if (!CommandRuleProvider.Verfy(routeRule))//无效请求,路由错误 { return(NullRequestInfo); } var requestInfo = new SocketRequestInfo(routeRule, CommandRuleProvider, requestBody); return(requestInfo); }
/// <summary> /// 验证路由是否符合规则 /// </summary> /// <param name="routeRule">路由</param> /// <param name="enableThrowException">验证失败是否抛出异常,默认为false</param> /// <returns>验证结果</returns> public bool Verfy(CommandRule routeRule, bool enableThrowException = false) { if (routeRule.Entry == 0) { if (enableThrowException) { throw new StackOverflowException("命令路由起点不能为0x00!"); } else { return(false); } } if (routeRule.Command.Length != 2) { if (enableThrowException) { throw new StackOverflowException("命令路由长度无效!"); } else { return(false); } } return(true); }
public BaseCommand(byte version, byte[] code) { Route = new CommandRule() { Entry = version, Command = code }; CommandRuleProvider = new CommandRuleProvider(); }
/// <summary> /// 获取命令格式化名称 /// </summary> /// <param name="routeRule"></param> /// <returns></returns> public string GetCommandName(CommandRule routeRule) { Verfy(routeRule, true); return(string.Format(RouteFormatString, routeRule.Entry, routeRule.Command[0], routeRule.Command[1])); }