-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
501 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Examples/UniNetty.Examples.DemoSupports/AnonymousDisposer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
|
||
namespace UniNetty.Examples.DemoSupports | ||
{ | ||
internal class AnonymousDisposer : IDisposable | ||
{ | ||
private Action _action; | ||
private bool _disposed; | ||
private readonly object _lock = new object(); | ||
|
||
public static IDisposable Create(Action action) | ||
{ | ||
return new AnonymousDisposer(action); | ||
} | ||
|
||
private AnonymousDisposer(Action action) | ||
{ | ||
_action = action ?? throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
~AnonymousDisposer() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
bool needToDispose = false; | ||
lock (_lock) | ||
{ | ||
if (!_disposed) | ||
{ | ||
needToDispose = true; | ||
_disposed = true; | ||
} | ||
} | ||
|
||
if (needToDispose) | ||
{ | ||
var temp = _action; | ||
_action = null; | ||
temp.Invoke(); | ||
} | ||
} | ||
} | ||
} |
209 changes: 209 additions & 0 deletions
209
Examples/UniNetty.Examples.DemoSupports/ExampleContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
using System; | ||
using System.Net; | ||
using System.Security.Cryptography.X509Certificates; | ||
using UniNetty.Examples.Discard.Client; | ||
using UniNetty.Examples.Discard.Server; | ||
using UniNetty.Examples.Echo.Client; | ||
using UniNetty.Examples.Echo.Server; | ||
using UniNetty.Examples.Factorial.Client; | ||
using UniNetty.Examples.Factorial.Server; | ||
using UniNetty.Examples.HttpServer; | ||
using UniNetty.Examples.QuoteOfTheMoment.Client; | ||
using UniNetty.Examples.QuoteOfTheMoment.Server; | ||
using UniNetty.Examples.SecureChat.Client; | ||
using UniNetty.Examples.SecureChat.Server; | ||
using UniNetty.Examples.Telnet.Client; | ||
using UniNetty.Examples.Telnet.Server; | ||
using UniNetty.Examples.WebSockets.Client; | ||
using UniNetty.Examples.WebSockets.Server; | ||
|
||
namespace UniNetty.Examples.DemoSupports | ||
{ | ||
public class ExampleContext | ||
{ | ||
public X509Certificate2 Cert { get; private set; } | ||
|
||
public void SetCertificate(X509Certificate2 cert) | ||
{ | ||
Cert = cert; | ||
} | ||
|
||
// discard | ||
public IDisposable RunDiscardServer(ExampleSetting setting) | ||
{ | ||
var server = new DiscardServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunDiscardClient(ExampleSetting setting) | ||
{ | ||
var client = new DiscardClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port, setting.Size); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
|
||
// echo | ||
public IDisposable RunEchoServer(ExampleSetting setting) | ||
{ | ||
var server = new EchoServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunEchoClient(ExampleSetting setting) | ||
{ | ||
var client = new EchoClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port, setting.Size); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
// factorial | ||
public IDisposable RunFactorialServer(ExampleSetting setting) | ||
{ | ||
var server = new FactorialServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunFactorialClient(ExampleSetting setting) | ||
{ | ||
var client = new FactorialClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port, setting.Count); | ||
|
||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
// QuoteOfTheMoment | ||
public IDisposable QuoteOfTheMomentServer(ExampleSetting setting) | ||
{ | ||
var server = new QuoteOfTheMomentServer(); | ||
_ = server.StartAsync(setting.Port); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunQuoteOfTheMomentClient(ExampleSetting setting) | ||
{ | ||
var client = new QuoteOfTheMomentClient(); | ||
_ = client.StartAsync(setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
// secure | ||
public IDisposable RunSecureChatServer(ExampleSetting setting) | ||
{ | ||
var server = new SecureChatServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunSecureChatClient(ExampleSetting setting) | ||
{ | ||
var client = new SecureChatClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
// | ||
public IDisposable RunTelnetServer(ExampleSetting setting) | ||
{ | ||
var server = new TelnetServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
|
||
public IDisposable RunTelnetClient(ExampleSetting setting) | ||
{ | ||
var client = new TelnetClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
|
||
// websocket | ||
public IDisposable RunWebSocketServer(ExampleSetting setting) | ||
{ | ||
var server = new WebSocketServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunWebSocketClient(ExampleSetting setting) | ||
{ | ||
var client = new WebSocketClient(); | ||
_ = client.StartAsync(Cert, IPAddress.Parse(setting.Ip), setting.Port, setting.Path); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = client.StopAsync(); | ||
}); | ||
} | ||
|
||
// http | ||
public IDisposable RunHelloHttpServer(ExampleSetting setting) | ||
{ | ||
var server = new HelloHttpServer(); | ||
_ = server.StartAsync(Cert, setting.Port); | ||
|
||
return AnonymousDisposer.Create(() => | ||
{ | ||
_ = server.StopAsync(); | ||
}); | ||
} | ||
|
||
public IDisposable RunHelloHttpClient(ExampleSetting setting) | ||
{ | ||
ExampleSupport.Shared.OpenUrl($"https://{setting.Ip}:{setting.Port}/json"); | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
|
||
namespace UniNetty.Examples.DemoSupports | ||
{ | ||
public class ExampleSetting | ||
{ | ||
public readonly ExampleType Example; | ||
|
||
public string Ip { get; private set; } | ||
public int Port { get; private set; } | ||
public int Size { get; private set; } | ||
public int Count { get; private set; } | ||
public string Path { get; private set; } | ||
|
||
public bool IsRunningServer => null != _stopServer; | ||
private Func<ExampleSetting, IDisposable> _runServer; | ||
private IDisposable _stopServer; | ||
|
||
public bool IsRunningClient => null != _stopClient; | ||
private Func<ExampleSetting, IDisposable> _runClient; | ||
private IDisposable _stopClient; | ||
|
||
public ExampleSetting(ExampleType example) | ||
{ | ||
Example = example; | ||
} | ||
|
||
public void SetServer(Func<ExampleSetting, IDisposable> runServer) | ||
{ | ||
_runServer = runServer; | ||
} | ||
|
||
public void SetClient(Func<ExampleSetting, IDisposable> runClient) | ||
{ | ||
_runClient = runClient; | ||
} | ||
|
||
public void SetIp(string ip) | ||
{ | ||
Ip = ip; | ||
} | ||
|
||
public void SetPort(int port) | ||
{ | ||
Port = port; | ||
} | ||
|
||
public void SetSize(int size) | ||
{ | ||
Size = size; | ||
} | ||
|
||
public void SetCount(int count) | ||
{ | ||
Count = count; | ||
} | ||
|
||
public void SetPath(string path) | ||
{ | ||
Path = path; | ||
} | ||
|
||
public void ToggleServer() | ||
{ | ||
if (null == _stopServer) | ||
{ | ||
_stopServer = _runServer?.Invoke(this); | ||
} | ||
else | ||
{ | ||
_stopServer.Dispose(); | ||
_stopServer = null; | ||
} | ||
} | ||
|
||
public void ToggleClient() | ||
{ | ||
if (null == _stopClient) | ||
{ | ||
_stopClient = _runClient?.Invoke(this); | ||
} | ||
else | ||
{ | ||
_stopClient.Dispose(); | ||
_stopClient = null; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.