Пример #1
0
        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"));
            }
        }
Пример #2
0
        /// <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);
                    }
                }
            }
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
 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);
     }
 }
Пример #5
0
 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);
     }
 }
Пример #6
0
    /// <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();
    }
Пример #7
0
    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);
        }
    }
Пример #8
0
    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);
        }
    }
Пример #9
0
    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);
        }
    }
Пример #10
0
    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());
        }
    }
Пример #11
0
        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"));
            }
        }
Пример #12
0
        /// <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);
            }
        }
Пример #13
0
    /// <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();      //递归调用
    }
Пример #14
0
        /// <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);
            }
        }
Пример #15
0
    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();
    }
Пример #16
0
        /// <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);
            }
        }
Пример #17
0
    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);
        }
    }
Пример #18
0
        /// <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);
            }
        }
Пример #19
0
        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"));
            }
        }
Пример #20
0
        /// <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);
            }
        }
Пример #21
0
        /// <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);
                }
            }
        }
Пример #22
0
        /// <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);
            }
        }
Пример #23
0
        /// <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);
            }
        }
Пример #24
0
        /// <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);
            }
        }
Пример #25
0
    /// <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();
    }
Пример #26
0
    /// <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(); // 递归
    }
Пример #27
0
        /// <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);
                }
            }
        }
Пример #28
0
    /// <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();
    }
Пример #29
0
        /// <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);
                }
            }
        }
Пример #30
0
    /// <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();
    }