mirror of
https://github.com/Azure/DotNetty.git
synced 2026-10-18 22:30:36 +08:00
4ad4ffaf3c
- Fixing examples following move to new csproj to load cert where it's placed - Fixing STEE behavior when loop crashes
70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
namespace Telnet.Server
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Threading.Tasks;
|
|
using DotNetty.Codecs;
|
|
using DotNetty.Handlers.Logging;
|
|
using DotNetty.Handlers.Tls;
|
|
using DotNetty.Transport.Bootstrapping;
|
|
using DotNetty.Transport.Channels;
|
|
using DotNetty.Transport.Channels.Sockets;
|
|
using Examples.Common;
|
|
|
|
class Program
|
|
{
|
|
static async Task RunServerAsync()
|
|
{
|
|
ExampleHelper.SetConsoleLogger();
|
|
|
|
var bossGroup = new MultithreadEventLoopGroup(1);
|
|
var workerGroup = new MultithreadEventLoopGroup();
|
|
|
|
var STRING_ENCODER = new StringEncoder();
|
|
var STRING_DECODER = new StringDecoder();
|
|
var SERVER_HANDLER = new TelnetServerHandler();
|
|
|
|
X509Certificate2 tlsCertificate = null;
|
|
if (ServerSettings.IsSsl)
|
|
{
|
|
tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
|
|
}
|
|
try
|
|
{
|
|
var bootstrap = new ServerBootstrap();
|
|
bootstrap
|
|
.Group(bossGroup, workerGroup)
|
|
.Channel<TcpServerSocketChannel>()
|
|
.Option(ChannelOption.SoBacklog, 100)
|
|
.Handler(new LoggingHandler(LogLevel.INFO))
|
|
.ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
|
|
{
|
|
IChannelPipeline pipeline = channel.Pipeline;
|
|
if (tlsCertificate != null)
|
|
{
|
|
pipeline.AddLast(TlsHandler.Server(tlsCertificate));
|
|
}
|
|
|
|
pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter()));
|
|
pipeline.AddLast(STRING_ENCODER, STRING_DECODER, SERVER_HANDLER);
|
|
}));
|
|
|
|
IChannel bootstrapChannel = await bootstrap.BindAsync(ServerSettings.Port);
|
|
|
|
Console.ReadLine();
|
|
|
|
await bootstrapChannel.CloseAsync();
|
|
}
|
|
finally
|
|
{
|
|
Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
|
|
}
|
|
}
|
|
|
|
static void Main() => RunServerAsync().Wait();
|
|
}
|
|
} |