Skip to content

Commit 2c01eac

Browse files
committed
Socket的IO事件回调改为本地方法
1 parent ad715e3 commit 2c01eac

File tree

1 file changed

+68
-106
lines changed

1 file changed

+68
-106
lines changed

WebApiClient/Internal/SocketExtend.cs

Lines changed: 68 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,38 @@ static class SocketExtend
2323
/// <returns></returns>
2424
public static async Task ConnectTaskAsync(this Socket socket, EndPoint remoteEndPoint, TimeSpan? timeout)
2525
{
26-
if (remoteEndPoint == null)
27-
{
28-
throw new ArgumentNullException(nameof(remoteEndPoint));
29-
}
30-
31-
var token = new TaskSetter<object>(timeout);
32-
var e = new SocketAsyncEventArgs
33-
{
34-
RemoteEndPoint = remoteEndPoint,
35-
UserToken = token
36-
};
37-
38-
using (e)
26+
void OnEndConnect(object sender, SocketAsyncEventArgs e)
3927
{
40-
e.Completed += OnEndConnect;
41-
if (socket.ConnectAsync(e) == false)
28+
var setter = e.UserToken as TaskSetter<object>;
29+
if (e.SocketError == SocketError.Success)
30+
{
31+
setter.SetResult(null);
32+
}
33+
else
4234
{
43-
OnEndConnect(socket, e);
35+
var ex = new SocketException((int)e.SocketError);
36+
setter.SetException(ex);
4437
}
45-
await token.Task.ConfigureAwait(false);
4638
}
47-
}
4839

4940

50-
51-
/// <summary>
52-
/// 连接完成
53-
/// </summary>
54-
/// <param name="sender"></param>
55-
/// <param name="e"></param>
56-
private static void OnEndConnect(object sender, SocketAsyncEventArgs e)
57-
{
58-
var token = e.UserToken as TaskSetter<object>;
59-
if (e.SocketError == SocketError.Success)
41+
if (remoteEndPoint == null)
6042
{
61-
token.SetResult(null);
43+
throw new ArgumentNullException(nameof(remoteEndPoint));
6244
}
63-
else
45+
46+
var taskSetter = new TaskSetter<object>(timeout);
47+
using (var args = new SocketAsyncEventArgs())
6448
{
65-
var ex = new SocketException((int)e.SocketError);
66-
token.SetException(ex);
49+
args.UserToken = taskSetter;
50+
args.RemoteEndPoint = remoteEndPoint;
51+
args.Completed += OnEndConnect;
52+
53+
if (socket.ConnectAsync(args) == false)
54+
{
55+
OnEndConnect(socket, args);
56+
}
57+
await taskSetter.Task.ConfigureAwait(false);
6758
}
6859
}
6960

@@ -79,43 +70,35 @@ private static void OnEndConnect(object sender, SocketAsyncEventArgs e)
7970
/// <returns></returns>
8071
public static async Task<int> SendTaskAsync(this Socket socket, ArraySegment<byte> arraySegment, TimeSpan? timeout)
8172
{
82-
var token = new TaskSetter<int>(timeout);
83-
var e = new SocketAsyncEventArgs
73+
void OnEndSend(object sender, SocketAsyncEventArgs e)
8474
{
85-
UserToken = token
86-
};
75+
var setter = e.UserToken as TaskSetter<int>;
76+
if (e.SocketError == SocketError.Success)
77+
{
78+
setter.SetResult(e.BytesTransferred);
79+
}
80+
else
81+
{
82+
var ex = new SocketException((int)e.SocketError);
83+
setter.SetException(ex);
84+
}
85+
}
8786

88-
using (e)
87+
var taskSetter = new TaskSetter<int>(timeout);
88+
using (var args = new SocketAsyncEventArgs { UserToken = taskSetter })
8989
{
90-
e.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
91-
e.Completed += OnEndSend;
90+
args.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
91+
args.Completed += OnEndSend;
9292

93-
if (socket.SendAsync(e) == false)
93+
if (socket.SendAsync(args) == false)
9494
{
95-
OnEndSend(socket, e);
95+
OnEndSend(socket, args);
9696
}
97-
return await token.Task.ConfigureAwait(false);
97+
return await taskSetter.Task.ConfigureAwait(false);
9898
}
9999
}
100100

101-
/// <summary>
102-
/// 发送完成
103-
/// </summary>
104-
/// <param name="sender"></param>
105-
/// <param name="e"></param>
106-
private static void OnEndSend(object sender, SocketAsyncEventArgs e)
107-
{
108-
var token = e.UserToken as TaskSetter<int>;
109-
if (e.SocketError == SocketError.Success)
110-
{
111-
token.SetResult(e.BytesTransferred);
112-
}
113-
else
114-
{
115-
var ex = new SocketException((int)e.SocketError);
116-
token.SetException(ex);
117-
}
118-
}
101+
119102

120103
/// <summary>
121104
/// 异步接收
@@ -128,77 +111,64 @@ private static void OnEndSend(object sender, SocketAsyncEventArgs e)
128111
/// <returns></returns>
129112
public static async Task<int> ReceiveTaskAsync(this Socket socket, ArraySegment<byte> arraySegment, TimeSpan? timeout)
130113
{
131-
var token = new TaskSetter<int>(timeout);
132-
var e = new SocketAsyncEventArgs
114+
void OnEndReceive(object sender, SocketAsyncEventArgs e)
133115
{
134-
UserToken = token
135-
};
116+
var setter = e.UserToken as TaskSetter<int>;
117+
if (e.SocketError == SocketError.Success)
118+
{
119+
setter.SetResult(e.BytesTransferred);
120+
}
121+
else
122+
{
123+
var ex = new SocketException((int)e.SocketError);
124+
setter.SetException(ex);
125+
}
126+
}
136127

137-
using (e)
128+
var taskSetter = new TaskSetter<int>(timeout);
129+
using (var args = new SocketAsyncEventArgs { UserToken = taskSetter })
138130
{
139-
e.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
140-
e.Completed += OnEndSend;
131+
args.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
132+
args.Completed += OnEndReceive;
141133

142-
if (socket.ReceiveAsync(e) == false)
134+
if (socket.ReceiveAsync(args) == false)
143135
{
144-
OnEndReceive(socket, e);
136+
OnEndReceive(socket, args);
145137
}
146-
return await token.Task.ConfigureAwait(false);
138+
return await taskSetter.Task.ConfigureAwait(false);
147139
}
148140
}
149141

150-
/// <summary>
151-
/// 接收完成
152-
/// </summary>
153-
/// <param name="sender"></param>
154-
/// <param name="e"></param>
155-
private static void OnEndReceive(object sender, SocketAsyncEventArgs e)
156-
{
157-
var token = e.UserToken as TaskSetter<int>;
158-
if (e.SocketError == SocketError.Success)
159-
{
160-
token.SetResult(e.BytesTransferred);
161-
}
162-
else
163-
{
164-
var ex = new SocketException((int)e.SocketError);
165-
token.SetException(ex);
166-
}
167-
}
168142

169143
/// <summary>
170144
/// 表示任务行为
171145
/// </summary>
172146
/// <typeparam name="TResult">任务结果类型</typeparam>
173-
private class TaskSetter<TResult> : IDisposable
147+
private class TaskSetter<TResult>
174148
{
175149
/// <summary>
176-
/// 任务源
150+
/// 取消源
177151
/// </summary>
178-
private readonly TaskCompletionSource<TResult> taskSource;
152+
private readonly CancellationTokenSource tokenSource;
179153

180154
/// <summary>
181-
/// 取消源
155+
/// 任务源
182156
/// </summary>
183-
private readonly CancellationTokenSource tokenSource;
157+
private readonly TaskCompletionSource<TResult> taskSource = new TaskCompletionSource<TResult>();
184158

185159
/// <summary>
186160
/// 获取任务对象
187161
/// </summary>
188162
public Task<TResult> Task
189163
{
190-
get
191-
{
192-
return this.taskSource.Task;
193-
}
164+
get => this.taskSource.Task;
194165
}
195166

196167
/// <summary>
197168
/// 任务行为
198169
/// </summary>
199170
public TaskSetter(TimeSpan? timeout)
200171
{
201-
this.taskSource = new TaskCompletionSource<TResult>();
202172
if (timeout.HasValue == true)
203173
{
204174
this.tokenSource = new CancellationTokenSource();
@@ -228,14 +198,6 @@ public bool SetException(Exception ex)
228198
this.tokenSource?.Dispose();
229199
return this.taskSource.TrySetException(ex);
230200
}
231-
232-
/// <summary>
233-
/// 释放资源
234-
/// </summary>
235-
public void Dispose()
236-
{
237-
this.tokenSource?.Dispose();
238-
}
239201
}
240202
}
241203
}

0 commit comments

Comments
 (0)