private async Task Run(IRtmpConnection connection) { var count = 1; var token = _tokenSource.Token; while (!token.IsCancellationRequested) { await Task.Delay(TimeSpan.FromMilliseconds(120000), token); if (token.IsCancellationRequested) return; var date = DateTime.UtcNow.ToString("ddd MMM d yyyy HH:mm:ss 'GMTZ'"); try { var session = _sessionManager.CurrentSession; if (session == null) continue; var invoke = new Invocation<int>("loginService", "performLCDSHeartBeat", session.accountSummary.accountId, session.token, count, date); await invoke.Execute(connection); } catch (AggregateException agg) { agg.Handle(e => e is TaskCanceledException); } count++; } _tokenSource = null; _heartbeat = null; }
public bool Disconnect() { try { _heartbeat.Cancel(); if (Connection != null) { _sessions.Logout(Connection).Wait(TimeSpan.FromSeconds(30)); Connection.Close(); Connection = null; } IsConnected = false; } catch (Exception) { IsConnected = false; throw; } finally { IsConnected = false; } return IsConnected; }
public async Task<bool> Connect(LeagueRegion region, AuthenticationCredentials credentials) { if (Connection == null) { var connection = Create(); var connected = await new ConnectionTask(connection).Initiate(region.ServerUri); if (!connected) return false; Connection = connection; } Connection.AddHeader(MessageBase.RequestTimeoutHeader, false, 60); Connection.AddHeader(MessageBase.FlexClientIdHeader, false, Guid.NewGuid().ToString()); Connection.AddHeader(MessageBase.EndpointHeader, false, Invocation.EndpointName); // Setup the session await _sessions.Login(Connection, credentials); // Subscribe to all of the fun messages await _subscriptions.Subscribe(Connection); // Startup the heartbeat _heartbeat.Start(Connection); return (IsConnected = true); }
public void Start(IRtmpConnection connection) { if (_heartbeat != null) return; if (_tokenSource != null) return; _tokenSource = new CancellationTokenSource(); _heartbeat = Task.Run(() => Run(connection), _tokenSource.Token); }
public async Task Logout(IRtmpConnection connection) { if (CurrentSession == null) return; if (string.IsNullOrWhiteSpace(CurrentSession.token)) return; var invoke = new Invocation<AcknowledgeMessage>("loginService", "logout", CurrentSession.token); var message = await invoke.Execute(connection); return; }
public async Task<LolSession> Login(IRtmpConnection connection, AuthenticationCredentials credentials) { Credentials = credentials; var invoke = new Invocation<LolSession>("loginService", "login", credentials); CurrentSession = await invoke.Execute(connection); await AuthorizeSession(connection, CurrentSession); if (_action != null) _action(); return CurrentSession; }
private static async Task AuthorizeSession(IRtmpConnection connection, LolSession session) { connection.AddHeader(MessageBase.FlexClientIdHeader, false, session.token); // note: username is case sensitive here for some reason... var token = string.Format("{0}:{1}", session.accountSummary.username, session.token); var tokenEncoded = Encoding.ASCII.GetBytes(token); var body = Convert.ToBase64String(tokenEncoded); var msg = new CommandMessage { operation = CommandMessage.LoginOperation, clientId = connection.ClientId, correlationId = null, messageId = Guid.NewGuid().ToString(), body = body, destination = string.Empty }; await new Invocation<string>("auth", msg).Execute(connection); }
public async Task Subscribe(IRtmpConnection connection) { var session = _sessionManager.CurrentSession; if (session == null) throw new InvalidOperationException("The session has not been created, yet. Unable to subscribe."); var bcMessage = CreateSubscribeMessage("bc", "bc-" + session.accountSummary.accountId); var cnMessage = CreateSubscribeMessage("cn-" + session.accountSummary.accountId, "cn-" + session.accountSummary.accountId); var gnMessage = CreateSubscribeMessage("gn-" + session.accountSummary.accountId, "gn-" + session.accountSummary.accountId); var bcInvoke = new Invocation<object>(bcMessage); await bcInvoke.Execute(connection); var cnInvoke = new Invocation<object>(cnMessage); await cnInvoke.Execute(connection); var gnInvoke = new Invocation<object>(gnMessage); await gnInvoke.Execute(connection); }
private void Dispose(bool disposing) { if (_disposed) return; if (!disposing) return; try { if (Connection != null) Disconnect(); if (_heartbeat != null) { _heartbeat.Dispose(); } if (_subscriptions != null) { _subscriptions.Dispose(); } if (_sessions != null) { _sessions.Dispose(); } } finally { _disposed = true; _heartbeat = null; _subscriptions = null; _sessions = null; _setup = null; _routingObject = null; Connection = null; } }
public ConnectionTask(IRtmpConnection connection) { _connection = connection; }