//Пототовая функция для прослушки новых соединений public void Session(object data) { total_players++; string name = (string)data; Console.WriteLine(" Started session with: " + name + "::ID: " + obj.Clients_ID[name] + " Total online: " + total_players); IPEndPoint addr = new IPEndPoint(IPAddress.Parse(obj.Clients_IP[name]), obj.Clients_PORT[name]); Console.WriteLine("Client addres: " + addr); if (obj.Characters_XYD.ContainsKey(name)) { ; //Characters_XYD[name] = new string[] { 55.ToString(), 55.ToString(), "W" }; } else { obj.Characters_XYD.Add(name, new string[] { 0.ToString(), 0.ToString(), "W" }); } Send("ACCEPTED", "", addr); try { //Цикл обновления инфы на клиенте while (obj.Clients_ONLINE[name] == true) { foreach (var item in obj.Mobs_XYD.Keys) { Thread.Sleep(5); Send("MOB_XYD", item + "¶X" + obj.Mobs_XYD[item].GetValue(0) + "¶Y" + obj.Mobs_XYD[item].GetValue(1) + "¶D" + obj.Mobs_XYD[item].GetValue(2), addr); } foreach (var item in obj.Characters_XYD.Keys) { Thread.Sleep(10); Send("CH_XYD", item + "¶X" + obj.Characters_XYD[item].GetValue(0) + "¶Y" + obj.Characters_XYD[item].GetValue(1) + "¶D" + obj.Characters_XYD[item].GetValue(2), addr); } byte[] pack = game_listener.Receive(ref remoteIp); string msg = Encoding.UTF8.GetString(pack); //Клиент просит зафиксировать информацию на серве switch (Cons.cmd(msg)) { case "SET_XYD": Console.WriteLine(name + " inSession: " + msg); Cons.Parse_Char_XYD(ref obj.Characters_XYD, Cons.data(msg)); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } }
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(); } }
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(); } }