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(); } }
private void AddToFavourites(ChannelListItem room) { if (this.favs.Find(x => x.IP.Equals(room.IP) && x.Port == room.Port) != null) { return; } FavouritesListItem f = room.ToFavouritesItem(); f.CountString = null; this.favs.Add(f); ChannelListViewItem vitem = new ChannelListViewItem(null, 0); this.gfx.RenderChannelListItem(vitem, f); this.g_favs.Add(vitem); this.channelListView2.Items.Add(vitem); this.SaveFavourites(); }
public static String EncodeHashlink(ChannelListItem room) { List <byte> list = new List <byte>(); list.AddRange(new byte[20]); list.AddRange(Encoding.UTF8.GetBytes("CHATCHANNEL")); list.Add(0); list.AddRange(room.IP.GetAddressBytes()); list.AddRange(BitConverter.GetBytes(room.Port)); list.AddRange(room.IP.GetAddressBytes()); list.AddRange(Encoding.UTF8.GetBytes(room.Name)); list.Add(0); list.Add(0); byte[] buf = list.ToArray(); buf = Zip.Compress(buf); buf = e67(buf, 28435); return(Convert.ToBase64String(buf)); }
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(); }
private void RefreshFromMarsProject() { this.reloading_list = true; this.LabelChanged(null, new ChannelListLabelChangedEventArgs { Text = "Searching..." }); this.Terminate = false; MarsProjectResult result = null; try { WebRequest request = WebRequest.Create("http://chatrooms.marsproject.net/list/json.aspx"); using (WebResponse response = request.GetResponse()) using (Stream stream = response.GetResponseStream()) { DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(MarsProjectResult)); result = (MarsProjectResult)json.ReadObject(stream); } } catch { } if (result != null) { List <ChannelListItem> items = new List <ChannelListItem>(); foreach (MarsProjectItem r in result.Items) { ChannelListItem i = new ChannelListItem(r.Name, r.Topic, IPAddress.Parse(r.IP), (ushort)r.Port); i.Lang = (RoomLanguage)Enum.Parse(typeof(RoomLanguage), r.Language); i.Users = (ushort)r.Count; items.Add(i); } if (items.Count > 2) { int n = items.Count; Random rnd = new Random((int)Helpers.UnixTime); while (n > 1) { n--; int k = rnd.Next(n + 1); ChannelListItem value = items[k]; items[k] = items[n]; items[n] = value; } } this.CheckNewItems(items.ToArray(), true); items.Clear(); items = null; } this.toolStrip1.BeginInvoke((Action)(() => this.toolStripButton1.Enabled = true)); this.reloading_list = false; }
public void RenderChannelListItem(ChannelListViewItem item, ChannelListItem org) { this.RenderChannelListItem(item, org.ToFavouritesItem()); }