Skip to content

Commit 0e54f96

Browse files
committed
Modified receiving frame
1 parent f661035 commit 0e54f96

File tree

3 files changed

+66
-71
lines changed

3 files changed

+66
-71
lines changed

websocket-sharp/WebSocket.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,9 @@ private void init (WebSocketContext context)
822822
private void open ()
823823
{
824824
_readyState = WebSocketState.OPEN;
825-
startReceiving ();
825+
826826
OnOpen.Emit (this, EventArgs.Empty);
827+
startReceiving ();
827828
}
828829

829830
private bool processAbnormalFrame ()
@@ -1230,28 +1231,29 @@ private void startReceiving ()
12301231
_exitReceiving = new AutoResetEvent (false);
12311232
_receivePong = new AutoResetEvent (false);
12321233

1233-
Action<WsFrame> completed = null;
1234-
completed = frame =>
1235-
{
1236-
try {
1234+
Action receive = null;
1235+
receive = () => _stream.ReadFrameAsync (
1236+
frame =>
1237+
{
12371238
if (processFrame (frame))
1238-
_stream.ReadFrameAsync (completed);
1239+
receive ();
12391240
else
12401241
_exitReceiving.Set ();
1241-
}
1242-
catch (WebSocketException ex) {
1243-
_logger.Fatal (ex.ToString ());
1244-
error ("An exception has occured.");
1245-
close (ex.Code, ex.Message, false);
1246-
}
1247-
catch (Exception ex) {
1242+
},
1243+
ex =>
1244+
{
12481245
_logger.Fatal (ex.ToString ());
12491246
error ("An exception has occured.");
1250-
close (CloseStatusCode.ABNORMAL, null, false);
1251-
}
1252-
};
1247+
if (ex.GetType () == typeof (WebSocketException))
1248+
{
1249+
var wsex = (WebSocketException) ex;
1250+
close (wsex.Code, wsex.Message, false);
1251+
}
1252+
else
1253+
close (CloseStatusCode.ABNORMAL, null, false);
1254+
});
12531255

1254-
_stream.ReadFrameAsync (completed);
1256+
receive ();
12551257
}
12561258

12571259
// As server

websocket-sharp/WsFrame.cs

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -573,42 +573,45 @@ public IEnumerator<byte> GetEnumerator ()
573573

574574
public static WsFrame Parse (byte [] src)
575575
{
576-
return Parse (src, true);
576+
return Parse (src, true, null);
577577
}
578578

579579
public static WsFrame Parse (Stream stream)
580580
{
581-
return Parse (stream, true);
581+
return Parse (stream, true, null);
582582
}
583583

584-
public static WsFrame Parse (byte [] src, bool unmask)
584+
public static WsFrame Parse (byte [] src, Action<Exception> error)
585585
{
586-
using (var stream = new MemoryStream (src))
587-
{
588-
return Parse (stream, unmask);
589-
}
586+
return Parse (src, true, error);
587+
}
588+
589+
public static WsFrame Parse (Stream stream, Action<Exception> error)
590+
{
591+
return Parse (stream, true, error);
590592
}
591593

592-
public static WsFrame Parse (Stream stream, bool unmask)
594+
public static WsFrame Parse (byte [] src, bool unmask, Action<Exception> error)
593595
{
594-
return Parse (stream, unmask, null);
596+
using (var stream = new MemoryStream (src))
597+
{
598+
return Parse (stream, unmask, error);
599+
}
595600
}
596601

597602
public static WsFrame Parse (Stream stream, bool unmask, Action<Exception> error)
598603
{
599604
WsFrame frame = null;
600-
try
601-
{
605+
try {
602606
var header = stream.ReadBytes (2);
603607
frame = header.Length == 2
604608
? parse (header, stream, unmask)
605609
: CreateCloseFrame (
606610
Mask.UNMASK,
607611
CloseStatusCode.ABNORMAL,
608-
"'Header' of a frame cannot be read from the data stream.");
612+
"The header part of a frame cannot be read from the 'stream'.");
609613
}
610-
catch (Exception ex)
611-
{
614+
catch (Exception ex) {
612615
if (error != null)
613616
error (ex);
614617
}
@@ -629,43 +632,25 @@ public static void ParseAsync (Stream stream, Action<WsFrame> completed, Action<
629632
public static void ParseAsync (
630633
Stream stream, bool unmask, Action<WsFrame> completed, Action<Exception> error)
631634
{
632-
var header = new byte [2];
633-
AsyncCallback callback = ar =>
634-
{
635-
WsFrame frame = null;
636-
try
635+
stream.ReadBytesAsync (
636+
2,
637+
header =>
637638
{
638-
var readLen = stream.EndRead (ar);
639-
if (readLen == 1)
640-
{
641-
var tmp = stream.ReadByte ();
642-
if (tmp > -1)
643-
{
644-
header [1] = (byte) tmp;
645-
readLen++;
646-
}
647-
}
648-
649-
frame = readLen == 2
650-
? parse (header, stream, unmask)
651-
: CreateCloseFrame (
652-
Mask.UNMASK,
653-
CloseStatusCode.ABNORMAL,
654-
"'Header' of a frame cannot be read from the data stream.");
655-
}
656-
catch (Exception ex)
639+
var frame = header.Length == 2
640+
? parse (header, stream, unmask)
641+
: CreateCloseFrame (
642+
Mask.UNMASK,
643+
CloseStatusCode.ABNORMAL,
644+
"The header part of a frame cannot be read from the 'stream'.");
645+
646+
if (completed != null)
647+
completed (frame);
648+
},
649+
ex =>
657650
{
658651
if (error != null)
659652
error (ex);
660-
}
661-
finally
662-
{
663-
if (completed != null)
664-
completed (frame);
665-
}
666-
};
667-
668-
stream.BeginRead (header, 0, 2, callback, null);
653+
});
669654
}
670655

671656
public void Print (bool dumped)

websocket-sharp/WsStream.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,26 @@ public WsFrame ReadFrame ()
174174
{
175175
lock (_forRead)
176176
{
177-
try {
178-
return WsFrame.Parse (_innerStream);
179-
}
180-
catch {
181-
return null;
182-
}
177+
return WsFrame.Parse (_innerStream, null);
178+
}
179+
}
180+
181+
public WsFrame ReadFrame (Action<Exception> error)
182+
{
183+
lock (_forRead)
184+
{
185+
return WsFrame.Parse (_innerStream, error);
183186
}
184187
}
185188

186189
public void ReadFrameAsync (Action<WsFrame> completed)
187190
{
188-
WsFrame.ParseAsync (_innerStream, completed);
191+
WsFrame.ParseAsync (_innerStream, completed, null);
192+
}
193+
194+
public void ReadFrameAsync (Action<WsFrame> completed, Action<Exception> error)
195+
{
196+
WsFrame.ParseAsync (_innerStream, completed, error);
189197
}
190198

191199
public string [] ReadHandshake ()

0 commit comments

Comments
 (0)