Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Created December 24, 2013 22:16
Show Gist options
  • Save ThePhD/8118329 to your computer and use it in GitHub Desktop.
Save ThePhD/8118329 to your computer and use it in GitHub Desktop.
Web Extension Stack, yaay!
using System.Collections.Generic;
namespace LoungeChat.Server {
public class WebSocketExtensionStack {
private List<IWebSocketExtension> _extensionstack = null;
private List<IWebSocketExtension> _acceptedextensionstack = new List<IWebSocketExtension>( );
public WebSocketExtensionStack( IEnumerable<IWebSocketExtension> extensions ) {
if ( extensions == null )
_extensionstack = new List<IWebSocketExtension>();
else
_extensionstack = new List<IWebSocketExtension>( extensions );
}
public IList<IWebSocketExtension> Accept ( WebSocketUpgradeRequest request ) {
_acceptedextensionstack.Clear( );
IList<string> targetstack = request.Extensions;
for ( int e = 0; e < _extensionstack.Count; ++e ) {
var ext = _extensionstack[e];
int targetindex = targetstack.IndexOf( ext.Name );
if ( targetindex == -1 ) {
if ( ext.Required )
throw new WebSocketExtensionException( "Extension required by this WebSocket was not found in the WebSocketUpgradeRequest" );
continue;
}
_acceptedextensionstack.Add( ext );
}
return _acceptedextensionstack;
}
public void Encode ( ref WebSocketFrameData data ) {
for ( int e = 0; e < _acceptedextensionstack.Count; ++e ) {
_acceptedextensionstack[ e ].Encode( ref data );
}
}
public void Decode( ref WebSocketFrameData data ) {
for ( int e = 0; e < _acceptedextensionstack.Count; ++e ) {
_acceptedextensionstack[ e ].Decode( ref data );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment