Exemplo n.º 1
0
        private void LoadFavourites()
        {
            List <byte> list = new List <byte>();

            try { list.AddRange(File.ReadAllBytes(Settings.DataPath + "favourites.dat")); }
            catch { }

            if (list.Count > 0)
            {
                UdpPacketReader buf = new UdpPacketReader(list.ToArray());
                list.Clear();
                list = null;

                while (buf.Remaining() > 0)
                {
                    FavouritesListItem item = new FavouritesListItem();
                    item.AutoJoin = buf.ReadByte() == 1;
                    item.IP       = buf.ReadIP();
                    item.Port     = buf.ReadUInt16();
                    item.Name     = buf.ReadString();
                    item.Topic    = buf.ReadString();
                    item.Password = buf.ReadString();
                    this.favs.Add(item);
                    ChannelListViewItem vitem = new ChannelListViewItem(null, 0);
                    this.gfx.RenderChannelListItem(vitem, item);
                    this.g_favs.Add(vitem);
                    this.channelListView2.Items.Add(vitem);
                }

                buf = null;
            }
        }
Exemplo n.º 2
0
        private void LoadCache()
        {
            List <byte> list = new List <byte>();

            try { list.AddRange(File.ReadAllBytes(Settings.DataPath + "cache.dat")); }
            catch { }

            if (list.Count > 0)
            {
                UdpPacketReader buf = new UdpPacketReader(list.ToArray());
                list.Clear();
                list = null;

                while (buf.Remaining() > 0)
                {
                    ChannelListItem item = new ChannelListItem();
                    item.IP   = buf.ReadIP();
                    item.Port = buf.ReadUInt16();
                    item.Lang = (RoomLanguage)buf.ReadByte();
                    item.Name = buf.ReadString();
                    StringBuilder sb = new StringBuilder();
                    int           i;

                    foreach (char c in item.Name.ToUpper().ToCharArray())
                    {
                        i = (int)c;

                        if ((i >= 65 && i <= 90) || (i >= 48 && i <= 57))
                        {
                            sb.Append(c);
                        }
                    }

                    item.StrippedName  = sb.ToString();
                    item.Topic         = buf.ReadString();
                    item.StrippedTopic = Helpers.StripColors(Helpers.FormatAresColorCodes(item.Topic)).ToUpper();
                    item.Users         = buf.ReadUInt16();
                    full_channel_list.Add(item);
                    ChannelListViewItem vitem = new ChannelListViewItem(item.StrippedName, item.Users);
                    this.gfx.RenderChannelListItem(vitem, item);
                    this.gfx_items.Add(vitem);
                }

                buf = null;
                this.FilterResults();
            }
        }
Exemplo n.º 3
0
        public ChannelListItem(IPEndPoint ep, UdpPacketReader packet)
        {
            this.IP    = ep.Address;
            this.Port  = packet.ReadUInt16();
            this.Users = packet.ReadUInt16();
            this.Name  = packet.ReadString();
            StringBuilder sb = new StringBuilder();
            int           i2;

            foreach (char c in this.Name.ToUpper().ToCharArray())
            {
                i2 = (int)c;

                if ((i2 >= 65 && i2 <= 90) || (i2 >= 48 && i2 <= 57))
                {
                    sb.Append(c);
                }
            }

            this.StrippedName  = sb.ToString();
            this.Topic         = packet.ReadString();
            this.StrippedTopic = Helpers.StripColors(Helpers.FormatAresColorCodes(this.Topic)).ToUpper();
            this.Lang          = (RoomLanguage)packet.ReadByte();
            packet.ReadString();
            byte count = packet.ReadByte();

            if (count > 0)
            {
                List <IPEndPoint> servers = new List <IPEndPoint>();

                for (int i = 0; i < count; i++)
                {
                    IPAddress ip   = packet.ReadIP();
                    ushort    port = packet.ReadUInt16();
                    servers.Add(new IPEndPoint(ip, port));
                }

                this.Servers = servers.ToArray();
            }
            else
            {
                this.Servers = new IPEndPoint[] { }
            };
        }
Exemplo n.º 4
0
        private void RefreshList()
        {
            new Thread(new ThreadStart(() =>
            {
                if (Settings.GetReg <int>("clist_src", 0) == 1)
                {
                    this.RefreshFromMarsProject();
                    return;
                }

                this.reloading_list = true;
                this.LabelChanged(null, new ChannelListLabelChangedEventArgs {
                    Text = "Searching..."
                });
                this.Terminate = false;
                byte[] raw     = null;

                if (File.Exists(Settings.DataPath + "servers.dat"))
                {
                    raw = File.ReadAllBytes(Settings.DataPath + "servers.dat");
                }
                else
                {
                    String cserv_location = default_remote_cache_server;

                    try
                    {
                        if (File.Exists(Settings.DataPath + "remotecserv.dat")) // alternative user defined server?
                        {
                            cserv_location = File.ReadAllText(Settings.DataPath + "remotecserv.dat");
                        }
                        else // create the default
                        {
                            File.WriteAllText(Settings.DataPath + "remotecserv.dat", default_remote_cache_server);
                        }
                    }
                    catch { }

                    try
                    {
                        WebRequest request = WebRequest.Create(cserv_location);

                        using (WebResponse response = request.GetResponse())
                            using (Stream stream = response.GetResponseStream())
                            {
                                List <byte> tmp = new List <byte>();
                                int s           = 0;
                                byte[] btmp     = new byte[1024];

                                while ((s = stream.Read(btmp, 0, 1024)) > 0)
                                {
                                    tmp.AddRange(btmp.Take(s));
                                }

                                raw = tmp.ToArray();
                            }
                    }
                    catch { }

                    if (raw == null) // unable to retrieve remote cache server so use the one in the installer
                    {
                        raw = File.ReadAllBytes(Settings.AppPath + "servers.dat");
                    }
                }

                if (raw != null)
                {
                    List <byte> list          = new List <byte>(raw);
                    List <IPEndPoint> to_send = new List <IPEndPoint>();

                    while (list.Count >= 6)
                    {
                        IPAddress ip = new IPAddress(list.GetRange(0, 4).ToArray());
                        list.RemoveRange(0, 4);
                        ushort port = BitConverter.ToUInt16(list.ToArray(), 0);
                        list.RemoveRange(0, 2);
                        to_send.Add(new IPEndPoint(ip, port));
                    }

                    Socket sock   = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sock.Blocking = false;

                    List <IPEndPoint> sent_items      = new List <IPEndPoint>();
                    List <ChannelListItem> recv_items = new List <ChannelListItem>();

                    EndPoint recv_ep       = new IPEndPoint(IPAddress.Any, 0);
                    uint time              = Settings.Time;
                    uint last_push         = time;
                    String last_filter     = this.filter_text.ToString();
                    RoomLanguage last_lang = this.filter_lang;

                    while (true)
                    {
                        if (this.Terminate)
                        {
                            return;
                        }

                        uint now = Settings.Time;

                        if (last_filter != this.filter_text.ToString())
                        {
                            last_filter = this.filter_text.ToString();
                            this.FilterResults();
                        }
                        else if (last_lang != this.filter_lang)
                        {
                            last_lang = this.filter_lang;
                            this.FilterResults();
                        }

                        if (now > last_push)
                        {
                            last_push = now;

                            if (recv_items.Count > 0)
                            {
                                this.CheckNewItems(recv_items.ToArray(), false);
                                recv_items.Clear();
                            }
                        }

                        if (now > (time + 15))
                        {
                            this.toolStrip1.BeginInvoke((Action)(() => this.toolStripButton1.Enabled = true));

                            try
                            {
                                sock.Close();
                                sock = null;
                            }
                            catch { }

                            this.reloading_list = false;
                            this.SaveServers();
                            this.SaveCache();

                            if (full_channel_list.Count == this.part_channel_list.Count)
                            {
                                this.LabelChanged(null, new ChannelListLabelChangedEventArgs {
                                    Text = "Channels (" + full_channel_list.Count + ")"
                                });
                            }
                            else
                            {
                                this.LabelChanged(null, new ChannelListLabelChangedEventArgs {
                                    Text = "Channels (" + this.part_channel_list.Count + "/" + full_channel_list.Count + ")"
                                });
                            }

                            return;
                        }

                        if (to_send.Count > 0)
                        {
                            try { sock.SendTo(new byte[] { 2 }, to_send[0]); }
                            catch { }

                            sent_items.Add(to_send[0]);
                            to_send.RemoveAt(0);
                            time = now;
                        }

                        while (sock.Available > 0)
                        {
                            byte[] buf = new byte[4096];
                            int size   = 0;

                            try { size = sock.ReceiveFrom(buf, ref recv_ep); }
                            catch { }

                            if (size > 0)
                            {
                                if (buf[0] == 3)
                                {
                                    UdpPacketReader packet = new UdpPacketReader(buf.Skip(1).Take(size - 1).ToArray());
                                    ChannelListItem item   = null;
                                    time = now;

                                    try { item = new ChannelListItem((IPEndPoint)recv_ep, packet); }
                                    catch { }

                                    if (item != null)
                                    {
                                        recv_items.Add(item);

                                        foreach (IPEndPoint e in item.Servers)
                                        {
                                            if (!to_send.Contains(e))
                                            {
                                                if (!sent_items.Contains(e))
                                                {
                                                    to_send.Add(e);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        Thread.Sleep(50);
                    }
                }
            })).Start();
        }