namespace StockSharp.Community
{
using System;
using System.Threading;
using Ecng.Common;
using StockSharp.Logging;
///
/// The client for access to .
///
public class SessionClient : BaseCommunityClient
{
private Timer _pingTimer;
private readonly SyncObject _pingSync = new SyncObject();
private long _sessionId;
///
/// Initializes a new instance of the .
///
public SessionClient()
: this(new Uri("http://stocksharp.com/services/sessionservice.svc"))
{
}
///
/// Initializes a new instance of the .
///
/// Server address.
public SessionClient(Uri address)
: base(address, "session")
{
}
///
/// Create a new activity session.
///
/// Product type.
public void CreateSession(Products product)
{
if (_sessionId != 0)
throw new InvalidOperationException();
var sessionId = TryGetSession ?? Guid.Empty;
#if DEBUG
_sessionId = DateTime.Now.Ticks + sessionId.GetHashCode();
#else
_sessionId = Invoke(f => f.CreateSession(product, sessionId));
#endif
_pingTimer = ThreadingHelper.Timer(() =>
{
try
{
lock (_pingSync)
{
#if !DEBUG
if (_sessionId == 0)
return;
Invoke(f => f.Ping(_sessionId));
#endif
}
}
catch (Exception ex)
{
ex.LogError();
}
}).Interval(TimeSpan.FromMinutes(10));
}
///
/// Close the session.
///
public void CloseSession()
{
if (_sessionId == 0)
throw new InvalidOperationException();
_pingTimer.Dispose();
lock (_pingSync)
{
#if !DEBUG
Invoke(f => f.CloseSession(_sessionId));
#endif
_sessionId = 0;
}
}
///
/// Release resources.
///
protected override void DisposeManaged()
{
if (_sessionId != 0)
CloseSession();
base.DisposeManaged();
}
}
}