/// <summary> /// 命令执行函数 /// </summary> /// <param name="Command">命令</param> /// <returns>执行结果</returns> public override BasisCommandResult Execute(BasisEventCommand Command) { BasisCommandResult result = new BasisCommandResult(); LDMachine terminal = (LDMachine)Command.Session; LaidianCommandModel cmd = (LaidianCommandModel)Command.Content; if (cmd.MessageType == MessageType.HEARTBEAT) { /*---------------------- * 1、更新时间 * -----------------------*/ terminal.ComLinkLastTime = DateTime.Now; } else { if (next == null) { // log.Info("用户" + client.ApplicationID + "执行的ID为" + Command.EventCommandID + "的指令没有对应执行器."); result.ResultType = BasisCommandResult.CommandResultTypeEnum.NoRespnoseExecutor; result.Content = null; return(result); } else { return(this.next.Execute(Command)); } } return(result); }
/// <summary> /// 命令执行函数 /// </summary> /// <param name="Command">命令</param> /// <returns>执行结果</returns> public override BasisCommandResult Execute(BasisEventCommand Command) { BasisCommandResult result = new BasisCommandResult(); LDMachine terminal = (LDMachine)Command.Session; LaidianCommandModel cmd = (LaidianCommandModel)Command.Content; if (cmd.MessageType == MessageType.LAIDIAN_BUYLINE_REQ) { //cmd.Result = 0; int lineType = 0; /*---------------------- * 1、发送售线指令 * -----------------------*/ //DN.cdport.SaleLine(); } else { if (next == null) { result.ResultType = BasisCommandResult.CommandResultTypeEnum.NoRespnoseExecutor; result.Content = null; return(result); } else { return(this.next.Execute(Command)); } } return(result); }
/// <summary> /// 定义一个client 每间隔10秒发送一次心跳,然后10分钟发送一次状态消息,借还购线等操作日志则马上传输 /// </summary> public void protobuf_checklink() { while (true) { string str = ""; try { if (bLoginFlag)//首先检测是否在线 { LaidianCommandModel.Builder laidianCommand = LaidianCommandModel.CreateBuilder(); laidianCommand.SetMessageType(MessageType.HEARTBEAT); sendMessage(client, laidianCommand.Build()); Thread.Sleep(500); } else { //连接服务器 client.Connect(); } } catch (IOException ex)//远程连接已经关闭 { } catch (Exception sd) { str = sd.Message; Console.WriteLine(str); } Thread.Sleep(3000); } }
/// <summary> /// 如果连接上去,则发送登陆指令。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Client_Connected(object sender, EventArgs e) { LaidianCommandModel.Builder loginbuilder = LaidianCommandModel.CreateBuilder(); loginbuilder.SetMessageType(MessageType.LOGIN_REQ); LaidianCommandModel logincmd = loginbuilder.Build(); sendMessage(client, logincmd); }
/** * 协议解析,把这里搞明白了,就没白看 */ public void OnDataReceive(Object sender, DataEventArgs e) { //DataEventArgs 里面有 byte[] Data是从协议层接收上来的字节数组,需要程序端进行缓存 Console.WriteLine("buff length: {0}, offset: {1}", e.Length, e.Offset); if (e.Length <= 0) { return; } //把收取上来的自己全部缓存到本地 buffer 中 Array.Copy(e.Data, 0, buffer.Data, buffer.Length, e.Length); buffer.Length += e.Length; CodedInputStream stream = CodedInputStream.CreateInstance(buffer.Data); while (!stream.IsAtEnd) { //标记读取的Position, 在长度不够时进行数组拷贝,到下一次在进行解析 int markReadIndex = (int)stream.Position; //Protobuf 变长头, 也就是消息长度 int varint32 = (int)stream.ReadRawVarint32(); if (varint32 <= (buffer.Length - (int)stream.Position)) { try { byte[] body = stream.ReadRawBytes(varint32); LaidianCommandModel response = LaidianCommandModel.ParseFrom(body); taskMsg.Enqueue(response); } catch (Exception exception) { Console.WriteLine(exception.Message); } } else { /** * 本次数据不够长度,缓存进行下一次解析 */ byte[] dest = new byte[8192]; int remainSize = buffer.Length - markReadIndex; Array.Copy(buffer.Data, markReadIndex, dest, 0, remainSize); /** * 缓存未处理完的字节 */ buffer.Data = dest; buffer.Offset = 0; buffer.Length = remainSize; break; } } }
public override void Process(LaidianCommandModel Event) { if (next == null) { return; } else { next.Process(Event); } }
/// <summary> /// /// </summary> public void PoolTcpChannel() { while (true) { if (tcpchannel.taskMsg.Count > 0) { LaidianCommandModel data = tcpchannel.taskMsg.Dequeue(); BasisEventCommand cmd = new BasisEventCommand(); cmd.Content = data; cmd.Session = this; cmd.EventCommandID = "tcp"; ServerTaskExecutorchainheader.Execute(cmd); } } }
public override void Process(LaidianCommandModel Event) { if (Event.MessageType == MessageType.HEARTBEAT) { } else { if (next == null) { log.Info("消息ID为" + Event.MessageType.ToString() + "的指令没有对应处理器."); } else { this.next.Process(Event); } } }
//public Queue<CDBStatusModel> status = new Queue<CDBStatusModel>(); /** * 这里 C# 的实现和Protobuf 官方给的Java实现是一样的 */ public void sendMessage(AsyncTcpSession client, LaidianCommandModel request) { using (MemoryStream stream = new MemoryStream()) { CodedOutputStream os = CodedOutputStream.CreateInstance(stream); //一定要去看它的代码实现, os.WriteMessageNoTag(request); /** * WriteMessageNoTag 等价于 WriteVarint32, WriteByte(byte[]) * 也就是:变长消息头 + 消息体 */ os.Flush(); byte[] data = stream.ToArray(); client.Send(new ArraySegment <byte>(data)); } }
/// <summary> /// 云端命令处理 /// </summary> public void ExecuteServerCmd() { while (bProcess) { try { if (terminal.tcpchannel.taskMsg.Count > 0) { LaidianCommandModel model = (LaidianCommandModel)terminal.tcpchannel.taskMsg.Dequeue(); BasisEventCommand cmd = new BasisEventCommand(); cmd.Content = model; cmd.EventCommandID = "tcp"; cmd.Session = terminal; ServerTaskExecutorchainheader.Execute(cmd); } else { Thread.Sleep(50); } } catch { } } }
/// <summary> /// 抽象方法,该方法体中实现对特定事件的处理 /// </summary> /// <param name="Event"></param> public abstract void Process(LaidianCommandModel Event);