SuperSocketをC〓〓ライブラリにパッケージ化する手順


SuperSocketを類型ライブラリにカプセル化した後、コンソールアプリケーションに限らず、様々な種類のアプリケーションに統合することができ、異なるシーンに対応する。ここではTelnet Serverを例にとって、どうやって操作するかを説明します。
まず、C〓〓〓ライブラリプロジェクトLibSocketServerを作成します。
SuperSocket引用を追加し、デフォルトのログフレームlogl 4 net.dll参照を追加します。ロゴ4 net.co-figをプロジェクトフォルダの「Config」フォルダにコピーし、その「生成操作」を「コンテンツ」に設定し、「出力ディレクトリにコピー」を「新規ならコピー」に設定します。
次に、SuperSocketの完全なTelnet Serverサービス関連カテゴリを追加し、Socketサービス管理クラスSocketServer Manager。
このうち、SocketServer ManagerのBootstrapに対する設定はSuperSocketのパッケージ化がクラスの鍵となります。
Telnet Session.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);
        }
    }
}
Telnet Server.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();
        }
    }
}
Add Command.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);
        }
    }
}
SocketServer Manager.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 Singleton
 
        private 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
    }
}
再度、プロファイルApp.com figをクラスライブラリに追加し、設定パラメータを設定します。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="superSocket"
                 type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
    </configSections>
 
    <!-- SuperSocket       -->
    <superSocket>
        <!--       -->
        <servers>
            <server name="TelnetServer" serverTypeName="TelnetServerType" ip="Any" port="2021"></server>
        </servers>
 
        <!--       -->
        <serverTypes>
            <add name="TelnetServerType" type="LibSocketServer.Server.TelnetServer, LibSocketServer" />
        </serverTypes>
    </superSocket>
</configuration>
最後に、コンソールプロジェクトTelnetServer Sampleを作成し、項目を追加してLibSocketServerを参照し、Program類でSocketServerManagerを使用してSuperSocketの呼び出しを行います。
Program.cs

using System;
using LibSocketServer;
 
namespace TelnetServerSample
{
    class Program
    {
        static void Main()
        {
            try
            {
                //  SuperSocket
                if (!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();
                }
 
                //  SuperSocket
                SocketServerManager.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〓〓〓〓類庫の資料をカプセル化します。私達のその他の関連している文章に注意してください。