Exemplo n.º 1
0
        static void Matchalgorithm()
        {
            while (true)
            {
                lock (matchLock)
                {
                    for (int j = 0; j < AllWaitforMatchpools.Count; j++)
                    {
                        int len = AllWaitforMatchpools[j].MatchHashsetPool.Count;
                        // Console.WriteLine("for ahead AllWaitforMatchpools len" + len.ToString());
                        for (int i = 0; i < len; i++)
                        {
                            //  Console.WriteLine("AllWaitforMatchpools len"+ len.ToString());

                            if (AllWaitforMatchpools[j].currentroom == null || AllWaitforMatchpools[j].currentroom.mprocess.HasExited)
                            {
                                int nvn = 0;
                                if (!Int32.TryParse(AllWaitforMatchpools[j].nvn, out nvn))
                                {
                                    nvn = 16;
                                }
                                AllWaitforMatchpools[j].currentroom               = new Room(nvn, LanchServer.CreateOneRoom());//the client who create room determine the nvn
                                AllWaitforMatchpools[j].currentroom.listroom      = roomlist;
                                AllWaitforMatchpools[j].currentroom.tcpclienttype = AllWaitforMatchpools[j];
                                roomlist.Add(AllWaitforMatchpools[j].currentroom);
                                //Thread.Sleep(100);//wait IP port take effect
                            }
                            TCPClient temp          = AllWaitforMatchpools[j].MatchHashsetPool.ElementAt(i);;
                            int       maxnumber     = AllWaitforMatchpools[j].currentroom.getroommaxpeoplenumber();
                            int       currentnumber = AllWaitforMatchpools[j].currentroom.getnumberofpeopleinroom();
                            if (currentnumber < maxnumber)
                            {
                                AllWaitforMatchpools[j].currentroom.Add(temp);
                            }
                            else//full
                            {
                                FMessagePackage mp = new FMessagePackage();
                                mp.MT = MessageType.MAPISFULL;
                                String str = JsonConvert.SerializeObject(mp);
                                temp.Send(str);// tell client the number of people in current map has reach the ceilling
                            }

                            temp.isinmatchpool = true;
                            AllWaitforMatchpools[j].MatchHashsetPool.Remove(temp);
                            len = AllWaitforMatchpools[j].MatchHashsetPool.Count;
                            i   = 0;
                            //if (!notfull)
                            //{
                            //    roomlist.Add(AllWaitforMatchpools[j].currentroom);
                            //    AllWaitforMatchpools[j].currentroom = null;
                            //}
                        }
                    }
                }
                Thread.Sleep(200);
            }
        }
Exemplo n.º 2
0
        private void AssignNumber(Room room)
        {
            FMessagePackage mp = new FMessagePackage();

            mp.MT      = MessageType.NUMBER;
            mp.PayLoad = room.entrycounter.ToString();
            String str = JsonConvert.SerializeObject(mp);

            Send(str);
        }
Exemplo n.º 3
0
        private void AssignGUID()
        {
            Guid g;

            // Create and display the value of two GUIDs.
            g = Guid.NewGuid();
            string          guidstring = g.ToString();
            FMessagePackage mp         = new FMessagePackage();

            mp.MT      = MessageType.GUID;
            mp.PayLoad = guidstring;
            String str = JsonConvert.SerializeObject(mp);

            Send(str);
        }
Exemplo n.º 4
0
        void allpeoplepresentjustgo()
        {
            int len = mPeopleinroom.Count;

            for (int i = 0; i < len; i++)
            {
                FMessagePackage mp = new FMessagePackage();
                mp.MT      = MessageType.EntryMAP;
                mp.PayLoad = roomipaddress;
                String str = JsonConvert.SerializeObject(mp);
                mPeopleinroom[i].Send(str);
                Console.WriteLine("mp.MT = MessageType.EntryMAP");
                while (!mPeopleinroom[i].getentrymapisok())
                {
                    Thread.Sleep(100);
                    Console.WriteLine("wait for entry room :");
                }
            }
        }
Exemplo n.º 5
0
        public bool Add(TCPClient mtc)
        {
            mtc.room = this;
            mPeopleinroom.Add(mtc);
            mtc.onjoinroom.Invoke(this);
            entrycounter++;
#if MODE1
            ///////////////////
            ///
            FMessagePackage mp = new FMessagePackage();
            mp.MT      = MessageType.EntryMAP;
            mp.PayLoad = roomipaddress;
            String str = JsonConvert.SerializeObject(mp);
            mtc.Send(str);
#endif
            if (pker.runing == false)
            {
                pker.runing = true;
                pker.threadkiller.Start();
            }
            ////////////////////////////
            int len = mPeopleinroom.Count;
            Console.WriteLine("Room :" + len.ToString());
            if (len < maxpeople)
            {
                return(true);
            }
            else
            {
#if MODE1
#else
                Thread mt = new Thread(new ThreadStart(allpeoplepresentjustgo));
                mt.Start();
                Console.WriteLine("allpeoplepresentjustgo :");
#endif
                return(false);
            }
        }
Exemplo n.º 6
0
        void messagehandler(byte[] buffer)
        {
            FMessagePackage mp;

            try
            {
#if UTF16
                var str = System.Text.Encoding.Unicode.GetString(buffer);
#else
                var str = System.Text.Encoding.UTF8.GetString(buffer);
#endif
                int    len        = str.Length;
                string filestr    = "{\r\n\t\"mT\": \"FILE";
                string fileendstr = "{\r\n\t\"mT\": \"FILEEND";
                if (isfile)
                {
                    if (str.StartsWith(fileendstr))
                    {
                        int size = filestringpayload.Length;
                        isfile = false;
                        return;
                    }

                    filestringpayload += str;
                    int             size1    = filestringpayload.Length;
                    FMessagePackage filesend = new FMessagePackage();
                    filesend.MT = MessageType.FILE;//go on
                    String strsend = JsonConvert.SerializeObject(filesend);
                    Send(strsend);
                    return;
                }
                if (str.StartsWith(filestr))
                {
                    isfile = true;
                    return;
                }

                mp = JsonConvert.DeserializeObject <FMessagePackage>(str);
                switch (mp.MT)
                {
                case MessageType.MATCH:
                    String[] strarray = mp.PayLoad.Split('?');
                    map   = strarray[0];  //map
                    mapID = strarray[1];  //mapID
                    nvn   = strarray[2];  //nvn
                    Console.WriteLine(map);
                    break;

                case MessageType.EntryMAPOK:
                    entrymapok = true;
                    break;

                case MessageType.EXITGAME:
                    mclosed = true;
                    CloseSocket();
                    room?.Remove(this);
                    ReceiveThread.Abort();
                    break;
                }
            }
            catch (Newtonsoft.Json.JsonSerializationException) {//buffer all zero//occur when mobile client force kill the game client
                mclosed = true;
                CloseSocket();
                room?.Remove(this);
                ReceiveThread.Abort();
            }
        }