using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tor.Helpers;
namespace Tor.Controller
{
///
/// A class containing the command to close an existing stream.
///
internal sealed class CloseStreamCommand : Command
{
private readonly StreamReason reason;
private readonly Stream stream;
///
/// Initializes a new instance of the class.
///
/// The stream which should be closed.
/// The reason for the stream being closed.
public CloseStreamCommand(Stream stream, StreamReason reason)
{
this.reason = reason;
this.stream = stream;
}
#region Tor.Controller.Command<>
///
/// Dispatches the command to the client control port and produces a response result.
///
/// The control connection where the command should be dispatched.
///
/// A object instance containing the response data.
///
protected override Response Dispatch(Connection connection)
{
if (stream == null || stream.ID <= 0)
return new Response(false);
if (stream.Status == StreamStatus.Failed || stream.Status == StreamStatus.Closed)
return new Response(false);
if (reason == StreamReason.None || reason == StreamReason.PrivateAddr || reason == StreamReason.End)
return new Response(false);
if (connection.Write("closestream {0} {1}", stream.ID, (int)reason))
{
ConnectionResponse response = connection.Read();
return new Response(response.Success);
}
return new Response(false);
}
#endregion
}
}