After encapsulating SuperSocket into a class library, it can be integrated into various types of applications, not just console applications, and is used in different scenarios. Here we use TelnetServer as an example to illustrate how to operate it.
First, create a C# class library project LibSocketServer
Add SuperSocket reference (,,), and add the default log frame reference. Copy to the "Config" folder of the project folder, then set its "Borrow Action" to "Content", and set its "Copy to Output Directory" to "Copy if newer".
Secondly, add SuperSocket's complete TelnetServer service-related classes and Socket service management class SocketServerManager.
Among them, the settings of Bootstrap by SocketServerManager is the key to encapsulating SuperSocket as a class library.
using System; using ; using ; namespace { public class TelnetSession : AppSession<TelnetSession> { protected override void OnSessionStarted() { ($"New Session Connected: {} " + $"@ {}."); Send("Welcome to SuperSocket Telnet Server."); } protected override void HandleUnknownRequest(StringRequestInfo requestInfo) { ($"Unknown request {}."); Send("Unknown request."); } protected override void HandleException(Exception e) { ($"Application error: {}."); Send($"Application error: {}."); } protected override void OnSessionClosed(CloseReason reason) { ($"Session {} @ {} " + $"Closed: {reason}."); (reason); } } }
using System; using ; using ; namespace { public class TelnetServer : AppServer<TelnetSession> { protected override bool Setup(IRootConfig rootConfig, IServerConfig config) { ("TelnetServer Setup"); return (rootConfig, config); } protected override void OnStarted() { ("TelnetServer OnStarted"); (); } protected override void OnStopped() { (); ("TelnetServer OnStopped"); (); } } }
using System; using ; using ; using ; using ; namespace { public class AddCommand : CommandBase<TelnetSession, StringRequestInfo> { public override string Name => "ADD"; public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo) { ($"{Name} command: {}."); ((p => Convert.ToInt32(p)).Sum().ToString()); } } }
using System; using ; using ; using ; namespace { public class EchoCommand : CommandBase<TelnetSession, StringRequestInfo> { public override string Name => "ECHO"; public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo) { ($"{Name} command: {}."); (); } } }
using System; using ; using ; using ; namespace LibSocketServer { public class SocketServerManager { private readonly IBootstrap _bootstrap; public bool Startup(int port) { if (!_bootstrap.Initialize()) { ("SuperSocket Failed to initialize!"); return false; } var ret = _bootstrap.Start(); ($"SuperSocket Start result: {ret}."); return ret == ; } public void Shutdown() { _bootstrap.Stop(); } #region Singleton private static SocketServerManager _instance; private static readonly object LockHelper = new object(); private SocketServerManager() { var location = ().Location; var configFile = $"{location}.config"; _bootstrap = (configFile); } public static SocketServerManager Instance { get { if (_instance != null) { return _instance; } lock (LockHelper) { _instance = _instance ?? new SocketServerManager(); } return _instance; } } #endregion } }
Again, add the configuration file to the class library and set its configuration parameters.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="superSocket" type=", "/> </configSections> <!-- SuperSocketThe configured root node --> <superSocket> <!-- Server instance --> <servers> <server name="TelnetServer" serverTypeName="TelnetServerType" ip="Any" port="2021"></server> </servers> <!-- Server Type --> <serverTypes> <add name="TelnetServerType" type=", LibSocketServer" /> </serverTypes> </superSocket> </configuration>
Finally, create the console project TelnetServerSample, add the project reference LibSocketServer, and then use the SocketServerManager to make SuperSocket calls in the Program class.
using System; using LibSocketServer; namespace TelnetServerSample { class Program { static void Main() { try { //Start SuperSocket if (!(2021)) { ("Failed to start TelnetServer!"); (); return; } ("TelnetServer is listening on port 2021."); (); ("Press key 'q' to stop it!"); (); while (().().ToUpper() != "Q") { (); } //Close SuperSocket (); } catch (Exception e) { ("Exception: {0}", ); } (); ("TelnetServer was stopped!"); ("Press any key to exit..."); (); } } }
GitHub Sample
The above is the detailed content of the steps for encapsulating SuperSocket into C# class library. For more information about encapsulating SuperSocket into C# class library, please pay attention to my other related articles!