|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Text;
|
| 5 | +using System.Threading; |
5 | 6 | using SuperSocket.ProtoBase;
|
6 | 7 | using SuperSocket.SocketBase.Command;
|
7 | 8 | using SuperSocket.SocketBase.Pool;
|
8 | 9 | using SuperSocket.SocketBase.Protocol;
|
9 | 10 |
|
| 11 | + |
10 | 12 | namespace SuperSocket.SocketBase
|
11 | 13 | {
|
12 | 14 | /// <summary>
|
13 |
| - /// Command Executing Context |
| 15 | + /// The interface for request executing context |
14 | 16 | /// </summary>
|
15 |
| - public class CommandExecutingContext |
| 17 | + /// <typeparam name="TAppSession">The type of the application session.</typeparam> |
| 18 | + /// <typeparam name="TPackageInfo">The type of the package information.</typeparam> |
| 19 | + public interface IRequestExecutingContext<TAppSession, TPackageInfo> |
| 20 | + where TPackageInfo : IPackageInfo |
| 21 | + where TAppSession : IAppSession, IThreadExecutingContext, IAppSession<TAppSession, TPackageInfo>, new() |
16 | 22 | {
|
| 23 | + |
17 | 24 | /// <summary>
|
18 |
| - /// Gets the session. |
| 25 | + /// Gets the current session. |
19 | 26 | /// </summary>
|
20 |
| - public IAppSession Session { get; private set; } |
| 27 | + /// <value> |
| 28 | + /// The session. |
| 29 | + /// </value> |
| 30 | + TAppSession Session { get; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets the current request. |
| 34 | + /// </summary> |
| 35 | + /// <value> |
| 36 | + /// The request information. |
| 37 | + /// </value> |
| 38 | + TPackageInfo RequestInfo { get; } |
| 39 | + } |
21 | 40 |
|
| 41 | + /// <summary> |
| 42 | + /// The interface for command excuting context |
| 43 | + /// </summary> |
| 44 | + public interface ICommandExecutingContext |
| 45 | + { |
22 | 46 | /// <summary>
|
23 |
| - /// Gets the request info. |
| 47 | + /// Gets the session which is executing the command. |
24 | 48 | /// </summary>
|
25 |
| - public IPackageInfo Request { get; private set; } |
| 49 | + /// <value> |
| 50 | + /// The session. |
| 51 | + /// </value> |
| 52 | + IAppSession Session { get; } |
26 | 53 |
|
27 | 54 | /// <summary>
|
28 |
| - /// Gets the current command. |
| 55 | + /// Gets the request which is being processed. |
29 | 56 | /// </summary>
|
30 |
| - public ICommand CurrentCommand { get; private set; } |
| 57 | + /// <value> |
| 58 | + /// The request. |
| 59 | + /// </value> |
| 60 | + IPackageInfo Request { get; } |
| 61 | + |
31 | 62 |
|
32 | 63 | /// <summary>
|
33 |
| - /// Gets the exception. |
| 64 | + /// Gets the current command which is being executed. |
| 65 | + /// </summary> |
| 66 | + /// <value> |
| 67 | + /// The current command. |
| 68 | + /// </value> |
| 69 | + ICommand CurrentCommand { get; } |
| 70 | + |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets the exception which was thrown in the command execution. |
34 | 74 | /// </summary>
|
35 | 75 | /// <value>
|
36 | 76 | /// The exception.
|
37 | 77 | /// </value>
|
38 |
| - public Exception Exception { get; internal set; } |
| 78 | + Exception Exception { get; } |
39 | 79 |
|
40 | 80 | /// <summary>
|
41 | 81 | /// Gets a value indicating whether [exception handled].
|
42 | 82 | /// </summary>
|
43 | 83 | /// <value>
|
44 | 84 | /// <c>true</c> if [exception handled]; otherwise, <c>false</c>.
|
45 | 85 | /// </value>
|
46 |
| - public bool ExceptionHandled { get; internal set; } |
| 86 | + bool ExceptionHandled { get; set; } |
| 87 | + |
47 | 88 |
|
48 | 89 | /// <summary>
|
49 |
| - /// Gets or sets a value indicating whether this command executing is cancelled. |
| 90 | + /// Gets or sets a value indicating whether the command exeuction is cancelled. |
50 | 91 | /// </summary>
|
51 | 92 | /// <value>
|
52 | 93 | /// <c>true</c> if cancel; otherwise, <c>false</c>.
|
53 | 94 | /// </value>
|
54 |
| - public bool Cancel { get; set; } |
55 |
| - |
56 |
| - /// <summary> |
57 |
| - /// Initializes a new instance of the <see cref="CommandExecutingContext" /> class. |
58 |
| - /// </summary> |
59 |
| - /// <param name="session">The session.</param> |
60 |
| - /// <param name="requestInfo">The request info.</param> |
61 |
| - /// <param name="command">The command.</param> |
62 |
| - public void Initialize(IAppSession session, IPackageInfo requestInfo, ICommand command) |
63 |
| - { |
64 |
| - Session = session; |
65 |
| - Request = requestInfo; |
66 |
| - CurrentCommand = command; |
67 |
| - } |
| 95 | + bool Cancel { get; set; } |
68 | 96 | }
|
69 | 97 |
|
70 |
| - interface IThreadExecutingContext |
| 98 | + /// <summary> |
| 99 | + /// The interface for thread execution context |
| 100 | + /// </summary> |
| 101 | + public interface IThreadExecutingContext |
71 | 102 | {
|
| 103 | + /// <summary> |
| 104 | + /// Increments the queued request count. |
| 105 | + /// </summary> |
| 106 | + /// <param name="value">The value.</param> |
72 | 107 | void Increment(int value);
|
73 | 108 |
|
| 109 | + /// <summary> |
| 110 | + /// Decrements the queued request count. |
| 111 | + /// </summary> |
| 112 | + /// <param name="value">The value.</param> |
74 | 113 | void Decrement(int value);
|
75 | 114 |
|
| 115 | + /// <summary> |
| 116 | + /// Gets or sets the prefered thread identifier. |
| 117 | + /// </summary> |
| 118 | + /// <value> |
| 119 | + /// The prefered thread identifier. |
| 120 | + /// </value> |
76 | 121 | int PreferedThreadId { get; set; }
|
77 | 122 | }
|
78 | 123 |
|
79 |
| - class RequestExecutingContext<TAppSession, TPackageInfo> : IThreadExecutingContext |
| 124 | + class RequestExecutingContext<TAppSession, TPackageInfo> : IRequestExecutingContext<TAppSession, TPackageInfo>, IThreadExecutingContext, ICommandExecutingContext |
80 | 125 | where TPackageInfo : IPackageInfo
|
81 | 126 | where TAppSession : IAppSession, IThreadExecutingContext, IAppSession<TAppSession, TPackageInfo>, new()
|
82 | 127 | {
|
@@ -126,6 +171,71 @@ public void Reset()
|
126 | 171 | {
|
127 | 172 | Session = default(TAppSession);
|
128 | 173 | RequestInfo = default(TPackageInfo);
|
| 174 | + CurrentCommand = null; |
| 175 | + Exception = null; |
| 176 | + ExceptionHandled = false; |
| 177 | + Cancel = false; |
| 178 | + } |
| 179 | + |
| 180 | + IAppSession ICommandExecutingContext.Session |
| 181 | + { |
| 182 | + get { return Session; } |
| 183 | + } |
| 184 | + |
| 185 | + IPackageInfo ICommandExecutingContext.Request |
| 186 | + { |
| 187 | + get { return RequestInfo; } |
| 188 | + } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Gets the current command which is being executed. |
| 192 | + /// </summary> |
| 193 | + /// <value> |
| 194 | + /// The current command. |
| 195 | + /// </value> |
| 196 | + public ICommand CurrentCommand { get; internal set; } |
| 197 | + |
| 198 | + /// <summary> |
| 199 | + /// Gets the exception which was thrown in the command execution. |
| 200 | + /// </summary> |
| 201 | + /// <value> |
| 202 | + /// The exception. |
| 203 | + /// </value> |
| 204 | + public Exception Exception { get; internal set; } |
| 205 | + |
| 206 | + /// <summary> |
| 207 | + /// Gets a value indicating whether [exception handled]. |
| 208 | + /// </summary> |
| 209 | + /// <value> |
| 210 | + /// <c>true</c> if [exception handled]; otherwise, <c>false</c>. |
| 211 | + /// </value> |
| 212 | + public bool ExceptionHandled { get; set; } |
| 213 | + |
| 214 | + /// <summary> |
| 215 | + /// Gets or sets a value indicating whether the command exeuction is cancelled. |
| 216 | + /// </summary> |
| 217 | + /// <value> |
| 218 | + /// <c>true</c> if cancel; otherwise, <c>false</c>. |
| 219 | + /// </value> |
| 220 | + public bool Cancel { get; set; } |
| 221 | + |
| 222 | + private const string m_SlotName = "REC"; //RequestExecutingContext |
| 223 | + |
| 224 | + public static RequestExecutingContext<TAppSession, TPackageInfo> GetFromThreadContext() |
| 225 | + { |
| 226 | + var slot = Thread.GetNamedDataSlot(m_SlotName); |
| 227 | + |
| 228 | + // look for existing context instance from thread local at first |
| 229 | + var context = Thread.GetData(slot) as RequestExecutingContext<TAppSession, TPackageInfo>; |
| 230 | + |
| 231 | + if (context != null) |
| 232 | + return context; |
| 233 | + |
| 234 | + // create a new context instance and then store it into thread local |
| 235 | + context = new RequestExecutingContext<TAppSession, TPackageInfo>(); |
| 236 | + Thread.SetData(slot, context); |
| 237 | + |
| 238 | + return context; |
129 | 239 | }
|
130 | 240 | }
|
131 | 241 | }
|
0 commit comments