/// <summary> /// 使用指定协议,指定命令号的UserToken.Send /// </summary> /// <param name="token">this UserToken 客户端</param> /// <param name="protocol">协议</param> /// <param name="commond">CommonProtocol 命令号</param> /// <param name="message">要发送的消息</param> public static void Send(this UserToken token, Protocol protocol, short commond, object message = null) { NetModle netModle = new NetModle(); netModle.Type = protocol; netModle.Command = commond; netModle.Message = message; token.Send(netModle); }
/// <summary> /// 反序列化,消息解码 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object Dncoad(byte[] value) { NetModle nem = new NetModle(); ByteArrary ba = new ByteArrary(value); ba.Read(out nem.Type);//一步步获取,拆出Type,Command,Message ba.Read(out nem.Command); ba.Read(ba.Length - ba.Position, out nem.Message); ba.Close(); return(nem); }
public override void ReceiveMessage(UserToken Uto, object message) { Console.WriteLine("接收到{0}用户的{1}信息", Uto.ReceSAEA.RemoteEndPoint, message); NetModle modle = (NetModle)message; //消息模块化 IHander hander = GetHandler(modle.Type); //得到消息对应的模块号 if (hander == null) { Console.WriteLine("hander==null"); return; } hander.ReciveMessage(Uto, modle);//消息塞进去 }
/// <summary> /// 序列化,消息编码 /// </summary> /// <param name="?"></param> /// <returns></returns> public static byte[] Encoad(object value) { NetModle nem = (NetModle)value;//将object类型消息转成标准的消息模块 ByteArrary ba = new ByteArrary(); ba.Write(nem.Type); //写入模块类型 ba.Write(nem.Command); //写入命令号 if (nem.Message != null) //如果有消息体 { ba.Write(nem.Message); } byte[] bt = ba.GetCopyBytes(); //转成二进制 ba.Close(); return(bt); }
/// <summary> /// 处理当前用户接收到的信息 /// </summary> /// <param name="token"></param> /// <param name="modle"></param> public void ReciveMessage(UserToken token, NetModle modle) { switch (modle.Command)//根据当前用户的消息模块的命令号 { case LoginProtocol.C2S_Login: //Login //Console.WriteLine("登入功能。。。。未完成"); Login(token, modle.GetMessage <AccountDto>()); break; case LoginProtocol.C2S_Register: //Reister //Console.WriteLine("注册功能。。。。未完成"); Register(token, modle.GetMessage <AccountDto>()); break; } }
/// <summary> /// 应用层进行发数据的接口(接收NetModle) /// </summary> public void Send(NetModle nm)//如果用户想要发送的消息时NetModle模块时,先进行消息编码和长度编码 { if (Setting.MessageEncoad == null) { isSend = false; throw new Exception("消息编码必须存在"); } byte[] bt = Setting.MessageEncoad(nm); //消息压缩,可选 if (Setting.CompressEncode != null) { bt = Setting.CompressEncode(bt); } // 消息加密,可选 if (Setting.MessageEncrypt != null) { bt = Setting.MessageEncrypt(bt); } if (Setting.LengthEncoad != null) { bt = Setting.LengthEncoad(bt); } Send(bt); }