Skip to content

Commit

Permalink
Issue #89 and StyleCop (#90)
Browse files Browse the repository at this point in the history
* Add Local and Remove Endpoints to WebSocket OnClientConnected event

* Update swan and minor stylecop

* Add FormDataParser and a lot of minor changes

* More stylecop

* Removing unused Chuck Stream

* Removing unused Authentication

* Fixing build

* Fixing Chunk stream issues

* Remove SendAsync stream in WebSocket

* Minor adjustments

* StyleCop

* VirtualPaths and RamCache classes and Stylecop

* Alexey suggestions

* Add WebSocketValidator

* Match Response Close to .NETFX Implementation

* Final commit, let's squash!
  • Loading branch information
geoperez authored Aug 25, 2017
1 parent c67be8d commit 2576886
Show file tree
Hide file tree
Showing 55 changed files with 3,868 additions and 5,867 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ TestResult.xml
/_site
/_api
/tools
/.vs/Unosquare.Labs.EmbedIO/v15/sqlite3/storage.ide
/.vs/Unosquare.Labs.EmbedIO/v15/sqlite3/*
43 changes: 34 additions & 9 deletions src/Unosquare.Labs.EmbedIO.Samples/WebSocketsSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,24 @@ protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuf
/// </value>
public override string ServerName => "Chat Server";

#if NET47
/// <summary>
/// Called when this WebSockets Server accepts a new WebSockets client.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="localEndPoint">The local endpoint.</param>
/// /// <param name="remoteEndPoint">The remote endpoint.</param>
protected override void OnClientConnected(
WebSocketContext context,
System.Net.IPEndPoint localEndPoint,
System.Net.IPEndPoint remoteEndPoint)
#else
/// <summary>
/// Called when this WebSockets Server accepts a new WebSockets client.
/// </summary>
/// <param name="context">The context.</param>
protected override void OnClientConnected(WebSocketContext context)
#endif
{
Send(context, "Welcome to the chat room!");
foreach (var ws in WebSockets)
Expand Down Expand Up @@ -111,9 +124,9 @@ protected override void OnClientDisconnected(WebSocketContext context)
[WebSocketHandler("/terminal")]
public class WebSocketsTerminalServer : WebSocketsServer
{

// we'll keep track of the processes here
private readonly Dictionary<WebSocketContext, Process> Processes = new Dictionary<WebSocketContext, Process>();
private readonly Dictionary<WebSocketContext, Process> _processes = new Dictionary<WebSocketContext, Process>();

// The SyncRoot is used to send 1 thing at a time and multithreaded Processes dictionary.
private readonly object SyncRoot = new object();

Expand All @@ -128,7 +141,7 @@ protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuf
lock (SyncRoot)
{
var arg = Encoding.UTF8.GetString(rxBuffer);
Processes[context].StandardInput.WriteLine(arg);
_processes[context].StandardInput.WriteLine(arg);
}
}

Expand All @@ -141,7 +154,6 @@ protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuf
protected override void OnFrameReceived(WebSocketContext context, byte[] rxBuffer, WebSocketReceiveResult rxResult)
{
// don't process partial frames
return;
}

/// <summary>
Expand All @@ -153,7 +165,7 @@ private WebSocketContext FindContext(Process p)
{
lock (SyncRoot)
{
foreach (var kvp in Processes)
foreach (var kvp in _processes)
{
if (kvp.Value == p)
return kvp.Key;
Expand All @@ -163,13 +175,26 @@ private WebSocketContext FindContext(Process p)
return null;
}

#if NET47
/// <summary>
/// Called when this WebSockets Server accepts a new WebSockets client.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="localEndPoint">The local endpoint.</param>
/// /// <param name="remoteEndPoint">The remote endpoint.</param>
protected override void OnClientConnected(
WebSocketContext context,
System.Net.IPEndPoint localEndPoint,
System.Net.IPEndPoint remoteEndPoint)
#else
/// <summary>
/// Called when this WebSockets Server accepts a new WebSockets client.
/// </summary>
/// <param name="context">The context.</param>
protected override void OnClientConnected(WebSocketContext context)
#endif
{
var process = new Process()
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo()
Expand Down Expand Up @@ -226,7 +251,7 @@ protected override void OnClientConnected(WebSocketContext context)
// add the process to the context
lock (SyncRoot)
{
Processes[context] = process;
_processes[context] = process;
}

process.Start();
Expand All @@ -243,8 +268,8 @@ protected override void OnClientDisconnected(WebSocketContext context)
{
lock (SyncRoot)
{
if (Processes[context].HasExited == false)
Processes[context].Kill();
if (_processes[context].HasExited == false)
_processes[context].Kill();
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Unosquare.Labs.EmbedIO/Constants/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/// </summary>
internal static class Strings
{
internal const string UrlEncodedContentType = "application/x-www-form-urlencoded";

/// <summary>
/// Default Browser time format
/// </summary>
Expand All @@ -23,6 +25,11 @@ internal static class Strings
/// </summary>
internal static readonly char[] CommaSplitChar = { ',' };

/// <summary>
/// The cookie split chars for String.Split method calls.
/// </summary>
internal static readonly char[] CookieSplitChars = {';', ','};

/// <summary>
/// The format culture used for header outputs
/// </summary>
Expand Down
35 changes: 13 additions & 22 deletions src/Unosquare.Labs.EmbedIO/Extensions.Fluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public static partial class Extensions
/// <returns>An instance of webserver</returns>
/// <exception cref="System.ArgumentNullException">webserver</exception>
public static WebServer WithStaticFolderAt(
this WebServer webserver,
this WebServer webserver,
string rootPath,
string defaultDocument = StaticFilesModule.DefaultDocumentName)
{
if (webserver == null)
throw new ArgumentNullException(nameof(webserver));

webserver.RegisterModule(new StaticFilesModule(rootPath) {DefaultDocument = defaultDocument});
webserver.RegisterModule(new StaticFilesModule(rootPath) { DefaultDocument = defaultDocument });
return webserver;
}

Expand All @@ -44,14 +44,14 @@ public static WebServer WithStaticFolderAt(
/// <returns>An instance of a web module</returns>
/// <exception cref="System.ArgumentNullException">webserver</exception>
public static WebServer WithVirtualPaths(
this WebServer webserver,
this WebServer webserver,
Dictionary<string, string> virtualPaths,
string defaultDocument = StaticFilesModule.DefaultDocumentName)
{
if (webserver == null)
throw new ArgumentNullException(nameof(webserver));

webserver.RegisterModule(new StaticFilesModule(virtualPaths) {DefaultDocument = defaultDocument});
webserver.RegisterModule(new StaticFilesModule(virtualPaths) { DefaultDocument = defaultDocument });
return webserver;
}

Expand Down Expand Up @@ -120,9 +120,7 @@ public static WebServer LoadApiControllers(this WebServer webserver, Assembly as
types.Where(x => x.GetTypeInfo().IsClass
&& !x.GetTypeInfo().IsAbstract
&& x.GetTypeInfo().IsSubclassOf(typeof(WebApiController))).ToArray();

if (!apiControllers.Any()) return webserver;


foreach (var apiController in apiControllers)
{
if (webserver.Module<WebApiModule>() == null) webserver = webserver.WithWebApi();
Expand Down Expand Up @@ -151,9 +149,7 @@ public static WebApiModule LoadApiControllers(this WebApiModule apiModule, Assem
types.Where(x => x.GetTypeInfo().IsClass
&& !x.GetTypeInfo().IsAbstract
&& x.GetTypeInfo().IsSubclassOf(typeof(WebApiController))).ToArray();

if (!apiControllers.Any()) return apiModule;


foreach (var apiController in apiControllers)
{
apiModule.RegisterController(apiController);
Expand All @@ -175,18 +171,13 @@ public static WebServer LoadWebSockets(this WebServer webserver, Assembly assemb
throw new ArgumentNullException(nameof(webserver));

var types = (assembly ?? Assembly.GetEntryAssembly()).GetTypes();
var sockerServers =
types.Where(x => x.GetTypeInfo().BaseType == typeof(WebSocketsServer)).ToArray();

if (sockerServers.Any())

foreach (var socketServer in types.Where(x => x.GetTypeInfo().BaseType == typeof(WebSocketsServer)))
{
foreach (var socketServer in sockerServers)
{
if (webserver.Module<WebSocketsModule>() == null) webserver = webserver.WithWebSocket();
if (webserver.Module<WebSocketsModule>() == null) webserver = webserver.WithWebSocket();

webserver.Module<WebSocketsModule>().RegisterWebSocketsServer(socketServer);
$"Registering WebSocket Server '{socketServer.Name}'".Info();
}
webserver.Module<WebSocketsModule>().RegisterWebSocketsServer(socketServer);
$"Registering WebSocket Server '{socketServer.Name}'".Info();
}

return webserver;
Expand All @@ -203,7 +194,7 @@ public static WebServer LoadWebSockets(this WebServer webserver, Assembly assemb
/// <returns>An instance of the tiny web server used to handle request</returns>
/// <exception cref="System.ArgumentNullException">webserver</exception>
public static WebServer EnableCors(
this WebServer webserver,
this WebServer webserver,
string origins = Strings.CorsWildcard,
string headers = Strings.CorsWildcard,
string methods = Strings.CorsWildcard)
Expand All @@ -223,7 +214,7 @@ public static WebServer EnableCors(
/// <param name="webserver">The webserver instance.</param>
/// <returns>An instance of webserver</returns>
/// <exception cref="System.ArgumentNullException">webserver</exception>
public static WebServer WithWebApiController<T>(this WebServer webserver)
public static WebServer WithWebApiController<T>(this WebServer webserver)
where T : WebApiController, new()
{
if (webserver == null)
Expand Down
Loading

0 comments on commit 2576886

Please sign in to comment.