Beispiel #1
0
 private void bt_posicao6_Click(object sender, EventArgs e)
 {
     painel_baliza.Enabled = false;
     byte[] msg = Encoding.UTF8.GetBytes("6");
     Form_Autenticar.EnviarDadosServidor(ProtocolSICmdType.USER_OPTION_7, msg);
     painel_esperar.Visible = true;
 }
Beispiel #2
0
        private void buttonEnviarMensagem_Click(object sender, EventArgs e)
        {
            byte[] msg = Encoding.UTF8.GetBytes(tb_enviarMensagem.Text);

            //para nao enviar mensagens sem nada
            if (msg.Length == 0)
            {
                return;
            }

            // LIMPA A CAIXA DE TEXTO
            tb_enviarMensagem.Text = "";


            //verificaçoes
            if (Form_Autenticar.EnviarDadosServidor(ProtocolSICmdType.DATA, msg) == 0)
            {
                MessageBox.Show("Comunicação falhou");
                return;
            }
            if (!Form_Autenticar.LerRespostaServidor())
            {
                MessageBox.Show("Integridade inválida");
                return;
            }
        }
Beispiel #3
0
        //Verificações
        private void verificarMensagensNovas()
        {
            //objetivo: efetuar o pedido para o servidor dizer se tem novas msgs ou nao

            try
            {
                if (Form_Autenticar.networkStream.CanWrite)
                {
                    // CRIA A MENSAGEM DO TIPO DATA UTILIZANDO O PROTOCOLO SI
                    byte[] packet = Form_Autenticar.protocolSI.Make(ProtocolSICmdType.USER_OPTION_6);
                    Form_Autenticar.networkStream.Write(packet, 0, packet.Length);

                    // VERIFICA O TIPO DE MENSAGEM (DO SERVIDOR)
                    if (Form_Autenticar.LerRespostaServidor())
                    {
                        // SE FOR DO TIPO NACK é porque nao é preciso atualizar
                        // ACK é preciso, existem novas msgs
                        pedirDados();
                    }
                }
            }
            catch (SocketException)
            {
                MessageBox.Show("Não foi possivel conectar ao servidor");
                backFormEntrada();
                return;
            }
        }
Beispiel #4
0
        static public string receberDadosServidor()
        {
            Form_Autenticar.networkStream.Read(Form_Autenticar.protocolSI.Buffer, 0, Form_Autenticar.protocolSI.Buffer.Length);
            byte[] fun = Form_Autenticar.decifrarDadosServidor();

            return(Form_Autenticar.emString(fun));
        }
Beispiel #5
0
        private void bt_login_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] msg = Encoding.UTF8.GetBytes(tb_username.Text + "_Ø_" + tb_password.Text);


                //verificar se a resposta do servidor é OK
                if (Form_Autenticar.EnviarDadosServidor(ProtocolSICmdType.USER_OPTION_2, msg) == 1 && Form_Autenticar.LerRespostaServidor())
                {
                    Form_Autenticar.nickname = tb_username.Text;

                    Form_Jogo form = new Form_Jogo();
                    form.ShowDialog();

                    /*FormPrincipal form = new FormPrincipal(TextBoxUsername);
                     * form.ShowDialog();*/
                }
                else     // ou se a resposta é NOK
                {
                    lb_validar.Visible = true;
                }
            }
            catch (SocketException)
            {
                MessageBox.Show("Não foi possivel conectar ao servidor tente outra vez");
                return;
            }
        }
Beispiel #6
0
        private void backFormEntrada()
        {
            //objetivo: voltar ao formLogin
            this.Close();
            Form_Autenticar form = new Form_Autenticar();

            form.Show();
        }
Beispiel #7
0
        private void verificarJogadas()
        {
            if (Form_Autenticar.networkStream.CanWrite)
            {
                byte[] packet = Form_Autenticar.protocolSI.Make(ProtocolSICmdType.USER_OPTION_8);
                Form_Autenticar.networkStream.Write(packet, 0, packet.Length);
                if (Form_Autenticar.LerRespostaServidor())
                {
                    string estado = receberDadosServidor();
                    QJogaram++;
                    painel_baliza.Enabled = true;
                    painel_esperar.Hide();

                    lb_estado.Text = estado;
                    atualizarPontuacao(estado);
                    string Funcao = trocarFuncao(funAtual);
                    layoutFuncao(Funcao);


                    if (QJogaram == 5 || pontosMeus >= 3 || pontosAdversario >= 3)
                    {
                        painel_baliza.Enabled = false;
                        bt_novoJogo.Visible   = true;

                        if (pontosMeus > pontosAdversario)
                        {
                            painel_estado.Visible  = true;
                            painel_estado.Location = new Point(121, 120);
                            painel_estado.BringToFront();
                            lb_tipo.Text = "Parabéns ganhou";
                        }
                        else
                        {
                            painel_estado.Visible  = true;
                            painel_estado.Location = new Point(121, 120);
                            painel_estado.BringToFront();
                            lb_tipo.Text = "Você perdeu";
                        }
                    }
                }
            }
        }
Beispiel #8
0
        private void btn_sala_Click(object sender, EventArgs e)
        {
            string nomeSala = tb_sala.Text;

            byte[] msg = Encoding.UTF8.GetBytes(nomeSala);

            if (Form_Autenticar.EnviarDadosServidor(ProtocolSICmdType.USER_OPTION_4, msg) == 1 && Form_Autenticar.LerRespostaServidor())
            {
                tb_sala.ResetText();
                bt_enviarMensagem.Enabled = true;
                painel_baliza.Enabled     = true;
                labelEscolha.Visible      = false;
                labelResult.Visible       = true;
                labelResultado.Visible    = true;

                tb_sala.Visible = false;
                bt_sala.Visible = false;

                string funcao = receberDadosServidor();
                layoutFuncao(funcao);
                funAtual = funcao;

                // saber se é o unico jogador para
                //# painel_esperar.Visible = true;
                painel_esperar.BringToFront();

                pedirDados();

                //iniciar o contador para verificar se ha mensagens novas de 2 em 2 segundos
                InitTimer();

                // colocar automaticamente o cursor na caixa e mostra o form
                ActiveControl = tb_enviarMensagem;
                this.Show();

                lb_nomesalas.Text = nomeSala;
            }
            else // voltar ao form de entrada
            {
                MessageBox.Show("A sala não está disponível");
            }
        }
Beispiel #9
0
        private void pedirDados()
        {
            //objetivo: efetuar o pedido de dados do cliente ao servidor

            // VARIAVEL PARA RECEBER OS DADOS
            StringBuilder textAux = new StringBuilder();

            //para saber quanto temos na text box atualmente
            byte[] lengthEmBytes = Encoding.UTF8.GetBytes(tb_sala.TextLength.ToString());

            //verificaçoes
            if (Form_Autenticar.EnviarDadosServidor(ProtocolSICmdType.USER_OPTION_5, lengthEmBytes) == 0)
            {
                MessageBox.Show("Comunicação falhou");
                return;
            }
            if (!Form_Autenticar.LerRespostaServidor())
            {
                MessageBox.Show("Integridade inválida");
                return;
            }

            //  ENQUANTO  HOUVER  COISAS  PARA  RECEBER  (OS  DADOS  PODEM  TER  SIDO  DIVIDIDOS  PARA  SEREM  ENVIADOS)
            while (true)
            {
                try
                {
                    //le da stream
                    Form_Autenticar.networkStream.Read(Form_Autenticar.protocolSI.Buffer, 0, Form_Autenticar.protocolSI.Buffer.Length);

                    // SE FOR O FIM DA RESPOSTA
                    if (Form_Autenticar.protocolSI.GetCmdType() == ProtocolSICmdType.EOF)
                    {
                        // SAI FORA DO WHILE
                        break;
                    }
                    // SENÃO, E se a comunicação for do tipo data
                    else if (Form_Autenticar.protocolSI.GetCmdType() == ProtocolSICmdType.DATA)
                    {
                        // RECEBE, DECIFRA, ESCREVE OS DADOS PARA A STRING
                        textAux.Append(Convert.ToBase64String(Form_Autenticar.decifrarDadosServidor()));
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Erro ao atualizar conversas");
                    break;
                }
            }

            Form_Autenticar.Enviar_ACK();

            byte[] dadosDecifrados = Convert.FromBase64String(textAux.ToString());

            //receber hash, decifrar e enviar ack
            byte[] hash_recebida = Form_Autenticar.Receber_Hash();

            if (Form_Autenticar.Integridade_Valida(hash_recebida, dadosDecifrados) == false)
            {
                return;
            }


            // ATUALIZA O TEXTO DA TEXTBOX
            tb_sala.AppendText(Encoding.UTF8.GetString(dadosDecifrados));
        }