internal void TryLink(string terminalPhoneNo, IJT808Session session) { DateTime curretDatetime = DateTime.Now; if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out IJT808Session cacheSession)) { if (session.SessionID != cacheSession.SessionID) { //从转发到直连的数据需要更新缓存 session.ActiveTime = curretDatetime; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, session, cacheSession); //会话通知 JT808SessionProducer?.ProduceAsync(JT808GatewayConstants.SessionOnline, terminalPhoneNo); } else { cacheSession.ActiveTime = curretDatetime; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, cacheSession, cacheSession); } } else { session.TerminalPhoneNo = terminalPhoneNo; if (TerminalPhoneNoSessions.TryAdd(terminalPhoneNo, session)) { //会话通知 JT808SessionProducer?.ProduceAsync(JT808GatewayConstants.SessionOnline, terminalPhoneNo); } } }
public void RemoveByTerminalPhoneNo(string terminalPhoneNo) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out var removeTerminalPhoneNoSessions)) { // 处理转发过来的是数据 这时候通道对设备是1对多关系,需要清理垃圾数据 //1.用当前会话的通道Id找出通过转发过来的其他设备的终端号 var terminalPhoneNos = TerminalPhoneNoSessions.Where(w => w.Value.SessionID == removeTerminalPhoneNoSessions.SessionID).Select(s => s.Key).ToList(); //2.存在则一个个移除 string tmpTerminalPhoneNo = terminalPhoneNo; if (terminalPhoneNos.Count > 0) { //3.移除包括当前的设备号 foreach (var item in terminalPhoneNos) { TerminalPhoneNoSessions.TryRemove(item, out _); } tmpTerminalPhoneNo = string.Join(",", terminalPhoneNos); } if (Sessions.TryRemove(removeTerminalPhoneNoSessions.SessionID, out var removeSession)) { removeSession.Close(); if (logger.IsEnabled(LogLevel.Information)) { logger.LogInformation($"[Session Remove]:{terminalPhoneNo}-{tmpTerminalPhoneNo}"); } if (SessionProducer != null) { SessionProducer.ProduceAsync(JT808GatewayConstants.SessionOffline, tmpTerminalPhoneNo); } } } }
/// <summary> /// /// </summary> /// <param name="terminalPhoneNo"></param> /// <param name="socket"></param> /// <param name="remoteEndPoint"></param> /// <returns></returns> public IJT808Session TryLink(string terminalPhoneNo, Socket socket, EndPoint remoteEndPoint) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out IJT808Session currentSession)) { currentSession.ActiveTime = DateTime.Now; currentSession.TerminalPhoneNo = terminalPhoneNo; currentSession.RemoteEndPoint = remoteEndPoint; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, currentSession, currentSession); } else { JT808UdpSession session = new JT808UdpSession(socket, remoteEndPoint); session.TerminalPhoneNo = terminalPhoneNo; Sessions.TryAdd(session.SessionID, session); TerminalPhoneNoSessions.TryAdd(terminalPhoneNo, session); currentSession = session; } //会话通知 //使用场景: //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 //有设备关联上来可以进行通知 例如:使用Redis发布订阅 if (SessionProducer != null) { SessionProducer.ProduceAsync(JT808GatewayConstants.SessionOnline, terminalPhoneNo); } return(currentSession); }
public string TryLink(string terminalPhoneNo, Socket socket, EndPoint remoteEndPoint) { string sessionId = string.Empty; if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out sessionId)) { if (Sessions.TryGetValue(sessionId, out IJT808Session sessionInfo)) { sessionInfo.ActiveTime = DateTime.Now; sessionInfo.TerminalPhoneNo = terminalPhoneNo; sessionInfo.RemoteEndPoint = remoteEndPoint; Sessions.TryUpdate(sessionId, sessionInfo, sessionInfo); } } else { JT808UdpSession session = new JT808UdpSession(socket, remoteEndPoint); Sessions.TryAdd(session.SessionID, session); TerminalPhoneNoSessions.TryAdd(terminalPhoneNo, session.SessionID); sessionId = session.SessionID; } //会话通知 //使用场景: //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 //有设备关联上来可以进行通知 例如:使用Redis发布订阅 JT808SessionProducer?.ProduceAsync(JT808GatewayConstants.SessionOnline, terminalPhoneNo); return(sessionId); }
public bool TrySendByTerminalPhoneNo(string terminalPhoneNo, byte[] data) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out var sessionid)) { if (Sessions.TryGetValue(sessionid, out var session)) { if (session.TransportProtocolType == JT808TransportProtocolType.tcp) { session.Client.Send(data, SocketFlags.None); } else { session.Client.SendTo(data, SocketFlags.None, session.RemoteEndPoint); } return(true); } else { return(false); } } else { return(false); } }
internal void TryLink(string terminalPhoneNo, IJT1078Session session) { DateTime curretDatetime = DateTime.Now; if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out IJT1078Session cacheSession)) { if (session.SessionID != cacheSession.SessionID) { //从转发到直连的数据需要更新缓存 session.ActiveTime = curretDatetime; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, session, cacheSession); //会话通知 SessionNoticeService.SessionNoticeBlockingCollection.Add((JT1078GatewayConstants.SessionOnline, terminalPhoneNo, session.TransportProtocolType.ToString())); } else { cacheSession.ActiveTime = curretDatetime; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, cacheSession, cacheSession); } } else { session.TerminalPhoneNo = terminalPhoneNo; if (TerminalPhoneNoSessions.TryAdd(terminalPhoneNo, session)) { //会话通知 SessionNoticeService.SessionNoticeBlockingCollection.Add((JT1078GatewayConstants.SessionOnline, terminalPhoneNo, session.TransportProtocolType.ToString())); } } }
public void RemoveByTerminalPhoneNo(string terminalPhoneNo) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out var removeTerminalPhoneNoSessions)) { // 处理转发过来的是数据 这时候通道对设备是1对多关系,需要清理垃圾数据 //1.用当前会话的通道Id找出通过转发过来的其他设备的终端号 var terminalPhoneNos = TerminalPhoneNoSessions.Where(w => w.Value.SessionID == removeTerminalPhoneNoSessions.SessionID).Select(s => s.Key).ToList(); //2.存在则一个个移除 string tmpTerminalPhoneNo = terminalPhoneNo; if (terminalPhoneNos.Count > 0) { //3.移除包括当前的设备号 foreach (var item in terminalPhoneNos) { TerminalPhoneNoSessions.TryRemove(item, out _); } tmpTerminalPhoneNo = string.Join(",", terminalPhoneNos); } if (Sessions.TryRemove(removeTerminalPhoneNoSessions.SessionID, out var removeSession)) { removeSession.Close(); if (logger.IsEnabled(LogLevel.Information)) { logger.LogInformation($"[Session Remove]:{terminalPhoneNo}-{tmpTerminalPhoneNo}"); } //会话通知 SessionNoticeService.SessionNoticeBlockingCollection.Add((JT1078GatewayConstants.SessionOffline, terminalPhoneNo, removeTerminalPhoneNoSessions.TransportProtocolType.ToString())); } } }
public async ValueTask <bool> TrySendByTerminalPhoneNoAsync(string terminalPhoneNo, byte[] data) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out var session)) { if (session.TransportProtocolType == JT808TransportProtocolType.tcp) { await session.Client.SendAsync(data, SocketFlags.None); } else { await session.Client.SendToAsync(data, SocketFlags.None, session.RemoteEndPoint); } return(true); } else { return(false); } }
public IJT1078Session TryLink(string terminalPhoneNo, Socket socket, EndPoint remoteEndPoint) { if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out IJT1078Session currentSession)) { currentSession.ActiveTime = DateTime.Now; currentSession.TerminalPhoneNo = terminalPhoneNo; currentSession.RemoteEndPoint = remoteEndPoint; TerminalPhoneNoSessions.TryUpdate(terminalPhoneNo, currentSession, currentSession); } else { JT1078UdpSession session = new JT1078UdpSession(socket); session.TerminalPhoneNo = terminalPhoneNo; session.RemoteEndPoint = remoteEndPoint; Sessions.TryAdd(session.SessionID, session); TerminalPhoneNoSessions.TryAdd(terminalPhoneNo, session); currentSession = session; } //会话通知 SessionNoticeService.SessionNoticeBlockingCollection.Add((JT1078GatewayConstants.SessionOnline, terminalPhoneNo, currentSession.TransportProtocolType.ToString())); return(currentSession); }