Example #1
0
        private static void TCPReceiver()
        {
            using (NetworkStream nStream = tcp.GetStream())             // Get the stream
            {
                using (BinaryReader reader = new BinaryReader(nStream)) // Read it
                {
                    uiWriter = new BinaryWriter(nStream);               // Set the writer

                    while (true)                                        // Permanently try to read
                    {
                        string newData = reader.ReadString();           // Unbelievably messy code for receiving commands (somebody else can improve it :)) )

                        if (newData.Equals("SHOWSAND"))
                        {
                            instance.Invoke(() => { MessageBox.Show("You can now start the Sandbox", "Connected!"); });
                        }
                        else if (newData.StartsWith("SHOWMSG"))
                        {
                            instance.Invoke(() => { MessageBox.Show(newData.Split('|')[1], "Message from TABS"); });
                        }
                        else if (newData.StartsWith("INGAME"))
                        {
                            isIngame = bool.Parse(newData.Split('|')[1]); // Change the ingame status
                        }
                        else if (newData.StartsWith("DEBUG"))
                        {
                            Console.WriteLine(newData.Split('|')[1]); // Print out debug text
                        }
                        else if (newData.StartsWith("WINH"))
                        {
                            ScreenshotHandler.unityWindow = new IntPtr(long.Parse(newData.Split('|')[1])); // Set the handle
                            new Thread(() => ScreenshotHandler.FramingThread()).Start();                   // Start the framing thread
                        }
                        else if (newData.StartsWith("GSTARTED"))
                        {
                            bool started = bool.Parse(newData.Split('|')[1]); // Is game started?
                            if (!isIngame)
                            {
                                continue;            // Continue if the player's in the main menu
                            }
                            if (!isHost)
                            {
                                if (started)
                                {
                                    screenshareForm.ArrangeWindow();
                                    screenshareForm.Visible = true;
                                    WinAPIs.SetForegroundWindow(screenshareForm.Handle);
                                    ScreenshotHandler.WriteToUdp(StrToByte("HELLO"));
                                    // Arrange and make the screenshare window ready for receiving
                                }
                                else
                                {
                                    screenshareForm.Visible = false;
                                    WinAPIs.SetForegroundWindow(ScreenshotHandler.unityWindow); // Hide the screenshare form
                                    // and set the game as the active one again
                                }
                            }
                            ScreenshotHandler.streaming = started;
                        }
                    }
                }
            }
        }