コード例 #1
0
        private static async Task ProvideItem(HttpListenerRequest request, HttpListenerResponse response)
        {
            var  param     = request.RequestUri.ParseQueryParameters();
            var  sessionId = param.GetByKey(Consts.SessionId);
            var  itemId    = param.GetByKey(Consts.ItemId);
            bool provided  = false;
            //能够提供文件的会话,可能是SendItemsRequester,或者是BrowseResponder,或者是GetItemsResponder(不再需要这个了?)
            IItemProviderConversation br = SuperDriveCore.Conversations.GetByKey(sessionId) as IItemProviderConversation;
            FileItem fi = br?.FindItem(itemId) as FileItem;

            if (fi != null)
            {
                try
                {
                    response.Headers.ContentLength = fi.Length;
                    response.UseMemoryStream       = false;
                    await response.WriteHeaders();

                    var buffer = new byte[1024 * 1024];
                    using (var stream = fi.Open(FileMode.Open, FileAccess.Read))
                    {
                        int count;
                        while (!fi.IsTransferEnd() && (count = await fi.ReadAsync(buffer)) != 0)
                        {
                            await response.GetSocketStream().WriteAsync(buffer, 0, count);

                            provided = true;
                            Env.Logger.Log("Get Item End " + itemId, "Http");
                        }
                    }
                    response.Dispose();
                }
                catch (Exception e)
                {
                    Env.Logger.Log($"Provide item[{fi}] exception", stackTrace: e.StackTrace);
                    //TODO 这里没机会改Stats code了。只能强制关闭socket。
                    response.CloseSocket();
                }
            }

            if (!provided)
            {
                response.Forbidden();
                response.Dispose();
            }
        }
コード例 #2
0
#pragma warning disable 1998
        internal static async Task <BrowseResponseMessage> Response(IItemProviderConversation conv, BrowseRequestMessage browseRequestMessage)
#pragma warning restore 1998
        {
            var pathId = browseRequestMessage.DirItemId;
            var dir    = conv.FindItem(pathId) as DirItem;
            var brpm   = new BrowseResponseMessage {
                Id = conv.Id
            };

            if (dir == null)
            {
                brpm.CurrentDir = null;
            }
            else
            {
                brpm.CurrentDir = dir;
                brpm.Items      = dir.Children;
            }
            return(brpm);
        }