Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 2656df9

Browse files
StephaneGrazianojustcoding121
authored andcommitted
- IsClosed doesn't solve the memleak (streams are still blocked inside SslStream), only throwing an exception can fix that behavior.
- BeginWrite was not correct, we should call this.WriteAsync instead of baseStream.WriteAsync rewritten
1 parent d663890 commit 2656df9

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/StreamExtended/Network/CustomBufferedStream.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ public bool FillBuffer()
471471
else
472472
{
473473
closed = true;
474+
throw new EndOfStreamException();
474475
}
475476

476477
return result;
@@ -514,6 +515,7 @@ public bool FillBuffer()
514515
else
515516
{
516517
closed = true;
518+
throw new EndOfStreamException();
517519
}
518520

519521
return result;
@@ -625,7 +627,7 @@ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, Asy
625627
{
626628
//use TaskExtended to pass State as AsyncObject
627629
//callback will call EndRead (otherwise, it will block)
628-
callback(new TaskResult<int>(pAsyncResult, state));
630+
callback?.Invoke(new TaskResult<int>(pAsyncResult, state));
629631
});
630632

631633
return vAsyncResult;
@@ -640,6 +642,7 @@ public override int EndRead(IAsyncResult asyncResult)
640642
return ((TaskResult<int>)asyncResult).Result;
641643
}
642644

645+
643646
/// <summary>
644647
/// Fix the .net bug with SslStream slow WriteAsync
645648
/// https://github.com/justcoding121/Titanium-Web-Proxy/issues/495
@@ -649,13 +652,20 @@ public override int EndRead(IAsyncResult asyncResult)
649652
/// <returns></returns>
650653
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
651654
{
652-
return baseStream.BeginWrite(buffer, offset, count, callback, state);
653-
}
655+
var vAsyncResult = this.WriteAsync(buffer, offset, count);
654656

657+
vAsyncResult.ContinueWith(pAsyncResult =>
658+
{
659+
callback?.Invoke(new TaskResult(pAsyncResult, state));
660+
});
661+
662+
return vAsyncResult;
663+
}
655664
public override void EndWrite(IAsyncResult asyncResult)
656665
{
657-
baseStream.EndWrite(asyncResult);
666+
((TaskResult)asyncResult).GetResult();
658667
}
668+
659669
#endif
660670
}
661671
}

src/StreamExtended/TaskExtended.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77

88
namespace StreamExtended
99
{
10+
/// <summary>
11+
/// Mimic a Task but you can set AsyncState
12+
/// </summary>
13+
/// <typeparam name="T"></typeparam>
14+
public class TaskResult : IAsyncResult
15+
{
16+
Task Task;
17+
object mAsyncState;
18+
19+
public TaskResult(Task pTask, object state)
20+
{
21+
Task = pTask;
22+
mAsyncState = state;
23+
}
24+
25+
public object AsyncState => mAsyncState;
26+
public WaitHandle AsyncWaitHandle => ((IAsyncResult)Task).AsyncWaitHandle;
27+
public bool CompletedSynchronously => ((IAsyncResult)Task).CompletedSynchronously;
28+
public bool IsCompleted => Task.IsCompleted;
29+
public void GetResult() { this.Task.GetAwaiter().GetResult(); }
30+
}
31+
1032
/// <summary>
1133
/// Mimic a Task<T> but you can set AsyncState
1234
/// </summary>
@@ -28,4 +50,6 @@ public TaskResult(Task<T> pTask, object state)
2850
public bool IsCompleted => Task.IsCompleted;
2951
public T Result => Task.Result;
3052
}
53+
54+
3155
}

0 commit comments

Comments
 (0)