/
SubjectCommand.cs
146 lines (125 loc) · 4.65 KB
/
SubjectCommand.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
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
using System;
using System.Threading;
using KimiStudio.Bangumi.Api.Models;
namespace KimiStudio.Bangumi.Api.Commands
{
public class SubjectCommand : Command, ICommand<SubjectCommandResult>
{
private readonly int subjectId;
private readonly AuthUser auth;
private class StateObj
{
public GetSubjectCommand GetSubjectCommand;
public SubjectStateCommand SubjectStateCommand;
public ProgressCommand ProgressCommand;
public Countdown Countdown;
public AsyncCallback Callback;
public object State;
public Exception Exception;
public readonly SubjectCommandResult Result = new SubjectCommandResult();
}
private class StateAsyncResult : IAsyncResult
{
public StateObj StateObj { get; set; }
public object AsyncState { get; set; }
public WaitHandle AsyncWaitHandle
{
get { return null; }
}
public bool CompletedSynchronously
{
get { return true; }
}
public bool IsCompleted
{
get { return true; }
}
}
public SubjectCommand(int subjectId, AuthUser auth)
{
this.subjectId = subjectId;
this.auth = auth;
;
}
private void WaitCallBack(object state)
{
var stateObj = state as StateObj;
if (stateObj == null) return;
stateObj.Countdown.Wait();
if (stateObj.Callback == null) return;
stateObj.Callback(new StateAsyncResult {StateObj = stateObj, AsyncState = stateObj.State});
}
public void BeginExecute(AsyncCallback asyncCallback, object state)
{
var stateObj = new StateObj
{
GetSubjectCommand = new GetSubjectCommand(subjectId),
SubjectStateCommand = new SubjectStateCommand(subjectId, auth),
ProgressCommand = new ProgressCommand(subjectId, auth),
Countdown = new Countdown(3),
Callback = asyncCallback,
State = state
};
ThreadPool.QueueUserWorkItem(WaitCallBack, stateObj);
//var data = CreateRequestData();
//BeginSend(data, asyncCallback, state);
stateObj.GetSubjectCommand.BeginExecute(GetSubjectCallBack, stateObj);
stateObj.SubjectStateCommand.BeginExecute(SubjectStateCallBack, stateObj);
stateObj.ProgressCommand.BeginExecute(ProgressCallBack, stateObj);
}
private void GetSubjectCallBack(IAsyncResult asyncResult)
{
var stateObj = (StateObj)asyncResult.AsyncState;
try
{
stateObj.Result.Subject = stateObj.GetSubjectCommand.EndExecute(asyncResult);
stateObj.Countdown.Signal();
}
catch (Exception e)
{
stateObj.Exception = e;
stateObj.Countdown.Free();
}
}
private void SubjectStateCallBack(IAsyncResult asyncResult)
{
var stateObj = (StateObj)asyncResult.AsyncState;
try
{
stateObj.Result.SubjectState = stateObj.SubjectStateCommand.EndExecute(asyncResult);
stateObj.Countdown.Signal();
}
catch (Exception e)
{
stateObj.Exception = e;
stateObj.Countdown.Free();
}
}
private void ProgressCallBack(IAsyncResult asyncResult)
{
var stateObj = (StateObj)asyncResult.AsyncState;
try
{
stateObj.Result.Progress = stateObj.ProgressCommand.EndExecute(asyncResult);
stateObj.Countdown.Signal();
}
catch (Exception e)
{
stateObj.Exception = e;
stateObj.Countdown.Free();
}
}
public SubjectCommandResult EndExecute(IAsyncResult asyncResult)
{
var stateAsyncResult = (StateAsyncResult)asyncResult;
if (stateAsyncResult.StateObj.Exception != null) throw stateAsyncResult.StateObj.Exception;
return stateAsyncResult.StateObj.Result;
}
}
public class SubjectCommandResult
{
public Subject Subject;
public SubjectState SubjectState;
public Progress Progress;
}
}