private void hostBtn_Click(object sender, EventArgs e) // Host Button { if (isIngame) { MessageBox.Show("Please stay in the main menu before starting a session!", "Error"); // Prevent the user from hosting/connecting while being ingame return; } WriteToTABS("HOSTNOW"); // Send the host command button1.Enabled = false; // Disable the host btn button2.Enabled = false; // Disable the connection btn isHost = true; new Thread(() => ScreenshotHandler.UdpThread()).Start(); // Start the udp listener }
private void connectBtn_Click(object sender, EventArgs e) // Connect Button { if (isIngame) { MessageBox.Show("Please stay in/enter the main menu before starting a session!", "Error"); return; } try { WriteToTABS("CONNECT|" + IPAddress.Parse(textBox1.Text).ToString()); // Send connect cmd + ip with parse checking button1.Enabled = false; // Disable the host btn button2.Enabled = false; // Disable the connection btn screenPartner = new IPEndPoint(IPAddress.Parse(textBox1.Text), 8042); // Connect to the other screenClient screenClient.Connect(screenPartner); new Thread(() => ScreenshotHandler.UdpThread()).Start(); // Start the udp listener ScreenshotHandler.WriteToUdp(StrToByte("HELLO")); // Send hello } catch (Exception) { MessageBox.Show("Invalid IP!", "Error"); } }
private void imageUpdater_Tick(object sender, EventArgs e) { if (!initImg && ScreenshotHandler.winSize.Width > 0) { initImg = true; pictureBox1.Image = new Bitmap(this.Width, this.Height); } if (ScreenshotHandler.winSize.Width > 0) { screenPercentageX = (float)this.Width / (float)ScreenshotHandler.winSize.Width; screenPercentageY = (float)this.Height / (float)ScreenshotHandler.winSize.Height; // Get our screen percentage } byte[] data; while (ScreenshotHandler.datas.TryTake(out data)) { string strData = MainUI.ByteToStr(data); if (strData.StartsWith("IMG")) // If it's an image { strData = strData.Split(new string[] { "|$|" }, StringSplitOptions.None)[0]; // Only get our header if (ScreenshotHandler.winSize.Width == 0) { string[] sizeData = strData.Split('|')[2].Split(','); ScreenshotHandler.winSize = new Size(int.Parse(sizeData[0]), int.Parse(sizeData[1])); } /*if(totalImage == null) * { * string[] sizeData = strData.Split('|')[2].Split(','); * totalImage = new Bitmap(int.Parse(sizeData[0]), int.Parse(sizeData[1])); // Make a total image if it's missing * }*/ string[] pointData = strData.Replace("{X=", "").Replace("}", "").Replace("Y=", "").Split('|')[1].Split(','); byte[] imgData = ScreenshotHandler.Decompress(ScreenshotHandler.GetImageData(data)); // Get the image bytes Point loc = new Point(int.Parse(pointData[0]), int.Parse(pointData[1])); // Get the box location MemoryStream imgMem = new MemoryStream(imgData); Image boxImg = Image.FromStream(imgMem); // Get image from bytes Bitmap boxBit = new Bitmap(boxImg); if (!ScreenshotHandler.imageBlocks.ContainsKey(loc)) { ScreenshotHandler.imageBlocks.Add(loc, boxBit); } else { ScreenshotHandler.imageBlocks[loc] = boxBit; } updated.Add(loc); pictureBox1.Invalidate(new Rectangle((int)((float)loc.X * screenPercentageX - 1), (int)((float)loc.Y * screenPercentageY), (int)((float)boxBit.Width * screenPercentageX + 1), (int)((float)boxBit.Height * screenPercentageY))); // Refresh the picturebox //pictureBox1.Update(); /*MainUI.screenshareForm.Invoke(() => { * /*int dW = (loc.X + boxImg.Width + 1) - totalImage.Width; * int dH = (loc.Y + boxImg.Height + 1) - totalImage.Height; * * if (dW > 0) // Crop width if it's too big * { * Bitmap toCrop = new Bitmap(boxImg); * boxImg = toCrop.Clone(new Rectangle(0, 0, toCrop.Width - dW, toCrop.Height), toCrop.PixelFormat); * toCrop.Dispose(); * } * if (dH > 0) // Crop height if it's too big * { * Bitmap toCrop = new Bitmap(boxImg); * boxImg = toCrop.Clone(new Rectangle(0, 0, toCrop.Width, toCrop.Height - dH), toCrop.PixelFormat); * toCrop.Dispose(); * } // Old client-side cropping (useless) * * using (Graphics g = Graphics.FromImage(totalImage)) // Draw the image on the totalImage * { * g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; * g.DrawImage(boxImg, loc); * } * boxImg.Dispose(); * });*/ //imgMem.Dispose(); // Clear memory } } }
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; } } } } }