Refactored WebSocket.cs
This commit is contained in:
parent
e7c9b19bf0
commit
a18993f2be
@ -261,12 +261,11 @@ namespace WebSocketSharp
|
|||||||
/// </value>
|
/// </value>
|
||||||
public IEnumerable<Cookie> Cookies {
|
public IEnumerable<Cookie> Cookies {
|
||||||
get {
|
get {
|
||||||
lock (_cookies.SyncRoot) {
|
lock (_cookies.SyncRoot)
|
||||||
foreach (Cookie cookie in _cookies)
|
foreach (Cookie cookie in _cookies)
|
||||||
yield return cookie;
|
yield return cookie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the credentials for the HTTP authentication (Basic/Digest).
|
/// Gets the credentials for the HTTP authentication (Basic/Digest).
|
||||||
@ -606,13 +605,13 @@ namespace WebSocketSharp
|
|||||||
if (extensions != null && extensions.Length > 0)
|
if (extensions != null && extensions.Length > 0)
|
||||||
acceptSecWebSocketExtensionsHeader (extensions);
|
acceptSecWebSocketExtensionsHeader (extensions);
|
||||||
|
|
||||||
return send (createHandshakeResponse ());
|
return sendHandshakeResponse (createHandshakeResponse ());
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool acceptPingFrame (WebSocketFrame frame)
|
private bool acceptPingFrame (WebSocketFrame frame)
|
||||||
{
|
{
|
||||||
var mask = _client ? Mask.Mask : Mask.Unmask;
|
var mask = _client ? Mask.Mask : Mask.Unmask;
|
||||||
if (send (WebSocketFrame.CreatePongFrame (mask, frame.PayloadData)))
|
if (sendWebSocketFrame (WebSocketFrame.CreatePongFrame (mask, frame.PayloadData)))
|
||||||
_logger.Trace ("Returned a Pong.");
|
_logger.Trace ("Returned a Pong.");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -700,7 +699,7 @@ namespace WebSocketSharp
|
|||||||
var headers = response.Headers;
|
var headers = response.Headers;
|
||||||
return response.IsUnauthorized
|
return response.IsUnauthorized
|
||||||
? String.Format (
|
? String.Format (
|
||||||
"HTTP {0} authorization is required.", response.AuthenticationChallenge.Scheme)
|
"HTTP {0} authentication is required.", response.AuthenticationChallenge.Scheme)
|
||||||
: !response.IsWebSocketResponse
|
: !response.IsWebSocketResponse
|
||||||
? "Not WebSocket connection response."
|
? "Not WebSocket connection response."
|
||||||
: !validateSecWebSocketAcceptHeader (headers["Sec-WebSocket-Accept"])
|
: !validateSecWebSocketAcceptHeader (headers["Sec-WebSocket-Accept"])
|
||||||
@ -776,11 +775,12 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool closeHandshake (byte [] frame, int timeout, Action release)
|
private bool closeHandshake (byte[] data, int millisecondsTimeout, Action release)
|
||||||
{
|
{
|
||||||
var sent = frame != null && _stream.WriteBytes (frame);
|
var sent = data != null && _stream.WriteBytes (data);
|
||||||
var received = timeout == 0 ||
|
var received =
|
||||||
(sent && _exitReceiving != null && _exitReceiving.WaitOne (timeout));
|
millisecondsTimeout == 0 ||
|
||||||
|
(sent && _exitReceiving != null && _exitReceiving.WaitOne (millisecondsTimeout));
|
||||||
|
|
||||||
release ();
|
release ();
|
||||||
if (_receivePong != null) {
|
if (_receivePong != null) {
|
||||||
@ -1045,57 +1045,6 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// As client
|
|
||||||
private HttpResponse receiveHandshakeResponse ()
|
|
||||||
{
|
|
||||||
var res = _stream.ReadHttpResponse (90000);
|
|
||||||
_logger.Debug ("A response to this WebSocket connection request:\n" + res.ToString ());
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool send (byte [] frame)
|
|
||||||
{
|
|
||||||
lock (_forConn) {
|
|
||||||
if (_readyState != WebSocketState.Open) {
|
|
||||||
_logger.Warn ("Sending has been interrupted.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _stream.WriteBytes (frame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// As client
|
|
||||||
private void send (HttpRequest request)
|
|
||||||
{
|
|
||||||
_logger.Debug (
|
|
||||||
String.Format ("A WebSocket connection request to {0}:\n{1}", _uri, request));
|
|
||||||
|
|
||||||
_stream.WriteBytes (request.ToByteArray ());
|
|
||||||
}
|
|
||||||
|
|
||||||
// As server
|
|
||||||
private bool send (HttpResponse response)
|
|
||||||
{
|
|
||||||
_logger.Debug (
|
|
||||||
"A response to the WebSocket connection request:\n" + response.ToString ());
|
|
||||||
|
|
||||||
return _stream.WriteBytes (response.ToByteArray ());
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool send (WebSocketFrame frame)
|
|
||||||
{
|
|
||||||
lock (_forConn) {
|
|
||||||
if (_readyState != WebSocketState.Open) {
|
|
||||||
_logger.Warn ("Sending has been interrupted.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _stream.WriteBytes (frame.ToByteArray ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool send (Opcode opcode, byte[] data)
|
private bool send (Opcode opcode, byte[] data)
|
||||||
{
|
{
|
||||||
lock (_forSend) {
|
lock (_forSend) {
|
||||||
@ -1108,7 +1057,8 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
var mask = _client ? Mask.Mask : Mask.Unmask;
|
var mask = _client ? Mask.Mask : Mask.Unmask;
|
||||||
sent = send (WebSocketFrame.CreateFrame (Fin.Final, opcode, mask, data, compressed));
|
sent = sendWebSocketFrame (
|
||||||
|
WebSocketFrame.CreateFrame (Fin.Final, opcode, mask, data, compressed));
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
_logger.Fatal (ex.ToString ());
|
_logger.Fatal (ex.ToString ());
|
||||||
@ -1196,43 +1146,47 @@ namespace WebSocketSharp
|
|||||||
var rem = (int) (len % FragmentLength);
|
var rem = (int) (len % FragmentLength);
|
||||||
var times = rem == 0 ? quo - 2 : quo - 1;
|
var times = rem == 0 ? quo - 2 : quo - 1;
|
||||||
|
|
||||||
byte [] buffer = null;
|
byte[] buff = null;
|
||||||
|
|
||||||
// Not fragmented
|
// Not fragmented
|
||||||
if (quo == 0) {
|
if (quo == 0) {
|
||||||
buffer = new byte [rem];
|
buff = new byte[rem];
|
||||||
return stream.Read (buffer, 0, rem) == rem &&
|
return stream.Read (buff, 0, rem) == rem &&
|
||||||
send (WebSocketFrame.CreateFrame (Fin.Final, opcode, mask, buffer, compressed));
|
sendWebSocketFrame (
|
||||||
|
WebSocketFrame.CreateFrame (Fin.Final, opcode, mask, buff, compressed));
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = new byte [FragmentLength];
|
buff = new byte[FragmentLength];
|
||||||
|
|
||||||
// First
|
// First
|
||||||
if (stream.Read (buffer, 0, FragmentLength) != FragmentLength ||
|
if (stream.Read (buff, 0, FragmentLength) != FragmentLength ||
|
||||||
!send (WebSocketFrame.CreateFrame (Fin.More, opcode, mask, buffer, compressed)))
|
!sendWebSocketFrame (
|
||||||
|
WebSocketFrame.CreateFrame (Fin.More, opcode, mask, buff, compressed)))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Mid
|
// Mid
|
||||||
for (long i = 0; i < times; i++) {
|
for (long i = 0; i < times; i++) {
|
||||||
if (stream.Read (buffer, 0, FragmentLength) != FragmentLength ||
|
if (stream.Read (buff, 0, FragmentLength) != FragmentLength ||
|
||||||
!send (WebSocketFrame.CreateFrame (Fin.More, Opcode.Cont, mask, buffer, compressed)))
|
!sendWebSocketFrame (
|
||||||
|
WebSocketFrame.CreateFrame (Fin.More, Opcode.Cont, mask, buff, compressed)))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final
|
// Final
|
||||||
var tmpLen = FragmentLength;
|
var tmpLen = FragmentLength;
|
||||||
if (rem != 0)
|
if (rem != 0)
|
||||||
buffer = new byte [tmpLen = rem];
|
buff = new byte[tmpLen = rem];
|
||||||
|
|
||||||
return stream.Read (buffer, 0, tmpLen) == tmpLen &&
|
return stream.Read (buff, 0, tmpLen) == tmpLen &&
|
||||||
send (WebSocketFrame.CreateFrame (Fin.Final, Opcode.Cont, mask, buffer, compressed));
|
sendWebSocketFrame (
|
||||||
|
WebSocketFrame.CreateFrame (Fin.Final, Opcode.Cont, mask, buff, compressed));
|
||||||
}
|
}
|
||||||
|
|
||||||
// As client
|
// As client
|
||||||
private HttpResponse sendHandshakeRequest ()
|
private HttpResponse sendHandshakeRequest ()
|
||||||
{
|
{
|
||||||
var req = createHandshakeRequest ();
|
var req = createHandshakeRequest ();
|
||||||
var res = sendHandshakeRequest (req);
|
var res = sendHandshakeRequest (req, 90000);
|
||||||
if (res.IsUnauthorized) {
|
if (res.IsUnauthorized) {
|
||||||
_authChallenge = res.AuthenticationChallenge;
|
_authChallenge = res.AuthenticationChallenge;
|
||||||
if (_credentials != null &&
|
if (_credentials != null &&
|
||||||
@ -1245,7 +1199,7 @@ namespace WebSocketSharp
|
|||||||
var authRes = new AuthenticationResponse (_authChallenge, _credentials, _nonceCount);
|
var authRes = new AuthenticationResponse (_authChallenge, _credentials, _nonceCount);
|
||||||
_nonceCount = authRes.NonceCount;
|
_nonceCount = authRes.NonceCount;
|
||||||
req.Headers["Authorization"] = authRes.ToString ();
|
req.Headers["Authorization"] = authRes.ToString ();
|
||||||
res = sendHandshakeRequest (req);
|
res = sendHandshakeRequest (req, 15000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1253,17 +1207,60 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// As client
|
// As client
|
||||||
private HttpResponse sendHandshakeRequest (HttpRequest request)
|
private HttpResponse sendHandshakeRequest (HttpRequest request, int millisecondsTimeout)
|
||||||
{
|
{
|
||||||
send (request);
|
_logger.Debug (
|
||||||
return receiveHandshakeResponse ();
|
String.Format ("A WebSocket connection request to {0}:\n{1}", _uri, request));
|
||||||
|
|
||||||
|
var res = _stream.SendHttpRequest (request, millisecondsTimeout);
|
||||||
|
_logger.Debug ("A response to this WebSocket connection request:\n" + res.ToString ());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// As server
|
||||||
|
private bool sendHandshakeResponse (HttpResponse response)
|
||||||
|
{
|
||||||
|
_logger.Debug (
|
||||||
|
"A response to the WebSocket connection request:\n" + response.ToString ());
|
||||||
|
|
||||||
|
return _stream.WriteHttp (response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool sendWebSocketFrame (byte[] data)
|
||||||
|
{
|
||||||
|
lock (_forConn) {
|
||||||
|
if (_readyState != WebSocketState.Open) {
|
||||||
|
_logger.Warn ("Sending has been interrupted.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _stream.WriteBytes (data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool sendWebSocketFrame (WebSocketFrame frame)
|
||||||
|
{
|
||||||
|
lock (_forConn) {
|
||||||
|
if (_readyState != WebSocketState.Open) {
|
||||||
|
_logger.Warn ("Sending has been interrupted.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _stream.WriteWebSocketFrame (frame);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// As client
|
// As client
|
||||||
private void setClientStream ()
|
private void setClientStream ()
|
||||||
{
|
{
|
||||||
|
var proxy = _proxyUri != null;
|
||||||
|
_tcpClient = proxy
|
||||||
|
? new TcpClient (_proxyUri.DnsSafeHost, _proxyUri.Port)
|
||||||
|
: new TcpClient (_uri.DnsSafeHost, _uri.Port);
|
||||||
|
|
||||||
_stream = WebSocketStream.CreateClientStream (
|
_stream = WebSocketStream.CreateClientStream (
|
||||||
_uri, _proxyUri, _proxyCredentials, _secure, _certValidationCallback, out _tcpClient);
|
_tcpClient, proxy, _uri, _proxyCredentials, _secure, _certValidationCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startReceiving ()
|
private void startReceiving ()
|
||||||
@ -1376,7 +1373,7 @@ namespace WebSocketSharp
|
|||||||
{
|
{
|
||||||
_readyState = WebSocketState.Closing;
|
_readyState = WebSocketState.Closing;
|
||||||
|
|
||||||
send (response);
|
sendHandshakeResponse (response);
|
||||||
closeServerResources ();
|
closeServerResources ();
|
||||||
|
|
||||||
_readyState = WebSocketState.Closed;
|
_readyState = WebSocketState.Closed;
|
||||||
@ -1389,7 +1386,7 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// As server
|
// As server
|
||||||
internal void Close (CloseEventArgs e, byte [] frame, int timeout)
|
internal void Close (CloseEventArgs e, byte[] data, int millisecondsTimeout)
|
||||||
{
|
{
|
||||||
lock (_forConn) {
|
lock (_forConn) {
|
||||||
if (_readyState == WebSocketState.Closing || _readyState == WebSocketState.Closed) {
|
if (_readyState == WebSocketState.Closing || _readyState == WebSocketState.Closed) {
|
||||||
@ -1400,7 +1397,7 @@ namespace WebSocketSharp
|
|||||||
_readyState = WebSocketState.Closing;
|
_readyState = WebSocketState.Closing;
|
||||||
}
|
}
|
||||||
|
|
||||||
e.WasClean = closeHandshake (frame, timeout, closeServerResources);
|
e.WasClean = closeHandshake (data, millisecondsTimeout, closeServerResources);
|
||||||
|
|
||||||
_readyState = WebSocketState.Closed;
|
_readyState = WebSocketState.Closed;
|
||||||
try {
|
try {
|
||||||
@ -1437,22 +1434,22 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
internal static string CreateResponseKey (string base64Key)
|
internal static string CreateResponseKey (string base64Key)
|
||||||
{
|
{
|
||||||
var buffer = new StringBuilder (base64Key, 64);
|
var buff = new StringBuilder (base64Key, 64);
|
||||||
buffer.Append (_guid);
|
buff.Append (_guid);
|
||||||
SHA1 sha1 = new SHA1CryptoServiceProvider ();
|
SHA1 sha1 = new SHA1CryptoServiceProvider ();
|
||||||
var src = sha1.ComputeHash (Encoding.UTF8.GetBytes (buffer.ToString ()));
|
var src = sha1.ComputeHash (Encoding.UTF8.GetBytes (buff.ToString ()));
|
||||||
|
|
||||||
return Convert.ToBase64String (src);
|
return Convert.ToBase64String (src);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool Ping (byte [] frame, int timeout)
|
internal bool Ping (byte[] data, int millisecondsTimeout)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
AutoResetEvent pong;
|
AutoResetEvent pong;
|
||||||
return _readyState == WebSocketState.Open &&
|
return _readyState == WebSocketState.Open &&
|
||||||
send (frame) &&
|
sendWebSocketFrame (data) &&
|
||||||
(pong = _receivePong) != null &&
|
(pong = _receivePong) != null &&
|
||||||
pong.WaitOne (timeout);
|
pong.WaitOne (millisecondsTimeout);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
_logger.Fatal ("An exception has occurred while Ping:\n" + ex.ToString ());
|
_logger.Fatal ("An exception has occurred while Ping:\n" + ex.ToString ());
|
||||||
@ -1537,15 +1534,15 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the WebSocket connection with the specified <see cref="ushort"/>, and releases all
|
/// Closes the WebSocket connection with the specified <see cref="ushort"/>,
|
||||||
/// associated resources.
|
/// and releases all associated resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in the
|
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/>
|
||||||
/// allowable range of the WebSocket close status code.
|
/// isn't in the allowable range of the WebSocket close status code.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// A <see cref="ushort"/> that represents the status code indicating the reason for closure.
|
/// A <see cref="ushort"/> that represents the status code indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Close (ushort code)
|
public void Close (ushort code)
|
||||||
{
|
{
|
||||||
@ -1553,12 +1550,12 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>, and
|
/// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>,
|
||||||
/// releases all associated resources.
|
/// and releases all associated resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
||||||
/// the reason for closure.
|
/// indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Close (CloseStatusCode code)
|
public void Close (CloseStatusCode code)
|
||||||
{
|
{
|
||||||
@ -1566,19 +1563,19 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the WebSocket connection with the specified <see cref="ushort"/> and
|
/// Closes the WebSocket connection with the specified <see cref="ushort"/>
|
||||||
/// <see cref="string"/>, and releases all associated resources.
|
/// and <see cref="string"/>, and releases all associated resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in the
|
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/>
|
||||||
/// allowable range of the WebSocket close status code or the size of <paramref name="reason"/>
|
/// isn't in the allowable range of the WebSocket close status code or the size
|
||||||
/// is greater than 123 bytes.
|
/// of <paramref name="reason"/> is greater than 123 bytes.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// A <see cref="ushort"/> that represents the status code indicating the reason for closure.
|
/// A <see cref="ushort"/> that represents the status code indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="reason">
|
/// <param name="reason">
|
||||||
/// A <see cref="string"/> that represents the reason for closure.
|
/// A <see cref="string"/> that represents the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Close (ushort code, string reason)
|
public void Close (ushort code, string reason)
|
||||||
{
|
{
|
||||||
@ -1599,19 +1596,19 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/> and
|
/// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>
|
||||||
/// <see cref="string"/>, and releases all associated resources.
|
/// and <see cref="string"/>, and releases all associated resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> is
|
/// This method emits a <see cref="OnError"/> event if the size
|
||||||
/// greater than 123 bytes.
|
/// of <paramref name="reason"/> is greater than 123 bytes.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
||||||
/// the reason for closure.
|
/// indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="reason">
|
/// <param name="reason">
|
||||||
/// A <see cref="string"/> that represents the reason for closure.
|
/// A <see cref="string"/> that represents the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Close (CloseStatusCode code, string reason)
|
public void Close (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
@ -1659,12 +1656,12 @@ namespace WebSocketSharp
|
|||||||
/// This method doesn't wait for the close to be complete.
|
/// This method doesn't wait for the close to be complete.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in the
|
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/>
|
||||||
/// allowable range of the WebSocket close status code.
|
/// isn't in the allowable range of the WebSocket close status code.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// A <see cref="ushort"/> that represents the status code indicating the reason for closure.
|
/// A <see cref="ushort"/> that represents the status code indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (ushort code)
|
public void CloseAsync (ushort code)
|
||||||
{
|
{
|
||||||
@ -1679,8 +1676,8 @@ namespace WebSocketSharp
|
|||||||
/// This method doesn't wait for the close to be complete.
|
/// This method doesn't wait for the close to be complete.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
||||||
/// the reason for closure.
|
/// indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (CloseStatusCode code)
|
public void CloseAsync (CloseStatusCode code)
|
||||||
{
|
{
|
||||||
@ -1688,8 +1685,8 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the WebSocket connection asynchronously with the specified <see cref="ushort"/> and
|
/// Closes the WebSocket connection asynchronously with the specified <see cref="ushort"/>
|
||||||
/// <see cref="string"/>, and releases all associated resources.
|
/// and <see cref="string"/>, and releases all associated resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <para>
|
/// <para>
|
||||||
@ -1702,10 +1699,10 @@ namespace WebSocketSharp
|
|||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// A <see cref="ushort"/> that represents the status code indicating the reason for closure.
|
/// A <see cref="ushort"/> that represents the status code indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="reason">
|
/// <param name="reason">
|
||||||
/// A <see cref="string"/> that represents the reason for closure.
|
/// A <see cref="string"/> that represents the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (ushort code, string reason)
|
public void CloseAsync (ushort code, string reason)
|
||||||
{
|
{
|
||||||
@ -1735,16 +1732,16 @@ namespace WebSocketSharp
|
|||||||
/// This method doesn't wait for the close to be complete.
|
/// This method doesn't wait for the close to be complete.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/>
|
/// This method emits a <see cref="OnError"/> event if the size
|
||||||
/// is greater than 123 bytes.
|
/// of <paramref name="reason"/> is greater than 123 bytes.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
|
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
|
||||||
/// the reason for closure.
|
/// indicating the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="reason">
|
/// <param name="reason">
|
||||||
/// A <see cref="string"/> that represents the reason for closure.
|
/// A <see cref="string"/> that represents the reason for the close.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void CloseAsync (CloseStatusCode code, string reason)
|
public void CloseAsync (CloseStatusCode code, string reason)
|
||||||
{
|
{
|
||||||
|
@ -182,23 +182,29 @@ namespace WebSocketSharp
|
|||||||
return readHttp<HttpResponse> (stream, HttpResponse.Parse, millisecondsTimeout);
|
return readHttp<HttpResponse> (stream, HttpResponse.Parse, millisecondsTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool writeBytes (Stream stream, byte[] data)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
stream.Write (data, 0, data.Length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Internal Methods
|
#region Internal Methods
|
||||||
|
|
||||||
internal static WebSocketStream CreateClientStream (
|
internal static WebSocketStream CreateClientStream (
|
||||||
|
TcpClient tcpClient,
|
||||||
|
bool proxy,
|
||||||
Uri targetUri,
|
Uri targetUri,
|
||||||
Uri proxyUri,
|
|
||||||
NetworkCredential proxyCredentials,
|
NetworkCredential proxyCredentials,
|
||||||
bool secure,
|
bool secure,
|
||||||
System.Net.Security.RemoteCertificateValidationCallback validationCallback,
|
System.Net.Security.RemoteCertificateValidationCallback validationCallback)
|
||||||
out TcpClient tcpClient)
|
|
||||||
{
|
{
|
||||||
var proxy = proxyUri != null;
|
|
||||||
tcpClient = proxy
|
|
||||||
? new TcpClient (proxyUri.DnsSafeHost, proxyUri.Port)
|
|
||||||
: new TcpClient (targetUri.DnsSafeHost, targetUri.Port);
|
|
||||||
|
|
||||||
var netStream = tcpClient.GetStream ();
|
var netStream = tcpClient.GetStream ();
|
||||||
if (proxy) {
|
if (proxy) {
|
||||||
var req = HttpRequest.CreateConnectRequest (targetUri);
|
var req = HttpRequest.CreateConnectRequest (targetUri);
|
||||||
@ -270,17 +276,26 @@ namespace WebSocketSharp
|
|||||||
WebSocketFrame.ParseAsync (_innerStream, true, completed, error);
|
WebSocketFrame.ParseAsync (_innerStream, true, completed, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal HttpResponse SendHttpRequest (HttpRequest request, int millisecondsTimeout)
|
||||||
|
{
|
||||||
|
return sendHttpRequest (_innerStream, request, millisecondsTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
internal bool WriteBytes (byte[] data)
|
internal bool WriteBytes (byte[] data)
|
||||||
{
|
{
|
||||||
lock (_forWrite) {
|
lock (_forWrite)
|
||||||
try {
|
return writeBytes (_innerStream, data);
|
||||||
_innerStream.Write (data, 0, data.Length);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal bool WriteHttp (HttpBase http)
|
||||||
|
{
|
||||||
|
return writeBytes (_innerStream, http.ToByteArray ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal bool WriteWebSocketFrame (WebSocketFrame frame)
|
||||||
|
{
|
||||||
|
lock (_forWrite)
|
||||||
|
return writeBytes (_innerStream, frame.ToByteArray ());
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user