Modified receiving frame

This commit is contained in:
sta 2013-10-18 23:07:43 +09:00
parent f6610352e2
commit 0e54f96aea
3 changed files with 65 additions and 70 deletions

View File

@ -822,8 +822,9 @@ namespace WebSocketSharp
private void open () private void open ()
{ {
_readyState = WebSocketState.OPEN; _readyState = WebSocketState.OPEN;
startReceiving ();
OnOpen.Emit (this, EventArgs.Empty); OnOpen.Emit (this, EventArgs.Empty);
startReceiving ();
} }
private bool processAbnormalFrame () private bool processAbnormalFrame ()
@ -1230,28 +1231,29 @@ namespace WebSocketSharp
_exitReceiving = new AutoResetEvent (false); _exitReceiving = new AutoResetEvent (false);
_receivePong = new AutoResetEvent (false); _receivePong = new AutoResetEvent (false);
Action<WsFrame> completed = null; Action receive = null;
completed = frame => receive = () => _stream.ReadFrameAsync (
{ frame =>
try { {
if (processFrame (frame)) if (processFrame (frame))
_stream.ReadFrameAsync (completed); receive ();
else else
_exitReceiving.Set (); _exitReceiving.Set ();
} },
catch (WebSocketException ex) { ex =>
{
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occured."); error ("An exception has occured.");
close (ex.Code, ex.Message, false); if (ex.GetType () == typeof (WebSocketException))
} {
catch (Exception ex) { var wsex = (WebSocketException) ex;
_logger.Fatal (ex.ToString ()); close (wsex.Code, wsex.Message, false);
error ("An exception has occured."); }
close (CloseStatusCode.ABNORMAL, null, false); else
} close (CloseStatusCode.ABNORMAL, null, false);
}; });
_stream.ReadFrameAsync (completed); receive ();
} }
// As server // As server

View File

@ -573,42 +573,45 @@ namespace WebSocketSharp
public static WsFrame Parse (byte [] src) public static WsFrame Parse (byte [] src)
{ {
return Parse (src, true); return Parse (src, true, null);
} }
public static WsFrame Parse (Stream stream) public static WsFrame Parse (Stream stream)
{ {
return Parse (stream, true); return Parse (stream, true, null);
} }
public static WsFrame Parse (byte [] src, bool unmask) public static WsFrame Parse (byte [] src, Action<Exception> error)
{
return Parse (src, true, error);
}
public static WsFrame Parse (Stream stream, Action<Exception> error)
{
return Parse (stream, true, error);
}
public static WsFrame Parse (byte [] src, bool unmask, Action<Exception> error)
{ {
using (var stream = new MemoryStream (src)) using (var stream = new MemoryStream (src))
{ {
return Parse (stream, unmask); return Parse (stream, unmask, error);
} }
} }
public static WsFrame Parse (Stream stream, bool unmask)
{
return Parse (stream, unmask, null);
}
public static WsFrame Parse (Stream stream, bool unmask, Action<Exception> error) public static WsFrame Parse (Stream stream, bool unmask, Action<Exception> error)
{ {
WsFrame frame = null; WsFrame frame = null;
try try {
{
var header = stream.ReadBytes (2); var header = stream.ReadBytes (2);
frame = header.Length == 2 frame = header.Length == 2
? parse (header, stream, unmask) ? parse (header, stream, unmask)
: CreateCloseFrame ( : CreateCloseFrame (
Mask.UNMASK, Mask.UNMASK,
CloseStatusCode.ABNORMAL, CloseStatusCode.ABNORMAL,
"'Header' of a frame cannot be read from the data stream."); "The header part of a frame cannot be read from the 'stream'.");
} }
catch (Exception ex) catch (Exception ex) {
{
if (error != null) if (error != null)
error (ex); error (ex);
} }
@ -629,43 +632,25 @@ namespace WebSocketSharp
public static void ParseAsync ( public static void ParseAsync (
Stream stream, bool unmask, Action<WsFrame> completed, Action<Exception> error) Stream stream, bool unmask, Action<WsFrame> completed, Action<Exception> error)
{ {
var header = new byte [2]; stream.ReadBytesAsync (
AsyncCallback callback = ar => 2,
{ header =>
WsFrame frame = null;
try
{ {
var readLen = stream.EndRead (ar); var frame = header.Length == 2
if (readLen == 1) ? parse (header, stream, unmask)
{ : CreateCloseFrame (
var tmp = stream.ReadByte (); Mask.UNMASK,
if (tmp > -1) CloseStatusCode.ABNORMAL,
{ "The header part of a frame cannot be read from the 'stream'.");
header [1] = (byte) tmp;
readLen++;
}
}
frame = readLen == 2 if (completed != null)
? parse (header, stream, unmask) completed (frame);
: CreateCloseFrame ( },
Mask.UNMASK, ex =>
CloseStatusCode.ABNORMAL,
"'Header' of a frame cannot be read from the data stream.");
}
catch (Exception ex)
{ {
if (error != null) if (error != null)
error (ex); error (ex);
} });
finally
{
if (completed != null)
completed (frame);
}
};
stream.BeginRead (header, 0, 2, callback, null);
} }
public void Print (bool dumped) public void Print (bool dumped)

View File

@ -174,18 +174,26 @@ namespace WebSocketSharp
{ {
lock (_forRead) lock (_forRead)
{ {
try { return WsFrame.Parse (_innerStream, null);
return WsFrame.Parse (_innerStream); }
} }
catch {
return null; public WsFrame ReadFrame (Action<Exception> error)
} {
lock (_forRead)
{
return WsFrame.Parse (_innerStream, error);
} }
} }
public void ReadFrameAsync (Action<WsFrame> completed) public void ReadFrameAsync (Action<WsFrame> completed)
{ {
WsFrame.ParseAsync (_innerStream, completed); WsFrame.ParseAsync (_innerStream, completed, null);
}
public void ReadFrameAsync (Action<WsFrame> completed, Action<Exception> error)
{
WsFrame.ParseAsync (_innerStream, completed, error);
} }
public string [] ReadHandshake () public string [] ReadHandshake ()