示例#1
0
        private static void ClientConnect()
        {
            if (!int.TryParse(MainWindow.Model.Port, out var port))
            {
                return;
            }
            var tcpClient = new TcpClient(MainWindow.Model.IpAddress, port);

            var stream = tcpClient.GetStream();

            MainWindow.Log($"Connected to {tcpClient.Client.RemoteEndPoint} as Client!", false);

            var saltPacket = HarmonyPacket.ReadPacket(stream);

            if (saltPacket.Type != HarmonyPacket.PacketType.SaltPacket)
            {
                MainWindow.Log($"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.SaltPacket}; Actual: {saltPacket.Type}", true);
                tcpClient.Close();
                return;
            }

            _salt = saltPacket.Pack;
            Crypto.Init(MainWindow.Password, _salt);
            MainWindow.Log("Successfully obtained Salt-Packet!", false);

            if (!_connections.TryAdd(tcpClient.Client.RemoteEndPoint, tcpClient))
            {
                return;
            }
            var sendthread = new Thread(ClientSend);

            _threads.Add(sendthread);
            sendthread.Start(tcpClient.Client.RemoteEndPoint);

            {
                var displayPacket = HarmonyPacket.Encode(new HarmonyPacket
                {
                    Type = HarmonyPacket.PacketType.DisplayPacket,
                    Pack = new HarmonyPacket.DisplayPacket
                    {
                        screens = DisplayManager.Displays
                    }
                });
                stream.Write(displayPacket, 0, displayPacket.Length);
                stream.Flush();
                MainWindow.Log("Send Display-Packet!", false);
            }

            {
                var displayPacket = HarmonyPacket.ReadPacket(stream);
                if (displayPacket.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    MainWindow.Log($"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.DisplayPacket}; Actual: {displayPacket.Type}", true);
                    stream.Close();
                    tcpClient.Close();
                    return;
                }
                DisplayManager.SetUp(((HarmonyPacket.DisplayPacket)displayPacket.Pack).screens);
                MainWindow.Log("Finished Handshake with Server!", false);
            }


            while (stream.CanRead)
            {
                var packet = HarmonyPacket.ReadPacket(stream);

                switch (packet.Type)
                {
                case HarmonyPacket.PacketType.MousePacket:
                    Mouse.SendInput(packet.Pack);
                    break;

                case HarmonyPacket.PacketType.KeyBoardPacket:
                    var kp = (HarmonyPacket.KeyboardPacket)packet.Pack;
                    Keyboard.SendInput(kp);
                    break;

                case HarmonyPacket.PacketType.DisplayPacket:
                    var dp = ((JObject)packet.Pack).ToObject <HarmonyPacket.DisplayPacket>();
                    DisplayManager.SetUp(dp.screens);
                    break;

                case HarmonyPacket.PacketType.MouseMovePacket:
                    var mmp = (HarmonyPacket.MouseMovePacket)packet.Pack;
                    NativeMethods.SetCursorPos(mmp.PosX - DisplayManager.SlaveMain.Location.X, mmp.PosY - DisplayManager.SlaveMain.Location.Y);
                    break;

                case HarmonyPacket.PacketType.SaltPacket:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
示例#2
0
        private void _updateDisplayCanvas()
        {
            if (!Model.IsServer)
            {
                return;
            }

            const double shrink = 10.0;

            DisplayCanvas.Children.Clear();

            var displays = DisplayManager.Displays;

            var main = DisplayManager.GetDisplayFromPoint(0, 0);

            foreach (var dis in displays)
            {
                var canv = new Canvas()
                {
                    Width      = dis.Screen.Width / shrink,
                    Height     = dis.Screen.Height / shrink,
                    MaxWidth   = dis.Screen.Width / shrink,
                    MaxHeight  = dis.Screen.Height / shrink,
                    Background = dis.OwnDisplay ? new SolidColorBrush(Color.FromRgb(0, 0, 150)) : new SolidColorBrush(Color.FromRgb(0, 100, 0)),
                    Opacity    = 100
                };

                var disNo = new TextBlock()
                {
                    Width         = canv.Width,
                    Height        = 25,
                    Text          = displays.IndexOf(dis).ToString(),
                    FontSize      = 14,
                    TextAlignment = TextAlignment.Center
                };

                var disSize = new TextBlock()
                {
                    Width         = canv.Width,
                    Height        = 25,
                    Text          = $"{dis.Screen.Width} x {dis.Screen.Height}",
                    FontSize      = 14,
                    TextAlignment = TextAlignment.Center
                };

                Canvas.SetLeft(disNo, canv.Width / 2 - disNo.Width / 2);
                Canvas.SetTop(disNo, canv.Height / 2 - 10 - disNo.Height / 2);
                Canvas.SetLeft(disSize, canv.Width / 2 - disSize.Width / 2);
                Canvas.SetTop(disSize, canv.Height / 2 + 10 - disSize.Height / 2);

                canv.Children.Add(disNo);
                canv.Children.Add(disSize);

                Canvas.SetLeft(canv, dis.Location.X / shrink - (main.Screen.Width / shrink) / 2);
                Canvas.SetTop(canv, dis.Location.Y / shrink - (main.Screen.Height / shrink) / 2);

                if (Model.IsServer && !dis.OwnDisplay)
                {
                    canv.MouseLeftButtonDown += (s, eArgs) => {
                        MoveAllScreens((Canvas)s, eArgs.GetPosition(DisplayCanvas), false);
                    };
                    canv.MouseLeftButtonUp += (s, eArgs) => {
                        var  c = (Canvas)s;
                        bool changed;
                        do
                        {
                            changed = false;
                            foreach (var obj in DisplayCanvas.Children)
                            {
                                if (!(obj is Canvas))
                                {
                                    continue;
                                }
                                var o = (Canvas)obj;
                                if (o.Background.Equals(c.Background))
                                {
                                    continue;
                                }

                                var xOverlap = Math.Round(Math.Max(0, Math.Min(Canvas.GetLeft(c) + c.Width, Canvas.GetLeft(o) + o.Width) - Math.Max(Canvas.GetLeft(c), Canvas.GetLeft(o))));
                                var yOverlap = Math.Round(Math.Max(0, Math.Min(Canvas.GetTop(c) + c.Height, Canvas.GetTop(o) + o.Height) - Math.Max(Canvas.GetTop(c), Canvas.GetTop(o))));

                                if (xOverlap == 0 || yOverlap == 0)
                                {
                                    continue;
                                }
                                changed = true;

                                MoveAllScreens(c,
                                               xOverlap < yOverlap ? new Point(-xOverlap, 0) : new Point(0, -yOverlap), true);
                                //break;
                            }
                            System.Windows.Forms.Application.DoEvents();
                        } while (changed);
                    };
                    canv.MouseMove += (s, eArgs) => {
                        var c = (Canvas)s;
                        if (eArgs.LeftButton != System.Windows.Input.MouseButtonState.Pressed)
                        {
                            return;
                        }
                        MoveAllScreens(c, Point.Add(eArgs.GetPosition(DisplayCanvas), new Vector(-c.Width / 2, -c.Height / 2)), false);
                    };
                }
                DisplayCanvas.Children.Add(canv);
            }
        }
示例#3
0
        private static void ServerClientHandler(object obj)
        {
            if (!_connections.TryGetValue((EndPoint)obj, out var client))
            {
                return;
            }
            MainWindow.Log($"Connected to {obj} as Server!", false);
            var stream = client.GetStream();
            {
                var saltPacket = HarmonyPacket.Encode(new HarmonyPacket()
                {
                    Type = HarmonyPacket.PacketType.SaltPacket,
                    Pack = _salt
                });

                stream.Write(saltPacket, 0, saltPacket.Length);
                stream.Flush();
                MainWindow.Log("Send Salt-Packet!", false);
            }

            var keyMap = new Dictionary <Keys, bool>();

            foreach (Keys key in Enum.GetValues(typeof(Keys)))
            {
                keyMap[key] = false;
            }

            {
                var displayPacket = HarmonyPacket.ReadPacket(stream);
                if (displayPacket.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    MainWindow.Log(
                        $"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.SaltPacket}; Actual: {displayPacket.Type}",
                        true);
                    client.Close();
                    _ = _connections.TryRemove((EndPoint)obj, out _);
                    return;
                }

                foreach (var d in ((HarmonyPacket.DisplayPacket)displayPacket.Pack).screens)
                {
                    if (d.Screen.Location.X == 0 && d.Screen.Location.Y == 0)
                    {
                        DisplayManager.SlaveMain = d;
                    }
                    d.OwnDisplay = false;
                    DisplayManager.AddRight(d);
                }
            }

            {
                var returnPacket = HarmonyPacket.Encode(new HarmonyPacket
                {
                    Type = HarmonyPacket.PacketType.DisplayPacket,
                    Pack = new HarmonyPacket.DisplayPacket
                    {
                        screens = DisplayManager.Displays
                    }
                });
                stream.Write(returnPacket, 0, returnPacket.Length);
                stream.Flush();
                MainWindow.Log($"Finished Handshake with Client {obj}!", false);
            }

            var receive = new Thread(ServerReceive);

            receive.Start(obj);
            _threads.Add(receive);

            var mouseX = 0;
            var mouseY = 0;

            while (stream.CanWrite)
            {
                var hp = _toSend.Take();
                if (hp.Type == HarmonyPacket.PacketType.MouseMovePacket)
                {
                    var mp = (HarmonyPacket.MouseMovePacket)hp.Pack;

                    if (OnSlave == 0)
                    {
                        var onScreen = DisplayManager.GetDisplayFromPoint(mp.PosX, mp.PosY);
                        if (onScreen == null)
                        {
                            continue;
                        }
                        mouseX = mp.PosX;
                        mouseY = mp.PosY;
                        if (onScreen.OwnDisplay)
                        {
                            continue;
                        }

                        OnSlave = 1;
                    }
                    else
                    {
                        var pos = NativeMethods.GetCursorPosition();
                        mouseX += mp.PosX - pos.X;
                        mouseY += mp.PosY - pos.Y;
                        var onScreen = DisplayManager.GetDisplayFromPoint(mouseX, mouseY);
                        if (onScreen == null)
                        {
                            mouseX -= mp.PosX - pos.X;
                            mouseY -= mp.PosY - pos.Y;
                            continue;
                        }

                        if (onScreen.OwnDisplay)
                        {
                            NativeMethods.SetCursorPos(mouseX, mouseY);
                            OnSlave = 0;
                        }

                        mp.PosX = mouseX;
                        mp.PosY = mouseY;
                    }
                }

                if (OnSlave == 0 && hp.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    continue;
                }
                var packet = HarmonyPacket.Encode(hp);
                stream.Write(packet, 0, packet.Length);
                stream.Flush();
            }
        }