Modified CloseEventArgs.cs

This commit is contained in:
sta
2013-03-03 16:19:05 +09:00
parent acd7533c96
commit 189a1fca3e
20 changed files with 30 additions and 19 deletions

Binary file not shown.

View File

@@ -36,16 +36,16 @@ namespace WebSocketSharp {
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The <see cref="WebSocket.OnClose"/> event occurs when the WebSocket receives a close control frame or /// The <see cref="WebSocket.OnClose"/> event occurs when the WebSocket receives a close control frame or
/// the <c>WebSocket.Close</c> method is called. If you want to get the reason for closure, you should access the <see cref="CloseEventArgs.Code"/> or /// the <c>WebSocket.Close</c> method is called. If you want to get the reason for closure, you should access
/// <see cref="CloseEventArgs.Reason"/> properties. /// the <see cref="CloseEventArgs.Code"/> or <see cref="CloseEventArgs.Reason"/> properties.
/// </remarks> /// </remarks>
public class CloseEventArgs : MessageEventArgs public class CloseEventArgs : MessageEventArgs
{ {
#region Fields #region Fields
private bool _clean;
private ushort _code; private ushort _code;
private string _reason; private string _reason;
private bool _wasClean;
#endregion #endregion
@@ -54,18 +54,9 @@ namespace WebSocketSharp {
internal CloseEventArgs(PayloadData data) internal CloseEventArgs(PayloadData data)
: base(Opcode.CLOSE, data) : base(Opcode.CLOSE, data)
{ {
_code = (ushort)CloseStatusCode.NO_STATUS_CODE; _code = getCodeFrom(data);
_reason = String.Empty; _reason = getReasonFrom(data);
_wasClean = false; _clean = false;
if (data.Length >= 2)
_code = data.ToBytes().SubArray(0, 2).To<ushort>(ByteOrder.BIG);
if (data.Length > 2)
{
var buffer = data.ToBytes().SubArray(2, (int)(data.Length - 2));
_reason = Encoding.UTF8.GetString(buffer);
}
} }
#endregion #endregion
@@ -97,21 +88,41 @@ namespace WebSocketSharp {
} }
/// <summary> /// <summary>
/// Indicates whether the connection closed cleanly or not. /// Indicates whether the WebSocket connection closed cleanly.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the connection closed cleanly; otherwise, <c>false</c>. /// <c>true</c> if the WebSocket connection closed cleanly; otherwise, <c>false</c>.
/// </value> /// </value>
public bool WasClean { public bool WasClean {
get { get {
return _wasClean; return _clean;
} }
internal set { internal set {
_wasClean = value; _clean = value;
} }
} }
#endregion #endregion
#region Private Method
private ushort getCodeFrom(PayloadData data)
{
return data.Length >= 2
? data.ToBytes().SubArray(0, 2).To<ushort>(ByteOrder.BIG)
: (ushort)CloseStatusCode.NO_STATUS_CODE;
}
private string getReasonFrom(PayloadData data)
{
if (data.Length <= 2)
return String.Empty;
var buffer = data.ToBytes().SubArray(2, (int)(data.Length - 2));
return Encoding.UTF8.GetString(buffer);
}
#endregion
} }
} }

Binary file not shown.