/// <summary> /// 消息体反序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object decode(byte[] value) { ByteArray ba = new ByteArray(value); SocketModel model = new SocketModel(); byte type; int area; int command; //从数据中读取 三层协议 读取数据顺序必须和写入顺序保持一致 ba.read(out type); ba.read(out area); ba.read(out command); model.type = type; model.area = area; model.command = command; //判断读取完协议后 是否还有数据需要读取 是则说明有消息体 进行消息体读取 if (ba.Readnable) { byte[] message; //将剩余数据全部读取出来 ba.read(out message, ba.Length - ba.Position); //反序列化剩余数据为消息体 model.message = SerializeUtil.decode(message); } ba.Close(); return(model); }
//消息接收 public void MessageReceive(NetFrame.UserToken token, NetFrame.auto.SocketModel model) { switch (model.command) { case LoginProtocol.LOGIN_CREQ: login(token, model.GetMessage <AccountInfoDTO>()); break; case LoginProtocol.REG_CREQ: reg(token, model.GetMessage <AccountInfoDTO>()); break; } }
/// <summary> /// 消息体序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static byte[] Encode(object value) { SocketModel model = value as SocketModel; ByteArray ba = new ByteArray(); ba.write(model.Type); ba.write(model.Area); ba.write(model.Command); if (model.Message != null) { ba.write(SerializeUtil.encode(model.Message)); } byte[] result = ba.getBuff(); ba.Close(); return(result); }
public void MessageReceive(NetFrame.UserToken token, NetFrame.auto.SocketModel message) { switch (message.command) { case UserProtocol.CREATE_CREQ: create(token, message.GetMessage <string>()); break; case UserProtocol.INFO_CREQ: info(token); break; case UserProtocol.ONLINE_CREQ: online(token); break; } }
/// <summary> /// 消息体序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static byte[] encode(object value) { SocketModel model = value as SocketModel; ByteArray ba = new ByteArray(); //从数据中读取三层协议 读取顺序必须和写入顺序相同 ba.write(model.type); ba.write(model.area); ba.write(model.command); if (model.message != null) { ba.write(SerializeUtil.encode(model.message)); } byte[] result = ba.getBuff(); ba.Close(); return(result); }
/// <summary> /// 消息体序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static byte[] encode(object value) { SocketModel model = value as SocketModel; ByteArray ba = new ByteArray(); ba.write(model.type); ba.write(model.area); ba.write(model.command); //判断消息体是否为空 不为空则序列化后写入 if (model.message != null) { ba.write(SerializeUtil.encode(model.message)); } byte[] result = ba.getBuff(); ba.Close(); return(result); }
/// <summary> /// 消息体反序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object Decode(byte[] value) { ByteArray ba = new ByteArray(value); SocketModel model = new SocketModel(); byte type; int area; int command; ba.read(out type); ba.read(out area); ba.read(out command); model.Type = type; model.Area = area; model.Command = command; if (ba.Readnable) { byte[] message; ba.read(out message, ba.Length - ba.Postion); model.Message = SerializeUtil.decode(message); } ba.Close(); return(model); }
/// <summary> /// 消息体反序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object decode(byte[] value) { ByteArray ba = new ByteArray(value); SocketModel model = new SocketModel(); byte type; int area; int command; ba.read(out type); ba.read(out area); ba.read(out command); model.type = type; model.area = area; model.command = command; //读取协议后 判断是否还有数据需要读取 有则说明有消息体 进行消息读取 if (ba.Readnable) { byte[] message; ba.read(out message, ba.Length - ba.Position); model.message = SerializeUtil.decode(message); } ba.Close(); return(model); }