Пример #1
0
        static StackObject *ReadAsync_13(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 4);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Int32 @count = ptr_of_this_method->Value;

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            System.Int32 @index = ptr_of_this_method->Value;

            ptr_of_this_method = ILIntepreter.Minus(__esp, 3);
            System.Char[] @buffer = (System.Char[]) typeof(System.Char[]).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 4);
            System.IO.StreamReader instance_of_this_method = (System.IO.StreamReader) typeof(System.IO.StreamReader).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.ReadAsync(@buffer, @index, @count);

            object obj_result_of_this_method = result_of_this_method;

            if (obj_result_of_this_method is CrossBindingAdaptorType)
            {
                return(ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance));
            }
            return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method));
        }
Пример #2
0
        async Task Consume(System.IO.StreamReader s, OutputKind kind)
        {
            var buffer   = new char[1024];
            var allSpans = new List <string>();

            while (!s.EndOfStream)
            {
                // Note that this does block the active thread; we must Task.Run.
                var count = await s.ReadAsync(buffer).ConfigureAwait(false);

                AppendData(kind, new string(buffer.AsSpan(0, count)));
            }

            void AppendData(OutputKind kind, string data)
            {
                allSpans.Add(data);
                Application.Current.Dispatcher.Invoke(() =>
                {
                    var shouldReplace = Output.Any() && Output[^ 1].Kind == kind;
                    var lines         = StringComposer.ToLines(shouldReplace ? Output[^ 1].Data : "", data);
                    foreach (var line in lines)
                    {
                        if (shouldReplace)
                        {
                            Output[^ 1]   = new(kind, line);
        public async static Task <string> ReadRowAsync(this System.IO.StreamReader reader, IFileMetaData fileMetaData)
        {
            if (reader.Peek() == -1)
            {
                return(null);
            }

            var  line   = new StringBuilder();
            char Quotes = fileMetaData.FieldsEnclosedBy.FirstOrDefault();

            if (Quotes == '\0')
            {
                line.Append(await reader.ReadLineAsync());
                return(line.Flush());
            }
            char terminatorStart = fileMetaData.LinesTerminatedBy.FirstOrDefault();
            bool inQuotes        = false;

            char[] c = new char[1];
            while (reader.Peek() != -1)
            {
                await reader.ReadAsync(c, 0, 1);

                if (!inQuotes && c[0] == terminatorStart)
                {
                    for (int i = 1; i < fileMetaData.LineTerminatorLength; i++)
                    {
                        await reader.ReadAsync(c, 0, 1);
                    }
                    return(line.Flush());
                }
                if (c[0] == Quotes)
                {
                    inQuotes = !inQuotes;
                }
                line.Append(c[0]);
            }
            return(line.Flush());
        }
Пример #4
0
        public IObservable<JToken> ConnectToTalkGadgetBind(HttpClient normalClient, HttpClient streamClient, Uri talkBaseUrl, CookieContainer checkTargetCookies, string pvtVal)
        {
            var observable = Observable.Create<JToken>(subject =>
            {
                var tokenSource = new System.Threading.CancellationTokenSource();
                var task = Task.Run(async () =>
                {
                    try
                    {
                        //通信で使用されるパラメータ。各段階で使用される変数が違うが、
                        //jsonRecieverで一括定義なので冒頭に宣言する必要があった

                        //aidVal: 再接続をする際に取得済み配列が返されないようにするために送る値
                        int aidVal = 0;
                        //各段階で必要なセッションID類
                        string sidVal = null, gsidVal = null, clidVal = null;

                        //受信したjson配列には一定の形式がある。各要素の種類に
                        //合わせた処理をここで一括定義
                        Action<JToken> jsonReciever = (jsonItem) =>
                        {
                            aidVal = (int)jsonItem[0];
                            var data = jsonItem[1];
                            JToken tmp;
                            switch ((string)data[0])
                            {
                                case "c":
                                    switch ((tmp = data[1]).Type)
                                    {
                                        case JTokenType.String:
                                            sidVal = (string)data[1];
                                            break;
                                        case JTokenType.Array:
                                            if ((tmp = tmp[1]).Type == JTokenType.Array && (string)tmp[0] == "ei")
                                                gsidVal = (string)tmp[1];
                                            break;
                                    }
                                    break;
                            }
                            try
                            { subject.OnNext(jsonItem); }
                            catch (Exception e)
                            { throw new OutsideException("通知先で例外が発生しました。", e); }
                        };

                        //clid, gsidを取得する
                        tokenSource.Token.ThrowIfCancellationRequested();
                        var vals = LoadNotifierClient(normalClient, talkBaseUrl, checkTargetCookies, pvtVal).Result["ds:4"][0];
                        clidVal = (string)vals[7];
                        gsidVal = (string)vals[3];

                        //sid値を取得する
                        tokenSource.Token.ThrowIfCancellationRequested();
                        var url = new Uri(talkBaseUrl, string.Format("talkgadget/_/channel/bind?{0}",
                            await MakeQuery(new Dictionary<string, string>()
                                {
                                    //無いと死ぬ
                                    { "clid", clidVal },
                                    //値は何でもおk。けど無いと死ぬ
                                    { "VER", "8" },
                                    { "RID", "64437" },
                                    //無くてもおk
                                    { "gsessionid", gsidVal },
                                    { "zx", "n9igqs467cbk" },
                                    //不変値。無くてもおk。
                                    { "prop", "homepage" },
                                    { "CVER", "1" },
                                    { "t", "1" },
                                    { "ec", "[\"ci:ec\",1,1,0,\"chat_wcs_20140612.120118_RC3\"]" },
                                })));
                        var res = await PostStringAsync(normalClient, url, new Dictionary<string, string>() { { "count", "0" } }, tokenSource.Token);
                        var json = JToken.Parse(ApiAccessorUtility.ConvertIntoValidJson(res.Substring(res.IndexOf("\n") + 1)));
                        foreach (var item in json)
                            jsonReciever(item);

                        //init
                        tokenSource.Token.ThrowIfCancellationRequested();
                        //post内容が意味不明だが、無いとapiが動かないために必要
                        url = new Uri(talkBaseUrl, string.Format("talkgadget/_/channel/bind?{0}",
                            await MakeQuery(new Dictionary<string, string>()
                                {
                                    //無いと死ぬ
                                    { "SID", sidVal },
                                    //値は何でもおk。けど無いと死ぬ
                                    { "RID", "64438" },
                                    //無くてもおk
                                    { "gsessionid", gsidVal },
                                    { "clid", clidVal },
                                    //不変値。無くてもおk。
                                    { "VER", "8" },
                                    { "prop", "homepage" },
                                    { "AID", aidVal.ToString() },
                                    { "t", "1" },
                                    { "zx", "7anb33bhfgp2" },
                                    { "ec", "[\"ci:ec\",1,1,0,\"chat_wcs_20140612.120118_RC3\"]" },
                                })));
                        res = await PostStringAsync(
                            normalClient, url, new Dictionary<string, string>()
                                {
                                    //reqXの数とcountの数を合わせないと死ぬ
                                    { "count", "1" },
                                    { "ofs", "0" },
                                    { "req0_m", "[\"connect-add-client\"]" },
                                    { "req0_c", clidVal },
                                    { "req0__sc", "c" },
                                }, tokenSource.Token);

                        DateTime debug_streamingTime;
                        while (true)
                        {
                            debug_streamingTime = DateTime.UtcNow;
                            tokenSource.Token.ThrowIfCancellationRequested();
                            //user streaming apiに接続する
                            //memo: 接続持続時間は3.5分が目安
                            url = new Uri(talkBaseUrl, string.Format("talkgadget/_/channel/bind?{0}",
                                await MakeQuery(new Dictionary<string, string>()
                                {
                                    //無いと死ぬ
                                    { "RID", "rpc" },
                                    { "SID", sidVal },
                                    { "CI", "0" },
                                    //無くてもおk
                                    { "clid", clidVal },//不要必要がちょっと微妙
                                    { "gsessionid", gsidVal },//接続更新でこいつが変化しているのは何故?
                                    { "AID", aidVal.ToString() },//無くても動くが、既読ページ数のようなものなので、正常動作には必要
                                    //不変値。無くてもおk
                                    { "VER", "8" },
                                    { "prop", "homepage" },
                                    { "TYPE", "xmlhttp" },
                                    { "zx", "p5guamwzkeiu" },
                                    { "t", "1" },
                                    { "ec", "[\"ci:ec\",1,1,0,\"chat_wcs_20140612.120118_RC3\"]" },
                                })));
                            using (var strm = await GetStreamAsync(streamClient, url, tokenSource.Token))
                            using (var reader = new System.IO.StreamReader(strm, Encoding.UTF8))
                            {
                                var builder = new StringBuilder();
                                var buffer = new char[1024];
                                var buffCnt = 0;
                                var length = -1;
                                while (!reader.EndOfStream)
                                {
                                    tokenSource.Token.ThrowIfCancellationRequested();
                                    if (length < 0)
                                        length = int.Parse(reader.ReadLine());

                                    buffCnt = await reader.ReadAsync(buffer, 0, Math.Min(buffer.Length, length - builder.Length));
                                    builder.Append(buffer, 0, buffCnt);
                                    if (length == builder.Length)
                                    {
                                        var str = builder.ToString();
                                        var replaced = ApiAccessorUtility.ConvertIntoValidJson(str);
                                        var recieveJson = JToken.Parse(replaced);

                                        //後始末
                                        builder.Clear();
                                        length = -1;

                                        foreach (var item in recieveJson)
                                        {
                                            tokenSource.Token.ThrowIfCancellationRequested();
                                            jsonReciever(item);
                                        }
                                    }
                                }
                            }
                            System.Diagnostics.Debug.WriteLine("DEBUG: StreamSessionTime\r\n{0}", DateTime.UtcNow - debug_streamingTime);
                        }
                    }
                    catch (Exception e)
                    {
                        if (tokenSource.Token.IsCancellationRequested)
                            subject.OnCompleted();
                        else
                            subject.OnError(e);
                    }
                }, tokenSource.Token);

                //購読中断時に呼び出してもらうメソッドを返す
                return tokenSource.Cancel;
            });
            return observable;
        }