-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathNWWebSocketResponse.cs
More file actions
94 lines (74 loc) · 3.84 KB
/
NWWebSocketResponse.cs
File metadata and controls
94 lines (74 loc) · 3.84 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
//
// NWWebSocketResponse.cs: Bindings the Network nw_browser_t API.
//
// Authors:
// Manuel de la Pena (mandel@microsoft.com)
//
// Copyrigh 2019 Microsoft Inc
//
#nullable enable
using System.Runtime.CompilerServices;
using CoreFoundation;
using OS_nw_ws_response = System.IntPtr;
namespace Network {
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
public class NWWebSocketResponse : NativeObject {
[Preserve (Conditional = true)]
internal NWWebSocketResponse (NativeHandle handle, bool owns) : base (handle, owns) { }
[DllImport (Constants.NetworkLibrary, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern unsafe OS_nw_ws_response nw_ws_response_create (NWWebSocketResponseStatus status, IntPtr selected_subprotocol);
static unsafe OS_nw_ws_response nw_ws_response_create (NWWebSocketResponseStatus status, string selected_subprotocol)
{
using var selected_subprotocolPtr = new TransientString (selected_subprotocol);
return nw_ws_response_create (status, selected_subprotocolPtr);
}
public NWWebSocketResponse (NWWebSocketResponseStatus status, string subprotocol)
=> InitializeHandle (nw_ws_response_create (status, subprotocol));
[DllImport (Constants.NetworkLibrary, EntryPoint = "nw_ws_response_get_selected_subprotocol", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr nw_ws_response_get_selected_subprotocol_ptr (OS_nw_ws_response response);
static string nw_ws_response_get_selected_subprotocol (OS_nw_ws_response response)
{
var ptr = nw_ws_response_get_selected_subprotocol_ptr (response);
return TransientString.ToStringAndFree (ptr, TransientString.Encoding.Ansi)!;
}
public string SelectedSubprotocol => nw_ws_response_get_selected_subprotocol (GetCheckedHandle ());
[DllImport (Constants.NetworkLibrary)]
static extern NWWebSocketResponseStatus nw_ws_response_get_status (OS_nw_ws_response response);
public NWWebSocketResponseStatus Status => nw_ws_response_get_status (GetCheckedHandle ());
[DllImport (Constants.NetworkLibrary)]
unsafe static extern byte nw_ws_response_enumerate_additional_headers (OS_nw_ws_response response, BlockLiteral* enumerator);
[UnmanagedCallersOnly]
static void TrampolineEnumerateHeadersHandler (IntPtr block, IntPtr headerPointer, IntPtr valuePointer)
{
var del = BlockLiteral.GetTarget<Action<string?, string?>> (block);
if (del is not null) {
var header = Marshal.PtrToStringAuto (headerPointer);
var value = Marshal.PtrToStringAuto (valuePointer);
del (header, value);
}
}
[BindingImpl (BindingImplOptions.Optimizable)]
public bool EnumerateAdditionalHeaders (Action<string?, string?> handler)
{
if (handler is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (handler));
unsafe {
delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> trampoline = &TrampolineEnumerateHeadersHandler;
using var block = new BlockLiteral (trampoline, handler, typeof (NWWebSocketResponse), nameof (TrampolineEnumerateHeadersHandler));
return nw_ws_response_enumerate_additional_headers (GetCheckedHandle (), &block) != 0;
}
}
[DllImport (Constants.NetworkLibrary, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void nw_ws_response_add_additional_header (OS_nw_ws_response response, IntPtr name, IntPtr value);
static void nw_ws_response_add_additional_header (OS_nw_ws_response response, string name, string value)
{
using var namePtr = new TransientString (name);
using var valuePtr = new TransientString (value);
nw_ws_response_add_additional_header (response, name, value);
}
public void SetHeader (string header, string value) => nw_ws_response_add_additional_header (GetCheckedHandle (), header, value);
}
}