Ejemplo n.º 1
0
        public void updateUI()
        {
            IPandPORT IPandPort = JsonConvert.DeserializeObject <IPandPORT>(Read.getIPandPort().ToString());

            IP.Text   = IPandPort.IP;
            PORT.Text = IPandPort.PORT.ToString();
        }
Ejemplo n.º 2
0
        /************************
         * START SERVICES
         */
        public void StartServer()
        {
            IPandPORT IPandPort = JsonConvert.DeserializeObject <IPandPORT>(Read.getIPandPort().ToString());

            try
            {
                server = new TcpListener(IPAddress.Parse(IPandPort.IP), IPandPort.PORT);

                // Start listening for client requests.
                server.Start();

                /*****************************************************
                 * LOOP PARA CRIAR CONEXÕES NODEJS E BROWSER
                 */
                while (true)
                {
                    Console.WriteLine("AGUARDANDO SOCKET...\n");

                    client = server.AcceptTcpClient();
                    stream = client.GetStream();

                    while (!stream.DataAvailable)
                    {
                        ;
                    }

                    bytes = new Byte[client.Available];
                    stream.Read(bytes, 0, bytes.Length);
                    data = Encoding.UTF8.GetString(bytes);
                    Console.WriteLine(data);
                    if ((new Regex("^GET").IsMatch(data)))
                    {
                        string statusResponse;
                        Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
                                                                 + "Connection: Upgrade" + Environment.NewLine
                                                                 + "Upgrade: websocket" + Environment.NewLine
                                                                 /* + "Sec-WebSocket-Protocol: websession" + Environment.NewLine*/
                                                                 + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                                                                     SHA1.Create().ComputeHash(
                                                                         Encoding.UTF8.GetBytes(
                                                                             new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                                                                             )
                                                                         )
                                                                     ) + Environment.NewLine
                                                                 + Environment.NewLine);

                        stream.Write(response, 0, response.Length);
                        statusResponse = System.Text.Encoding.ASCII.GetString(response, 0, response.Length);

                        Console.WriteLine("[WS]: Conected");

                        //LEVEL'S
                        if (new Regex("level1").IsMatch(data)) //LEVEL 1
                        {
                            //BROWSER1 CONNECTION +++
                            if (conBROW1Thread.IsAlive)
                            {
                                streamBROW1.Dispose();
                                streamBROW1 = stream;
                                stream      = null;

                                Console.WriteLine("conBROW1Thread Number: " + conBROW1Thread.GetHashCode().ToString());
                                conBROW1canWrite      = true;
                                MainWindow.flagUserId = "modifyed";
                                Console.WriteLine("flagUserId = modifyed");
                            }
                            else
                            {
                                conBROW1Thread.Start();
                                Console.WriteLine("conBROW1Thread Number: " + conBROW1Thread.GetHashCode().ToString() + "\n");
                                conBROW1canWrite      = true;
                                MainWindow.flagUserId = "modifyed";
                                Console.WriteLine("flagUserId = modifyed");
                            }
                            //BROWSER1 CONNECTION ---
                        }
                        else
                        {
                            if (new Regex("level2").IsMatch(data)) //LEVEL 2
                            {
                                //BROWSER2 CONNECTION +++
                                if (conBROW2Thread.IsAlive)
                                {
                                    streamBROW2.Dispose();
                                    streamBROW2 = stream;
                                    stream      = null;
                                    Console.WriteLine("conBROW2Thread Number: " + conBROW2Thread.GetHashCode().ToString());
                                    conBROW2canWrite      = true;
                                    MainWindow.flagUserId = "modifyed";
                                    Console.WriteLine("flagUserId = modifyed");
                                }
                                else
                                {
                                    conBROW2Thread.Start();
                                    Console.WriteLine("conBROW2Thread Number: " + conBROW2Thread.GetHashCode().ToString());
                                    conBROW2canWrite      = true;
                                    MainWindow.flagUserId = "modifyed";
                                    Console.WriteLine("flagUserId = modifyed");
                                }
                                //BROWSER2 CONNECTION ---
                            }
                            else //level3
                            {
                                //BROWSER3 CONNECTION +++
                                if (conBROW3Thread.IsAlive)
                                {
                                    streamBROW3.Dispose();
                                    streamBROW3 = stream;
                                    stream      = null;
                                    Console.WriteLine("conBROW3Thread Number: " + conBROW3Thread.GetHashCode().ToString());
                                    conBROW3canWrite      = true;
                                    MainWindow.flagUserId = "modifyed";
                                    Console.WriteLine("flagUserId = modifyed");
                                }
                                else
                                {
                                    conBROW3Thread.Start();
                                    Console.WriteLine("conBROW3Thread Number: " + conBROW3Thread.GetHashCode().ToString());
                                    conBROW3canWrite      = true;
                                    MainWindow.flagUserId = "modifyed";
                                    Console.WriteLine("flagUserId = modifyed");
                                }
                                //BROWSER2 CONNECTION ---
                            }
                        }
                    }
                    else //net.socket != ^GET
                    {
                        if (conNJSThread.IsAlive)
                        {
                            streamNJS.Dispose();
                            streamNJS = stream;
                            stream    = null;
                            Console.WriteLine("conNJSThread Number: " + conNJSThread.GetHashCode().ToString());
                            conNJScanWrite = true;
                        }
                        else
                        {
                            conNJSThread.Start();
                            Console.WriteLine("conNJSThread Number: " + conNJSThread.GetHashCode().ToString());
                            conNJScanWrite = true;
                        }
                    }//net.socket
                }
            }
            catch// (System.IO.IOException e)
            {
                //Console.WriteLine("System.IO.IOException: {0}", e);
            }

            /* finally
             * {
             *   //server.Stop();
             * }*/

            // informa e pausa o console para dar tempo de ler a exceção
            Console.WriteLine("\nÉ necessário configurar o IP e a PORTA corretamente\nPressione enter para continuar...");
            MessageBox.Show("É necessário configurar o IP e a PORTA corretamente");
            Console.Read();
        }