List <ClientObject> clients = new List <ClientObject>(); // все подключения public void AddConnection(ClientObject clientObject) { clients.Add(clientObject); }
protected internal void AddConnection(ClientObject clientObject) { clients.Add(clientObject); }
protected internal void AddBusyClient(ClientObject clientObject) { BusyClients.Add(clientObject); }
void AcceptedClientThreadFunction(object Client) { TcpClient tcpClient = Client as TcpClient; List <string> InstructionArray; string message = ""; try { message = GetMessage(tcpClient); } catch { } Console.WriteLine(message); InstructionArray = CommandTranslator.Parse(message); //0-команда(signup, login, online) //1-логин //2-пароль //3-геолокация switch (InstructionArray[0]) { case "!signup": try { UserBaseDao.Write(InstructionArray[1], InstructionArray[2]); SendMessage("!accepted" + CommandTranslator.Encode(GetClientsNames), tcpClient); //+список онлайна ClientObject RegClientObject = new ClientObject(tcpClient, this, InstructionArray[1], InstructionArray[3]); Thread RegClientThread = new Thread(new ThreadStart(RegClientObject.Process)); RegClientThread.Start(); } catch { SendMessage("!unacceptedsignup", tcpClient); } //добавление в базу break; case "!login": if (UserBaseDao.Find(InstructionArray[1], InstructionArray[2])) { SendMessage("!accepted" + CommandTranslator.Encode(GetClientsNames), tcpClient); //+список онлайна ClientObject clientObject = new ClientObject(tcpClient, this, InstructionArray[1], InstructionArray[3]); Thread clientThread = new Thread(new ThreadStart(clientObject.Process)); clientThread.Start(); } else { SendMessage("!unaccepted", tcpClient); Console.WriteLine("!unaccepted"); } break; case "!online": Console.WriteLine("In online"); SendMessage(CommandTranslator.Encode(GetClientsNames), tcpClient); break; case "!file": Console.WriteLine("File!!!!"); break; } }
protected internal void AddConnection(ClientObject clientObject, string Id) { clients.Add(clientObject); usersId.Add(Id); setUsersOnline(); }
public void Process() { try { bool flag = false; do { Stream = client.GetStream(); // получаем имя пользователя string message = GetMessage(); if (message.Length >= 1) { userName = message.Substring(1); } else { return; } if (message[0] == '2') { if (server.SignUp(userName, this.Id)) { server.SignIn(userName, this.Id); message = "0"; flag = true; Console.WriteLine($"User {userName} enter in game"); } else { message = "-1"; } } else if (message[0] == '1') { if (server.SignIn(userName, this.Id)) { message = "0"; flag = true; Console.WriteLine($"User {userName} enter in game"); } else { message = "-1"; } } else { message = "not correct data"; } // посылаем сообщение о входе server.CallBack(message, this); }while (!flag); server.AddConnection(this); // в бесконечном цикле получаем сообщения от клиента while (true) { int k = 0; try { string message = GetMessage(); string msg = String.Format("{0}: {1}", userName, message); if (message != "") { Console.WriteLine(msg); } if (message == "exit") { Close(); } if (message == "" && k < 1) { message = String.Format("{0}: leave game 1", userName); Console.WriteLine(message); k++; k = 0; break; } //parsing messages //работа с получением списка активных пользователей if (message == "3") { string s = server.GetActiveUsers(); server.CallBack(s, this); } //получаем статистику пользователя else if (message == "4") { string[] str = userName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string s = server.GetStatistic(str[0]); server.CallBack(s, this); } //приглашаем опонентав игру else if (message.Length > 1 && message[0] == '5') { //отправляем пользователю приглашение string uName = message.Substring(1); bool ans = server.PossibleInvite(uName); if (ans) { ClientObject client = server.clients.FirstOrDefault(c => c.GetName() == uName); if (client == null) { server.CallBack("-2", this); } else { if (client != null) { InviteInGame(client); } else { server.CallBack("-2", this); } } } else { server.CallBack("-2", this); } } else if (message.Contains("type")) { //получаем список задач заданного типа string s = message[4].ToString(); Game game = server.games.FirstOrDefault(c => c.player1 == this || c.player2 == this); if (game != null) { server.GetTasks(s, game); game.SendTasks(); } else { server.CallBack("er", this); } } //принятие приглашения else if (message.Contains("acp")) { AcceptInvating(message); } //отправляем ответы противникy else if (message.Length > 2 && message[0] == 'U' && message[1] == 'A') { Game game = server.games.FirstOrDefault(c => c.player1 == this || c.player2 == this); if (game != null) { ClientObject player = game.player1 == this ? game.player2 : game.player1; server.CallBack(message, player); } } //добавляем ответы в статистику else if (message.Length > 2 && message[0] == 'M' && message[1] == 'A') { string[] UsAns = ParseAnsw(message); server.AddStatistic(this.GetName(), idTasks, UsAns); } //удаляем поля InGame и Game else if (message == "remGame") { server.RemoveInGame(GetName()); try { Game g = server.games.FirstOrDefault(c => c.player1 == this || c.player2 == this); if (g != null) { server.games.Remove(g); server.DeleteGame(g.idGame); g = null; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } catch (Exception ex) { Console.WriteLine(ex.Message); string message = String.Format("{0}: leave game 2", userName); Console.WriteLine(message); server.CallBack("exit", this); Close(); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { // в случае выхода из цикла закрываем ресурсы server.RemoveConnection(this.Id); Close(); } }
public void Procces() { try { userName = GetMessege(); Console.WriteLine(userName); server.SendToAll(userName + " вошел в чат", this.id); string msg_info; string msg; while (true) { try { msg_info = Response(); msg = GetMessege(); Console.WriteLine(msg); if (msg_info == "-1") { server.SendToAll(msg, this.id); continue; } else if (msg_info == "-2") { foreach (byte[] clientdata in ServerObject.GetList()) { stream.Write(clientdata); } continue; } ClientObject tmp = ServerObject.getClientFromNAME(msg_info); if (tmp != null && tmp.active) { ServerObject.AddConectionWith(this, tmp); server.SendTo(msg, this.id, msg_info); } else { Console.WriteLine("пользователя не существует"); } } catch { Console.WriteLine("-" + userName); server.SendToAll(userName + " покинул чат", this.id); break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { ServerObject.DelConnection(id); stream.Close(); tcp.Close(); } }