private void embaralhar() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "EMBARALHAR"; byte[] baralho = this.funcoes.ObjectToByteArray(comando); TrySend(baralho); }
public void perder() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "PERDER"; comando.ID = this.ID; byte[] perder = this.funcoes.ObjectToByteArray(comando); TrySend(perder); }
private void b_jogar_Click(object sender, EventArgs e) { timer1.Enabled = false; Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "JOGAR"; string id = funcoes.idSocket(socket.RemoteEndPoint.ToString()); int idInt = Convert.ToInt32(id); comando.jogadores[0].ID = this.ID; byte[] jogar = this.funcoes.ObjectToByteArray(comando); socket.Send(jogar); timer1.Enabled = true; }
public Itens_Compartilhados.RetornoJogada buscarBarralho() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "BUSCAR_BARALHO"; byte[] baralho = this.funcoes.ObjectToByteArray(comando); TrySend(baralho); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(dataReceived); return(retornoJogada); }
private void verificaStatus() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "STATUS_JOGADORES"; byte[] envioStatusJogador = this.funcoes.ObjectToByteArray(comando); socket.Send(envioStatusJogador); byte[] buff = new byte[32768]; int received = socket.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); refreshLista(retornoJogada.jogadores, retornoJogada.embaralhado); iniciaJogo(retornoJogada.jogadores); }
public Itens_Compartilhados.RetornoJogada verificaStatus(String atrib) { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "STATUS_JOGADORES_ATRIBUTO"; comando.atributo = atrib; comando.ID = this.ID; byte[] envioStatusJogador = this.funcoes.ObjectToByteArray(comando); socketClient.Send(envioStatusJogador); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); return(retornoJogada); }
public Itens_Compartilhados.RetornoJogada jogada(Itens_Compartilhados.Carta cartaEnv) { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "JOGADA"; comando.carta = cartaEnv; comando.ID = this.ID; byte[] jogada = this.funcoes.ObjectToByteArray(comando); TrySend(jogada); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); return(retornoJogada); }
private void testaServer(string faixaIP, int i) { string retorno = ""; string ip = faixaIP + i.ToString(); Socket socketClientLocal = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int tentativas = 0; while ((!socketClientLocal.Connected) && (tentativas < 2)) { try { socketClientLocal.Connect(IPAddress.Parse(ip), porta); byte[] buff = new byte[256]; int received = socketClientLocal.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); string text = Encoding.UTF8.GetString(dataReceived); } catch (SocketException) { tentativas++; } } if (socketClientLocal.Connected) { retorno = ip; Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "DESCONECTAR"; byte[] desconectar = this.funcoes.ObjectToByteArray(comando); socketClientLocal.Send(desconectar); } socketClientLocal.Close(); lock (this) { ipCount++; } this.ipArray[i] = retorno; }
public Boolean conectaServ(Boolean status, String ip) { try { if (status) { TryConnect(ip); if (socketClient.Connected) { CargaJogadores carregaJogadores = new CargaJogadores(socketClient, ID); carregaJogadores.ShowDialog(); if (carregaJogadores.DialogResult == DialogResult.OK) { this.embaralhar(); return(true); } else { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "DESCONECTAR"; byte[] desconectar = this.funcoes.ObjectToByteArray(comando); TrySend(desconectar); socketClient.Close(); socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (socketClient.Connected) { return(true); } else { return(false); } } } else { return(false); } } else { if (socketClient.Connected) { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "DESCONECTAR"; byte[] desconectar = this.funcoes.ObjectToByteArray(comando); TrySend(desconectar); socketClient.Close(); socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } if (socketClient.Connected) { return(true); } else { return(false); } } } catch (SocketException e) { Console.WriteLine(e.Message); return(false); } }
/// <summary>ReceiveCallBack método da classe Server /// Evento realizado para receber e enviar dados do cliente através de um IASyncResult /// </summary> private void ReceiveCallback(IAsyncResult asyncronousResult) { Socket current = (Socket)asyncronousResult.AsyncState; int received; try { received = current.EndReceive(asyncronousResult); } catch (SocketException) /*Catch realizado caso houver perca de conexão com o cliente*/ { Console.WriteLine("Conexão com o cliente " + current.RemoteEndPoint.ToString() + " perdida."); jogo.removeJogador(current.RemoteEndPoint.ToString()); current.Close(); ListaDeClientesSockets.Remove(current); return; } byte[] recBuf = new byte[received]; Array.Copy(_buffer, recBuf, received); Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando = (Itens_Compartilhados.Comandos)funcoes.ByteArrayToObject(recBuf); if (comando.comando == "JOGADA") { int primeiro; if (comando.ID == jogo.jogadorDaVez) { jogo.jogadorDaVezJogou = true; } funcoes.escreveInfoCarta(comando.carta); jogo.jogadoresEmEspera++; primeiro = jogo.jogadoresEmEspera; jogo.jogadaEmEspera[jogo.jogadoresEmEspera] = comando.carta; if (primeiro == 0) { while (jogo.qtdJogadores > (jogo.jogadoresEmEspera + 1)) { ; } Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = jogo.jogada(); byte[] envioRetornoJogada = this.funcoes.ObjectToByteArray(retornoJogada); Send(envioRetornoJogada); jogo.resetJogada(); } else { while (jogo.jogadoresEmEspera > -1) { ; } } } else if (comando.comando == "DESCONECTAR") { jogo.removeJogador(current.RemoteEndPoint.ToString()); current.Shutdown(SocketShutdown.Both); current.Close(); ListaDeClientesSockets.Remove(current); Console.WriteLine("Cliente Desconectado "); jogo.resetBarralho(); return; } else if (comando.comando == "STATUS_JOGADORES") { Itens_Compartilhados.RetornoJogada statusJogadores = new Itens_Compartilhados.RetornoJogada(); statusJogadores = jogo.statusJogadores(); statusJogadores.embaralhado = jogo.isEmbaralhado; byte[] envioStatusJogadores = this.funcoes.ObjectToByteArray(statusJogadores); current.Send(envioStatusJogadores); } else if (comando.comando == "STATUS_JOGADORES_ATRIBUTO") { Itens_Compartilhados.RetornoJogada statusJogadores = new Itens_Compartilhados.RetornoJogada(); statusJogadores = jogo.statusJogadores(); statusJogadores.jogadorDaVezJogou = jogo.jogadorDaVezJogou; if (comando.ID == jogo.jogadorDaVez) { jogo.atributoDaVez = comando.atributo; } byte[] envioStatusJogadores = this.funcoes.ObjectToByteArray(statusJogadores); current.Send(envioStatusJogadores); } else if (comando.comando == "EMBARALHAR") { int primeiro; jogo.jogadoresEmEspera++; primeiro = jogo.jogadoresEmEspera; if (primeiro == 0) { jogo.embaralhar(); while (!jogo.isEmbaralhado) { ; } } jogo.resetJogada(); } else if (comando.comando == "BUSCAR_BARALHO") { Itens_Compartilhados.RetornoJogada barralho = new Itens_Compartilhados.RetornoJogada(); barralho = jogo.embaralhado; byte[] envioBarralho = this.funcoes.ObjectToByteArray(barralho); current.Send(envioBarralho); } else if (comando.comando == "JOGAR") { jogo.setJogar(comando.jogadores[0]); } else if (comando.comando == "PERDER") { for (int i = 0; i < jogo.jogadores.Length; i++) { if (jogo.jogadores[i].ID == comando.ID) { jogo.jogadores[i].jogando = false; jogo.qtdJogadores--; if (jogo.jogadorDaVez == comando.ID) { jogo.reJogador(); } Thread.Sleep(550); } } } current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); }