/// <summary> /// 收到会话发来数据 /// </summary> /// <param name="session">会话</param> /// <param name="buffer">收到的历史数据</param> protected sealed override void OnReceive(TelnetSession session, ReceiveStream buffer) { while (true) { var request = TelnetRequest.Parse(buffer); if (request == null) { break; } this.ExecTelnetRequest(session, request); } }
/// <summary> /// 执行Telnet请求 /// </summary> /// <param name="session">传话</param> /// <param name="request">请求</param> private void ExecTelnetRequest(TelnetSession session, TelnetRequest request) { var api = default(ApiAction); if (this.apiList.TryGetValue(request.Command, out api) == false) { session.Send("not supported command .."); } else { // 在工作线程中执行业务代码,当前线程为Socket的IO线程 LimitedTask.Factory.StartNew(() => this.ExecApi(session, api, request.Argument)); } }
/// <summary> /// 从收到的数据流中解析请求体 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static TelnetRequest Parse(ReceiveStream stream) { stream.Position = 0; var index = stream.IndexOf(Crlf); // 以CRLF为结束标识 if (index < 0) { return(null); } var input = stream.ReadString(index, Encoding.ASCII); stream.Clear(index + Crlf.Length); if (string.IsNullOrWhiteSpace(input) == true) // 空内容 { return(TelnetRequest.Parse(stream)); } var items = input.Split(new string[] { " " }, 2, StringSplitOptions.RemoveEmptyEntries); var cmd = items.First(); var arg = items.Length > 1 ? items.Last() : null; return(new TelnetRequest(cmd, arg)); }