public void Write(UserToken token, byte type, int area, int command, object message) { //消息体的编码 byte[] value = MessageEncoding.MessageEncode(CreateSocketModel(type, area, command, message)); //长度编码,因为在服务器传入的时候进行了长度编码,所以在处理的时候也要进行长度编码 value = LengthEncoding.Encode(value); token.Write(value); }
public static void OnceSend <T>(UserToken token, byte type, int area, int command, T message) { SocketModel socketModel = new SocketModel(); socketModel.type = type; socketModel.area = area; socketModel.command = command; socketModel.message = CommonTool.Serialize <T>(message); byte[] value = MessageEncoding.Encode(socketModel); value = LengthEncoding.Encode(value); token.write(value); }
public void Brocast(byte type, int area, int command, object message, UserToken exToken = null) { byte[] value = MessageEncoding.MessageEncode(CreateSocketModel(type, area, command, message)); value = LengthEncoding.Encode(value); foreach (UserToken item in userList) { if (item == exToken) { continue; } byte[] bs = new byte[value.Length]; //防治将元数据变更 Array.Copy(value, 0, bs, 0, value.Length); item.Write(bs); } }
public void writeToUsers(int[] users, byte type, int area, int command, object message) { byte[] value = MessageEncoding.Encode(CreateSocketModel(type, area, command, message)); value = LengthEncoding.Encode(value); foreach (int item in users) { UserToken token = userBiz.getToken(item); if (token == null) { continue; } byte[] bs = new byte[value.Length]; Array.Copy(value, 0, bs, 0, value.Length); token.Write(bs); } }
/// <summary> /// 编码数据成二进制协议 /// </summary> /// <param name="type"></param> /// <param name="area"></param> /// <param name="command"></param> /// <param name="message"></param> public byte[] Encode(byte type, int area, int command, object message) { SocketModel model = new SocketModel() { Type = type, Area = area, Command = command, Message = message }; #region 原本写法,感觉有问题 //ByteArray ba = new ByteArray(); //ba.write(type); //ba.write(area); //ba.write(command); ////判断消息体是否为空 不为空则序列化后写入 //if (message != null) { // ba.write(SerializeUtil.Encode(message)); //} //ByteArray arr1 = new ByteArray(); //arr1.write(ba.Length); //arr1.write(ba.getBuff()); //return arr1.getBuff(); #endregion return(LengthEncoding.Encode(MessageEncoding.Encode(model))); }
/// <summary> /// 发送报文 /// </summary> /// <param name="type"></param> /// <param name="command"></param> /// <param name="message"></param> public void Write(byte type, int command, object message) { //长编码<-报文编码<-socket模型 this.Write(LengthEncoding.Encode(MessageEncoding.Encode((object)new SocketModel(type, command, message)))); }
public void write(UserToken token, byte type, int area, int command, object message) { byte[] value = MessageEncoding.Encode(CreateSocketModel(type, area, command, message)); value = LengthEncoding.Encode(value); token.Write(value); }