예제 #1
0
        public void Loop()
        {
            Configure();
            transmitter = new ScreenTransmitter(RemoteIPAddress);

            SendPacket(new LoginPacket()
            {
                Name    = Name,
                Id      = Id,
                Mode    = LoginMode.Client,
                Version = PacketBase.ProtocolVersion
            }).Flush();

            var pkt = new DisplayInfoPacket();

            for (int d = 0; d < transmitter.GetDisplaysCount(); d++)
            {
                var res = SnvBackend.ScrGetResolution(d);
                if (res == 0)
                {
                    goto Halt;
                }

                pkt.Displays.Add(new DisplaySize((ushort)(res >> 16), (ushort)res));
            }

            SendPacket(pkt).Flush();

            while (IsRunning)
            {
                Thread.Sleep(delay);
                if (!transmitter.Send())
                {
                    break;
                }
            }

Halt:
            transmitter.Close();
            Shutdown();
        }
예제 #2
0
        protected override bool HandlePacket(string packet)
        {
            var pongPkt = new PongPacket();

            if (pongPkt.ParsePacket(packet))
            {
                IsAnyPongsAccepted = true;
                return(true);
            }

            var loginPkt = new LoginPacket();

            if (loginPkt.ParsePacket(packet))
            {
                if (loginPkt.Version != PacketBase.ProtocolVersion)
                {
                    SendPacket(new ErrorPacket()
                    {
                        Text = "Unsupported protocol"
                    });
                    return(false);
                }

                if (loginPkt.Id == null && Id == null)
                {
                    SendPacket(new ErrorPacket()
                    {
                        Text = "Please provide me with ID"
                    });
                    return(true);
                }

                if (loginPkt.Id != null)
                {
                    foreach (var cl in server.Clients)
                    {
                        if (cl == this || cl.Id == null)
                        {
                            continue;
                        }

                        /* ID check disabled
                         * if (cl.Id == loginPkt.Id)
                         * {
                         *  SendPacket(new ErrorPacket()
                         *  {
                         *      Text = "ID is already registered"
                         *  });
                         *  return true;
                         * }
                         */
                    }

                    id = (Guid)loginPkt.Id;
                    IdChanged?.Invoke(this, new ValueCarrierEventArgs <Guid?>(id));
                }

                name = loginPkt.Name;
                mode = loginPkt.Mode;
                SendPacket(new AckPacket());

                var ln = (name == null ? "" : name + " ") + "(" + (id == null ? "?" : id.ToString().Substring(0, 8)) + ")";
                server.PrintLog(logName + " has registered as " + ln);
                logName = ln;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
                NameChanged?.Invoke(this, new ValueCarrierEventArgs <string>(name));
                return(true);
            }

            if (mode == LoginMode.Unspecified)
            {
                SendPacket(new ErrorPacket()
                {
                    Text = "Not logged in"
                });
                return(true);
            }

            var displayPkt = new DisplayInfoPacket();

            if (displayPkt.ParsePacket(packet))
            {
                PurgeDisplays();
                var port  = startingPort - 1;
                var ports = new List <ushort>();
                for (int d = 0; d < displayPkt.Displays.Count; d++)
                {
                    port++;
                    if (port > portMax)
                    {
                        throw new Exception("No more network ports available in range");
                    }

                    if (!IsTcpPortAvailable((ushort)port))
                    {
                        d--;
                        continue;
                    }

                    var ds = displayPkt.Displays[d];
                    Displays.Add(new Display((ushort)port, this, ds.Width, ds.Height));
                    ports.Add((ushort)port);
                }

                SendPacket(new DisplayPortsPacket(ports)).Flush();
                return(true);
            }

            var pingPkt = new PingPacket();

            if (pingPkt.ParsePacket(packet))
            {
                SendPacket(new PongPacket()
                {
                    Text = pingPkt.Text
                });
                return(true);
            }

            return(true);
        }