public IActionResult Post([FromBody] DecodeParam param) { if (param == null || string.IsNullOrWhiteSpace(param.Code)) { _logger.LogError("No param in decoding."); return(BadRequest("no param")); } var code = param.Code.Trim(); if (code.Length > _decodeMaxLength) { _logger.LogError("Decoding param too long: {0}", code.Length); return(BadRequest("param too long")); } try { var sourceAes = EncodeTool.StringToByteArray(code); var source = AESTool.Decrypt(sourceAes); var sourceStr = Encoding.UTF8.GetString(source); _logger.LogInformation("Decoding successfully.\nCode: {0} \nSource: {1}", code, sourceStr); return(new JsonResult(new DecodeResult() { Source = sourceStr })); } catch (Exception ex) { _logger.LogError("Decoding error for string:\n {0} \nError: {1}", code, ex.Message); return(BadRequest("decode error")); } }
/// <summary> /// 广播给一个房间内的一个玩家 /// </summary> /// <param name="room"></param> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> /// <param name="exClient"></param> private void singleBrocast(FightRoom room, int opCode, int subCode, object value, int userId, ClientPeer exClient = null) { SocketMsg msg = new SocketMsg(); msg.OpCode = opCode; msg.SubCode = subCode; msg.Value = value; byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in room.PlayerList) { if (userCache.IsOnline(player.UserId)) { ClientPeer client = userCache.GetClientPeer(player.UserId); if (player.UserId == userId) { if (client == exClient) { continue; } client.Send(packet); } } } }
/// <summary> /// 优化网络消息 /// </summary> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> /// <returns></returns> private byte[] GetMsg(int opCode, int subCode, object value) { MessageData msg = new MessageData(opCode, subCode, value); byte[] msgBytes = EncodeTool.EncodeMsg(msg); byte[] msgPacket = EncodeTool.EncodeMessage(msgBytes); return(msgPacket); }
public void SendMsg(NetMsg msg) { try { byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); clientSocket.Send(packet); } catch (Exception e) { Debug.LogError(e.Message); } }
public void Send(SocketMessage smg) { byte[] data = EncodeTool.EncodeMessage(smg); byte[] package = EncodeTool.EncodePackage(data); try { socket.Send(package); } catch (Exception ex) { Debug.LogError(ex.Message); } }
/// <summary> /// 处理接收到的数据 /// </summary> private void ProcessReceive() { isProcessingReceive = true; byte[] packet = EncodeTool.DecodePacket(ref receiveCache); if (packet == null) { isProcessingReceive = false; return; } NetMsg msg = EncodeTool.DecodeMsg(packet); netMsgQueue.Enqueue(msg); ProcessReceive(); }
public void Send(NetMessage msg) { byte[] data = EncodeTool.Serialize(msg); byte[] packet = EncodeTool.EncodePacket(data); try { socket.Send(packet); } catch (Exception e) { Debug.LogError(e.Message); } }
public void SendMessage(SocketMsg msg) { try { byte[] data = EncodeTool.EncodeSocketMgr(msg); byte[] packet = EncodeTool.EncodeMessage(data); clientSocket.Send(packet); } catch (Exception e) { Debug.LogWarning(e.Message); } }
public void Send(SocketMsg msg) { byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); try { socket.Send(packet); } catch (Exception e) { Debug.Log(e.Message); } }
public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] packet = EncodeTool.EncodeMessage((EncodeTool.EncodeMsg(msg))); try { socket.Send(packet); } catch (Exception e) { Debug.Log(e.ToString()); } }
public IActionResult Post([FromBody] EncodeParam param) { _logger.LogDebug("Start Encoding."); if (param == null || string.IsNullOrWhiteSpace(param.Source)) { _logger.LogError("No param in encoding."); return(BadRequest("no param")); } var source = param.Source.Trim(); if (source.Length > _encodeMaxLength) { _logger.LogError($"Encoding param too long: {source.Length}"); return(BadRequest("param too long")); } var remoteIP = Request.HttpContext.Connection.RemoteIpAddress.ToString(); try { var sourceAes = AESTool.Encrypt(Encoding.UTF8.GetBytes(source)); var code = EncodeTool.ByteArrayToString(sourceAes); //var testaes = EncodeTool.StringToByteArray(code); //if (testaes.Length != sourceAes.Length) //{ // for (var i = 0; i < testaes.Length; i++) // { // if (testaes[i] != sourceAes[i]) // { // _logger.LogError($"Encoding error in {i} of {sourceAes.Length}"); // break; // } // } //} _logger.LogInformation($"(IP:{remoteIP})Encoding successfully.\nSource: {source} \nCode: {code}"); return(new JsonResult(new EncodeResult() { Code = code })); } catch (Exception ex) { _logger.LogError($"(IP:{remoteIP})Encoding error for string: \n{source} \nError:{ex.Message}"); return(BadRequest("encode error")); } }
/// <summary> /// 广播房间内的所有玩家信息 /// </summary> public void Brocast(int opCode, int subCode, object value, ClientPeer exclient = null) { SocketMsg msg = new SocketMsg(); msg.OpCode = opCode; msg.SubCode = subCode; msg.Value = value; byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in PlayerList) { ClientPeer client = userCache.GetClientPeer(player.UserId); client.Send(packet); } }
/// <summary> /// 处理收到的数据 /// </summary> private void ProcessReceive() { isReceiveProcess = true; byte[] data = EncodeTool.DecodePackage(ref dataCache); if (data == null) { isReceiveProcess = false; return; } SocketMessage smg = EncodeTool.DecodeMessage(data); SmgQueue.Enqueue(smg); //保存等待处理 // Debug.Log("收到服务器消息:"+smg.value); ProcessReceive(); //递归调用 }
/// <summary> /// 广播发消息,给除自己以外的客户端发消息 /// </summary> public void Broadcast(int opCode, int subCode, object value, ClientPeer exceptClient = null) { NetMsg msg = new NetMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var client in clientList) { if (client == exceptClient) { continue; } client.SendMsg(packet); } }
private void ProcessReceive() { isProcess = true; byte[] data = EncodeTool.DecodeMessage(dataCache); if (data == null) { isProcess = false; return; } SocketMsg msg = EncodeTool.DecodeMsg(data); MsgQueue.Enqueue(msg); ProcessReceive(); }
/// <summary> /// 广播发消息,给除自己以外的客户端发消息 /// </summary> public void Broadcast(int opCode, int subCode, object value, ClientPeer exceptClient = null) { NetMsg msg = new NetMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in playerList) { ClientPeer client = DatabaseManager.GetClientPeerByUserId(player.id); if (client == exceptClient) { continue; } client.SendMsg(packet); } }
public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); try { socket.Send(packet); } catch (Exception e) { Debug.LogError(e.Message); } }
/// <summary> /// broadcast to all palyer in room /// </summary> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> /// <param name="exClient"></param> public void Broadcast(int opCode, int subCode, object value, ClientPeer currentClient = null) { SocketMessage msg = new SocketMessage(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var client in UserIdClientDict.Values) { if (client == currentClient) { continue; } client.Send(packet); } }
public IActionResult Post([FromBody] EncodeParam param) { _logger.LogDebug("Start Encoding."); if (param == null || string.IsNullOrWhiteSpace(param.Source)) { _logger.LogError("No param in encoding."); return(BadRequest("no param")); } var source = param.Source.Trim(); if (source.Length > _encodeMaxLength) { _logger.LogError("Encoding param too long: {0}", source.Length); return(BadRequest("param too long")); } try { var sourceAes = AESTool.Encrypt(Encoding.UTF8.GetBytes(source)); var code = EncodeTool.ByteArrayToString(sourceAes); var testaes = EncodeTool.StringToByteArray(code); if (testaes.Length != sourceAes.Length) { for (var i = 0; i < testaes.Length; i++) { if (testaes[i] != sourceAes[i]) { _logger.LogError("Encoding error in {0} of {1}", i, sourceAes.Length); break; } } } _logger.LogInformation("Encoding successfully.\nSource: {0} \nCode: {1}", source, code); return(new JsonResult(new EncodeResult() { Code = code })); } catch (Exception ex) { _logger.LogError("Encoding error for string: \n{0} \nError:{1}", source, ex.Message); return(BadRequest("encode error")); } }
/// <summary> /// 向房间内的所有人广播 /// </summary> /// <param name="room"></param> public void Broadcast(int opcode, int subcode, object value, ClientPeer exclientPeer = null) { SocketMsg mgr = new SocketMsg(opcode, subcode, value); byte[] data = EncodeTool.EncodeSocketMgr(mgr); byte[] packet = EncodeTool.EncodeMessage(data);//构造数据包 foreach (var uid in UidClientpeerDic.Keys) { ClientPeer clientPeer = UidClientpeerDic[uid]; if (clientPeer == exclientPeer) { continue;//不用给自己广播消息 } clientPeer.StartSend(packet); } }
/// <summary> /// 向房间内玩家广播消息 /// </summary> /// <param name="opCode"></param> /// <param name="fightCode"></param> /// <param name="value"></param> /// <param name="clientPeer"></param> public void Brocast(FightRoomModel fightRoom, int opCode, int fightCode, object value, ClientPeer exClientPeer = null) { SocketMsg socketMsg = new SocketMsg(opCode, fightCode, value); byte[] vs = EncodeTool.EncodeMsg(socketMsg); byte[] packet = EncodeTool.EncodeMessage(vs); foreach (var player in fightRoom.PlayerDtos) { UserModel userModel = userModelCache.GetModelByUid(player.Uid); AccountModel accountModel = accountCache.GetModel(userModel.Aid); ClientPeer clientPeer = accountCache.GetClientPeerByAcc(accountModel.acc); if (clientPeer != exClientPeer) { clientPeer.Send(packet); } } }
/// <summary> /// 向房间内的所有人广播 /// </summary> /// <param name="room"></param> public void Broadcast(int opcode, int subcode, object value, ClientPeer exclientPeer = null) { SocketMsg mgr = new SocketMsg(opcode, subcode, value); byte[] data = EncodeTool.EncodeSocketMgr(mgr); byte[] packet = EncodeTool.EncodeMessage(data);//构造数据包 foreach (var player in playerDtos) { ClientPeer clientPeer = UserCache.Instance.GetClientPeer(player.UserID); if (clientPeer == exclientPeer) { continue;//不用给自己广播消息 } clientPeer.StartSend(packet); } }
/// <summary> /// 广播房间内的所有玩家信息 /// </summary> public void Brocast(int opCode, int subCode, object value, ClientPeer exClient = null) { SocketMsg msg = new SocketMsg(); msg.OpCode = opCode; msg.SubCode = subCode; msg.Value = value; byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var client in UIdClientDict.Values) { if (client == exClient) { continue; } client.Send(packet); } }
/// <summary> /// 广播发消息 /// </summary> public void Broadcase(int opCode, int subCode, object value, ClientPeer exceptClient = null) { Console.WriteLine("--广播有玩家加入的消息--" + opCode + subCode); NetMsg msg = new NetMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var client in clientList) { if (client == exceptClient) { Console.WriteLine("--wangzhi--房间里玩家的id--" + client.Id); Console.WriteLine("--wangzhi--忽略的玩家ID--" + exceptClient.Id); continue; } client.SendMsg(packet); } }
/// <summary> /// 处理收到的数据 /// </summary> private void processReceive() { isProcessReceive = true; //解析数据包 byte[] data = EncodeTool.DecodePacket(ref dataCache); if (data == null) { isProcessReceive = false; return; } SocketMsg msg = EncodeTool.DecodeMsg(data); //存储消息 等待处理 SocketMsgQueue.Enqueue(msg); //尾递归 processReceive(); }
/// <summary> /// 处理接收到的数据 /// </summary> private void ProcessReceive() { //Debug.Log("ClientPeer - ProcessReceive - 处理接收的数据"); isProcessingReceive = true; // 将接收到的数据转成 NetMsg 对象 byte[] packet = EncodeTool.DecodePacket(ref receiveCache); if (packet == null) { isProcessingReceive = false; return; } //Debug.Log("packe - " + packet.Length); NetMsg msg = EncodeTool.DecodeMsg(packet); //Debug.Log("处理接收到的数据"); // 加入消息队列 netMsgQueue.Enqueue(msg); ProcessReceive(); // 递归 }
/// <summary> /// 广播 /// </summary> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> /// <param name="clientPeer"></param> public void Brocast(FightRoom room, int opCode, int subCode, object value, ClientPeer exClient = null) { SocketMessage msg = new SocketMessage(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in room.PlayerList) { if (userCache.IsOnline(player.UserId)) { ClientPeer client = userCache.GetClientPeer(player.UserId); if (client == exClient) { continue; } client.Send(packet); } } }
/// <summary> /// 处理收到的数据 /// </summary> private void processReceive() { isProcessReceive = true; byte[] data = EncodeTool.DeCodePacket(ref dataCache); if (data == null) { isProcessReceive = false; return; } SocketMsg msg = EncodeTool.DecodeMsg(data); SocketMsgQueue.Enqueue(msg); //Debug.Log(msg.Value); // 尾递归 processReceive(); }
/// <summary> /// 广播 /// </summary> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> /// <param name="exClient"></param> private void brocast(_21MutiFightRoom room, int opCode, int subCode, object value, ClientPeer exClient = null) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); foreach (var player in room.PlayerList) { //fixbug923 if (player != null && userCache.IsOnline(player.UserId) && !room.LeaveUIdList.Contains(player.UserId)) { ClientPeer client = userCache.GetClientPeer(player.UserId); if (client == exClient) { continue; } client.Send(packet); } } }
/// <summary> /// 处理接收到的数据 /// </summary> private void processReceive() { isprocessReceive = true; byte[] data = EncodeTool.DecodeMessage(ref dataCache); if (data == null) { isprocessReceive = false; return; } SocketMsg msg = EncodeTool.DeCodeSocketMgr(data); Debug.Log(msg.Value); msgQueue.Enqueue(msg);//保存到消息列表中 //尾递归 processReceive(); }