forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketHelpers.cs
More file actions
102 lines (87 loc) · 3.39 KB
/
SocketHelpers.cs
File metadata and controls
102 lines (87 loc) · 3.39 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.ClearScript.Util
{
internal static class SocketHelpers
{
public static Task SendStringAsync(this Socket socket, string value, Encoding encoding = null)
{
return socket.SendBytesAsync((encoding ?? Encoding.UTF8).GetBytes(value));
}
public static async Task<string> ReceiveLineAsync(this Socket socket, Encoding encoding = null)
{
var lineBytes = new List<byte>(1024);
var bytes = new byte[1];
while (true)
{
await socket.ReceiveBytesAsync(bytes, 0, 1).ConfigureAwait(false);
var lastIndex = lineBytes.Count - 1;
if ((lastIndex >= 0) && (lineBytes[lastIndex] == Convert.ToByte('\r')) && (bytes[0] == Convert.ToByte('\n')))
{
lineBytes.RemoveAt(lastIndex);
break;
}
lineBytes.Add(bytes[0]);
}
return (encoding ?? Encoding.UTF8).GetString(lineBytes.ToArray());
}
public static Task SendBytesAsync(this Socket socket, byte[] bytes)
{
return socket.SendBytesAsync(bytes, 0, bytes.Length);
}
public static async Task SendBytesAsync(this Socket socket, byte[] bytes, int offset, int count)
{
while (count > 0)
{
var sentCount = await socket.SendAsync(bytes, offset, count).ConfigureAwait(false);
if (sentCount < 1)
{
throw new IOException("Failed to send data to socket");
}
offset += sentCount;
count -= sentCount;
}
}
public static async Task<byte[]> ReceiveBytesAsync(this Socket socket, int count)
{
var bytes = new byte[count];
await socket.ReceiveBytesAsync(bytes, 0, count).ConfigureAwait(false);
return bytes;
}
public static async Task ReceiveBytesAsync(this Socket socket, byte[] bytes, int offset, int count)
{
while (count > 0)
{
var receivedCount = await socket.ReceiveAsync(bytes, offset, count).ConfigureAwait(false);
if (receivedCount < 1)
{
throw new IOException("Failed to receive data from socket");
}
offset += receivedCount;
count -= receivedCount;
}
}
private static Task<int> SendAsync(this Socket socket, byte[] bytes, int offset, int count)
{
return Task<int>.Factory.FromAsync(
(callback, state) => socket.BeginSend(bytes, offset, count, SocketFlags.None, callback, state),
socket.EndSend,
null
);
}
private static Task<int> ReceiveAsync(this Socket socket, byte[] bytes, int offset, int count)
{
return Task<int>.Factory.FromAsync(
(callback, state) => socket.BeginReceive(bytes, offset, count, SocketFlags.None, callback, state),
socket.EndReceive,
null
);
}
}
}