コード例 #1
0
        public async void SendS(byte command, byte[] msg, IPEndPoint addr)
        {
            byte[] s_packet = new byte[1 + msg.Length];
            for (int i = 1; i < msg.Length + 1; i++)
            {
                s_packet[i] = msg[i - 1];
            }
            s_packet[0] = command;

            Ping      pinger = new Ping();
            PingReply reply  = pinger.Send(addr.Address);

            try
            {
                for (int i = 0; i < 100; i++)
                {
                    game_listener.Send(s_packet, s_packet.Length, addr);

                    //Console.WriteLine("Game SENDS:[" + i + "] To:" + addr + " Message:[" + Encoding.UTF8.GetString(msg) + "]");

                    await Task.Delay((int)reply.RoundtripTime + 1);

                    //Console.WriteLine("Received:[" + clients_iport[Cons.ToIport(addr)] + "]");

                    if (clients_iport.ContainsKey(Cons.ToIport(addr)) && clients_iport[Cons.ToIport(addr)] == Encoding.UTF8.GetString(msg))
                    {
                        clients_iport[Cons.ToIport(remoteIp)] = null; break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Game serv: SendS exception: To::" + addr + "::" + ex.ToString());
            }
        }
コード例 #2
0
        void Listen_Clients()
        {
            //Создание лог. файла
            //DateTime dt = DateTime.Now;
            //string writePath = @"Logs ["+dt.Hour.ToString()+"h "+dt.Minute.ToString()+"m] ["+dt.ToLongDateString()+"].txt";
            //StreamWriter sw = new StreamWriter(writePath, false, Encoding.Default);

            string name = "";

            byte[] costyl = new byte[512];
            byte[] pack   = new byte[512];
            Console.WriteLine("Game server: Started listen");
            try
            {
                while (game_up)
                {
                    pack = game_listener.Receive(ref remoteIp);

                    //Запись логов
                    //sw.WriteLine("[" + dt.DayOfWeek + " " + dt.ToLongTimeString() + "]: " +pack[0].ToString()+"::"+Cons.GetData(pack));
                    //Console.WriteLine("Pack len = " + pack.Length + " " + Cons.GetData(pack) + "name=" + Cons.GetName13b(pack) + "|");

                    //Проверка легитимности пакета
                    if (!clients_iport.ContainsKey(Cons.ToIport(remoteIp)))
                    {
                        Console.WriteLine("Packet from unregistred IP:Port"); continue;
                    }

                    switch (pack[0])
                    {
                    //END
                    case 3:
                        name = Cons.GetData(pack);
                        if (obj._Char.ContainsKey(name) &&
                            (remoteIp.ToString() == obj._Char[name].Addr.ToString() && remoteIp.Port == obj._Char[name].Addr.Port))
                        {
                            Array.Copy(pack, 1, obj._Char[name].packet.END.data, 0, pack.Length - 1);
                            obj._Char[name].packet.END.changed = true;
                        }
                        break;

                    //SET_XYD
                    case 4:
                        name = Cons.GetName14b(pack);
                        if (obj._Char.ContainsKey(name) &&
                            (remoteIp.ToString() == obj._Char[name].Addr.ToString() && remoteIp.Port == obj._Char[name].Addr.Port))
                        {
                            Cons.SetPlayer_XYD(ref obj, pack);
                            Array.Copy(pack, 1, costyl, 0, pack.Length - 1);    //Записывается с пустотами
                            obj._Char[name].packet.SET_XYD.data    = costyl;
                            obj._Char[name].packet.SET_XYD.changed = true;
                        }
                        break;

                    //PLAYER_END_OK
                    case 6:
                        clients_iport[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;

                    //HIT: 1[type of attack] 2-5[x] 6-9[y] 10[npc or player] 11-end[id or name]
                    case 7:
                        foreach (var item in obj._Char.Keys)
                        {
                            if (obj._Char[item].port == remoteIp.Port && obj._Char[item].IP == remoteIp.Address.ToString())
                            {
                                var entrance = obj.players.First(p => p.Name == obj._Char[item].name);
                                entrance.AttackFunc(obj.mobs);
                            }
                        }
                        break;

                    //DROP_TAKEN | 1-4[id] 5-8[ex_id]
                    case 8:
                        if (drop_Handle[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        drop_Handle[Cons.ToIport(remoteIp)] = pack;
                        break;

                    //INV_UPDATED | 1 - 4[items_updated]
                    case 9:
                        if (clients_iport[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        clients_iport[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;

                    //ITEM_USED	| 1-4[id] 5-8[id_ex]
                    case 10:
                        if (item_Used[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        item_Used[Cons.ToIport(remoteIp)] = pack;
                        break;

                    //11		GROCERY_BUY | 1-4[id]
                    case 11:
                        if (grocery_buy[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        grocery_buy[Cons.ToIport(remoteIp)] = pack;
                        break;

                    //12		TAKE_QUEST  | 1-4[Q_ID] 5-8[NPC_ID]
                    case 12:
                        if (quest_Handle[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        quest_Handle[Cons.ToIport(remoteIp)] = pack;
                        break;

                    //13		QUESTS_ALL	| 1-end[same_data]
                    case 13:
                        if (clients_iport[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        clients_iport[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;

                    //14		QUEST_UPDATED	| 1-end[same_data]
                    case 14:
                        if (clients_iport[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        clients_iport[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;

                    //15      NPCxyd_REQUEST | 1 - end[same_data]
                    case 15:
                        //Console.WriteLine("15: " + Cons.GetData(pack));
                        if (clients_iport[Cons.ToIport(remoteIp)] != null)
                        {
                            continue;
                        }
                        clients_iport[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Game catch: Listner:: while[" + remoteIp + "]" + e.ToString() + "code:" + e.ErrorCode);
                if (game_up)
                {
                    Listen_Clients();
                }
            }
        }
コード例 #3
0
        //Для каждого игрока создается отдельный поток с сессией
        void Session(object char_name)
        {
            string     name   = (string)char_name;
            IPEndPoint addr   = new IPEndPoint(IPAddress.Parse(obj._Char[name].IP), obj._Char[name].port);
            bool       IsExit = false;

            bool CharsXYD   = false;
            bool MyXYD      = false;
            bool MobsTH     = false;
            bool DroppedTH  = false;
            bool DropHandle = false;
            bool Inventory  = false;
            bool Quests     = false;
            bool NpcTH      = false;

            try
            {
                List <Player> players  = new List <Player>(obj.players);
                var           entrance = players.Find(r => r.Name == name);

                //Цикл обновления инфы на клиенте
                while (obj._Char.ContainsKey(name) && obj._Char[name].online == true)
                {
                    if (obj._Char[name].packet.END.changedP == true)
                    {
                        //Указываем что происходит выход,
                        //Чтобы при повторных пакетах о выходе не вызывать функцию выхода еще раз
                        if (!IsExit)
                        {
                            IsExit = true;
                            Send(252, new byte[] { 0 }, addr);
                            Disconnect(name);
                        }
                    }

                    //Отправка координат всех игроков, после того как были обновлены свои координаты игрока
                    if (CharsXYD == false)
                    {
                        CharsXYD = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("Started CharsXYD Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                players = new List <Player>(obj.players);

                                foreach (var player in players)
                                {
                                    if (player == null || player.Name == name)
                                    {
                                        continue;
                                    }

                                    float X = player.X - entrance.X;
                                    float Y = player.Y - entrance.Y;

                                    if (Math.Sqrt(X * X + Y * Y) < 12d)
                                    {
                                        Send_CHXYD(player.X, player.Y, player.Rotation, player.Direction, player.Hp, player.Name, addr);//CH_XYD
                                    }
                                }
                                Thread.Sleep(10);
                            }
                        });
                    }

                    //Передача своих координат
                    if (MyXYD == false)
                    {
                        MyXYD = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started MyXYD Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                entrance.CheckDeath();
                                switch (entrance.Direction)
                                {
                                case 0: break;

                                case 1: entrance.MovedByControl(false, false, false, true, obj.collision_map); break;

                                case 2: entrance.MovedByControl(true, false, false, true, obj.collision_map); break;

                                case 3: entrance.MovedByControl(true, false, false, false, obj.collision_map); break;

                                case 4: entrance.MovedByControl(true, true, false, false, obj.collision_map); break;

                                case 5: entrance.MovedByControl(false, true, false, false, obj.collision_map); break;

                                case 6: entrance.MovedByControl(false, true, true, false, obj.collision_map); break;

                                case 7: entrance.MovedByControl(false, false, true, false, obj.collision_map); break;

                                case 8: entrance.MovedByControl(false, false, true, true, obj.collision_map); break;
                                }
                                try
                                {
                                    if (item_Used[Cons.ToIport(addr)] != null)
                                    {
                                        Cons.UseItem(ref entrance, item_Used[Cons.ToIport(remoteIp)]);
                                        item_Used[Cons.ToIport(addr)] = null;
                                        entrance.Inventory.Changed    = true;
                                    }
                                }
                                catch (Exception e) { Console.WriteLine(e.ToString()); }

                                SendMyXYD(entrance.X, entrance.Y, entrance.Rotation, entrance.Direction, entrance.Hp, name, addr);
                                Thread.Sleep(10);
                            }
                        });
                    }

                    //Передача координат мобов
                    if (MobsTH == false)
                    {
                        MobsTH = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started mobs Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                for (int i = 0; i < obj.mobs.Length; i++)
                                {
                                    if (entrance == null || obj.mobs[i] == null)
                                    {
                                        continue;
                                    }
                                    float X = obj.mobs[i].X - entrance.X;
                                    float Y = obj.mobs[i].Y - entrance.Y;

                                    if (Math.Sqrt(X * X + Y * Y) < 12d)
                                    {
                                        Send_MobXYD(obj.mobs[i].X, obj.mobs[i].Y, 0, 0, obj.mobs[i].HP, obj.mobs[i].Id, i, addr);//CH_XYD
                                    }
                                    // Thread.Sleep(1);
                                }
                                Thread.Sleep(150);
                            }
                        });
                    }

                    //Передача координат NPC
                    if (NpcTH == false)
                    {
                        NpcTH = true;
                        Task.Run(() =>
                        {
                            bool[] NPCsent = new bool[100];
                            for (int i = 0; i < 100; i++)
                            {
                                NPCsent[i] = false;
                            }
                            try
                            {
                                Console.WriteLine("     Started NPC Thread, id thread: " + Task.CurrentId);
                                while (obj._Char.ContainsKey(name) && obj._Char[name].online == true)
                                {
                                    for (int i = 0; i < obj.NPCs.Length; i++)
                                    {
                                        if (entrance == null || obj.NPCs[i] == null)
                                        {
                                            continue;
                                        }
                                        float X = obj.NPCs[i].X - entrance.X;
                                        float Y = obj.NPCs[i].Y - entrance.Y;

                                        if (NPCsent[i] == false && (Math.Sqrt(X * X + Y * Y) < 12d))
                                        {
                                            Send_NPCXYDs(obj.NPCs[i].X, obj.NPCs[i].Y, 0, 0, obj.NPCs[i].Id, obj.NPCs[i].Name, addr);
                                            NPCsent[i] = true;
                                        }
                                        else
                                        {
                                            if (!(Math.Sqrt(X * X + Y * Y) < 12d))
                                            {
                                                NPCsent[i] = false;
                                            }
                                        }
                                    }
                                    Thread.Sleep(150);
                                }
                            }
                            catch (Exception e) { Console.WriteLine(e.ToString()); }
                        });
                    }

                    //Передача дропа
                    if (DroppedTH == false)
                    {
                        DroppedTH = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started drop sender Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                for (int i = 0; i < obj.items.Count; i++)
                                {
                                    if (entrance == null || obj.items[i] == null)
                                    {
                                        continue;
                                    }
                                    float X = obj.items[i].X - entrance.X;
                                    float Y = obj.items[i].Y - entrance.Y;

                                    if ((Math.Sqrt(X * X + Y * Y) < 12d))
                                    {
                                        if (obj.items[i].Dropped)
                                        {
                                            Send_Drop(obj.items[i].X, obj.items[i].Y, obj.items[i].Id, obj.items[i].Quantity, obj.items[i].ID_EX, addr);
                                        }
                                        else if (obj.items[i].Taken)
                                        {
                                            Del_Drop(obj.items[i].ID_EX, addr);
                                        }
                                    }
                                }
                                Thread.Sleep(100);
                            }
                        });
                    }

                    //Обработка запросов на лут персонажа
                    if (DropHandle == false)
                    {
                        DropHandle = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started drop handler Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                if (drop_Handle[Cons.ToIport(addr)] != null)
                                {
                                    try
                                    {
                                        //Console.WriteLine("Item handling");

                                        byte[] id = new byte[4];
                                        byte[] ex = new byte[4];

                                        Array.Copy(drop_Handle[Cons.ToIport(addr)], 1, id, 0, 4);
                                        Array.Copy(drop_Handle[Cons.ToIport(addr)], 5, ex, 0, 4);

                                        int ID = BitConverter.ToInt32(id, 0);
                                        int EX = BitConverter.ToInt32(ex, 0);

                                        Item item = obj.items.Find(i => (i.Id == ID && i.ID_EX == EX));

                                        if (item == null)
                                        {
                                            //Console.WriteLine("Item not found");
                                            Send(245, "NO" + Cons.GetData(drop_Handle[Cons.ToIport(addr)]), remoteIp);
                                            drop_Handle[Cons.ToIport(addr)] = null;
                                            continue;
                                        }

                                        if (item.Dropped == false)
                                        {
                                            //Console.WriteLine("Item not dropped");
                                            Send(245, "NO" + Cons.GetData(drop_Handle[Cons.ToIport(addr)]), remoteIp);
                                            drop_Handle[Cons.ToIport(addr)] = null;
                                            continue;
                                        }
                                        if (entrance.Inventory.Items.Contains(item) == true)
                                        {
                                            //Console.WriteLine("Item is in Inventory");
                                            Send(245, "NO" + Cons.GetData(drop_Handle[Cons.ToIport(addr)]), remoteIp);
                                            drop_Handle[Cons.ToIport(addr)] = null;
                                            continue;
                                        }

                                        float X = item.X - entrance.X;
                                        float Y = item.Y - entrance.Y;

                                        if (Math.Sqrt(X * X + Y * Y) < 1.5d)
                                        {
                                            item.Dropped = false;
                                            item.Taken   = true;
                                            entrance.Inventory.Add(item);

                                            obj.items.Remove(item);

                                            //Console.WriteLine("DROP_TAKEN:: To:" + remoteIp + "Data:" + Cons.GetData(drop_Handle[Cons.ToIport(addr)]));
                                            Send(245, Cons.GetData(drop_Handle[Cons.ToIport(addr)]), remoteIp);

                                            drop_Handle[Cons.ToIport(addr)] = null;
                                        }
                                        else
                                        {
                                            //Console.WriteLine("Far distance");
                                            drop_Handle[Cons.ToIport(addr)] = null;
                                        }
                                    }
                                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                                }

                                Thread.Sleep(1);
                            }
                        });
                    }

                    //Обработка инвентаря
                    if (Inventory == false)
                    {
                        Inventory = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started Inventory handler Thread, id thread: " + Task.CurrentId);
                            while (obj._Char[name].online == true)
                            {
                                if (entrance.Inventory.Changed == true)
                                {
                                    List <int> Id       = new List <int>();
                                    List <int> Ex       = new List <int>();
                                    List <int> Quantity = new List <int>();
                                    int total           = 0;

                                    for (int i = 0; i < entrance.Inventory.Items.Length; i++, total++)
                                    {
                                        if (entrance.Inventory.Items[i] == null)
                                        {
                                            break;
                                        }

                                        //Console.WriteLine("Adding: Item_" + i + ", ID = " + entrance.Inventory.Items[i].Id + ", EX = "+ entrance.Inventory.Items[i].ID_EX +", Q = " + entrance.Inventory.Items[i].Quantity);
                                        Id.Add(entrance.Inventory.Items[i].Id);
                                        Ex.Add(entrance.Inventory.Items[i].ID_EX);
                                        Quantity.Add(entrance.Inventory.Items[i].Quantity);
                                    }
                                    UpdateInv(total, Id, Ex, Quantity, addr);
                                    entrance.Inventory.Changed = false;
                                }
                                if (grocery_buy[Cons.ToIport(addr)] != null)
                                {
                                    Cons.Buy(ref entrance, grocery_buy[Cons.ToIport(addr)]);
                                    grocery_buy[Cons.ToIport(addr)] = null;
                                }
                                Thread.Sleep(50);
                            }
                        });
                    }

                    if (Quests == false)
                    {
                        Quests = true;
                        Task.Run(() =>
                        {
                            Console.WriteLine("     Started Quests handler Thread, id thread: " + Task.CurrentId);
                            List <int> Id       = new List <int>();
                            List <int> St       = new List <int>();
                            List <int> Quantity = new List <int>();
                            int total           = entrance.Quests.Count;

                            for (int i = 0; i < total; i++)
                            {
                                Id.Add(entrance.Quests[i].Id);
                                St.Add(entrance.Quests[i].State);
                                Quantity.Add(entrance.Quests[i].Counter);
                            }
                            Initialize_Quests(total, Id, St, Quantity, addr);

                            while (obj._Char[name].online == true)
                            {
                                entrance.CheckQuests();
                                try
                                {
                                    if (quest_Handle[Cons.ToIport(addr)] != null)
                                    {
                                        Cons.QuestHandle(ref entrance, quest_Handle[Cons.ToIport(addr)]);
                                        quest_Handle[Cons.ToIport(addr)] = null;
                                    }

                                    for (int i = 0; i < entrance.Quests.Count; i++)
                                    {
                                        if (entrance.Quests[i].UpdClient)
                                        {
                                            Quest_Update(entrance.Quests[i].Id, entrance.Quests[i].State, entrance.Quests[i].Counter, addr);
                                            entrance.Quests[i].UpdClient = false;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                                catch (Exception e) { Console.WriteLine(e.ToString()); }
                            }
                        });
                    }
                    Thread.Sleep(1);
                }
            }

            catch (SocketException e)
            {
                Console.WriteLine("     Game catch: Socket ex in session [" + char_name + "]" + ":: " + e.Message + "code:" + e.ErrorCode);
                if (obj._Char[name].online == true)
                {
                    Session(char_name);
                }
            }
            catch (Exception e) {
                //Console.WriteLine(e.ToString());
            }
        }
コード例 #4
0
        //Метод для нахождения и запуска свободного потока в листе
        public int Find_thSpace(string char_name, IPEndPoint addr)
        {
            if (game_up)
            {
                //По количеству макс. пользователей начиная с 0 ID
                for (int ID = 0; ID < max_users; ID++)
                {
                    //Если поток с данным ID не активен, то создаем на его месте
                    if (!session_threads[ID].IsAlive)
                    {
                        session_threads.Insert(ID, new Thread(new ParameterizedThreadStart(Session)));

                        //Инициализация сессионных параметров игрока
                        if (!obj._Char.ContainsKey(char_name))
                        {
                            obj._Char.Add(char_name, new Char_Stat {
                                IP = addr.Address.ToString()
                            });
                        }
                        else
                        {
                            obj._Char[char_name].IP = addr.Address.ToString();
                        }
                        obj._Char[char_name].finding_thread = true;
                        obj._Char[char_name].online         = true;
                        obj._Char[char_name].port           = addr.Port;
                        obj._Char[char_name].ID             = ID;


                        if (!clients_iport.ContainsKey(Cons.ToIport(addr)))
                        {
                            clients_iport.Add(Cons.ToIport(addr), null);
                        }
                        else
                        {
                            clients_iport[Cons.ToIport(addr)] = null;
                        }

                        //Словарь дропа
                        if (!drop_Handle.ContainsKey(Cons.ToIport(addr)))
                        {
                            drop_Handle.Add(Cons.ToIport(addr), null);
                        }
                        else
                        {
                            drop_Handle[Cons.ToIport(addr)] = null;
                        }

                        //Словарь обновления инвентаря
                        if (!item_Used.ContainsKey(Cons.ToIport(addr)))
                        {
                            item_Used.Add(Cons.ToIport(addr), null);
                        }
                        else
                        {
                            item_Used[Cons.ToIport(addr)] = null;
                        }

                        if (!grocery_buy.ContainsKey(Cons.ToIport(addr)))
                        {
                            grocery_buy.Add(Cons.ToIport(addr), null);
                        }
                        else
                        {
                            grocery_buy[Cons.ToIport(addr)] = null;
                        }

                        if (!quest_Handle.ContainsKey(Cons.ToIport(addr)))
                        {
                            quest_Handle.Add(Cons.ToIport(addr), null);
                        }
                        else
                        {
                            quest_Handle[Cons.ToIport(addr)] = null;
                        }

                        //Создаем обьект игрока с заданным именем, если его нету
                        obj.Initialize_Player(char_name);

                        //Говорим что инвентар поменялся чтобы, персонаж получил итемы при старте
                        var player = obj.players.Find(p => p.Name == char_name);
                        player.Inventory.Changed = true;

                        total_players++;

                        Console.WriteLine("Game server: Started session with:[" + char_name + "], ID: " + obj._Char[char_name].ID
                                          + ", Total online: [" + total_players + "]");
                        session_threads[ID].Start(char_name);
                        obj._Char[char_name].finding_thread = false;
                        return(0);
                    }
                }
                Console.WriteLine("Starting session: Max users");
                //obj._Char[char_name].finding_thread = false;
                return(2);
            }

            else
            {
                Console.WriteLine("Starting session: Game is down");
                // obj._Char[char_name].finding_thread = false;
                return(3);
            }
        }
コード例 #5
0
ファイル: Serv.cs プロジェクト: karust/qol_server
        public void Listen_Connections()
        {
            string msg = "", data = "", cmd = "";
            int    ID = 0;

            Console.WriteLine("Started to listen");
            try
            {
                while (Serv_UP)
                {
                    byte[] pack = listener.Receive(ref remoteIp);
                    msg  = Encoding.UTF8.GetString(pack);
                    cmd  = Cons.cmd(msg);
                    data = Cons.data(msg);//Пока не заменил на это для удобства
                    Console.WriteLine(cmd + "-" + remoteIp.Address + "::" + remoteIp.Port + "  " + data);

                    switch (cmd)
                    {
                    //Проверка сервера на живость
                    case "CHECK":
                        Send("ALIVE", "", remoteIp);
                        break;

                    //Клиент хочет начать сессию
                    case "BEGIN":
                        if (Clients_ONLINE.ContainsKey(data) && Clients_ONLINE[data] == true)
                        {
                            Send("DENIED", "", remoteIp);
                        }
                        else
                        {
                            if (ID == max_users)
                            {
                                Send("MAX_ONLINE", total_players.ToString(), remoteIp);
                            }
                            else
                            {
                                //Ищим свободный поток для клиента
                                for (ID = 0; ID < max_users; ID++)
                                {
                                    if (!session_threads[ID].IsAlive)
                                    {
                                        session_threads.Insert(ID, new Thread(new ParameterizedThreadStart(Session)));

                                        Clients_IP[data]                 = remoteIp.Address.ToString();
                                        Clients_PORT[data]               = remoteIp.Port;
                                        Clients_ONLINE[data]             = true;
                                        Clients_ID[data]                 = ID;
                                        Clients_ConnectFailedTimes[data] = 0;

                                        session_threads[ID].Start(data);
                                        break;
                                    }
                                }
                            }
                        }
                        break;

                    //Клиент хочет кончить сессию
                    case "END":
                        if (Clients_ONLINE.ContainsKey(Cons.data(msg)) && Clients_ONLINE[Cons.data(msg)] == true)
                        {
                            Clients_ONLINE[Cons.data(msg)] = false;
                            total_players--;
                            session_threads[Clients_ID[Cons.data(msg)]].Join();
                            session_threads[Clients_ID[Cons.data(msg)]].Interrupt();
                            session_threads[Clients_ID[Cons.data(msg)]].Abort();
                            Console.WriteLine("END1   " + Clients_ID[Cons.data(msg)] + "  " + session_threads[Clients_ID[Cons.data(msg)]].IsAlive + " Total online: " + total_players);
                            Send("SUCCESSFUL", "", remoteIp);
                        }
                        else
                        {
                            Send("ALREADY_OFFLINE", "", remoteIp);
                        }
                        break;
                    }
                }
            }
            catch (SocketException ex)
            {
                Listen_Connections();
            }
        }
コード例 #6
0
        public void Listen_Connections()
        {
            string name = "";

            Console.WriteLine("Login server: started");

            try
            {
                while (login_UP)
                {
                    byte[] pack = log_listen.Receive(ref remoteIp);

                    Console.WriteLine("Login server: [" + Encoding.UTF8.GetString(pack) + "] from:" + remoteIp.Address + "::" + remoteIp.Port);
                    if (!clients_iport_login.ContainsKey(Cons.ToIport(remoteIp)))
                    {
                        clients_iport_login.Add(Cons.ToIport(remoteIp), null);
                    }

                    switch (pack[0])
                    {
                    //1		CHECK       | --
                    case 1:
                        Send(255, "ALIVE", remoteIp);
                        break;

                    //2		BEGIN       | 1-end[char_name]
                    case 2:
                        name = Cons.GetData(pack);
                        //Если содержится игрок с таким именем и он онлайн
                        if (game_Obj._Char.ContainsKey(name) && game_Obj._Char[name].online == true)
                        {
                            if (game_Obj._Char[name].finding_thread == false && game_Obj._Char[name].client_informed == true)
                            {
                                Send(253, "Session is active", remoteIp);
                            }
                            //Если отключили игрока, то ставиться флаг онлайна - false.
                            //game_Obj._Char[name].online = !game_Serv.Disconnect(name);
                        }
                        else
                        {
                            //Запускаем поток-сессию для нового клиента
                            int result = game_Serv.Find_thSpace(name, remoteIp);
                            switch (result)
                            {
                            case 0:        //Успешно
                                SendS(254, game_Obj._Char[name].ID.ToString(), remoteIp, name);
                                game_Obj._Char[name].name = name;
                                break;

                            case 1:
                                Send(253, "Char is disconnecting, " + game_Serv.total_players.ToString(), remoteIp);        //Удалил вариант с этой ошибкой
                                break;

                            case 2:
                                Send(253, "Max online= " + game_Serv.total_players.ToString(), remoteIp);
                                break;

                            case 3:
                                Send(253, "Game server is down, " + game_Serv.total_players.ToString(), remoteIp);
                                break;
                            }
                        }
                        break;

                    //5         BEGIN_OK    | 1[id]
                    case 5:
                        //Записывается дата сюда, надо для механизма важных сообщений
                        Console.WriteLine("BEGIN_OK:  " + clients_iport_login[Cons.ToIport(remoteIp)]);
                        clients_iport_login[Cons.ToIport(remoteIp)] = Cons.GetData(pack);
                        break;
                    }
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Login server: socket ex in listener:: " + ex.Message);
                if (login_UP)
                {
                    Listen_Connections();
                }
            }
        }
コード例 #7
0
        //Сенд для важных сообщений
        //Послаю клиенту сообщение пока он не ответит - ОК+Сообщение которое я ему отправил
        public async void SendS(byte command, string message, IPEndPoint addr, string name)
        {
            byte[] msg      = Encoding.UTF8.GetBytes(message);
            byte[] s_packet = new byte[1 + msg.Length];
            for (int i = 1; i < msg.Length + 1; i++)
            {
                s_packet[i] = msg[i - 1];
            }
            s_packet[0] = command;

            try
            {
                for (int i = 0; i < 100; i++)
                {
                    //Console.WriteLine("ImpSend: " + i + " | " + addr + "::" + message);
                    log_listen.Send(s_packet, s_packet.Length, addr);
                    await Task.Delay(10);

                    //Console.WriteLine("Received: " + i + " | " + addr + ":: " + clients_iport_login[Cons.ToIport(addr)]);
                    if (clients_iport_login[Cons.ToIport(addr)] == "OK")
                    {
                        game_Obj._Char[name].client_informed = true; clients_iport_login[Cons.ToIport(addr)] = null; break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Возникло исключение в отправке по аддресу: " + addr + "::" + ex.ToString());
            }
        }
コード例 #8
0
ファイル: LoginServ.cs プロジェクト: karust/qol_server
        public void Listen_Connections()
        {
            string msg = "", data = "", cmd = "";
            int    ID = 0;

            Console.WriteLine("Log started");

            try
            {
                while (Login_UP)
                {
                    byte[] pack = log_listen.Receive(ref remoteIp);
                    msg  = Encoding.UTF8.GetString(pack);
                    cmd  = Cons.cmd(msg);
                    data = Cons.data(msg);
                    Console.WriteLine(cmd + "-" + remoteIp.Address + "::" + remoteIp.Port + "  " + data);

                    switch (cmd)
                    {
                    //Проверка сервера на живость
                    case "CHECK":
                        Send("ALIVE", "", remoteIp);
                        break;

                    //Клиент хочет начать сессию
                    case "BEGIN":
                        if (game_Obj.Clients_ONLINE.ContainsKey(data) && game_Obj.Clients_ONLINE[data] == true)
                        {
                            Send("DENIED", "", remoteIp);
                        }
                        else
                        {
                            if (ID == max_users)
                            {
                                Send("MAX_ONLINE", game_Serv.total_players.ToString(), remoteIp);
                            }
                            else
                            {
                                //Запускаем поток-сессию для нового клиента
                                if (game_Serv.Find_thSpace(ref ID, data, remoteIp))
                                {
                                    Send("STARTED", ID.ToString(), remoteIp);
                                }
                                else
                                {
                                    Send("MAX_ONLINE", game_Serv.total_players.ToString(), remoteIp);
                                }

                                /*//Ищим свободный поток для клиента
                                 * for (ID = 0; ID < max_users; ID++)
                                 * {
                                 *  if (!session_threads[ID].IsAlive)
                                 *  {
                                 *      session_threads.Insert(ID, new Thread(new ParameterizedThreadStart(Session)));
                                 *
                                 *      Clients_IP[data] = remoteIp.Address.ToString();
                                 *      Clients_PORT[data] = remoteIp.Port;
                                 *      Clients_ONLINE[data] = true;
                                 *      Clients_ID[data] = ID;
                                 *      Clients_ConnectFailedTimes[data] = 0;
                                 *
                                 *      session_threads[ID].Start(data);
                                 *      break;
                                 *  }
                                 * }*/
                            }
                        }
                        break;

                    //Клиент хочет кончить сессию
                    case "END":
                        //Конец сессии нужно перенести на гейм серв

                        /*  if (Clients_ONLINE.ContainsKey(Cons.data(msg)) && Clients_ONLINE[Cons.data(msg)] == true)
                         * {
                         *    Clients_ONLINE[Cons.data(msg)] = false;
                         *    total_players--;
                         *    session_threads[Clients_ID[Cons.data(msg)]].Join();
                         *    session_threads[Clients_ID[Cons.data(msg)]].Interrupt();
                         *    session_threads[Clients_ID[Cons.data(msg)]].Abort();
                         *    Console.WriteLine("END1   " + Clients_ID[Cons.data(msg)] + "  " + session_threads[Clients_ID[Cons.data(msg)]].IsAlive + " Total online: " + total_players);
                         *    Send("SUCCESSFUL", "", remoteIp);
                         * }
                         * else Send("ALREADY_OFFLINE", "", remoteIp);*/
                        break;
                    }
                }
            }
            catch (SocketException ex)
            {
                Listen_Connections();
            }
        }