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 CountSpliterReceiveFilterServer:AppServer<CountSpliterReceiveFilterSession>
    {
        /// 請求格式:#part1#part2#part3#part4#part5#part6#part7#
        public CountSpliterReceiveFilterServer()
           : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) //8個分隔符,7個引數。除使用預設的過濾工廠,還可以參照上一個例項定製協議
        {
 
        }
}
static void Main(string[] args)
        {
            Console.WriteLine("請按任何鍵進行啟動SuperSocket服務!");
            Console.ReadKey();
            Console.WriteLine();
 
            var CountSpliterReceiveFilterServer = new CountSpliterReceiveFilterServer();
            // 設定埠號
            int port = 2017;
            //啟動應用服務埠
            if (!CountSpliterReceiveFilterServer.Setup(port)) //啟動時監聽埠2017
            {
                Console.WriteLine("服務埠啟動失敗!");
                Console.ReadKey();
                return;
            }
 
            Console.WriteLine();
            //註冊連線事件
            CountSpliterReceiveFilterServer.NewSessionConnected += CountSpliterReceiveFilterServer_NewSessionConnected;
            //註冊請求事件
            CountSpliterReceiveFilterServer.NewRequestReceived += CountSpliterReceiveFilterServer_NewRequestReceived;
            //註冊Session關閉事件
            CountSpliterReceiveFilterServer.SessionClosed += CountSpliterReceiveFilterServer_SessionClosed;
            //嘗試啟動應用服務
            if (!CountSpliterReceiveFilterServer.Start())
            {
                Console.WriteLine("服務啟動失敗!");
                Console.ReadKey();
                return;
            }
 
            Console.WriteLine("伺服器狀態:" + CountSpliterReceiveFilterServer.State.ToString());
 
            Console.WriteLine("服務啟動成功,請按'q'停止服務!");
 
            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }
 
            //停止服務
            CountSpliterReceiveFilterServer.Stop();
            Console.WriteLine("服務已停止!");
            Console.ReadKey();
        }
 
        static void CountSpliterReceiveFilterServer_SessionClosed(CountSpliterReceiveFilterSession session, SuperSocket.SocketBase.CloseReason value)
        {
            Console.WriteLine(session.RemoteEndPoint.ToString() + "連線斷開. 斷開原因:" + value);
        }
 
        static void CountSpliterReceiveFilterServer_NewSessionConnected(CountSpliterReceiveFilterSession session)
        {
            Console.WriteLine(session.RemoteEndPoint.ToString() + " 已連線.");
        }
 
        /// <summary>
        /// 協議並沒有什麼太多複雜邏輯,不需要用到命令模式,直接用這種方式就可以了
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        private static void CountSpliterReceiveFilterServer_NewRequestReceived(CountSpliterReceiveFilterSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
        {
            Console.WriteLine();
            Console.WriteLine("資料來源: " + session.RemoteEndPoint.ToString());
            Console.WriteLine("接收資料內容:" + requestInfo.Body);
 
        }
來源:https://blog.csdn.net/ba_wang_mao/article/details/128286684

相關文章