/// <summary> /// On connection open. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnConnected(object sender, EventArgs e) { // Create the recorder and audio buffer. _recorder = new IO.Audio.WaveRecorder(_device); _recorder.DataAvailable += _recorder_DataAvailable; _recorder.RecordingStopped += _recorder_RecordingStopped; _recorder.BufferMilliseconds = _bufferMilliseconds; // Open the audio buffer. _audioBuffer = new IO.Stream.StreamBufferBase(); _recorder.Open(_audioBuffer, new IO.Audio.Wave.WaveFormatProvider(_waveSampleRate, _waveBitsPerSample, _waveChannels), IO.Audio.AudioRecordingFormat.WaveInEvent); // Create a default audio sample rate. // Write the first header data to the buffer. Nequeo.IO.Audio.WaveStructure waveStructure = Nequeo.IO.Audio.WaveStructure.CreateDefaultStructure(_waveChannels, _waveSampleRate, _waveBitsPerSample, new byte[0]); Nequeo.IO.Audio.WaveFormat waveFormat = new Nequeo.IO.Audio.WaveFormat(); bool ret = waveFormat.Write(_audioBuffer, waveStructure); // Get the number of wave header bytes. long headerSize = _audioBuffer.Length; // Lock the client lock (_syncObject) { byte[] buffer = new byte[(int)headerSize]; int read = _audioBuffer.Read(buffer, 0, (int)headerSize); // Send the header data to the speech server. _client.Send(buffer.Take(read).ToArray(), false); } // Start recording. _recorder.Start(); OnRecording?.Invoke(this, new EventArgs()); }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // Note disposing has been done. _disposed = true; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (_audioBuffer != null) { _audioBuffer.Dispose(); } if (_recorder != null) { _recorder.Dispose(); } if (_client != null) { _client.Dispose(); } } _audioBuffer = null; _recorder = null; _client = null; _syncObject = null; } }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // Note disposing has been done. _disposed = true; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (_requestBuffer != null) { _requestBuffer.Dispose(); } if (_responseBuffer != null) { _responseBuffer.Dispose(); } } // Call the appropriate methods to clean up // unmanaged resources here. _requestBuffer = null; _responseBuffer = null; } }
/// <summary> /// Http server request. /// </summary> public HttpRequest() { // Create the original header list. OriginalHeaders = new List <NameValue>(); // Create the buffers. _input = new Nequeo.IO.Stream.StreamBufferBase(); }
/// <summary> /// Open the recorder. /// </summary> /// <param name="outStream">The stream to write the audio to.</param> /// <param name="format">The audio wave format.</param> /// <param name="audioRecordingFormat">The audio recording format.</param> public void Open(Nequeo.IO.Stream.StreamBufferBase outStream, WaveFormatProvider format = null, AudioRecordingFormat audioRecordingFormat = AudioRecordingFormat.WaveIn) { // If not created. if (!_waveInCreated) { _audioStream = outStream; _internalStream = false; _isBufferStream = true; // Initialise. Init(format, audioRecordingFormat); _waveInCreated = true; } }
/// <summary> /// On connection closed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDisconnected(object sender, EventArgs e) { if (_recorder != null) { _recorder.Close(); } if (_audioBuffer != null) { _audioBuffer.Dispose(); } if (_recorder != null) { _recorder.Dispose(); } _audioBuffer = null; _recorder = null; }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { // Note disposing has been done. _disposed = true; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (_input != null) { _input.Dispose(); } } // Call the appropriate methods to clean up // unmanaged resources here. _input = null; } }
/// <summary> /// On web socket context. /// </summary> /// <param name="webSocketContext">The asp net web socket context.</param> private async void OnChatWebSocketContext(System.Web.WebSockets.AspNetWebSocketContext context) { WebSocket webSocket = null; Nequeo.Net.WebSockets.WebSocketMember member = null; Nequeo.Collections.CircularBuffer <byte> requestBuffer = null; Nequeo.IO.Stream.StreamBufferBase requestStream = null; try { // Get the current web socket. webSocket = context.WebSocket; // Create the web socket member and // add to the member collection. member = new Nequeo.Net.WebSockets.WebSocketMember(webSocket); AddMember(member); // Holds the receive data. bool hasBeenFound = false; byte[] store = new byte[0]; byte[] receiveBuffer = new byte[READ_BUFFER_SIZE]; // Create the stream buffers. requestBuffer = new Collections.CircularBuffer <byte>(base.RequestBufferCapacity); requestStream = new Nequeo.IO.Stream.StreamBufferBase(requestBuffer); requestBuffer.RemoveItemsWritten = true; // Create the current chat state. Chat.ChatWebSocketState chatState = new Chat.ChatWebSocketState() { Member = member, RequestStream = requestStream, WebSocket = webSocket }; CancellationTokenSource receiveCancelToken = new CancellationTokenSource(); // While the WebSocket connection remains open run a // simple loop that receives data and sends it back. while (webSocket.State == WebSocketState.Open) { // Receive the next set of data. ArraySegment <byte> arrayBuffer = new ArraySegment <byte>(receiveBuffer); WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(arrayBuffer, receiveCancelToken.Token); // Assign the member properties. member.ReceiveResult = receiveResult; member.TimeoutTime = DateTime.Now; requestStream.Write(receiveBuffer, 0, receiveResult.Count); // If the connection has been closed. if (receiveResult.MessageType == WebSocketMessageType.Close) { // Close the connection. member.Close(); break; } else { // Store the data. byte[] temp = null; if (!hasBeenFound) { temp = store.CombineParallel(receiveBuffer); // Find the end of the data. hasBeenFound = Nequeo.Net.Utility.IsParse2CRLF(temp); // Store the data until the end. store = temp; temp = null; } else { // If this is the end of the message. if (receiveResult.EndOfMessage) { // Clear the store. store = null; store = new byte[0]; hasBeenFound = false; string resource = ""; // Get the request headers. List <Nequeo.Model.NameValue> headers = base.ParseHeaders(requestStream, out resource, base.HeaderTimeout); // All headers have been found. if (headers != null) { // Get the execution member. // Set the calling member. string executionMember = headers.First(m => m.Name.ToUpper().Contains("MEMBER")).Value; string actionName = headers.First(m => m.Name.ToUpper().Contains("ACTIONNAME")).Value; // Assign the values. chatState.Headers = headers; chatState.ExecutionMember = executionMember; chatState.ErrorCode = new Exceptions.ErrorCodeException("OK", 200); try { // Validate the current user token. bool isTokenValid = ValidateToken(chatState); } catch (Exceptions.ErrorCodeException exc) { // Get the error code. chatState.ErrorCode = exc; } catch (Exception ex) { // Internal error. chatState.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500); } // Send a message back to the client indicating that // the message was recivied and was sent. await webSocket.SendAsync(new ArraySegment <byte>( CreateResponse(chatState.ErrorCode.ErrorCode, true, executionMember, actionName, chatState.ErrorCode.Message)), WebSocketMessageType.Binary, true, CancellationToken.None); } } } } } // Cancel the receive request. if (webSocket.State != WebSocketState.Open) { receiveCancelToken.Cancel(); } } catch { } finally { // If a member context exists. if (member != null) { try { // Remove the member context // from the collection. RemoveMember(member); } catch { } member = null; } // Clean up by disposing the WebSocket. if (webSocket != null) { webSocket.Dispose(); } if (requestBuffer != null) { requestBuffer.Dispose(); } if (requestStream != null) { requestStream.Dispose(); } if (_communication != null) { try { // Remove the client from the communication service. _communication.RemoveClient(member.UniqueIdentifier, base.ServiceName, _machineName, null, actionName: member.UniqueIdentifier); } catch { } } } }
/// <summary> /// Http server request. /// </summary> public HttpResponse() { // Create the buffers. _output = new Nequeo.IO.Stream.StreamBufferBase(); }
/// <summary> /// Http server request. /// </summary> public HttpResponse() { // Create the buffers. _output = new Nequeo.IO.Stream.StreamBufferBase(); ProtocolVersion = "HTTP/2"; }