From 0e2006ff332b4eb11acc2b29517c0312aaea4ebe Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 09:45:10 -0500 Subject: [PATCH 1/6] Added Custom Headers to handshake request --- .../Server/WebSocketServiceHostManager.cs | 2 +- .../Server/WebSocketSessionManager.cs | 2 +- websocket-sharp/WebSocket.cs | 47 +++++++++++-------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs index 11c6966d7..45e4eb8d8 100644 --- a/websocket-sharp/Server/WebSocketServiceHostManager.cs +++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs @@ -703,7 +703,7 @@ public Dictionary> Broadping (string message) if (message == null || message.Length == 0) return Broadping (); - byte [] data; + byte [] data = null; var msg = _state.CheckIfStarted () ?? (data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData (); diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs index b5da20cb5..00ce5f8e4 100644 --- a/websocket-sharp/Server/WebSocketSessionManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -601,7 +601,7 @@ public Dictionary Broadping (string message) if (message == null || message.Length == 0) return Broadping (); - byte [] data; + byte [] data = null; var msg = _state.CheckIfStarted () ?? (data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData (); diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 2f6702619..852b5f5ea 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -245,6 +245,9 @@ internal bool IsOpened { #region Public Properties + // TODO: Submit pull request + public IEnumerable> CustomHeaders { get; set; } + /// /// Gets or sets the compression method used to compress the payload data of the WebSocket Data frame. /// @@ -714,6 +717,14 @@ private HandshakeRequest createHandshakeRequest () req.AddHeader ("Sec-WebSocket-Version", _version); + if (CustomHeaders != null) + { + foreach (var header in CustomHeaders) + { + req.AddHeader(header.Key, header.Value); + } + } + if (_preAuth && _credentials != null) req.SetAuthorization (new AuthenticationResponse (_credentials)); @@ -822,9 +833,8 @@ private void init (WebSocketContext context) private void open () { _readyState = WebSocketState.OPEN; - - OnOpen.Emit (this, EventArgs.Empty); startReceiving (); + OnOpen.Emit (this, EventArgs.Empty); } private bool processAbnormalFrame () @@ -1231,29 +1241,28 @@ private void startReceiving () _exitReceiving = new AutoResetEvent (false); _receivePong = new AutoResetEvent (false); - Action receive = null; - receive = () => _stream.ReadFrameAsync ( - frame => - { + Action completed = null; + completed = frame => + { + try { if (processFrame (frame)) - receive (); + _stream.ReadFrameAsync (completed); else _exitReceiving.Set (); - }, - ex => - { + } + catch (WebSocketException ex) { _logger.Fatal (ex.ToString ()); error ("An exception has occured."); - if (ex.GetType () == typeof (WebSocketException)) - { - var wsex = (WebSocketException) ex; - close (wsex.Code, wsex.Message, false); - } - else - close (CloseStatusCode.ABNORMAL, null, false); - }); + close (ex.Code, ex.Message, false); + } + catch (Exception ex) { + _logger.Fatal (ex.ToString ()); + error ("An exception has occured."); + close (CloseStatusCode.ABNORMAL, null, false); + } + }; - receive (); + _stream.ReadFrameAsync (completed); } // As server From 23338cdeb2ecfd5f28814d2a642b34f8bfe50d56 Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 09:48:34 -0500 Subject: [PATCH 2/6] Removed reminder to create a pull request :) --- websocket-sharp/WebSocket.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 852b5f5ea..b033f0f6c 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -245,7 +245,6 @@ internal bool IsOpened { #region Public Properties - // TODO: Submit pull request public IEnumerable> CustomHeaders { get; set; } /// From 894e5db8ac2b1470ef1f08eb5ec059e6801e6266 Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 09:58:00 -0500 Subject: [PATCH 3/6] Restored changes inbetween my changes yesterday and other changes today --- websocket-sharp/WebSocket.cs | 58 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index b033f0f6c..6af7071c0 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -832,8 +832,9 @@ private void init (WebSocketContext context) private void open () { _readyState = WebSocketState.OPEN; + + OnOpen.Emit(this, EventArgs.Empty); startReceiving (); - OnOpen.Emit (this, EventArgs.Empty); } private bool processAbnormalFrame () @@ -1235,34 +1236,35 @@ private void setClientStream () _stream = WsStream.CreateClientStream (_tcpClient, _secure, host, _certValidationCallback); } - private void startReceiving () - { - _exitReceiving = new AutoResetEvent (false); - _receivePong = new AutoResetEvent (false); - - Action completed = null; - completed = frame => - { - try { - if (processFrame (frame)) - _stream.ReadFrameAsync (completed); - else - _exitReceiving.Set (); - } - catch (WebSocketException ex) { - _logger.Fatal (ex.ToString ()); - error ("An exception has occured."); - close (ex.Code, ex.Message, false); - } - catch (Exception ex) { - _logger.Fatal (ex.ToString ()); - error ("An exception has occured."); - close (CloseStatusCode.ABNORMAL, null, false); - } - }; + private void startReceiving() + { + _exitReceiving = new AutoResetEvent(false); + _receivePong = new AutoResetEvent(false); - _stream.ReadFrameAsync (completed); - } + Action receive = null; + receive = () => _stream.ReadFrameAsync( + frame => + { + if (processFrame(frame)) + receive(); + else + _exitReceiving.Set(); + }, + ex => + { + _logger.Fatal(ex.ToString()); + error("An exception has occured."); + if (ex.GetType() == typeof(WebSocketException)) + { + var wsex = (WebSocketException)ex; + close(wsex.Code, wsex.Message, false); + } + else + close(CloseStatusCode.ABNORMAL, null, false); + }); + + receive(); + } // As server private bool validateConnectionRequest (WebSocketContext context) From c5dbbdcfea3d5db7e08fc5cc816c702f138f91ce Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 10:02:31 -0500 Subject: [PATCH 4/6] Fixed indentation --- websocket-sharp/WebSocket.cs | 76 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 6af7071c0..76adf1ad9 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -245,7 +245,7 @@ internal bool IsOpened { #region Public Properties - public IEnumerable> CustomHeaders { get; set; } + public IEnumerable> CustomHeaders { get; set; } /// /// Gets or sets the compression method used to compress the payload data of the WebSocket Data frame. @@ -716,13 +716,13 @@ private HandshakeRequest createHandshakeRequest () req.AddHeader ("Sec-WebSocket-Version", _version); - if (CustomHeaders != null) - { - foreach (var header in CustomHeaders) - { - req.AddHeader(header.Key, header.Value); - } - } + if (CustomHeaders != null) + { + foreach (var header in CustomHeaders) + { + req.AddHeader(header.Key, header.Value); + } + } if (_preAuth && _credentials != null) req.SetAuthorization (new AuthenticationResponse (_credentials)); @@ -833,7 +833,7 @@ private void open () { _readyState = WebSocketState.OPEN; - OnOpen.Emit(this, EventArgs.Empty); + OnOpen.Emit(this, EventArgs.Empty); startReceiving (); } @@ -1236,35 +1236,35 @@ private void setClientStream () _stream = WsStream.CreateClientStream (_tcpClient, _secure, host, _certValidationCallback); } - private void startReceiving() - { - _exitReceiving = new AutoResetEvent(false); - _receivePong = new AutoResetEvent(false); - - Action receive = null; - receive = () => _stream.ReadFrameAsync( - frame => - { - if (processFrame(frame)) - receive(); - else - _exitReceiving.Set(); - }, - ex => - { - _logger.Fatal(ex.ToString()); - error("An exception has occured."); - if (ex.GetType() == typeof(WebSocketException)) - { - var wsex = (WebSocketException)ex; - close(wsex.Code, wsex.Message, false); - } - else - close(CloseStatusCode.ABNORMAL, null, false); - }); - - receive(); - } + private void startReceiving() + { + _exitReceiving = new AutoResetEvent(false); + _receivePong = new AutoResetEvent(false); + + Action receive = null; + receive = () => _stream.ReadFrameAsync( + frame => + { + if (processFrame(frame)) + receive(); + else + _exitReceiving.Set(); + }, + ex => + { + _logger.Fatal(ex.ToString()); + error("An exception has occured."); + if (ex.GetType() == typeof(WebSocketException)) + { + var wsex = (WebSocketException)ex; + close(wsex.Code, wsex.Message, false); + } + else + close(CloseStatusCode.ABNORMAL, null, false); + }); + + receive(); + } // As server private bool validateConnectionRequest (WebSocketContext context) From ac4ac9af80ce27e6e699133b6b6e8b19bf48e176 Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 10:04:21 -0500 Subject: [PATCH 5/6] Fixed spacing to match style conventions --- websocket-sharp/WebSocket.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 76adf1ad9..7b72cadf8 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -833,7 +833,7 @@ private void open () { _readyState = WebSocketState.OPEN; - OnOpen.Emit(this, EventArgs.Empty); + OnOpen.Emit (this, EventArgs.Empty); startReceiving (); } @@ -1236,34 +1236,34 @@ private void setClientStream () _stream = WsStream.CreateClientStream (_tcpClient, _secure, host, _certValidationCallback); } - private void startReceiving() + private void startReceiving () { - _exitReceiving = new AutoResetEvent(false); - _receivePong = new AutoResetEvent(false); + _exitReceiving = new AutoResetEvent (false); + _receivePong = new AutoResetEvent (false); Action receive = null; - receive = () => _stream.ReadFrameAsync( + receive = () => _stream.ReadFrameAsync ( frame => { if (processFrame(frame)) receive(); else - _exitReceiving.Set(); + _exitReceiving.Set (); }, ex => { - _logger.Fatal(ex.ToString()); - error("An exception has occured."); - if (ex.GetType() == typeof(WebSocketException)) + _logger.Fatal (ex.ToString ()); + error ("An exception has occured."); + if (ex.GetType () == typeof (WebSocketException)) { - var wsex = (WebSocketException)ex; - close(wsex.Code, wsex.Message, false); + var wsex = (WebSocketException) ex; + close (wsex.Code, wsex.Message, false); } else - close(CloseStatusCode.ABNORMAL, null, false); + close (CloseStatusCode.ABNORMAL, null, false); }); - receive(); + receive (); } // As server From 19909d96768dbceb73b3f7725d7f4ab36507feb1 Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 18 Oct 2013 10:05:53 -0500 Subject: [PATCH 6/6] Missed one ;) --- websocket-sharp/WebSocket.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 7b72cadf8..28559a49a 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -1245,8 +1245,8 @@ private void startReceiving () receive = () => _stream.ReadFrameAsync ( frame => { - if (processFrame(frame)) - receive(); + if (processFrame (frame)) + receive (); else _exitReceiving.Set (); },