Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Apr 12, 2019
2 parents be2094d + 1f9d6ee commit e7d203d
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .build/setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Install-DocFx()
{
if(!(Test-Path $env:ChocolateyInstall\lib\docfx\tools*))
{
choco install docfx
choco install docfx --version 2.40.1
}
$env:Path += ";$env:ChocolateyInstall\lib\docfx\tools"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.ClientHelloInfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class ClientHelloInfo
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.DefaultBufferPool.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class DefaultBufferPool
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.IBufferPool.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Interface IBufferPool
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Models.SslExtension.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class SslExtension
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Models.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Namespace StreamExtended.Models
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.CopyStream.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class CopyStream
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.CustomBufferedStream.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class CustomBufferedStream
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.DataEventArgs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class DataEventArgs
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.ICustomStreamReader.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Interface ICustomStreamReader
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.ICustomStreamWriter.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Interface ICustomStreamWriter
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.Network.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Namespace StreamExtended.Network
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.ServerHelloInfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class ServerHelloInfo
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.SslTools.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Class SslTools
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
2 changes: 1 addition & 1 deletion docs/api/StreamExtended.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="viewport" content="width=device-width">
<meta name="title" content="Namespace StreamExtended
| Stream Extended ">
<meta name="generator" content="docfx 2.39.2.0">
<meta name="generator" content="docfx 2.40.1.0">

<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" href="../styles/docfx.vendor.css">
Expand Down
65 changes: 51 additions & 14 deletions src/StreamExtended/Network/CustomBufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,8 @@ public override void Write(byte[] buffer, int offset, int count)
{
if (bufferLength > 0)
{

//WriteAsync has a bug in Net45
//See https://github.com/justcoding121/Titanium-Web-Proxy/issues/495
#if NET45
destination.Write(streamBuffer, bufferPos, bufferLength);
#else
await destination.WriteAsync(streamBuffer, bufferPos, bufferLength, cancellationToken);
#endif

bufferLength = 0;
}

Expand Down Expand Up @@ -293,14 +287,7 @@ public byte ReadByteFromBuffer()
{
OnDataWrite(buffer, offset, count);

//WriteAsync has a bug in Net45
//See https://github.com/justcoding121/Titanium-Web-Proxy/issues/495
#if NET45
baseStream.Write(buffer, offset, count);
await Task.FromResult(true);
#else
await baseStream.WriteAsync(buffer, offset, count, cancellationToken);
#endif
}

/// <summary>
Expand Down Expand Up @@ -592,5 +579,55 @@ private static void ResizeBuffer(ref byte[] buffer, long size)
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
buffer = newBuffer;
}

#if NET45

/// <summary>
/// Base Stream.BeginRead will call this.Read and block thread (we don't want this, Network stream handles async)
/// In order to really async Reading Launch this.ReadAsync as Task will fire NetworkStream.ReadAsync
/// See Threads here :
/// https://github.com/justcoding121/Stream-Extended/pull/43
/// https://github.com/justcoding121/Titanium-Web-Proxy/issues/575
/// </summary>
/// <returns></returns>
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
var vAsyncResult = this.ReadAsync(buffer, offset, count);

vAsyncResult.ContinueWith(pAsyncResult =>
{
//use TaskExtended to pass State as AsyncObject
//callback will call EndRead (otherwise, it will block)
callback(new TaskResult<int>(pAsyncResult, state));
});

return vAsyncResult;
}

/// <summary>
/// override EndRead to handle async Reading (see BeginRead comment)
/// </summary>
/// <returns></returns>
public override int EndRead(IAsyncResult asyncResult)
{
return ((TaskResult<int>)asyncResult).Result;
}

/// <summary>
/// Fix the .net bug with SslStream slow WriteAsync
/// https://github.com/justcoding121/Titanium-Web-Proxy/issues/495
/// Stream.BeginWrite + Stream.BeginRead uses the same SemaphoreSlim(1)
/// That's why we need to call NetworkStream.BeginWrite only (while read is waiting SemaphoreSlim)
/// </summary>
/// <returns></returns>
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return baseStream.BeginWrite(buffer, offset, count, callback, state);
}
public override void EndWrite(IAsyncResult asyncResult)
{
baseStream.EndWrite(asyncResult);
}
#endif
}
}
31 changes: 31 additions & 0 deletions src/StreamExtended/TaskExtended.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StreamExtended
{
/// <summary>
/// Mimic a Task<T> but you can set AsyncState
/// </summary>
/// <typeparam name="T"></typeparam>
public class TaskResult<T> : IAsyncResult
{
Task<T> Task;
object mAsyncState;

public TaskResult(Task<T> pTask, object state)
{
Task = pTask;
mAsyncState = state;
}

public object AsyncState => mAsyncState;
public WaitHandle AsyncWaitHandle => ((IAsyncResult)Task).AsyncWaitHandle;
public bool CompletedSynchronously => ((IAsyncResult)Task).CompletedSynchronously;
public bool IsCompleted => Task.IsCompleted;
public T Result => Task.Result;
}
}

0 comments on commit e7d203d

Please sign in to comment.