using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Inspectron.HawkEye.RTSP
{
///
/// TCP Connection for Rtsp
///
public class RtspTcpTransport : IRtspTransport, IDisposable
{
private IPEndPoint _currentEndPoint;
private TcpClient _RtspServerClient;
///
/// Initializes a new instance of the class.
///
/// The underlying TCP connection.
public RtspTcpTransport(TcpClient tcpConnection)
{
if (tcpConnection == null)
throw new ArgumentNullException("tcpConnection");
Contract.EndContractBlock();
_currentEndPoint = (IPEndPoint)tcpConnection.Client.RemoteEndPoint;
_RtspServerClient = tcpConnection;
}
///
/// Initializes a new instance of the class.
///
/// A host.
/// A port number.
public RtspTcpTransport(string aHost, int aPortNumber)
: this(new TcpClient(aHost, aPortNumber))
{
}
#region IRtspTransport Membres
///
/// Gets the stream of the transport.
///
/// A stream
public Stream GetStream()
{
return _RtspServerClient.GetStream();
}
///
/// Gets the remote address.
///
/// The remote address.
public string RemoteAddress
{
get
{
return string.Format(CultureInfo.InvariantCulture,"{0}:{1}", _currentEndPoint.Address, _currentEndPoint.Port);
}
}
///
/// Closes this instance.
///
public void Close()
{
Dispose(true);
}
///
/// Gets a value indicating whether this is connected.
///
/// true if connected; otherwise, false.
public bool Connected
{
get { return _RtspServerClient.Client != null && _RtspServerClient.Connected; }
}
///
/// Reconnect this instance.
/// Must do nothing if already connected.
///
/// Error during socket
public void Reconnect()
{
if (Connected)
return;
_RtspServerClient = new TcpClient();
_RtspServerClient.Connect(_currentEndPoint);
}
#endregion
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_RtspServerClient.Close();
/* // free managed resources
if (managedResource != null)
{
managedResource.Dispose();
managedResource = null;
}*/
}
}
}
}