public void write(UserToken token, int type, int area, int command, object message) { byte[] bs = MessageEncoding.Encode(createSocketModel(type, area, command, message)); bs = LengthEncoding.encode(bs); Console.WriteLine("有消息发出"); token.write(bs); }
/// <summary> /// 缓存中的数据处理,收到消息后先解码 /// </summary> private void OnData() { //长度解码 byte[] result = LengthEncoding.Decode(ref cache); //长度解码为空,说明消息体不全,等待下条消息过来补全 if (result == null) { isReading = false; return; } SocketModel messsage = (SocketModel)MessageEncoding.MessageDecode(result); if (messsage == null) { isReading = false; return; } //进行消息处理 messages.Add(messsage); //尾递归 防止在消息处理过程中 有其他消息到达而没有经过处理 OnData(); }
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); }
internal void Brocast(int type, int area, int command, object msg, UserToken exclude) { MSGModel message = MSGModel.CreateMessage(type, area, command, msg); byte[] tmp = MessageEncoding.Encode(message); byte[] buffer = LengthEncoding.encode(tmp); foreach (RoomPlayer rp in teamOne) { if (rp.Token != exclude) { tmp = new byte[buffer.Length]; Buffer.BlockCopy(buffer, 0, tmp, 0, buffer.Length); rp.Token.Write(tmp); } } foreach (RoomPlayer rp in teamTwo) { if (rp.Token != exclude) { tmp = new byte[buffer.Length]; Buffer.BlockCopy(buffer, 0, tmp, 0, buffer.Length); rp.Token.Write(tmp); } } }
public void write(UserToken token, int type, int area, int command, object message) { byte[] bs = MessageEncoding.Encode(createSocketModel(type, area, command, message)); bs = LengthEncoding.encode(bs); LoggerHelper.Info(token.connectSocket.RemoteEndPoint.ToString() + " --返回处理结果"); token.write(bs); }
/// <summary> /// Generates random nested X.690 node. /// </summary> /// <param name="branches">Maximum number of branches to use.</param> /// <param name="leaves">Maximum number of leaves to use.</param> /// <param name="depth">Maximum nessting level.</param> /// <param name="lengthEncoding">Definite, indefinite or random.</param> /// <returns>Random X.690 node.</returns> static public X690.Node RandomBranch(int branches, int leaves, int depth, LengthEncoding lengthEncoding = LengthEncoding.Definite) { var stack = new Stack <X690.Node>(); for (int i = 0; i < PRNG.Next(branches + 1); i++) { stack.Push(RandomBranch(lengthEncoding)); } for (int i = 0; i < PRNG.Next(leaves + 1); i++) { stack.Push(RandomLeaf(lengthEncoding)); } var root = RandomBranch(lengthEncoding); foreach (var node in stack.ToArray().Shuffled()) { root.Nodes.Add(node); } IEnumerable <X690.Node> target = root.Nodes.Where(i => i.IsConstructed); while (--depth > 0) { foreach (var node in target) { node.Nodes.Add(RandomBranch(branches, leaves, 0, lengthEncoding)); } target = target.SelectMany(i => i.Nodes.Where(j => j.HasNodes)); } return(root); }
public static void write(UserToken token, SocketModel model) { //Console.WriteLine("有消息发出"); byte[] bs = MessageEncoding.Encode(model); bs = LengthEncoding.encode(bs); token.write(bs); }
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); }
/// <summary> /// Generates a single branch of X.690 node tree. /// </summary> /// <param name="lengthEncoding">Definite, indefinite or random.</param> /// <returns>A tree branch.</returns> static X690.Node RandomBranch(LengthEncoding lengthEncoding = LengthEncoding.Definite) => PRNG.NextBool() ? new X690.Sequence() { IsDefiniteLength = lengthEncoding.IsDefiniteLength() } as X690.Node : new X690.Set() { IsDefiniteLength = lengthEncoding.IsDefiniteLength() } as X690.Node;
public bool Send(byte[] bytes, int len) { if (!Connected || currentSocket == null) { return(false); } bytes = LengthEncoding.LengthEncode(bytes); currentSocket.Send(bytes); return(true); }
/// <summary> /// Returns true if requested length encoding represents definite length encoding. /// </summary> /// <param name="encoding">Requested encoding.</param> /// <returns>True for definite encoding.</returns> public static bool IsDefiniteLength(this LengthEncoding encoding) { switch (encoding) { case LengthEncoding.Definite: return(true); case LengthEncoding.Indefinite: return(false); default: return(PRNG.NextBool()); } }
private void ReceiveDataHandle() { byte[] data = LengthEncoding.LengthDecode(ref readBuffer); if (data == null) { isReading = false; return; } listener.OnReceive(this, data, data.Length); ReceiveDataHandle(); }
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); }
static byte[] createBattleRoom() { SocketModel model = new SocketModel(); model.type = TypeProtocol.TYPE_BATTLE_ROOM; model.area = 0; model.command = BattleRoomProtocol.CREATE_ONE_C; model.message = null; var message = MessageEncoding.encode(model); return(LengthEncoding.encode(message)); }
public void brocast(int type, int area, int command, object message) { byte[] bs = MessageEncoding.Encode(createSocketModel(type, area, command, message)); bs = LengthEncoding.encode(bs); // Console.WriteLine("有群发"); //遍历当前模块所有用户 进行消息发送 foreach (UserToken item in list) { byte[] truebs = new byte[bs.Length]; Array.Copy(bs, 0, truebs, 0, bs.Length); item.write(truebs); } }
public void write(int id, int type, int area, int command, object message) { UserToken token = userBiz.getToken(id); if (token == null) { return; } Console.WriteLine("有消息发出"); byte[] bs = MessageEncoding.Encode(createSocketModel(type, area, command, message)); bs = LengthEncoding.encode(bs); token.write(bs); }
public void brocast(byte type, int area, int command, object message, UserToken exToken = null) { byte[] value = MessageEncoding.encode(CreateSocketModel(type, area, command, message)); value = LengthEncoding.encode(value); foreach (UserToken item in list) { if (item != exToken) { byte[] bs = new byte[value.Length]; Array.Copy(value, 0, bs, 0, value.Length); item.write(bs); } } }
public void brocast(byte type, int area, int command, ReturnDTO message, UserToken exToken = null) { byte[] value = MessageEncoding.encode(createSocketModel(type, area, command, message)); value = LengthEncoding.encode(value); foreach (var item in list) { if (exToken != item) { byte[] bs = new byte[value.Length]; Array.Copy(value, bs, value.Length); item.write(bs); } } }
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); } }
static byte[] login() { AccountInfoDTO accountInfoDTO = new AccountInfoDTO(); accountInfoDTO.accountName = "admin"; accountInfoDTO.password = "******"; SocketModel model = new SocketModel(); model.type = TypeProtocol.TYPE_LOGIN; model.area = 0; model.command = LoginProtocol.LOGIN_CREQ; model.message = accountInfoDTO; var message = MessageEncoding.encode(model); return(LengthEncoding.encode(message)); }
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); } }
public void writeToUsers(long[] users, byte type, int area, int command, ReturnDTO message) { byte[] value = MessageEncoding.encode(createSocketModel(type, area, command, message)); value = LengthEncoding.encode(value); foreach (var item in users) { UserToken token = getToken(item); if (token == null) { continue; } byte[] bs = new byte[value.Length]; Array.Copy(value, bs, value.Length); token.write(bs); } }
public void writeToUsers(int[] users, int type, int area, int command, object message) { byte[] bs = MessageEncoding.Encode(createSocketModel(type, area, command, message)); bs = LengthEncoding.encode(bs); foreach (int item in users) { UserToken token = userBiz.getToken(item); if (token != null) { Console.WriteLine("群发给指定ID"); byte[] truebs = new byte[bs.Length]; Array.Copy(bs, 0, truebs, 0, bs.Length); token.write(truebs); } } }
private static byte[] useskill() { SocketModel model = new SocketModel(); model.type = TypeProtocol.TYPE_BATTLE_ROOM; model.area = 0; model.command = BattleRoomProtocol.USE_SKILL_C; RoomDTO dto = new RoomDTO(); dto.roomId = roomDTO.roomId; dto.roomRoleId = roomDTO.roomRoleId; model.message = dto; var message = MessageEncoding.encode(model); return(LengthEncoding.encode(message)); }
/// <summary> /// Generates a single random leaf of X.690 node tree. /// </summary> /// <param name="lengthEncoding">Definite, indefinite or random.</param> /// <returns>A tree leaf.</returns> static X690.Node RandomLeaf(LengthEncoding lengthEncoding = LengthEncoding.Definite) { switch (PRNG.Next(0, 4)) { case 0: return(new X690.Null()); case 1: return(new X690.Boolean(PRNG.NextBool())); case 2: return(new X690.Integer(PRNG.Next(int.MinValue, int.MaxValue))); case 3: return(new X690.Text(RandomString()) { IsDefiniteLength = lengthEncoding.IsDefiniteLength() }); } throw new InvalidOperationException(); }
public void exBrocast(UserToken token, int command, object message) { byte[] bs = MessageEncoding.Encode(createSocketModel(getType(), getArea(), command, message)); bs = LengthEncoding.encode(bs); Console.WriteLine("有排除模式群发"); //遍历当前模块所有用户 进行消息发送 foreach (UserToken item in list) { if (item.Equals(token)) { continue; } byte[] truebs = new byte[bs.Length]; Array.Copy(bs, 0, truebs, 0, bs.Length); item.write(truebs); } }
public bool Send(byte[] bytes, int len) { if (ConnentSocket != null) { bytes = LengthEncoding.LengthEncode(bytes); writeBuffer.Enqueue(bytes); if (!isWriting) { isWriting = true; SendDataHandle(); } } else { closeAction(this, "socket 不存在"); return(false); } return(true); }
private static byte[] usecard() { SocketModel model = new SocketModel(); model.type = TypeProtocol.TYPE_BATTLE_ROOM; model.area = 0; model.command = BattleRoomProtocol.USE_CARD_C; RoomDTO dto = new RoomDTO(); dto.roomId = roomDTO.roomId; dto.roomRoleId = roomDTO.roomRoleId; dto.map.Add(CommonFieldProtocol.useCardId, selfInfo[0].cardIds[0]); dto.map.Add(CommonFieldProtocol.targetIds, new List <string> { otherInfo[0].roomRoleId }); model.message = dto; var message = MessageEncoding.encode(model); return(LengthEncoding.encode(message)); }
/// <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))); }
static void Main(string[] args) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress serverIP = IPAddress.Parse(globalInfo.ipAddr); int port = globalInfo.port; IPEndPoint ipEndPoint = new IPEndPoint(serverIP, port); socket.Connect(ipEndPoint); //socket.ReceiveAsync(SocketAsyncEventArgs) //Parse将字符串转换为IP地址类型 //IPAddress myIP = IPAddress.Parse("127.0.0.1"); ////构造一个TcpClient类对象,TCP客户端 //TcpClient client = new TcpClient(); ////与TCP服务器连接 //client.Connect(myIP, globalInfo.port); //Console.WriteLine("服务器已经连接...请输入对话内容..."); ////创建网络流,获取数据流 //NetworkStream stream = client.GetStream(); ////读数据流对象 //StreamReader sr = new StreamReader(stream); ////写数据流对象 //StreamWriter sw = new StreamWriter(stream); //Thread th2 = new Thread(SendMsg(sw)); //也可简写为new Thread(ThreadMethod); //Thread th = new Thread(Receive(sr)); //也可简写为new Thread(ThreadMethod); //th.IsBackground = true; //th2.Start(); //启动线程 //th.Start(); //启动线程 //Console.WriteLine("服务器:" + sr.ReadLine()); Thread receiveServerThread = new Thread(new ThreadStart(delegate() { // 接受和显示服务器信息 while (true) { socket.Receive(result); var k = result.ToList <byte>(); byte[] bytes = LengthEncoding.decode(ref k); SocketModel model = (SocketModel)MessageEncoding.decode(bytes); switch (model.command) { case BattleRoomProtocol.OVER_GAME_S: break; case LoginProtocol.LOGIN_SRES: Console.WriteLine("登录完毕"); break; case BattleRoomProtocol.CREATE_ONE_S: Console.WriteLine("房间创建成功"); break; case BattleRoomProtocol.START_TIME_S: Console.WriteLine("得到初始化信息"); var dto = model.getMessage <ReturnDTO>(); RoomDTO room = (RoomDTO)dto.message; roomDTO = room; Console.WriteLine("战斗房间的其他人信息:"); selfInfo = (List <RoomInfoDTO>)roomDTO.map[CommonFieldProtocol.battleRoomBaseSelfInfo]; otherInfo = (List <RoomInfoDTO>)roomDTO.map[CommonFieldProtocol.battleRoomBaseOtherInfo]; Console.WriteLine(otherInfo[0]); Console.WriteLine("战斗房间的自己信息:"); Console.WriteLine(selfInfo[0]); break; default: Console.WriteLine("未知命令"); Console.WriteLine(JsonConvert.SerializeObject(model)); break; } bytes = new byte[20480]; } })); receiveServerThread.Start(); string sendMsg = string.Empty; sendMsg = Console.ReadLine(); while (sendMsg != "" + BattleRoomProtocol.OVER_GAME_S) { switch (sendMsg) { case "login": { Console.WriteLine("准备登录"); var bytes = login(); socket.Send(bytes); //发送数据 } break; case "createroom": { Console.WriteLine("准备创建房间"); var bytes = createBattleRoom(); socket.Send(bytes); //发送数据 } break; case "usecard": { Console.WriteLine("使用卡牌"); var bytes = usecard(); socket.Send(bytes); //发送数据 } break; case "useskill": { Console.WriteLine("使用技能"); var bytes = useskill(); socket.Send(bytes); //发送数据 } break; default: Console.WriteLine("未知命令"); break; } sendMsg = Console.ReadLine(); } // client.Close(); // Console.Read(); }