C# SuperSocket 基礎六【CountSpliterReceiveFilte-固定數量分隔符協議】使用COMMAND

【君莫笑】發表於2024-10-28
public class CountSpliterReceiveFilterSession : AppSession<CountSpliterReceiveFilterSession>
    {
        public override void Send(string message)
        {
            Console.WriteLine("傳送訊息:" + message);
            base.Send(message);
        }

        protected override void OnSessionStarted()
        {
            //輸出客戶端IP地址  
            Console.WriteLine(this.LocalEndPoint.Address.ToString());
            this.Send("Hello User,Welcome to SuperSocket Telnet Server!");
        }


        /// <summary>  
        /// 連線關閉  
        /// </summary>  
        /// <param name="reason"></param>  
        protected override void OnSessionClosed(CloseReason reason)
        {
            base.OnSessionClosed(reason);
        }


        protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
        {
            Console.WriteLine($"遇到未知的請求 Key:" + requestInfo.Key + $" Body:" + requestInfo.Body);
            base.HandleUnknownRequest(requestInfo);
        }

        /// <summary>  
        /// 捕捉異常並輸出  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void HandleException(Exception e)
        {
            this.Send("error: {0}", e.Message);
        }
    }
public class CountSpliterReceiveFilterServer : AppServer<CountSpliterReceiveFilterSession>
    {
        /// 請求格式:#part1#part2#part3#part4#part5#part6#part7#
        public CountSpliterReceiveFilterServer()
           : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) //8個分隔符,7個引數。除使用預設的過濾工廠,還可以參照上一個例項定製協議
        {

        }

        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            Console.WriteLine("正在準備配置檔案");
            return base.Setup(rootConfig, config);
        }

        protected override void OnStarted()
        {
            Console.WriteLine("服務已開始");
            base.OnStarted();
        }

        protected override void OnStopped()
        {
            Console.WriteLine("服務已停止");
            base.OnStopped();
        }

        /// <summary>  
        /// 輸出新連線資訊  
        /// </summary>  
        /// <param name="session"></param>  
        protected override void OnNewSessionConnected(CountSpliterReceiveFilterSession session)
        {
            base.OnNewSessionConnected(session);
            //輸出客戶端IP地址  
            Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":連線");
        }


        /// <summary>  
        /// 輸出斷開連線資訊  
        /// </summary>  
        /// <param name="session"></param>  
        /// <param name="reason"></param>  
        protected override void OnSessionClosed(CountSpliterReceiveFilterSession session, CloseReason reason)
        {
            base.OnSessionClosed(session, reason);
            Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":斷開連線");
        }

    }
 public class LOGIN : CommandBase<CountSpliterReceiveFilterSession, StringRequestInfo>
    {
        /// <summary>  
        /// 自定義執行命令方法,注意傳入的變數session型別為MySession  
        /// </summary>  
        /// <param name="session">會話</param>  
        /// <param name="requestInfo">請求資料資訊</param>  
        public override void ExecuteCommand(CountSpliterReceiveFilterSession session, StringRequestInfo requestInfo)
        {
            session.Send(string.Format("LOGIN {0}:{1} {2}", session.Config.Ip, session.Config.Port, requestInfo.Body));
        }
    }
 Console.WriteLine("請按任何鍵進行啟動SuperSocket服務!");
            //Console.ReadKey();
            //Console.WriteLine();
            var bootstrap = BootstrapFactory.CreateBootstrap();

            if (!bootstrap.Initialize())
            {
                //Console.WriteLine("初始化失敗!");
                //Console.ReadKey();
                return;
            }

            var result = bootstrap.Start();

            //Console.WriteLine("服務正在啟動: {0}!", result);

            if (result == StartResult.Failed)
            {
                Console.WriteLine("服務啟動失敗!");
                Console.ReadKey();
                return;
            }
            //Console.WriteLine("服務啟動成功,請按'q'停止服務!");

            //while (Console.ReadKey().KeyChar != 'q')
            //{
            //    Console.WriteLine();
            //    continue;
            //}

            ////停止服務
            //bootstrap.Stop();
            //Console.WriteLine("服務已停止!");
            //Console.ReadKey();
來源:https://blog.csdn.net/ba_wang_mao/article/details/128283899

相關文章