コード例 #1
0
        public void Handle(AddDataMessage <T> message)
        {
            List <T> result = new List <T>();

            if (File.Exists(this._tempFileName))
            {
                string data = "";
                using (this._locker.Acquire(Consts.DefaultWaitTime))
                {
                    data = File.ReadAllText(this._tempFileName, Encoding.UTF8);
                }
                result = JsonConvert.DeserializeObject <List <T> >(data);
            }

            if (!result.Any(r => r.Id.Equals(message.Data.Id)))
            {
                result.Add(message.Data);
            }

            string serializedResult = JsonConvert.SerializeObject(result);

            using (this._locker.Acquire(Consts.DefaultWaitTime))
            {
                File.WriteAllText(this._tempFileName, serializedResult, Encoding.UTF8);
            }
        }
コード例 #2
0
        /// <summary>
        /// Send a block of data as a sequence of AddData and binary messages.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private async Task SendData(ArraySegment <byte> data)
        {
            // This is the size of the binary websocket message
            const int messageBlockSize = 1024;

            var arrayCopy          = data.ToArray();
            var finalSectionOffset = 0;

            var msg = new AddDataMessage
            {
                size   = data.Count,
                offset = 0,
                seq_no = Interlocked.Add(ref _sequenceNumber, 1)
            };

            await msg.Send(_wsClient, _api.CancelToken);

            if (data.Count > messageBlockSize)
            {
                for (var offset = 0; offset < data.Count / messageBlockSize; offset += messageBlockSize)
                {
                    finalSectionOffset += messageBlockSize;
                    await _wsClient.SendAsync(new ArraySegment <byte>(arrayCopy, offset, messageBlockSize),
                                              WebSocketMessageType.Binary, false, _api.CancelToken);
                }
            }

            await _wsClient.SendAsync(
                new ArraySegment <byte>(arrayCopy, finalSectionOffset, data.Count - finalSectionOffset),
                WebSocketMessageType.Binary, true, _api.CancelToken);
        }
コード例 #3
0
        /// <summary>
        /// Send new chunk of audio data to recognize.
        /// </summary>
        /// <param name="data">Binary audio data. Must be less then <see cref="MaxChunkSize"/>.</param>
        /// <param name="lastChunk">Indicates this chunk is the last chunk in current session. If true server forms final results and closes connection after next result request.</param>
        /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="OperationCanceledException"></exception>
        public async Task <SendChunkResult> SendChunkAsync(byte[] data, bool lastChunk = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (data == null & !lastChunk)
            {
                throw new InvalidOperationException("Null data allowed only for last chunk.");
            }

            if (data?.Length > MaxChunkSize)
            {
                throw new ArgumentOutOfRangeException(nameof(data), data.Length, "Chunk size must be less than 1 MB.");
            }

            ThrowIfDisposed();

            var message = new AddDataMessage
            {
                AudioData = data,
                LastChunk = lastChunk
            };

            try
            {
                await _newtworkStream.SendMessageAsync(message, cancellationToken).ConfigureAwait(false);

                return(SendChunkResult.OkResult);
            }
            catch (IOException ioException) when(ioException.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut)
            {
                return(SendChunkResult.TimedOut);
            }
            catch (SocketException socketException) when(socketException.SocketErrorCode == SocketError.TimedOut)
            {
                return(SendChunkResult.TimedOut);
            }
            catch (SocketException socketException)
            {
                Dispose();
                return(new SendChunkResult(socketException.SocketErrorCode));
            }
        }