时间:2021-05-20
将SuperSocket封装成类库之后可以将其集成进各种类型的应用,而不仅仅局限于控制台应用程序了,从而应用于不同的场景。这里以TelnetServer为例说明如何进行操作。
添加SuperSocket引用(SuperSocket.Common.dll,SuperSocket.SocketBase.dll,SuperSocket.SocketEngine.dll),添加默认的日志框架log4net.dll引用。将log4net.config拷贝到项目文件夹的“Config”文件夹,然后设置它的“生成操作”为“内容”,设置它的“复制到输出目录”为“如果较新则复制”。
其中SocketServerManager对Bootstrap的设置是SuperSocket封装为类库的关键。
TelnetSession.cs
using System;using SuperSocket.SocketBase;using SuperSocket.SocketBase.Protocol;namespace LibSocketServer.Server{public class TelnetSession : AppSession<TelnetSession>{protected override void OnSessionStarted(){Console.WriteLine($"New Session Connected: {RemoteEndPoint.Address} " +$"@ {RemoteEndPoint.Port}.");Send("Welcome to SuperSocket Telnet Server.");}protected override void HandleUnknownRequest(StringRequestInfo requestInfo){Console.WriteLine($"Unknown request {requestInfo.Key}.");Send("Unknown request.");}protected override void HandleException(Exception e){Console.WriteLine($"Application error: {e.Message}.");Send($"Application error: {e.Message}.");}protected override void OnSessionClosed(CloseReason reason){Console.WriteLine($"Session {RemoteEndPoint.Address} @ {RemoteEndPoint.Port} " +$"Closed: {reason}.");base.OnSessionClosed(reason);}}}TelnetServer.cs
using System;using SuperSocket.SocketBase;using SuperSocket.SocketBase.Config;namespace LibSocketServer.Server{public class TelnetServer : AppServer<TelnetSession>{protected override bool Setup(IRootConfig rootConfig, IServerConfig config){Console.WriteLine("TelnetServer Setup");return base.Setup(rootConfig, config);}protected override void OnStarted(){Console.WriteLine("TelnetServer OnStarted");base.OnStarted();}protected override void OnStopped(){Console.WriteLine();Console.WriteLine("TelnetServer OnStopped");base.OnStopped();}}}AddCommand.cs
using System;using System.Linq;using LibSocketServer.Server;using SuperSocket.SocketBase.Command;using SuperSocket.SocketBase.Protocol;namespace LibSocketServer.Command{public class AddCommand : CommandBase<TelnetSession, StringRequestInfo>{public override string Name => "ADD";public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo){Console.WriteLine($"{Name} command: {requestInfo.Body}.");session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());}}}EchoCommand.cs
using System;using LibSocketServer.Server;using SuperSocket.SocketBase.Command;using SuperSocket.SocketBase.Protocol;namespace LibSocketServer.Command{public class EchoCommand : CommandBase<TelnetSession, StringRequestInfo>{public override string Name => "ECHO";public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo){Console.WriteLine($"{Name} command: {requestInfo.Body}.");session.Send(requestInfo.Body);}}}SocketServerManager.cs
using System;using System.Reflection;using SuperSocket.SocketBase;using SuperSocket.SocketEngine;namespace LibSocketServer{public class SocketServerManager{private readonly IBootstrap _bootstrap;public bool Startup(int port){if (!_bootstrap.Initialize()){Console.WriteLine("SuperSocket Failed to initialize!");return false;}var ret = _bootstrap.Start();Console.WriteLine($"SuperSocket Start result: {ret}.");return ret == StartResult.Success;}public void Shutdown(){_bootstrap.Stop();}#region Singletonprivate static SocketServerManager _instance;private static readonly object LockHelper = new object();private SocketServerManager(){var location = Assembly.GetExecutingAssembly().Location;var configFile = $"{location}.config";_bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile(configFile);}public static SocketServerManager Instance{get{if (_instance != null){return _instance;}lock (LockHelper){_instance = _instance ?? new SocketServerManager();}return _instance;}}#endregion}}Program.cs
using System;using LibSocketServer;namespace TelnetServerSample{class Program{static void Main(){try{//启动SuperSocketif (!SocketServerManager.Instance.Startup(2021)){Console.WriteLine("Failed to start TelnetServer!");Console.ReadKey();return;}Console.WriteLine("TelnetServer is listening on port 2021.");Console.WriteLine();Console.WriteLine("Press key 'q' to stop it!");Console.WriteLine();while (Console.ReadKey().KeyChar.ToString().ToUpper() != "Q"){Console.WriteLine();}//关闭SuperSocketSocketServerManager.Instance.Shutdown();}catch (Exception e){Console.WriteLine("Exception: {0}", e.Message);}Console.WriteLine();Console.WriteLine("TelnetServer was stopped!");Console.WriteLine("Press any key to exit...");Console.ReadKey();}}}GitHub Sample
以上就是SuperSocket封装成C#类库的步骤的详细内容,更多关于SuperSocket封装成C#类库的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Node.js中也有一些功能的封装,类似C#的类库,封装成模块这样方便使用,安装之后用require()就能引入调用.一、Node.js模块封装1.创建一个名为
Node.js中也有一些功能的封装,类似C#的类库,封装成模块这样方便使用,安装之后用require()就能引入调用.一、Node.js模块封装1.创建一个名为
logging的基本用法网上很多,这里就不介绍了。在引入正文之前,先来看一个需求:假设需要将某功能封装成类库供他人使用,如何处理类库中的日志?数年前在一个C#开
本文以实例形式讲述了C#解析JSON的方法,C#封装了对XML和JSON解析的类库,使用相当方便!具体用法如下:1.主要用到的类:主要用到了JavaScript
c#下压缩解压,主要是用第三方类库进行封装的。ICSharpCode.SharpZipLib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩