示例#1
0
        /// <summary>
        /// Starts a new stream session. Call ExtendSession to extend the session.
        /// </summary>
        public async Task <StreamSession> OpenStreamAsync(
            int streamId,
            CancellationToken cancel)
        {
            var streamConfig = (await GetStreamConfigsAsync(cancel)).FirstOrDefault(s => s.Id == streamId);

            if (streamConfig == null)
            {
                throw new NotFoundException("No stream found by Id");
            }

            var token    = Rando.GetReallyRandomString(30);
            var dirToken = Rando.GetReallyRandomString(30);
            var session  = new StreamSession()
            {
                Id         = Guid.NewGuid(),
                StreamId   = streamConfig.Id,
                StreamName = streamConfig.Name,
                Token      = token,
                Expirers   = DateTime.UtcNow.AddSeconds(_Config.ClientSessionTTLSeconds),
                TTLSeconds = _Config.ClientSessionTTLSeconds,
                Uri        = $"Stream/{dirToken}/{StreamName}"
            };

            lock (_Sync){
                _Sessions.Add(session);
            }

            var ready = new TaskCompletionSource <bool>();

            RunStreamAsync(session, streamConfig, dirToken, ready, cancel, _Cancel.Token).LogErrors();

            using (cancel.Register(() => {
                ready.TrySetCanceled();
            }))
            {
                await ready.Task;
            }

            return(session);
        }