Пример #1
0
        async void ScreenConfig(System.IO.Stream stream)
        {
            var config = new CheckInMessage(state.ClientName, _screen.ScreenConfiguration[state.ClientName]);
            var buffer = config.GetBytes();
            await stream.WriteAsync(buffer, 0, buffer.Length);

            await stream.FlushAsync();
        }
Пример #2
0
        public async Task Send(byte[] message)
        {
            if (stream != null)
            {
                await stream.WriteAsync(message, 0, message.Length, cancellationToken : System.Threading.CancellationToken.None);

                await stream.FlushAsync(System.Threading.CancellationToken.None);
            }
        }
Пример #3
0
        public static async Task SerializeJsonIntoStreamAsync <T>(this T instance, System.IO.Stream stream)
        {
            Debug.Assert(stream.CanWrite);
            var body = System.Text.UTF8Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(instance));

            stream.Seek(0, System.IO.SeekOrigin.Begin);
            await stream.WriteAsync(body, 0, body.Length);

            stream.SetLength(body.Length);
            await stream.FlushAsync();
        }
Пример #4
0
        private static async Task SendResponse(HttpListenerContext context, byte[] buffer)
        {
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            await output.WriteAsync(buffer, 0, buffer.Length);

            await output.FlushAsync();

            output.Close();
        }
Пример #5
0
        static int _m_FlushAsync(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.IO.Stream gen_to_be_invoked = (System.IO.Stream)translator.FastGetCSObj(L, 1);


                int gen_param_count = LuaAPI.lua_gettop(L);

                if (gen_param_count == 1)
                {
                    System.Threading.Tasks.Task gen_ret = gen_to_be_invoked.FlushAsync(  );
                    translator.Push(L, gen_ret);



                    return(1);
                }
                if (gen_param_count == 2 && translator.Assignable <System.Threading.CancellationToken>(L, 2))
                {
                    System.Threading.CancellationToken _cancellationToken; translator.Get(L, 2, out _cancellationToken);

                    System.Threading.Tasks.Task gen_ret = gen_to_be_invoked.FlushAsync(
                        _cancellationToken);
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }

            return(LuaAPI.luaL_error(L, "invalid arguments to System.IO.Stream.FlushAsync!"));
        }
Пример #6
0
            private async Task <long> DoResumableDownloadAsync(int bufferSize)
            {
                try
                {
                    var buffer    = new byte[bufferSize];
                    var bytesRead = await serverStream.ReadAsync(buffer, 0, bufferSize);

                    await fileStream.WriteAsync(buffer, 0, bytesRead);

                    await fileStream.FlushAsync();

                    return(bytesRead);
                }
                catch (Exception error)
                {
                    MessageAppended?.Invoke(error.ToString());
                    Status = DownloadStatus.ErrorNeedResume;
                    return(-1);
                }
            }
Пример #7
0
        ///// <summary>
        ///// 设置全局数据缓存,如果存在仅修改值
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键值名称</param>
        ///// <param name="objObject">值</param>
        //public static void SetCache(this HttpContext context, string cacheKey, object objObject)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        context.Cache.Insert(cacheKey, objObject);
        //        return;
        //    }
        //    context.Cache[cacheKey] = objObject;
        //}

        ///// <summary>
        ///// 设置数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键值名称</param>
        ///// <param name="objObject">值</param>
        ///// <param name="timeout">单位秒,默认7200秒</param>
        //public static void SetCache(this HttpContext context, string cacheKey, object objObject, int timeout = 7200)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    //相对过期
        //    //context.Cache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
        //    //绝对过期时间
        //    context.Cache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
        //}

        ///// <summary>
        ///// 获取数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键</param>
        ///// <returns>返回一个值,如果键值名称不存在则返回NULL</returns>
        //public static object GetCache(this HttpContext context, string cacheKey)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        return null;
        //    }
        //    return context.Cache.Get(cacheKey); ;
        //}

        ///// <summary>
        ///// 移除指定数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键</param>
        //public static void RemoveCache(this HttpContext context, string cacheKey)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        return;
        //    }
        //    context.Cache.Remove(cacheKey);
        //}

        ///// <summary>
        ///// 移除全部缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        //public static void RemoveAllCache(this HttpContext context)
        //{
        //    if (context.Cache == null)
        //    {
        //        return;
        //    }
        //    var cacheEnum = context.Cache.GetEnumerator();
        //    while (cacheEnum.MoveNext())
        //    {
        //        context.Cache.Remove(cacheEnum.Key.ToString());
        //    }
        //}

        ///// <summary>
        ///// 调用此方法可以对请求的信息进行压缩处理
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <returns>返回当前发起请求的用户浏览器是否支持压缩信息 状态:<see cref="bool"/>类型</returns>
        //public static bool Deflate_Gzip(this HttpContext reques)
        //{
        //    string Accept = reques.Request.Headers["Accept-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Accept))
        //    {
        //        return false;
        //    }

        //    if (Accept.Contains("gzip"))
        //    {
        //        reques.Response.AppendHeader("Content-Encoding", "gzip");
        //        reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Compress);
        //        return true;
        //    }
        //    else if (Accept.Contains("deflate"))
        //    {
        //        reques.Response.AppendHeader("Content-Encoding", "deflate");
        //        reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Compress);
        //        return true;
        //    }
        //    return false;
        //}

        ///// <summary>
        ///// 调用此方法可以对请求的信息进行压缩处理
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <param name="httpEncoding">压缩枚举</param>
        ///// <returns>返回当前发起请求的用户浏览器是否支持压缩信息 状态:<see cref="bool"/>类型</returns>
        //public static bool Deflate_Gzip(this HttpContext reques, HttpEncoding httpEncoding)
        //{
        //    string Accept = reques.Request.Headers["Accept-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Accept))
        //    {
        //        return false;
        //    }

        //    if (httpEncoding == HttpEncoding.gzip)
        //    {
        //        if (Accept.Contains("gzip"))
        //        {
        //            reques.Response.AppendHeader("Content-Encoding", "gzip");
        //            reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Compress);
        //            return true;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    else if (httpEncoding == HttpEncoding.deflate)
        //    {
        //        if (Accept.Contains("deflate"))
        //        {
        //            reques.Response.AppendHeader("Content-Encoding", "deflate");
        //            reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Compress);
        //            return true;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        ///// <summary>
        ///// 解开以压缩的输出流
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <returns>返回状态</returns>
        //public static bool DecodeDeflate_Gzip(this HttpContext reques)
        //{
        //    string Content = reques.Response.Headers["Content-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Content))
        //    {
        //        return false;
        //    }

        //    if (Content.Contains("deflate"))
        //    {
        //        //reques.Response.Headers.Remove("Accept-Encoding");
        //        reques.Response.Headers.Remove("Content-Encoding");
        //        reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Decompress);
        //        return true;
        //    }
        //    else if (Content.Contains("gzip"))
        //    {
        //        //reques.Response.Headers.Remove("Accept-Encoding");
        //        reques.Response.Headers.Remove("Content-Encoding");
        //        reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Decompress);
        //        return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 将文件流分流写入
        /// </summary>
        /// <param name="ReadStream">原文件流</param>
        /// <param name="WriteStream">更新文件流</param>
        /// <param name="minlen">最小资源大小</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task StreamMove(System.IO.Stream ReadStream, System.IO.Stream WriteStream, int minlen)
        {
            long length = ReadStream.Length;
            long len    = (minlen) > length ? length : (minlen);

            byte[] slice = new byte[len];

            long seekiength = 0;

            do
            {
                int i = await ReadStream.ReadAsync(slice.AsMemory(0, slice.Length));

                await WriteStream.WriteAsync(slice.AsMemory(0, i));

                await WriteStream.FlushAsync();

                seekiength += i;
                Array.Clear(slice, 0, i);
            } while (length > seekiength);

            await ReadStream.DisposeAsync();
        }
Пример #8
0
        public async Task <string> DownloadAndCacheImage(int index, string url)
        {
            var outputFile = CreateTempFile();

            var httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));

            httpReq.Method = "GET";
            using (var response = await httpReq.GetResponseAsync())
            {
                using (System.IO.Stream s = response.GetResponseStream())
                {
                    using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(outputFile.AbsolutePath))
                    {
                        await s.CopyToAsync(fileStream);

                        await s.FlushAsync();

                        await fileStream.FlushAsync();
                    }
                }
            }

            return(outputFile.AbsolutePath);
        }
Пример #9
0
 /// <inheritdoc/>
 public override Task FlushAsync(CancellationToken cancellationToken)
 {
     return(_underlyingStream.FlushAsync(cancellationToken));
 }