Adding UvPipeHandle

This commit is contained in:
Louis DeJardin 2015-07-23 12:51:31 -07:00
parent a457f6da25
commit ceeb4edabd
13 changed files with 237 additions and 53 deletions

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private readonly UvStreamHandle _socket;
private Frame _frame;
long _connectionId;
long _connectionId = 0;
public Connection(ListenerContext context, UvStreamHandle socket) : base(context)
{

View File

@ -4,7 +4,6 @@
using Microsoft.AspNet.Server.Kestrel.Networking;
using System;
using System.Threading;
using System.Threading;
namespace Microsoft.AspNet.Server.Kestrel.Http
{

View File

@ -58,6 +58,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public int Check(int statusCode)
{
var x = Marshal.GetLastWin32Error();
Exception error;
var result = Check(statusCode, out error);
if (error != null)
@ -85,7 +87,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_loop_init(UvLoopHandle a0);
uv_loop_init _uv_loop_init;
uv_loop_init _uv_loop_init = default(uv_loop_init);
public void loop_init(UvLoopHandle handle)
{
Check(_uv_loop_init(handle));
@ -93,7 +95,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_loop_close(IntPtr a0);
uv_loop_close _uv_loop_close;
uv_loop_close _uv_loop_close = default(uv_loop_close);
public void loop_close(UvLoopHandle handle)
{
handle.Validate(closed: true);
@ -102,7 +104,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_run(UvLoopHandle handle, int mode);
uv_run _uv_run;
uv_run _uv_run = default(uv_run);
public int run(UvLoopHandle handle, int mode)
{
handle.Validate();
@ -111,7 +113,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void uv_stop(UvLoopHandle handle);
uv_stop _uv_stop;
uv_stop _uv_stop = default(uv_stop);
public void stop(UvLoopHandle handle)
{
handle.Validate();
@ -120,7 +122,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void uv_ref(UvHandle handle);
uv_ref _uv_ref;
uv_ref _uv_ref = default(uv_ref);
public void @ref(UvHandle handle)
{
handle.Validate();
@ -129,7 +131,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void uv_unref(UvHandle handle);
uv_unref _uv_unref;
uv_unref _uv_unref = default(uv_unref);
public void unref(UvHandle handle)
{
handle.Validate();
@ -141,7 +143,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public delegate void uv_close_cb(IntPtr handle);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void uv_close(IntPtr handle, uv_close_cb close_cb);
uv_close _uv_close;
uv_close _uv_close = default(uv_close);
public void close(UvHandle handle, uv_close_cb close_cb)
{
handle.Validate(closed: true);
@ -156,7 +158,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public delegate void uv_async_cb(IntPtr handle);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb);
uv_async_init _uv_async_init;
uv_async_init _uv_async_init = default(uv_async_init);
public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
{
loop.Validate();
@ -166,7 +168,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_async_send(UvAsyncHandle handle);
uv_async_send _uv_async_send;
uv_async_send _uv_async_send = default(uv_async_send);
public void async_send(UvAsyncHandle handle)
{
Check(_uv_async_send(handle));
@ -174,7 +176,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_tcp_init(UvLoopHandle loop, UvTcpHandle handle);
uv_tcp_init _uv_tcp_init;
uv_tcp_init _uv_tcp_init = default(uv_tcp_init);
public void tcp_init(UvLoopHandle loop, UvTcpHandle handle)
{
loop.Validate();
@ -184,18 +186,46 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_tcp_bind(UvTcpHandle handle, ref sockaddr addr, int flags);
uv_tcp_bind _uv_tcp_bind;
uv_tcp_bind _uv_tcp_bind = default(uv_tcp_bind);
public void tcp_bind(UvTcpHandle handle, ref sockaddr addr, int flags)
{
handle.Validate();
Check(_uv_tcp_bind(handle, ref addr, flags));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_tcp_open(UvTcpHandle handle, IntPtr hSocket);
uv_tcp_open _uv_tcp_open = default(uv_tcp_open);
public void tcp_open(UvTcpHandle handle, IntPtr hSocket)
{
handle.Validate();
Check(_uv_tcp_open(handle, hSocket));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_pipe_init(UvLoopHandle loop, UvPipeHandle handle, int ipc);
uv_pipe_init _uv_pipe_init = default(uv_pipe_init);
public void pipe_init(UvLoopHandle loop, UvPipeHandle handle, bool ipc)
{
loop.Validate();
handle.Validate();
Check(_uv_pipe_init(loop, handle, ipc ? -1 : 0));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
delegate int uv_pipe_bind(UvPipeHandle loop, string name);
uv_pipe_bind _uv_pipe_bind = default(uv_pipe_bind);
public void pipe_bind(UvPipeHandle handle, string name)
{
handle.Validate();
Check(_uv_pipe_bind(handle, name));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_connection_cb(IntPtr server, int status);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_listen(UvStreamHandle handle, int backlog, uv_connection_cb cb);
uv_listen _uv_listen;
uv_listen _uv_listen = default(uv_listen);
public void listen(UvStreamHandle handle, int backlog, uv_connection_cb cb)
{
handle.Validate();
@ -204,7 +234,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_accept(UvStreamHandle server, UvStreamHandle client);
uv_accept _uv_accept;
uv_accept _uv_accept = default(uv_accept);
public void accept(UvStreamHandle server, UvStreamHandle client)
{
server.Validate();
@ -212,13 +242,25 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
Check(_uv_accept(server, client));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_connect_cb(IntPtr req, int status);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
unsafe delegate int uv_pipe_connect(UvConnectRequest req, UvPipeHandle handle, string name, uv_connect_cb cb);
uv_pipe_connect _uv_pipe_connect = default(uv_pipe_connect);
unsafe public void pipe_connect(UvConnectRequest req, UvPipeHandle handle, string name, uv_connect_cb cb)
{
req.Validate();
handle.Validate();
Check(_uv_pipe_connect(req, handle, name, cb));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_alloc_cb(IntPtr server, int suggested_size, out uv_buf_t buf);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_read_cb(IntPtr server, int nread, ref uv_buf_t buf);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
uv_read_start _uv_read_start;
uv_read_start _uv_read_start = default(uv_read_start);
public void read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
{
handle.Validate();
@ -227,7 +269,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_read_stop(UvStreamHandle handle);
uv_read_stop _uv_read_stop;
uv_read_stop _uv_read_stop = default(uv_read_stop);
public void read_stop(UvStreamHandle handle)
{
handle.Validate();
@ -236,7 +278,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_try_write(UvStreamHandle handle, Libuv.uv_buf_t[] bufs, int nbufs);
uv_try_write _uv_try_write;
uv_try_write _uv_try_write = default(uv_try_write);
public int try_write(UvStreamHandle handle, Libuv.uv_buf_t[] bufs, int nbufs)
{
handle.Validate();
@ -246,20 +288,30 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_write_cb(IntPtr req, int status);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate int uv_write(UvWriteReq req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, uv_write_cb cb);
uv_write _uv_write;
unsafe public void write(UvWriteReq req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, uv_write_cb cb)
unsafe delegate int uv_write(UvRequest req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, uv_write_cb cb);
uv_write _uv_write = default(uv_write);
unsafe public void write(UvRequest req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, uv_write_cb cb)
{
req.Validate();
handle.Validate();
Check(_uv_write(req, handle, bufs, nbufs, cb));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate int uv_write2(UvRequest req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb);
uv_write2 _uv_write2 = default(uv_write2);
unsafe public void write2(UvRequest req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb)
{
req.Validate();
handle.Validate();
Check(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb));
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void uv_shutdown_cb(IntPtr req, int status);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb);
uv_shutdown _uv_shutdown;
uv_shutdown _uv_shutdown = default(uv_shutdown);
public void shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb)
{
req.Validate();
@ -269,7 +321,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr uv_err_name(int err);
uv_err_name _uv_err_name;
uv_err_name _uv_err_name = default(uv_err_name);
public unsafe String err_name(int err)
{
IntPtr ptr = _uv_err_name(err);
@ -278,7 +330,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr uv_strerror(int err);
uv_strerror _uv_strerror;
uv_strerror _uv_strerror = default(uv_strerror);
public unsafe String strerror(int err)
{
IntPtr ptr = _uv_strerror(err);
@ -287,7 +339,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_loop_size();
uv_loop_size _uv_loop_size;
uv_loop_size _uv_loop_size = default(uv_loop_size);
public int loop_size()
{
return _uv_loop_size();
@ -295,7 +347,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_handle_size(HandleType handleType);
uv_handle_size _uv_handle_size;
uv_handle_size _uv_handle_size = default(uv_handle_size);
public int handle_size(HandleType handleType)
{
return _uv_handle_size(handleType);
@ -303,7 +355,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_req_size(RequestType reqType);
uv_req_size _uv_req_size;
uv_req_size _uv_req_size = default(uv_req_size);
public int req_size(RequestType reqType)
{
return _uv_req_size(reqType);
@ -312,7 +364,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_ip4_addr(string ip, int port, out sockaddr addr);
uv_ip4_addr _uv_ip4_addr;
uv_ip4_addr _uv_ip4_addr = default(uv_ip4_addr);
public int ip4_addr(string ip, int port, out sockaddr addr, out Exception error)
{
return Check(_uv_ip4_addr(ip, port, out addr), out error);
@ -321,7 +373,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int uv_ip6_addr(string ip, int port, out sockaddr addr);
uv_ip6_addr _uv_ip6_addr;
uv_ip6_addr _uv_ip6_addr = default(uv_ip6_addr);
public int ip6_addr(string ip, int port, out sockaddr addr, out Exception error)
{
return Check(_uv_ip6_addr(ip, port, out addr), out error);
@ -331,7 +383,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public delegate void uv_walk_cb(IntPtr handle, IntPtr arg);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate int uv_walk(UvLoopHandle loop, uv_walk_cb walk_cb, IntPtr arg);
uv_walk _uv_walk;
uv_walk _uv_walk = default(uv_walk);
unsafe public void walk(UvLoopHandle loop, uv_walk_cb walk_cb, IntPtr arg)
{
loop.Validate();
@ -345,6 +397,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public struct sockaddr
{
public sockaddr(long ignored) { x3 = x0 = x1 = x2 = x3 = 0; }
long x0;
long x1;
long x2;

View File

@ -0,0 +1,70 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Microsoft.AspNet.Server.Kestrel.Networking
{
/// <summary>
/// Summary description for UvWriteRequest
/// </summary>
public class UvConnectRequest : UvRequest
{
private readonly static Libuv.uv_connect_cb _uv_connect_cb = UvConnectCb;
Action<UvConnectRequest, int, Exception, object> _callback;
object _state;
public void Init(UvLoopHandle loop)
{
var requestSize = loop.Libuv.req_size(Libuv.RequestType.CONNECT);
CreateMemory(
loop.Libuv,
loop.ThreadId,
requestSize);
}
public void Connect(
UvPipeHandle pipe,
string name,
Action<UvConnectRequest, int, Exception, object> callback,
object state)
{
_callback = callback;
_state = state;
Pin();
Libuv.pipe_connect(this, pipe, name, _uv_connect_cb);
}
private static void UvConnectCb(IntPtr ptr, int status)
{
var req = FromIntPtr<UvConnectRequest>(ptr);
req.Unpin();
var callback = req._callback;
req._callback = null;
var state = req._state;
req._state = null;
Exception error = null;
if (status < 0)
{
req.Libuv.Check(status, out error);
}
try
{
callback(req, status, error, state);
}
catch (Exception ex)
{
Trace.WriteLine("UvConnectRequest " + ex.ToString());
}
}
}
}

View File

@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net;
namespace Microsoft.AspNet.Server.Kestrel.Networking
{
public class UvPipeHandle : UvStreamHandle
{
public void Init(UvLoopHandle loop, bool ipc)
{
CreateMemory(
loop.Libuv,
loop.ThreadId,
loop.Libuv.handle_size(Libuv.HandleType.NAMED_PIPE));
_uv.pipe_init(loop, this, ipc);
}
public void Init(UvLoopHandle loop, Action<Action<IntPtr>, IntPtr> queueCloseHandle)
{
CreateHandle(
loop.Libuv,
loop.ThreadId,
loop.Libuv.handle_size(Libuv.HandleType.TCP), queueCloseHandle);
_uv.pipe_init(loop, this, false);
}
public void Bind(string name)
{
_uv.pipe_bind(this, name);
}
//public void Open(IntPtr hSocket)
//{
//}
}
}

View File

@ -1,17 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Server.Kestrel.Networking
{
public abstract class UvReq : UvMemory
{
protected override bool ReleaseHandle()
{
DestroyMemory(handle);
handle = IntPtr.Zero;
return true;
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Server.Kestrel.Networking
{
public class UvRequest : UvMemory
{
GCHandle _pin;
protected override bool ReleaseHandle()
{
DestroyMemory(handle);
handle = IntPtr.Zero;
return true;
}
public virtual void Pin()
{
_pin = GCHandle.Alloc(this, GCHandleType.Normal);
}
public virtual void Unpin()
{
_pin.Free();
}
}
}

View File

@ -9,13 +9,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
/// <summary>
/// Summary description for UvShutdownRequest
/// </summary>
public class UvShutdownReq : UvReq
public class UvShutdownReq : UvRequest
{
private readonly static Libuv.uv_shutdown_cb _uv_shutdown_cb = UvShutdownCb;
Action<UvShutdownReq, int, object> _callback;
object _state;
GCHandle _pin;
public void Init(UvLoopHandle loop)
{
@ -29,14 +28,14 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{
_callback = callback;
_state = state;
_pin = GCHandle.Alloc(this, GCHandleType.Normal);
Pin();
_uv.shutdown(this, handle, _uv_shutdown_cb);
}
private static void UvShutdownCb(IntPtr ptr, int status)
{
var req = FromIntPtr<UvShutdownReq>(ptr);
req._pin.Free();
req.Unpin();
req._callback(req, status, req._state);
req._callback = null;
req._state = null;

View File

@ -48,5 +48,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
_uv.tcp_bind(this, ref addr, 0);
}
public void Open(IntPtr hSocket)
{
_uv.tcp_open(this, hSocket);
}
}
}

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
/// <summary>
/// Summary description for UvWriteRequest
/// </summary>
public class UvWriteReq : UvReq
public class UvWriteReq : UvRequest
{
private readonly static Libuv.uv_write_cb _uv_write_cb = UvWriteCb;
@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public void Init(UvLoopHandle loop)
{
var requestSize = loop.Libuv.req_size(Libuv.RequestType.WRITE);
var bufferSize = Marshal.SizeOf(typeof(Libuv.uv_buf_t)) * BUFFER_COUNT;
var bufferSize = Marshal.SizeOf<Libuv.uv_buf_t>() * BUFFER_COUNT;
CreateMemory(
loop.Libuv,
loop.ThreadId,

View File

@ -14,6 +14,9 @@
}
}
},
"compilationOptions": {
"allowUnsafe": true
},
"commands": {
"run": "xunit.runner.aspnet",
"test": "xunit.runner.aspnet"