private static void ThreadClient_OnKill(object sender, EventArgs e) { ThreadClient killEvent = sender as ThreadClient; Console.WriteLine("CASE 3"); ErroDisconect(killEvent); }
static void EnviarMessage(ThreadClient threadClientRem, string messageText) { foreach (ThreadClient threadClient in threadClients) { if (threadClient != threadClientRem) { threadClient.Write(new Message() { Type = 1, Text = messageText }); } } }
static void Main(string[] args) { bool isRunning = true; IPAddress iPAddress = IPAddress.Parse("127.0.0.1"); int port = 5000; TcpListener tcpListener = new TcpListener(iPAddress, port); tcpListener.Start(); Socket socket; ThreadClient threadClient; while (isRunning == true) { socket = tcpListener.AcceptSocket(); clientNumber++; threadClient = new ThreadClient(clientNumber, socket); threadClient.OnMessage += ThreadClient_OnMessage; threadClient.OnKill += ThreadClient_OnKill; threadClient.Sleep(); threadClients.Add(threadClient); Console.WriteLine($"Adicionando a lista {threadClients.Count}"); threadClient.Start(); if (clientNumber == 2) { StartGame(); } else { threadClient.Write(new Message() { Type = 1, Text = "Entrando na partida..." }); } Console.WriteLine("Qauntidade de cliente :" + clientNumber); } }
private static void ThreadClient_OnMessage(object sender, EventArgs e) { ThreadClient threadClient = sender as ThreadClient; MessageEventArgs messageEventArgs = e as MessageEventArgs; if (threadClient != null && messageEventArgs != null) { Console.WriteLine(messageEventArgs.Message.Type); switch (messageEventArgs.Message.Type) { case 1: //Texto if (threadClients.Count == 2) { Console.WriteLine("CASE 1"); EnviarMessage(threadClient, Convert.ToString((messageEventArgs.Message.Text))); } break; case 2: //Opção optionClients.Add(threadClient.GetNumber(), Convert.ToInt32(messageEventArgs.Message.Text)); Console.WriteLine("CASE 2"); if (optionClients.Count == 2) { EndGame(); } break; case 3: if (optionClients.Count > 0) { Console.WriteLine("CASE 3"); ErroDisconect(threadClient); } break; } } }
static void ErroDisconect(ThreadClient threadClientSender) { Console.WriteLine($"Removendo >>>>>>>>>>>>>> {threadClients.Count}"); threadClients.Remove(threadClientSender); clientNumber = 0; Console.WriteLine($"removido >>>>>>>>>>>>>> {threadClients.Count}"); ThreadClient thr = null; if (threadClients.Count >= 1) { foreach (ThreadClient threadClient in threadClients) { Console.WriteLine("ENVIAR para o outro jogador"); thr = threadClient; threadClient.Write(new Message() { Type = 3, Text = "Remove" }); } threadClients.Remove(thr); } }
private void TatamentoCliente(SocketServer server, Socket clientSocket) { this.thread = new ThreadClient(clientSocket, server); thread.Start(); }