diff --git a/src/JT1078.Gateway/HLSRequestManager.cs b/src/JT1078.Gateway/HLSRequestManager.cs index a83f63b..0fa673a 100644 --- a/src/JT1078.Gateway/HLSRequestManager.cs +++ b/src/JT1078.Gateway/HLSRequestManager.cs @@ -8,9 +8,12 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net; using System.Security.Principal; using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace JT1078.Gateway { @@ -42,6 +45,21 @@ namespace JT1078.Gateway SessionManager = sessionManager; Configuration = jT1078ConfigurationAccessor.Value; Logger = loggerFactory.CreateLogger(); + + Task.Run(()=> { + while (true) + { + var expireds= HttpSessionManager.GetAll().Where(m => DateTime.Now.Subtract(m.StartTime).TotalSeconds > 20).ToList(); + foreach (var item in expireds) + { + //移除httpsession + HttpSessionManager.TryRemoveBySim(item.Sim); + //移除tcpsession + SessionManager.RemoveByTerminalPhoneNo(item.Sim); + } + Thread.Sleep(TimeSpan.FromSeconds(10)); + } + }); } /// /// 处理hls实时视频请求 @@ -71,7 +89,8 @@ namespace JT1078.Gateway { if (context.Response.ContentLength64 != 0) return; //wwwroot\1234_2\live.m3u8 - var key = arg.FullPath.Replace(arg.Name, "").Substring(arg.FullPath.Replace(arg.Name, "").IndexOf("\\")).Replace("\\", ""); + //var key = arg.FullPath.Replace(arg.Name, "").Substring(arg.FullPath.Replace(arg.Name, "").IndexOf("\\")).Replace("\\", ""); + var key = arg.FullPath.Substring(arg.FullPath.IndexOf("\\")+1,arg.FullPath.LastIndexOf("\\")); var sim = key.Split("_")[0]; var channel = int.Parse(key.Split("_")[1]); try @@ -135,21 +154,8 @@ namespace JT1078.Gateway var jT1078HttpContext = new JT1078HttpContext(context, principal); jT1078HttpContext.Sim = sim; jT1078HttpContext.ChannelNo = int.Parse(channelNo); - HttpSessionManager.TryAdd(jT1078HttpContext); - //如果过了30s,还未收到浏览器请求,则移除掉session - memoryCache.Set(key, DateTime.Now, new MemoryCacheEntryOptions - { - AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10) - }.RegisterPostEvictionCallback((key, value, reason, state) => - { - if (reason != EvictionReason.Expired) return; - //当清空httpssion时,同时清除tcpsseion - var removeSim = key.ToString().Split('_')[0]; - //移除httpsession - HttpSessionManager.TryRemoveBySim(removeSim); - //移除tcpsession - SessionManager.RemoveByTerminalPhoneNo(removeSim); - })); + jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Hls; + HttpSessionManager.AddOrUpdate(jT1078HttpContext); } } } diff --git a/src/JT1078.Gateway/JT1078HttpServer.cs b/src/JT1078.Gateway/JT1078HttpServer.cs index e9cec5b..30e7b9d 100644 --- a/src/JT1078.Gateway/JT1078HttpServer.cs +++ b/src/JT1078.Gateway/JT1078HttpServer.cs @@ -131,6 +131,7 @@ namespace JT1078.Gateway var jT1078HttpContext = new JT1078HttpContext(context, wsContext,principal); jT1078HttpContext.Sim = sim; jT1078HttpContext.ChannelNo = channelNo; + jT1078HttpContext.RTPVideoType = RTPVideoType.Ws_Flv; SessionManager.TryAdd(jT1078HttpContext); //这个发送出去,flv.js就报错了 //await jT1078HttpContext.WebSocketSendHelloAsync(); @@ -176,6 +177,7 @@ namespace JT1078.Gateway { var jT1078HttpContext = new JT1078HttpContext(context,principal); jT1078HttpContext.Sim = sim; + jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Flv; jT1078HttpContext.ChannelNo = channelNo; SessionManager.TryAdd(jT1078HttpContext); } diff --git a/src/JT1078.Gateway/Metadata/JT1078HttpContext.cs b/src/JT1078.Gateway/Metadata/JT1078HttpContext.cs index 0b64a8e..31eeeaf 100644 --- a/src/JT1078.Gateway/Metadata/JT1078HttpContext.cs +++ b/src/JT1078.Gateway/Metadata/JT1078HttpContext.cs @@ -16,6 +16,7 @@ namespace JT1078.Gateway.Metadata [JsonIgnore] public HttpListenerWebSocketContext WebSocketContext { get; } public IPrincipal User { get; } + public RTPVideoType RTPVideoType { get; set; } public string Sim { get; set; } public int ChannelNo { get; set; } public bool IsWebSocket @@ -45,4 +46,10 @@ namespace JT1078.Gateway.Metadata FirstSend = false; } } + public enum RTPVideoType + { + Http_Flv, + Ws_Flv, + Http_Hls, + } } diff --git a/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs b/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs index a6841ce..6575ef5 100644 --- a/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs +++ b/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs @@ -25,9 +25,21 @@ namespace JT1078.Gateway.Sessions public bool TryAdd(JT1078HttpContext httpContext) { + return Sessions.TryAdd(httpContext.SessionId, httpContext); } + public void AddOrUpdate(JT1078HttpContext httpContext) { + var session = Sessions.FirstOrDefault(m => m.Value.Sim == httpContext.Sim && m.Value.ChannelNo == httpContext.ChannelNo); + if (string.IsNullOrEmpty(session.Key)) + { + Sessions.TryAdd(httpContext.SessionId, httpContext); + } + else { + Sessions.TryUpdate(session.Key, httpContext, session.Value); + } + } + public async void TryRemove(string sessionId) { if(Sessions.TryRemove(sessionId, out JT1078HttpContext session))