forked from DarkActive/PVPNetConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeSslStream.cs
More file actions
93 lines (81 loc) · 2.43 KB
/
SafeSslStream.cs
File metadata and controls
93 lines (81 loc) · 2.43 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Security;
namespace PVPNetConnect
{
internal sealed class SafeSslStream
{
private readonly object _streamLock = new object();
private readonly SslStream _stream;
public SafeSslStream(SslStream stream)
{
_stream = stream;
}
public IAsyncResult BeginAuthenticateAsClient(string targetHost, AsyncCallback asyncCallback, object asyncState)
{
return _stream.BeginAuthenticateAsClient(targetHost, asyncCallback, asyncState);
}
public void EndAuthenticateAsClient(IAsyncResult asyncResult)
{
_stream.EndAuthenticateAsClient(asyncResult);
}
public int Read(byte[] buffer)
{
return Read(buffer, 0, buffer.Length);
}
public int Read(byte[] buffer, int offset, int count)
{
var state = new StateObject();
lock (_streamLock)
{
_stream.BeginRead(buffer, offset, count, ReadCallback, state);
}
state.Done.WaitOne();
return state.BytesRead;
}
public int ReadByte()
{
byte[] buffer = new byte[1];
var state = new StateObject();
lock (_streamLock)
{
_stream.BeginRead(buffer, 0, 1, ReadCallback, state);
}
state.Done.WaitOne();
return buffer[0];
}
public void Write(byte[] buffer)
{
Write(buffer, 0, buffer.Length);
}
public void Write(byte[] buffer, int offset, int count)
{
var state = new StateObject();
lock (_streamLock)
{
_stream.BeginWrite(buffer, offset, count, WriteCallback, state);
}
state.Done.WaitOne();
}
private void ReadCallback(IAsyncResult ar)
{
var state = (StateObject)ar.AsyncState;
lock (_streamLock)
{
state.BytesRead = _stream.EndRead(ar);
}
state.Done.Set();
}
private void WriteCallback(IAsyncResult ar)
{
var state = (StateObject)ar.AsyncState;
lock (_streamLock)
{
_stream.EndWrite(ar);
}
state.Done.Set();
}
}
}