This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 523
[Wip] Differentiate write and write2 + simplify #526
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/Microsoft.AspNet.Server.Kestrel/Networking/UvWriteReq2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// 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.Runtime.InteropServices; | ||
using Microsoft.AspNet.Server.Kestrel.Infrastructure; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNet.Server.Kestrel.Networking | ||
{ | ||
/// <summary> | ||
/// Summary description for UvWriteRequest2 | ||
/// </summary> | ||
public class UvWriteReq2 : UvRequest | ||
{ | ||
private readonly static Libuv.uv_write_cb _uv_write2_cb = (IntPtr ptr, int status) => UvWrite2Cb(ptr, status); | ||
|
||
private IntPtr _bufs; | ||
|
||
private Action<UvWriteReq2, int, Exception, object> _callback; | ||
private object _state; | ||
private const int BUFFER_COUNT = 1; | ||
|
||
private GCHandle _pinUvWrite2Req; | ||
private GCHandle _pinBuffer; | ||
private bool _bufferIsPinned; | ||
|
||
public UvWriteReq2(IKestrelTrace logger) : base(logger) | ||
{ | ||
} | ||
|
||
public void Init(UvLoopHandle loop) | ||
{ | ||
var requestSize = loop.Libuv.req_size(Libuv.RequestType.WRITE); | ||
var bufferSize = Marshal.SizeOf<Libuv.uv_buf_t>() * BUFFER_COUNT; | ||
CreateMemory( | ||
loop.Libuv, | ||
loop.ThreadId, | ||
requestSize + bufferSize); | ||
_bufs = handle + requestSize; | ||
} | ||
|
||
public unsafe void Write2( | ||
UvStreamHandle handle, | ||
ArraySegment<byte> buf, | ||
UvStreamHandle sendHandle, | ||
Action<UvWriteReq2, int, Exception, object> callback, | ||
object state) | ||
{ | ||
try | ||
{ | ||
// add GCHandle to keeps this SafeHandle alive while request processing | ||
_pinUvWrite2Req = GCHandle.Alloc(this, GCHandleType.Normal); | ||
|
||
var pBuffers = (Libuv.uv_buf_t*)_bufs; | ||
_pinBuffer = GCHandle.Alloc(buf.Array, GCHandleType.Pinned); | ||
_bufferIsPinned = true; | ||
|
||
pBuffers[0] = Libuv.buf_init( | ||
_pinBuffer.AddrOfPinnedObject() + buf.Offset, | ||
buf.Count); | ||
|
||
_callback = callback; | ||
_state = state; | ||
_uv.write2(this, handle, pBuffers, BUFFER_COUNT, sendHandle, _uv_write2_cb); | ||
} | ||
catch | ||
{ | ||
_callback = null; | ||
_state = null; | ||
Unpin(this); | ||
throw; | ||
} | ||
} | ||
|
||
private static void Unpin(UvWriteReq2 req) | ||
{ | ||
req._pinUvWrite2Req.Free(); | ||
if (req._bufferIsPinned) | ||
{ | ||
req._pinBuffer.Free(); | ||
req._bufferIsPinned = false; | ||
} | ||
} | ||
|
||
private static void UvWrite2Cb(IntPtr ptr, int status) | ||
{ | ||
var req = FromIntPtr<UvWriteReq2>(ptr); | ||
Unpin(req); | ||
|
||
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) | ||
{ | ||
req._log.LogError("UvWrite2Cb", ex); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would be better to put the dummy buffer in this class and use a static
pBuffers
object. Better encapsulation probably.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better yet. Create the dummy buffer in this class using
CreateMemory
. This way the onlyGCHandle
we have to bother with is_pinUvWrite2Req
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made change for this but can't test due to dotnet/aspnetcore#1207
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benaadams Do you still have this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, though will have to add it to PR after Saturday (Playtest this weekend so focused on that)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. Good luck with the playtest!