예제 #1
0
        private void Parse()
        {
            string message = "";
            lock (Program.messageQueue)
            {
                if (Program.messageQueue.Count > 0)
                    message = Program.messageQueue.Dequeue();
            }

            if (message != "")
            {
                try
                {
                    JsonData data = JsonMapper.ToObject(message);
                    int type = (int)data["type"];
                    switch (type)
                    {
                        case (int)Packet.eType.CG_CHAT:
                            {
                                string nickname = (string)data["nickName"];
                                string chatdata = (string)data["chatData"];

                                Packet.GCAckChat sendPacket = new Packet.GCAckChat();
                                sendPacket.chatData = string.Format("[{0}] : {1}", nickname, chatdata);

                                string response = JsonMapper.ToJson(sendPacket);
                                Console.WriteLine(response);

                                foreach (UserObject user in Program.ms_ClientList)
                                {
                                    user.Send(response);
                                }
                            }
                            break;
                        case (int)Packet.eType.CG_PLAYER_STATE:
                            {
                                Console.WriteLine("Packet.eType.CG_PLAYER_STATE");
                                
                                uint uniqueConnID = uint.Parse(data["uniqueConnID"].ToString());
                                int state = int.Parse(data["state"].ToString());
                                float posX = float.Parse(data["posX"].ToString());
                                float posY = float.Parse(data["posY"].ToString());
                                float posZ = float.Parse(data["posZ"].ToString());
                                float dirX = float.Parse(data["dirX"].ToString());
                                float dirY = float.Parse(data["dirY"].ToString());
                                float dirZ = float.Parse(data["dirZ"].ToString());
                                float speed = float.Parse(data["speed"].ToString());

                                // ================ Send Packet to All Users =============//
                                Packet.GCAckPlayerState sendPacket = new Packet.GCAckPlayerState();
                                sendPacket.uniqueConnID = uniqueConnID;
                                sendPacket.state = state;
                                sendPacket.posX = posX.ToString();
                                sendPacket.posY = posY.ToString();
                                sendPacket.posZ = posZ.ToString();
                                sendPacket.dirX = dirX.ToString();
                                sendPacket.dirY = dirY.ToString();
                                sendPacket.dirZ = dirZ.ToString();
                                sendPacket.speed = speed.ToString();                                

                                string response = JsonMapper.ToJson(sendPacket);
                                Console.WriteLine(response);

                                foreach (UserObject user in Program.ms_ClientList)
                                {
                                    user.Send(response);
                                }

                                // === Synchronize Player Pool in server side ===//
                                PlayerCharacter player = null;
                                if (dicPlayers.ContainsKey(uniqueConnID))
                                {
                                    player = dicPlayers[uniqueConnID];                                    
                                }
                                else
                                {
                                    player = new PlayerCharacter();
                                    dicPlayers.Add(uniqueConnID, player);
                                }

                                if(player != null)
                                {
                                    player.transform.position = new Vector3(posX, posY, posZ);
                                    player.transform.forward = new Vector3(dirX, dirY, dirZ);
                                    player.SetStateByNetwork(state);
                                }
                                

                            }
                            break;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }


            }
        }
예제 #2
0
        private void Parse()
        {
            string message = "";

            lock (Program.messageQueue)
            {
                if (Program.messageQueue.Count > 0)
                {
                    message = Program.messageQueue.Dequeue();
                }
            }

            if (message != "")
            {
                try
                {
                    JsonData data = JsonMapper.ToObject(message);
                    int      type = (int)data["type"];
                    switch (type)
                    {
                    case (int)Packet.eType.CG_CHAT:
                    {
                        string nickname = (string)data["nickName"];
                        string chatdata = (string)data["chatData"];

                        Packet.GCAckChat sendPacket = new Packet.GCAckChat();
                        sendPacket.chatData = string.Format("[{0}] : {1}", nickname, chatdata);

                        string response = JsonMapper.ToJson(sendPacket);
                        Console.WriteLine(response);

                        foreach (UserObject user in Program.ms_ClientList)
                        {
                            user.Send(response);
                        }
                    }
                    break;

                    case (int)Packet.eType.CG_PLAYER_STATE:
                    {
                        Console.WriteLine("Packet.eType.CG_PLAYER_STATE");

                        uint  uniqueConnID = uint.Parse(data["uniqueConnID"].ToString());
                        int   state        = int.Parse(data["state"].ToString());
                        float posX         = float.Parse(data["posX"].ToString());
                        float posY         = float.Parse(data["posY"].ToString());
                        float posZ         = float.Parse(data["posZ"].ToString());
                        float dirX         = float.Parse(data["dirX"].ToString());
                        float dirY         = float.Parse(data["dirY"].ToString());
                        float dirZ         = float.Parse(data["dirZ"].ToString());
                        float speed        = float.Parse(data["speed"].ToString());

                        // ================ Send Packet to All Users =============//
                        Packet.GCAckPlayerState sendPacket = new Packet.GCAckPlayerState();
                        sendPacket.uniqueConnID = uniqueConnID;
                        sendPacket.state        = state;
                        sendPacket.posX         = posX.ToString();
                        sendPacket.posY         = posY.ToString();
                        sendPacket.posZ         = posZ.ToString();
                        sendPacket.dirX         = dirX.ToString();
                        sendPacket.dirY         = dirY.ToString();
                        sendPacket.dirZ         = dirZ.ToString();
                        sendPacket.speed        = speed.ToString();

                        string response = JsonMapper.ToJson(sendPacket);
                        Console.WriteLine(response);

                        foreach (UserObject user in Program.ms_ClientList)
                        {
                            user.Send(response);
                        }

                        // === Synchronize Player Pool in server side ===//
                        PlayerCharacter player = null;
                        if (dicPlayers.ContainsKey(uniqueConnID))
                        {
                            player = dicPlayers[uniqueConnID];
                        }
                        else
                        {
                            player = new PlayerCharacter();
                            dicPlayers.Add(uniqueConnID, player);
                        }

                        if (player != null)
                        {
                            player.transform.position = new Vector3(posX, posY, posZ);
                            player.transform.forward  = new Vector3(dirX, dirY, dirZ);
                            player.SetStateByNetwork(state);
                        }
                    }
                    break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }