Esempio n. 1
0
        HybridStreamReader UncompressBytes(HybridStreamReader stream, int clen)
        {
            byte[] bytes = new byte[clen];

            stream.Read(bytes);

            var ms = new MemoryStream(bytes);

            var df = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress);

            HybridStreamReader sr = new HybridStreamReader(df);

            return(sr);
        }
Esempio n. 2
0
        public void HandleHost(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker b = (BackgroundWorker)sender;
            DictBundle       pending = null;
            string           a1, a2, a3, a4;

            for (;;)
            {
                try
                {
                    hostClient = new System.Net.Sockets.TcpClient(server, 6668);

                    NetworkStream      stream      = hostClient.GetStream();
                    HybridStreamReader r           = new HybridStreamReader(stream);
                    HybridStreamReader savedStream = null;

                    SendHost("nick " + nick);
                    SendHost("dir /");
                    SendHost("dir _maps/default");

                    string line;

                    for (; ;)
                    {
                        if ((line = r.ReadLine()) == null)
                        {
                            if (savedStream != null)
                            {
                                r           = savedStream;
                                savedStream = null;
                                continue;
                            }

                            break;
                        }

                        if (line.StartsWith("hours "))
                        {
                            int hours;
                            Parse2(line, out a1, out a2);
                            Int32.TryParse(a2, out hours);

                            var h = new HoursReport();
                            h.hours = hours;
                            b.ReportProgress(1, h);
                        }
                        else if (line.StartsWith("audio "))
                        {
                            int count;
                            Parse3(line, out a1, out a2, out a3);

                            var audio = new AudioReport();
                            audio.desc = a2;
                            if (a2 == "ownage")
                            {
                                Int32.TryParse(a3, out count);
                                audio.killcount = count;
                            }
                            else
                            {
                                audio.text = a3;
                            }

                            b.ReportProgress(1, audio);
                        }
                        else if (line.StartsWith("download "))
                        {
                            int len;
                            Parse3(line, out a1, out a2, out a3);
                            Int32.TryParse(a3, out len);
                            var bytes = new byte[len];
                            r.Read(bytes);

                            var download = new DownloadFile();
                            download.name  = a2;
                            download.bytes = bytes;
                            b.ReportProgress(1, download);
                        }
                        else if (line.StartsWith("compressed "))
                        {
                            int clen, len;
                            Parse3(line, out a1, out a2, out a3);
                            Int32.TryParse(a2, out clen);
                            Int32.TryParse(a3, out len);

                            // switch to the saved stream
                            savedStream = r;
                            r           = UncompressBytes(r, clen);

                            if (r == null)
                            {
                                r = savedStream;
                            }
                            continue;
                        }
                        else if (line.StartsWith("begin eval "))
                        {
                            pending = new EvalBundle();
                            Parse3(line, out a1, out a2, out a3);
                            pending.path = a3;
                        }
                        else if (line.StartsWith("begin dir "))
                        {
                            pending = new DictBundle();
                            Parse3(line, out a1, out a2, out a3);
                            pending.path = a3;
                        }
                        else if (pending != null && line.StartsWith("v "))
                        {
                            Parse4(line, out a1, out a2, out a3, out a4);
                            if (a1 != "v" || a2 != pending.path)
                            {
                                continue;
                            }

                            if (!pending.dict.ContainsKey(a3))
                            {
                                pending.dict.Add(a3, a4);
                            }
                        }
                        else if (line.StartsWith("end dir "))
                        {
                            b.ReportProgress(1, pending);
                            pending = null;
                        }
                        else if (line.StartsWith("end eval "))
                        {
                            b.ReportProgress(1, pending);
                            pending = null;
                        }
                        else if (pending == null)
                        {
                            b.ReportProgress(1, line);
                        }
                    }
                }
                catch (Exception ex)
                {
                    b.ReportProgress(0, ex);
                    System.Threading.Thread.Sleep(5000);
                }
            }
        }