Proper Disposal of an RtspClient.

This commit is contained in:
feyris-tan 2025-08-18 22:52:23 +02:00
parent 4f1c0c8b10
commit f8521c0b62

View File

@ -12,7 +12,7 @@ using skyscraper8.SatIp.RtspResponses;
namespace skyscraper8.SatIp
{
internal class RtspClient : Validatable
internal class RtspClient : Validatable, IDisposable
{
private uint cseqCounter;
private const string USER_AGENT = "sophiaNetRtspClient/1.0";
@ -33,6 +33,7 @@ namespace skyscraper8.SatIp
this.Valid = rtspOptionsResponse.Valid;
}
public bool AutoReconnect { get; set; }
public IPAddress ListenIp { get; set; }
private IPAddress GetListenIp(EndPoint clientLocalEndPoint)
@ -143,6 +144,10 @@ namespace skyscraper8.SatIp
if (response.RtspStatusCode == 200)
{
setupData.InvokeCancellationTokens();
if (AutoReconnect)
{
Reconnect();
}
}
return response;
}
@ -214,6 +219,27 @@ namespace skyscraper8.SatIp
return result;
}
public void Reconnect()
{
EndPoint clientRemoteEndPoint = this.TcpClient.Client.RemoteEndPoint;
IPEndPoint ipEndPoint = clientRemoteEndPoint as IPEndPoint;
if (ipEndPoint == null)
{
throw new NotImplementedException(clientRemoteEndPoint.GetType().ToString());
}
this.TcpClient.Close();
this.TcpClient = new TcpClient();
this.TcpClient.Connect(ipEndPoint);
this.RootPath = string.Format("rtsp://{0}:{1}", ipEndPoint.Address, ipEndPoint.Port);
this.NetworkStream = TcpClient.GetStream();
this.BufferedStream = new BufferedStream(this.NetworkStream);
this.StreamReader = new StreamReader(this.BufferedStream);
this.StreamWriter = new StreamWriter(this.BufferedStream);
this.cseqCounter = 2;
this.ListenIp = GetListenIp(this.TcpClient.Client.LocalEndPoint);
}
public static string MakeUrl(DiSEqC_Opcode diseqcChannel, int freq, bool isS2, int symbolrate)
{
byte diseqc;
@ -258,5 +284,26 @@ namespace skyscraper8.SatIp
return sb.ToString();
}
private bool disposed;
public void Dispose()
{
if (disposed)
throw new ObjectDisposedException(nameof(RtspClient));
ListenIp = null;
RootPath = null;
if (TcpClient.Connected)
{
TcpClient.Close();
}
TcpClient.Dispose();
NetworkStream = null;
BufferedStream = null;
StreamReader = null;
StreamWriter = null;
disposed = true;
}
}
}