forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangesCurrentDatabaseForwardingHandler.cs
62 lines (55 loc) · 1.74 KB
/
ChangesCurrentDatabaseForwardingHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// -----------------------------------------------------------------------
// <copyright file="ChangesCurrentDatabaseForwardingHandler.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Threading.Tasks;
using System.Web;
using Raven.Abstractions.Util;
using Raven.Database.Server;
using Raven.Database.Server.Abstractions;
namespace Raven.Web
{
public class ChangesCurrentDatabaseForwardingHandler : IHttpAsyncHandler
{
private readonly HttpServer server;
public ChangesCurrentDatabaseForwardingHandler(HttpServer server)
{
this.server = server;
}
public Task ProcessRequestAsync(HttpContext context)
{
var tcs = new TaskCompletionSource<object>();
server.HandleChangesRequest(new HttpContextAdapter(HttpContext.Current, server.Configuration), ()=> tcs.TrySetResult(null))
.ContinueWith(task =>
{
if(task.IsFaulted)
tcs.TrySetException(task.Exception);
else if (task.IsCanceled)
tcs.TrySetCanceled();
});
return tcs.Task;
}
public void ProcessRequest(HttpContext context)
{
ProcessRequestAsync(context).Wait();
}
public bool IsReusable
{
get { return false; }
}
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.BufferOutput = false;
return ProcessRequestAsync(context)
.ContinueWith(task => cb(task));
}
public void EndProcessRequest(IAsyncResult result)
{
var task = result as Task;
if (task != null)
task.Wait(); // ensure we get proper errors
}
}
}