//protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, string value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = Encoding.UTF8.GetBytes(value ?? ""); //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, byte[] value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = value; //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, int value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = convertToBytes(value); //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, long value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = convertToBytes(value); //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, decimal value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = convertToBytes(value); //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, bool value) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // throw new NotSupportedException(); // } // par.Value = convertToBytes(value); //} //protected virtual void SetCommandParameter(WSBinaryCommandType command, string name, object value) //{ // string xml = Dynamic.Core.Runtime.SerializationUtility.ToXmlString(value); // SetCommandParameter(command, name, xml); //} //private byte[] convertToBytes(int value) //{ // byte[] result = new byte[4]; // result[0] = (byte) value; // result[1] = (byte) (value >> 8); // result[2] = (byte) (value >> 0x10); // result[3] = (byte) (value >> 0x18); // return result; //} //private byte[] convertToBytes(ulong value) //{ // byte[] result = new byte[8]; // result[0] = (byte)value; // result[1] = (byte)(value >> 8); // result[2] = (byte)(value >> 0x10); // result[3] = (byte)(value >> 0x18); // result[4] = (byte)(value >> 0x20); // result[5] = (byte)(value >> 40); // result[6] = (byte)(value >> 0x30); // result[7] = (byte)(value >> 0x38); // return result; //} //private byte[] convertToBytes(decimal value) //{ // byte[] result = new byte[16]; // int[] bits = decimal.GetBits(value); // byte[] tmp = convertToBytes(bits[0]); // Array.Copy(tmp, 0, result, 0, 4); // tmp = convertToBytes(bits[1]); // Array.Copy(tmp, 0, result, 4, 4); // tmp = convertToBytes(bits[2]); // Array.Copy(tmp, 0, result, 8, 4); // tmp = convertToBytes(bits[3]); // Array.Copy(tmp, 0, result, 12, 4); // return result; //} //private byte[] convertToBytes(Boolean value) //{ // byte[] result = new byte[1]; // result[0] = value ? ((byte)1) : ((byte)0); // return result; //} //protected virtual string GetCommandParameterStringValue(WSBinaryCommandType command, string name) //{ // byte[] parValue = GetCommandParameterBinaryValue(command, name); // if (parValue == null) // { // return ""; // } // return Encoding.UTF8.GetString(parValue); //} //protected virtual byte[] GetCommandParameterBinaryValue(WSBinaryCommandType command, string name) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // return null; // } // return par.Value; //} //protected virtual int GetCommandParameterIntValue(WSBinaryCommandType command, string name) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // return int.MinValue; // } // return convertToInt(par.Value); //} //protected virtual long GetCommandParameterLongValue(WSBinaryCommandType command, string name) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // return long.MinValue; // } // return convertToLong(par.Value); //} //protected virtual decimal GetCommandParameterDecimalValue(WSBinaryCommandType command, string name) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // return decimal.MinValue; // } // return convertToDecimal(par.Value); //} //protected virtual Boolean GetCommandParameterBooleanValue(WSBinaryCommandType command, string name) //{ // WSBinaryCommandTypeParameter par = command.Parameters.FirstOrDefault(x => (x.Name ?? "").Equals(name, StringComparison.OrdinalIgnoreCase)); // if (par == null) // { // return false; // } // return convertToBoolean(par.Value); //} //protected virtual T GetCommandParameterObjectValue<T>(WSBinaryCommandType command, string name) //{ // string xml = GetCommandParameterStringValue(command, name); // if (String.IsNullOrEmpty(xml)) // { // return default(T); // } // return Dynamic.Core.Runtime.SerializationUtility.ToObject<T>(xml); //} //private int convertToInt(byte[] value) //{ // return (((value[0] | (value[1] << 8)) | (value[2] << 0x10)) | (value[3] << 0x18)); //} //private long convertToLong(byte[] value) //{ // uint num = (uint)(((value[0] | (value[1] << 8)) | (value[2] << 0x10)) | (value[3] << 0x18)); // uint num2 = (uint)(((value[4] | (value[5] << 8)) | (value[6] << 0x10)) | (value[7] << 0x18)); // return (long)((num2 << 0x20) | num); //} //private decimal convertToDecimal(byte[] value) //{ // int[] bits = new int[4]; // byte[] tmps = new byte[4]; // Array.Copy(value, 0, tmps, 0, 4); // bits[0] = convertToInt(tmps); // Array.Copy(value, 4, tmps, 0, 4); // bits[1] = convertToInt(tmps); // Array.Copy(value, 8, tmps, 0, 4); // bits[2] = convertToInt(tmps); // Array.Copy(value, 12, tmps, 0, 4); // bits[3] = convertToInt(tmps); // return new decimal(bits); //} //private bool convertToBoolean(byte[] value) //{ // return (value[0] != 0); //} #region ICommand 成员 public virtual bool CanExecute(Dynamic.Net.WebSocket.CommandProtocol.WSCommandTypeBase command) { if (command == null) { return(false); } if (command.CommandName == this.Name && command.CommandType == this.Type && command.ParameterCount == this.commandParameters.Count) { if (this.commandParameters.Count == 0) { return(true); } bool isSuccess = true; this.commandParameters.All(x => { if (!command.HasParameter(x)) { isSuccess = false; return(false); } return(true); }); return(isSuccess); } return(false); }
public override ICommand Parse(WSCommandTypeBase command) { SetCommandParserRequest cmd = new SetCommandParserRequest(); cmd.LoadCommand(command); return(cmd); }
public void Broadcast(WSCommandTypeBase command, Func <WebSocketSessionBase, bool> checkFunc) { if (command == null) { return; } if (SessionList == null || SessionList.Count == 0) { return; } lock (Application.SyncLocker) { SessionList.Where(x => { if (x.Value == null) { return(false); } return(checkFunc(x.Value.Session)); }).All(session => { if (session.Value != null) { try { session.Value.SendCommand(command); } catch { } } return(true); }); } }
public override ICommand Execute(WSCommandTypeBase command, ExecuteCommandContext context) { SetCommandParserRequest cmd = new SetCommandParserRequest(command); SetCommandParserResponse response = new SetCommandParserResponse(); response.IsSuccess = false; if (context != null && context.CommandSession != null) { if (Parsers.ContainsKey(cmd.Type)) { context.CommandSession.CommandParser = Parsers[cmd.Type]; response.IsSuccess = true; } else { response.Message = "未知的命令格式"; } } else { response.Message = "内部错误,会话为空"; } return(response); }
public void Broadcast(WSCommandTypeBase command) { if (command == null) { return; } if (SessionList == null || SessionList.Count == 0) { return; } lock (Application.SyncLocker) { SessionList.All(session => { if (session.Value != null) { try { session.Value.SendCommand(command); } catch { } } return(true); }); } }
public virtual CommandItem <WSCommandTypeBase> SendCommandSync(WSCommandTypeBase command, TimeSpan timeout) { if (command == null) { return(null); } command.RequestID = Guid.NewGuid(); Session.LastRequestTime = DateTime.Now; CommandItem <WSCommandTypeBase> ci = new CommandItem <WSCommandTypeBase>() { CommandRequest = command, CommandResponse = null, RetryCount = 0, ResponseCallback = null, RequestTime = DateTime.Now, IsSync = true, Wait = new System.Threading.AutoResetEvent(false) }; lock (SyncLocker) { Commands.Add(ci); } logger.Trace("发送命令:{0}", command.CommandName); Session.SendMessage(command.ToBinary(), command.TransferEncoder); ci.Wait.WaitOne(timeout); return(ci); }
public virtual void SendCommand(CommandSession session, WSCommandTypeBase command, CommandResponse callback) { if (session == null) { return; } session.SendCommand(command, callback); }
public virtual void SendReplyCommand(WSCommandTypeBase command) { if (command == null) { return; } Session.SendMessage(CommandParser.ToBinary(command), CommandParser.TransferEncoder); }
public override bool IsPairCommand(WSCommandTypeBase command) { if (command is WSBinaryCommandType) { return((command as WSBinaryCommandType).RequestID == this.RequestID); } return(false); }
protected WSCommandTypeBase GetRequestCommand(MessageReceivedEventArgs e, out ICommandParser cmdParser) { WSCommandTypeBase cmd = null; cmdParser = CommandParser.FirstOrDefault(c => c.CanRead(e, out cmd)); if (cmdParser == null) { return(null); } return(cmd); }
public void SetReplyCommandData(WSCommandTypeBase command, MessageReceivedEventArgs args) { if (ContentType == MessageContentType.Text) { args.ResponseContent = SerializationUtility.ToXmlString(command); } else if (ContentType == MessageContentType.Binary) { args.ResponseData = BinaryCommandTypeSerializer.ToBinary(command as WSBinaryCommandType); } throw new NotSupportedException("不支持的命令格式:" + ContentType.ToString()); }
public virtual void SendReplyCommand(ICommand command, Guid requestGuid) { if (command == null) { return; } WSCommandTypeBase wsCommand = CommandParser.Create(); command.ToCommand(wsCommand); wsCommand.RequestID = requestGuid; Session.SendMessage(CommandParser.ToBinary(wsCommand), CommandParser.TransferEncoder); }
public virtual void GetEmptyCommand(WSCommandTypeBase command) { command.CommandName = this.Name; command.CommandType = this.Type; if (commandParameters.Count > 0) { command.CreateParameters(commandParameters.Count); for (int i = 0; i < commandParameters.Count; i++) { command.AddParameter(commandParameters[i], i); } } }
public override bool CanRead(MessageReceivedEventArgs args, out WSCommandTypeBase command) { command = null; if (args.ContentType == TransferEncoder) { if (!String.IsNullOrEmpty(args.Content) && args.Content.StartsWith("{XML}")) { command = Dynamic.Core.Runtime.SerializationUtility.ToObject <WSCommandType>(args.Content.Substring(5)); return(true); } } return(false); }
public override bool CanRead(MessageReceivedEventArgs args, out WSCommandTypeBase command) { command = null; if (args.ContentType == TransferEncoder) { if (args.Data != null && args.Data.Length >= 2 && args.Data[0] == 0 && args.Data[1] == 0) { command = BinaryCommandTypeSerializer.ToCommandType(args.Data); return(true); } } return(false); }
public WSCommandTypeBase GetCommand(MessageReceivedEventArgs args) { WSCommandTypeBase command = null; switch (ContentType) { case MessageContentType.Text: command = SerializationUtility.ToObject <WSCommandType>(args.Content); break; case MessageContentType.Binary: command = BinaryCommandTypeSerializer.ToCommandType(args.Data); break; } return(command); }
protected void SetReplyCommand(CommandSession client, ICommand replayCmd, MessageReceivedEventArgs e, Guid requestGuid, ICommandParser cmdParser) { if (replayCmd != null) { WSCommandTypeBase replyCommandType = cmdParser.Create(); replayCmd.ToCommand(replyCommandType); replyCommandType.RequestID = requestGuid; cmdParser.SetReplyCommand(e, replyCommandType); if (IsAync) { //异步需要自己向Session发送消息 client.Session.SendMessage(cmdParser.ToBinary(replyCommandType), cmdParser.TransferEncoder); } } }
public virtual void SendCommand(WSCommandTypeBase command, CommandResponse callback) { if (command == null) { return; } command.RequestID = Guid.NewGuid(); Session.LastRequestTime = DateTime.Now; lock (SyncLocker) { Commands.Add(new CommandItem <WSCommandTypeBase>() { CommandRequest = command, CommandResponse = null, RetryCount = 0, ResponseCallback = callback, RequestTime = DateTime.Now, IsSync = false }); } logger.Trace("发送命令:{0}", command.CommandName); Session.SendMessage(command.ToBinary(), command.TransferEncoder); }
// protected abstract void SetCommandParameters(WSCommandTypeBase command); // public abstract ICommand Parse(WSCommandTypeBase command); public override ICommand Execute(WSCommandTypeBase command, ExecuteCommandContext context) { TRequest request = Parse(command) as TRequest; TReply reply = null; if (typeof(TReply) == typeof(EmptyReplyCommand)) { reply = EmptyReplyCommand.Empty as TReply; } EventCommandOnExecuteEventArgs <TRequest, TReply> args = new EventCommandOnExecuteEventArgs <TRequest, TReply>(request, reply); OnExecuteCommand(args); if (args.ReplyCommand != null && !(args.ReplyCommand is EmptyReplyCommand)) { return(args.ReplyCommand); } return(null); }
public override byte[] ToBinary(WSCommandTypeBase command) { return(BinaryCommandTypeSerializer.ToBinary(command as WSBinaryCommandType)); }
public bool ProcessMessage(MessageReceivedEventArgs e, List <ICommand> commandList) { ICommandParser cmdParser; WSCommandTypeBase cmd = GetRequestCommand(e, out cmdParser); if (cmd == null) { return(false); } bool isProcessed = false; if (cmd != null) { // 关于是否触发CommandReceived事件: // 当使用带有回调方法的SendCommand发送命令时,接收到应答命令时不会触发CommandReceived事件 // 当使用SendCommandSync方法发送同步返回的命令时,接收到应答命令时不会触发CommandReceived事件 // 当接收到的命令能够被内置的CommandList处理时,会触发CommandReceived事件,并且自动设置ReplyCommand为执行完后返回的命令, // 同时将事件参数中的IsUnknown属性设置为false。 // 当接收到的命令不属于上述任一情况时,将触发CommandReceived事件,并将IsUnknown属性设置为true CommandItem <WSCommandTypeBase> ci = null; lock (Session.SyncLocker) { ci = Session.Commands.OfType <CommandItem <WSCommandTypeBase> >().FirstOrDefault(x => x.CommandRequest != null && x.CommandRequest.IsPairCommand(cmd)); } CommandReceivedEventArgs <WSCommandTypeBase> args = new CommandReceivedEventArgs <WSCommandTypeBase>(cmd); args.IsUnknwon = true; // 检查命令是否能被内置命令列表处理 ICommand exeCommand = commandList.FirstOrDefault(x => x.CanExecute(cmd)); ICommand replyCommand = null; bool isFireReceivedEvent = true; if (exeCommand != null) { try { replyCommand = exeCommand.Execute(cmd, new ExecuteCommandContext() { CommandSession = Session }); isProcessed = true; } catch (Exception ex) { logger.Error("执行命令失败:{0} \r\n{1}", cmd.CommandName, ex.ToString()); } args.IsUnknwon = false; } args.Session = Session; if (ci != null) { ci.CommandResponse = cmd; args.RequestCommand = ci.CommandRequest; lock (Session.SyncLocker) { if (cmd.IsOver) { Session.Commands.Remove(ci); } } if (ci.ResponseCallback != null) { ci.ResponseCallback(ci); isFireReceivedEvent = false; } if (ci.IsSync) { ci.Wait.Set(); isFireReceivedEvent = false; } } if (isFireReceivedEvent) { onCommandReceived(args); } SetReplyCommand(Session, replyCommand, e, cmd.RequestID, cmdParser); } return(isProcessed); }
//protected byte[] GetCommandValue(string name) //{ //} //public virtual WSBinaryCommandType Create() //{ // Command = CreateCommand(); // return Command; //} public abstract void LoadCommand(WSCommandTypeBase command);
public override void LoadCommand(WSCommandTypeBase command) { Type = command.GetCommandParameterStringValue("Type"); }
public SetCommandParserRequest(WSCommandTypeBase command) : this() { LoadCommand(command); }
protected override void SetCommandParameters(WSCommandTypeBase command) { command.SetCommandParameter("Type", Type); }
//public WSCommandTypeBase GetEmptyCommand(IWebSocketCommandFactory commandFactory) //{ // return null; //} public ICommand Execute(WSCommandTypeBase command, ExecuteCommandContext context) { return(null); }
public void ToCommand(WSCommandTypeBase command) { }
public ICommand Parse(WSCommandTypeBase command) { return(Empty); }
public bool CanExecute(WSCommandTypeBase command) { return(false); }
public override void SetReplyCommand(MessageReceivedEventArgs args, WSCommandTypeBase command) { args.ResponseData = BinaryCommandTypeSerializer.ToBinary(command as WSBinaryCommandType); }