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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace IceInternal
{
using System.Collections.Generic;
using System.Diagnostics;
public class RetryTask : TimerTask, CancellationHandler
{
public RetryTask(Instance instance, RetryQueue retryQueue, ProxyOutgoingAsyncBase outAsync)
{
_instance = instance;
_retryQueue = retryQueue;
_outAsync = outAsync;
}
public void runTimerTask()
{
_outAsync.retry();
//
// NOTE: this must be called last, destroy() blocks until all task
// are removed to prevent the client thread pool to be destroyed
// (we still need the client thread pool at this point to call
// exception callbacks with CommunicatorDestroyedException).
//
_retryQueue.remove(this);
}
public void asyncRequestCanceled(OutgoingAsyncBase outAsync, Ice.LocalException ex)
{
Debug.Assert(_outAsync == outAsync);
if(_retryQueue.cancel(this))
{
if(_instance.traceLevels().retry >= 1)
{
_instance.initializationData().logger.trace(_instance.traceLevels().retryCat,
string.Format("operation retry canceled\n{0}", ex));
}
if(_outAsync.exception(ex))
{
_outAsync.invokeExceptionAsync();
}
}
}
public void destroy()
{
try
{
_outAsync.abort(new Ice.CommunicatorDestroyedException());
}
catch(Ice.CommunicatorDestroyedException)
{
// Abort can throw if there's no callback, just ignore in this case
}
}
private Instance _instance;
private RetryQueue _retryQueue;
private ProxyOutgoingAsyncBase _outAsync;
}
public class RetryQueue
{
public RetryQueue(Instance instance)
{
_instance = instance;
}
public void add(ProxyOutgoingAsyncBase outAsync, int interval)
{
lock(this)
{
if(_instance == null)
{
throw new Ice.CommunicatorDestroyedException();
}
RetryTask task = new RetryTask(_instance, this, outAsync);
outAsync.cancelable(task); // This will throw if the request is canceled.
_instance.timer().schedule(task, interval);
_requests.Add(task, null);
}
}
public void destroy()
{
lock(this)
{
Dictionary<RetryTask, object> keep = new Dictionary<RetryTask, object>();
foreach(RetryTask task in _requests.Keys)
{
if(_instance.timer().cancel(task))
{
task.destroy();
}
else
{
keep.Add(task, null);
}
}
_requests = keep;
_instance = null;
while(_requests.Count > 0)
{
System.Threading.Monitor.Wait(this);
}
}
}
public void remove(RetryTask task)
{
lock(this)
{
if(_requests.Remove(task))
{
if(_instance == null && _requests.Count == 0)
{
// If we are destroying the queue, destroy is probably waiting on the queue to be empty.
System.Threading.Monitor.Pulse(this);
}
}
}
}
public bool cancel(RetryTask task)
{
lock(this)
{
if(_requests.Remove(task))
{
if(_instance == null && _requests.Count == 0)
{
// If we are destroying the queue, destroy is probably waiting on the queue to be empty.
System.Threading.Monitor.Pulse(this);
}
return _instance.timer().cancel(task);
}
return false;
}
}
private Instance _instance;
private Dictionary<RetryTask, object> _requests = new Dictionary<RetryTask, object>();
}
}
|