This repository was archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathSocketExtensions.cs
More file actions
109 lines (104 loc) · 4.31 KB
/
SocketExtensions.cs
File metadata and controls
109 lines (104 loc) · 4.31 KB
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
// <copyright file="SocketExtensions.cs" company="The Android Open Source Project, Ryan Conrad, Quamotion">
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion. All rights reserved.
// </copyright>
namespace SharpAdbClient
{
using System;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Provides extension methods for the <see cref="Socket"/> class.
/// </summary>
internal static class SocketExtensions
{
/// <summary>
/// Asynchronously receives data from a connected socket.
/// </summary>
/// <param name="socket">
/// The socket from which to read data.
/// </param>
/// <param name="buffer">
/// An array of type <see cref="byte"/> that is the storage location for
/// the received data.
/// </param>
/// <param name="offset">
/// The zero-based position in the <paramref name="buffer"/> parameter at which to
/// start storing data.
/// </param>
/// <param name="size">
/// The number of bytes to receive.
/// </param>
/// <param name="socketFlags">
/// A bitwise combination of the <see cref="SocketFlags"/> values.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous task.
/// </param>
/// <remarks>
/// Cancelling the task will also close the socket.
/// </remarks>
/// <returns>
/// The number of bytes received.
/// </returns>
public static Task<int> ReceiveAsync(
this Socket socket,
byte[] buffer,
int offset,
int size,
SocketFlags socketFlags,
CancellationToken cancellationToken)
{
// Register a callback so that when a cancellation is requested, the socket is closed.
// This will cause an ObjectDisposedException to bubble up via TrySetResult, which we can catch
// and convert to a TaskCancelledException - which is the exception we expect.
var cancellationTokenRegistration = cancellationToken.Register(() => socket.Dispose());
ArraySegment<byte> array = new ArraySegment<byte>(buffer, offset, size);
return socket.ReceiveAsync(array, socketFlags);
}
/// <summary>
/// Asynchronously sends data to a connected socket.
/// </summary>
/// <param name="socket">
/// The socket from which to read data.
/// </param>
/// <param name="buffer">
/// An array of type <see cref="byte"/> that is the storage location for
/// the received data.
/// </param>
/// <param name="offset">
/// The zero-based position in the <paramref name="buffer"/> parameter at which to
/// start storing data.
/// </param>
/// <param name="size">
/// The number of bytes to receive.
/// </param>
/// <param name="socketFlags">
/// A bitwise combination of the <see cref="SocketFlags"/> values.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous task.
/// </param>
/// <remarks>
/// Cancelling the task will also close the socket.
/// </remarks>
/// <returns>
/// The number of bytes received.
/// </returns>
public static Task<int> SendAsync(
this Socket socket,
byte[] buffer,
int offset,
int size,
SocketFlags socketFlags,
CancellationToken cancellationToken)
{
// Register a callback so that when a cancellation is requested, the socket is closed.
// This will cause an ObjectDisposedException to bubble up via TrySetResult, which we can catch
// and convert to a TaskCancelledException - which is the exception we expect.
var cancellationTokenRegistration = cancellationToken.Register(() => socket.Dispose());
ArraySegment<byte> array = new ArraySegment<byte>(buffer, offset, size);
return socket.SendAsync(array, socketFlags);
}
}
}