Пример #1
0
        public static void OnDeleteBlockReq(Player player, MemoryStream stream)
        {
            Ultilities.Print("OnDeleteBlockReq");
            CSDeleteBlockReq req = NetworkManager.Deserialize <CSDeleteBlockReq>(stream);

            Vector2Int chunk   = Ultilities.GetChunk(req.position);
            bool       deleted = TerrainData.RemoveBlockInChunk(chunk, req.position);

            CSDeleteBlockRes res = new CSDeleteBlockRes();

            if (deleted)
            {
                res.RetCode  = 0;
                res.position = req.position;
            }
            else
            {
                res.RetCode = 3;
            }
            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_DELETE_BLOCK_RES, res);
            if (deleted)
            {
                //同步给有该chunk视野的其他玩家
                foreach (Player p in TerrainData.GetChunkViewPlayers(chunk))
                {
                    if (p.id != player.id)
                    {
                        DeleteBlockNotify(p, req.position);
                    }
                }
            }
        }
Пример #2
0
        public static void OnRegisterReq(Player player, MemoryStream stream)
        {
            CSRegisterReq req = NetworkManager.Deserialize <CSRegisterReq>(stream);

            Ultilities.Print($"CSRegisterReq,account={req.Account},req.Name={req.Name},req.Password={req.Password}");
            bool hasRegistered = Redis.GetAccountData(req.Account, out AccountData accountData);

            //检测是否已注册
            if (hasRegistered)
            {
                CSRegisterRes res = new CSRegisterRes {
                    RetCode = 8
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_REGISTER_RES, res);
            }
            else
            {
                accountData = new AccountData
                {
                    playerID = Redis.GetPlayerIndexAdd(),
                    account  = req.Account,
                    password = req.Password,
                    name     = req.Name
                };
                Ultilities.Print($"SetAccountData,playerID={accountData.playerID},account={accountData.account},password={accountData.password},name={accountData.name}");
                Redis.SetAccountData(accountData.account, accountData);
                CSRegisterRes res = new CSRegisterRes {
                    RetCode = 0
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_REGISTER_RES, res);
            }
        }
Пример #3
0
        public static void OnAddBlockReq(Player player, MemoryStream stream)
        {
            Ultilities.Print("OnAddBlockReq");
            CSAddBlockReq req = NetworkManager.Deserialize <CSAddBlockReq>(stream);

            Vector2Int chunk      = Ultilities.GetChunk(req.block.position);
            bool       addSuccess = TerrainData.AddBlockInChunk(chunk, req.block);

            //回包
            CSAddBlockRes res = new CSAddBlockRes();

            if (addSuccess)
            {
                res.RetCode = 0;
                res.block   = req.block;
            }
            else
            {
                res.RetCode = 2;
            }
            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_ADD_BLOCK_RES, res);
            if (addSuccess)
            {
                //同步给有该chunk视野的其他玩家
                foreach (Player p in TerrainData.GetChunkViewPlayers(chunk))
                {
                    if (p.id != player.id)
                    {
                        AddBlockNotify(p, req.block);
                    }
                }
            }
        }
Пример #4
0
 static void Send()
 {
     while (true)
     {
         if (_message.Count > 0)
         {
             Package     package = _message.Dequeue();
             List <byte> bytes   = new List <byte>();
             bytes.AddRange(BitConverter.GetBytes((ushort)package.type));
             bytes.AddRange(BitConverter.GetBytes((uint)package.data.Length));
             bytes.AddRange(package.data);
             try
             {
                 int num = package.socket.Send(bytes.ToArray());
                 if (package.type != ENUM_CMD.CS_PLAYER_MOVE_NOTIFY)
                 {
                     Ultilities.Print($"send mesage, num={num}, length={package.data.Length}, type={package.type}");
                 }
             }
             catch
             {
                 Console.WriteLine("send failed");
                 continue;
             }
         }
     }
 }
Пример #5
0
 public static void Init()
 {
     if (redis == null)
     {
         redis = ConnectionMultiplexer.Connect("127.0.0.1");
         Ultilities.Print("Redis Ready!");
     }
 }
Пример #6
0
        static void Receive(object obj)
        {
            Player player = obj as Player;

            while (true)
            {
                byte[] data    = new byte[6];
                int    receive = 0;
                try
                {
                    receive = player.socket.Receive(data);
                }
                catch
                {
                    PlayerDisconnect(player);
                    break;
                }

                if (receive > 0)
                {
                    MemoryStream stream = new MemoryStream(data);
                    BinaryReader binary = new BinaryReader(stream, Encoding.UTF8);
                    ENUM_CMD     type   = (ENUM_CMD)binary.ReadUInt16();
                    uint         length = binary.ReadUInt32();

                    data = new byte[length];
                    player.socket.Receive(data);
                    stream = new MemoryStream(data);

                    if (type != ENUM_CMD.CS_HERO_MOVE_REQ)
                    {
                        Ultilities.Print($"recieved message, type={type}, length={length}");
                    }
                    if (_callbacks.ContainsKey(type))
                    {
                        CallBackFunction func = _callbacks[type];
                        func(player, stream);
                    }
                }
                else
                {
                    PlayerDisconnect(player);
                    break;
                }
            }
        }
Пример #7
0
 static void AddPlayer(Player player, CSPlayer playerData)
 {
     player.id       = playerData.PlayerID;
     player.name     = playerData.Name;
     player.position = new Vector3 {
         x = playerData.Position.x, y = playerData.Position.y, z = playerData.Position.z
     };
     player.rotation = new Vector3 {
         x = playerData.Rotation.x, y = playerData.Rotation.y, z = playerData.Rotation.z
     };
     player.inRoom   = true;
     player.curChunk = new Vector2Int {
         x = (int)Math.Floor(player.position.x / 16f), y = (int)Math.Floor(player.position.z / 16f)
     };
     TerrainData.GetChunkPlayers(player.curChunk).Add(player);
     players.Add(player.id, player);
     Ultilities.Print($"player {player.name}({player.socket.RemoteEndPoint}) has logged in!");
 }
Пример #8
0
        public static void OnChunksEnterLeaveViewReq(Player player, MemoryStream stream)
        {
            CSChunksEnterLeaveViewReq req = NetworkManager.Deserialize <CSChunksEnterLeaveViewReq>(stream);

            Ultilities.Print("CSChunksEnterLeaveViewReq," + req.EnterViewChunks.Count + "," + req.LeaveViewChunks.Count);

            bool leaveView = true;
            bool enterView = ProcessChunksEnterView(player, req.EnterViewChunks, out List <CSChunk> enterViewChunks);
            List <Vector2Int> leaveViewChunks = null;

            if (enterView)
            {
                if (req.LeaveViewChunks.Count > 0)
                {
                    leaveView = ProcessChunksLeaveView(player, req.LeaveViewChunks, out leaveViewChunks);
                }
            }

            if (enterView && leaveView)
            {
                CSChunksEnterLeaveViewRes res = new CSChunksEnterLeaveViewRes
                {
                    RetCode = 0
                };
                res.EnterViewChunks.AddRange(enterViewChunks);
                if (leaveViewChunks != null)
                {
                    res.LeaveViewChunks.AddRange(Vector2Int.Vector2IntList_To_CSVector2IntList(leaveViewChunks));
                }
                Ultilities.Print("CSChunksEnterLeaveViewRes," + res.EnterViewChunks.Count + "," + res.LeaveViewChunks.Count);
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_CHUNKS_ENTER_LEAVE_VIEW_RES, res);
            }
            else
            {
                CSChunksEnterLeaveViewRes res = new CSChunksEnterLeaveViewRes
                {
                    RetCode = 4
                };
                Ultilities.Print("CSChunksEnterLeaveViewRes," + res.EnterViewChunks.Count + "," + res.LeaveViewChunks.Count);
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_CHUNKS_ENTER_LEAVE_VIEW_RES, res);
            }
        }
Пример #9
0
        public static void OnSendMessageReq(Player player, MemoryStream stream)
        {
            CSSendMessageReq req = NetworkManager.Deserialize <CSSendMessageReq>(stream);
            //Console.WriteLine($"OnSendMessageReq,content={req.Content}");

            int retCode = player.inRoom ? 0 : -1;

            if (player.inRoom)
            {
                Ultilities.Print(req.Content, player.name);
            }

            CSSendMessageRes res = new CSSendMessageRes
            {
                RetCode = retCode
            };

            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_SEND_MESSAGE_RES, res);

            MessageNotify(player.name, req.Content);
        }
Пример #10
0
        public static void Start()
        {
            string ip = Ultilities.GetIpv4Address();
            //string ip = new WebClient().DownloadString("http://icanhazip.com");
            //ip = ip.Substring(0,ip.Length-1);//remove the \n character
            Socket     _socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);

            try
            {
                _socket.Bind(endPoint);
            }
            catch (Exception ex)
            {
                Ultilities.Print("bind socket failed");
                Ultilities.Print(ex.ToString());
                return;
            }
            _socket.Listen(0);

            Ultilities.Print($"server started, ip = {ip}, port = {port}");

            Thread sendThread = new Thread(Send);

            sendThread.Start();

            while (true)
            {
                Socket client = _socket.Accept();

                Player player = new Player();
                player.socket = client;

                Ultilities.Print($"{player.socket.RemoteEndPoint} connected");

                Thread receiveThread = new Thread(Receive);
                receiveThread.Start(player);
            }
        }
Пример #11
0
        public static void OnLoginReq(Player player, MemoryStream stream)
        {
            CSLoginReq req = NetworkManager.Deserialize <CSLoginReq>(stream);

            Ultilities.Print($"CSLoginReq,account={req.Account}");

            //检测是否已注册
            bool bAccountExist = Redis.GetAccountData(req.Account, out AccountData accountData);

            //Ultilities.Print($"bAccountExist={bAccountExist}");
            if (!bAccountExist)
            {
                CSLoginRes res = new CSLoginRes {
                    RetCode = 6
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
            }
            else
            {
                //检测是否已登录,防止顶号
                bool hasLoggedIn = players.ContainsKey(accountData.playerID);
                //Ultilities.Print($"hasLoggedIn={hasLoggedIn}");
                if (hasLoggedIn)
                {
                    CSLoginRes res = new CSLoginRes {
                        RetCode = 5
                    };
                    NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
                }
                else
                {
                    //检查密码是否正确
                    bool bCorrect = req.Password.Equals(accountData.password);
                    //Ultilities.Print($"req.Password={req.Password},accountData.password={accountData.password},bCorrect={bCorrect}");
                    if (!bCorrect)
                    {
                        CSLoginRes res = new CSLoginRes {
                            RetCode = 7
                        };
                        NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
                    }
                    else
                    {
                        bool hasData = Redis.GetPlayerData(accountData.playerID, out CSPlayer playerData);
                        //Ultilities.Print($"hasData={hasData}");
                        if (!hasData)
                        {
                            //如果是第一次登陆,则写数据库
                            playerData = new CSPlayer
                            {
                                PlayerID = accountData.playerID,
                                Name     = accountData.name,
                                Position = new CSVector3 {
                                    x = 0, y = 30, z = 0
                                },
                                Rotation = new CSVector3 {
                                    x = 0, y = 0, z = 0
                                }
                            };
                            Redis.SetPlayerData(playerData.PlayerID, playerData);
                        }

                        AddPlayer(player, playerData);
                        //Ultilities.Print($"playerData,playerID={accountData.playerID},account={accountData.account},password={accountData.password},name={accountData.name}");
                        //Ultilities.Print($"player,id={player.id},name={player.name}");

                        CSLoginRes res = new CSLoginRes
                        {
                            RetCode    = 0,
                            PlayerData = playerData
                        };
                        NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);

                        MessageNotify("system", "player " + player.name + " has logged in, current number of player : " + players.Count);
                    }
                }
            }
        }
Пример #12
0
 static void PlayerDisconnect(Player player)
 {
     Ultilities.Print($"player {player.name} disconnected");
     player.socket.Close();
     GameLogic.RemovePlayer(player);
 }