/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposing = true; if (_bufferThread != null && _bufferThread.ThreadState != ThreadState.Stopped && !_bufferThread.Join(500)) { _bufferThread.Abort(); } if (_buffer != null) { _buffer.Dispose(); _buffer = null; } if (_decoder != null) { _decoder.Dispose(); _decoder = null; } CloseResponse(); } _disposed = true; }
private void BufferProc(object o) { var resetEvent = o as EventWaitHandle; try { if (_stream == null || _stream.CanRead == false) { throw new Exception("Mp3WebStream not initialized"); } byte[] buffer = null; int read; Mp3Format format, prevFormat; Mp3Frame frame; read = ReadRawDataFromFrame(ref buffer, out frame); format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate); _buffer = new FixedSizeBuffer <byte>(format.BytesPerSecond * 2); do { _buffer.Write(buffer, 0, read); read = ReadRawDataFromFrame(ref buffer, out frame); prevFormat = format; format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate); } while (_buffer.Buffered < _buffer.Length / 10 || !format.Equals(prevFormat)); _decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false); if (resetEvent != null) { resetEvent.Set(); } do { if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing) { Thread.Sleep(250); } else { _buffer.Write(buffer, 0, read); read = ReadRawDataFromFrame(ref buffer, out frame); } } while (!_disposing); } finally { if (resetEvent != null) { resetEvent.Set(); } } }
private void CreateStream(bool async) { _disposing = false; Func <bool> func = () => { var resetEvent = new ManualResetEvent(false); bool success = InitializeConnection(); if (success) { _bufferThread = new Thread(BufferProc); _bufferThread.Start(resetEvent); success = resetEvent.WaitOne(); if (success) { _decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false); } } EventHandler <ConnectionEstablishedEventArgs> handler = this.ConnectionEstablished; if (handler != null && async) { handler(this, new ConnectionEstablishedEventArgs(_address, success)); } return(success); }; if (async) { ThreadPool.QueueUserWorkItem(o => func()); } else { func(); } }
private async Task<IWaveSource> GetSoundSource(IPlaySource track, long position) { _soundSourceLoadingToken?.Cancel(); _soundSourceLoadingToken = new CancellationTokenSource(); var token = _soundSourceLoadingToken.Token; IWaveSource result = null; TrackBitrate = track.Bitrate; try { switch (track.Type) { case PlaySourceType.LocalFile: result = await Task.Run(() => CodecFactory.Instance.GetCodec(((LocalFilePlaySource) track).Path), token); break; case PlaySourceType.Http: result = await Task.Run(() => CodecFactory.Instance.GetCodec(((HttpPlaySource) track).WebUri), token); break; case PlaySourceType.Stream: result = new DmoMp3Decoder(((StreamPlaySource) track).Stream); break; default: throw new ArgumentOutOfRangeException(); } // ReSharper disable once AccessToDisposedClosure await Task.Run(() => result.Position = position, token); } catch (TaskCanceledException) { result?.Dispose(); return null; } return token.IsCancellationRequested ? null : result; }
/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposing = true; if (_bufferThread != null && _bufferThread.ThreadState != ThreadState.Stopped && !_bufferThread.Join(500)) _bufferThread.Abort(); if (_buffer != null) { _buffer.Dispose(); _buffer = null; } if (_decoder != null) { _decoder.Dispose(); _decoder = null; } CloseResponse(); } _disposed = true; }
private void CreateStream(bool async) { _disposing = false; Func<bool> func = () => { var resetEvent = new ManualResetEvent(false); bool success = InitializeConnection(); if (success) { _bufferThread = new Thread(BufferProc); _bufferThread.Start(resetEvent); success = resetEvent.WaitOne(); if(success) _decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false); } EventHandler<ConnectionEstablishedEventArgs> handler = this.ConnectionEstablished; if (handler != null && async) handler(this, new ConnectionEstablishedEventArgs(_address, success)); return success; }; if (async) ThreadPool.QueueUserWorkItem(o => func()); else func(); }
private void OnReceive(IAsyncResult ar) { try { clientSocket.EndReceive(ar); if (byteData[0] == 8) { EviData data = new EviData(byteData); Evidence evi = new Evidence(); evi.name = data.strName; evi.desc = data.strDesc; evi.note = data.strNote; evi.index = data.index; using (MemoryStream ms = new MemoryStream(data.dataBytes)) { evi.icon = Image.FromStream(ms, false, true); } bool found = false; foreach (Evidence item in eviList) { if (item.index == evi.index) { found = true; item.name = evi.name; item.note = evi.note; item.desc = evi.desc; item.icon = evi.icon; break; } } if (found == false) eviList.Add(evi); testimonyPB.Location = new Point(257, 3); testimonyPB.BringToFront(); PictureBox icon = new PictureBox(); icon.Image = evi.icon; icon.Location = new Point(6, 5); icon.Size = new Size(70, 70); icon.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(icon); }); Label name = new Label(); name.Text = evi.name; name.Location = new Point(91, 8); name.Size = new Size(155, 17); name.TextAlign = ContentAlignment.MiddleCenter; name.ForeColor = Color.DarkOrange; name.BackColor = Color.Transparent; //name.Font = new Font(fonts.Families[0], 12.0f, FontStyle.Bold); name.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(name); }); Label note = new Label(); note.Text = evi.note; note.Location = new Point(92, 26); note.Size = new Size(153, 44); //note.Font = new Font(fonts.Families[0], 12.0f); note.BackColor = Color.Transparent; note.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(note); }); Label desc = new Label(); desc.Text = evi.desc; desc.Location = new Point(9, 81); desc.Size = new Size(238, 45); //desc.Font = new Font(fonts.Families[0], 12.0f); desc.BackColor = Color.Transparent; desc.ForeColor = Color.White; desc.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(desc); }); testimonyPB.Size = new Size(256, 127); testimonyPB.Image = Image.FromFile("base/misc/inventory_update.png"); wr = new WaveFileReader("base/sounds/general/sfx-selectjingle.wav"); sfxPlayer.Initialize(wr); if (!mute) sfxPlayer.Play(); for (int x = 0; x <= 64; x++) { testimonyPB.Location = new Point(256 - (4 * x), 3); //icon.Location = new Point(256 + 6 - (2 * x), 3 + 5); //name.Location = new Point(256 + 91 - (2 * x), 3 + 8); //note.Location = new Point(256 + 92 - (2 * x), 3 + 26); //desc.Location = new Point(256 + 9 - (2 * x), 3 + 81); icon.Refresh(); name.Refresh(); note.Refresh(); desc.Refresh(); } System.Threading.Thread.Sleep(3000); for (int x = 0; x <= 64; x++) { testimonyPB.Location = new Point(0 - (4 * x), 3); //icon.Location = new Point(6 - (2 * x), 3 + 5); //name.Location = new Point(91 - (2 * x), 3 + 8); //note.Location = new Point(92 - (2 * x), 3 + 26); //desc.Location = new Point(9 - (2 * x), 3 + 81); icon.Refresh(); name.Refresh(); note.Refresh(); desc.Refresh(); } testimonyPB.Image = null; name.Dispose(); icon.Dispose(); desc.Dispose(); note.Dispose(); } else { Data msgReceived = new Data(byteData); //Accordingly process the message received switch (msgReceived.cmdCommand) { case Command.Login: break; case Command.Logout: break; case Command.ChangeMusic: if (msgReceived.strMessage != null && msgReceived.strMessage != "" & msgReceived.strName != null) { appendTxtLogSafe("<<<" + msgReceived.strName + " changed the music to " + msgReceived.strMessage + ">>>\r\n"); musicReader = new DmoMp3Decoder("base/sounds/music/" + msgReceived.strMessage); if (musicPlayer.PlaybackState != PlaybackState.Stopped) musicPlayer.Stop(); musicPlayer.Initialize(musicReader); if (!mute) musicPlayer.Play(); } break; case Command.ChangeHealth: if (msgReceived.strName == "def") { if (msgReceived.strMessage == "-1") defHealth--; else if (msgReceived.strMessage == "+1") defHealth++; } else if (msgReceived.strName == "pro") { if (msgReceived.strMessage == "-1") proHealth--; else if (msgReceived.strMessage == "+1") proHealth++; } updateHealth(); break; case Command.Message: case Command.Present: if (latestMsg != null && msgReceived.strName == latestMsg.strName) { newGuy = false; } else { newGuy = true; testimonyPB.Image = null; } latestMsg = msgReceived; objectLayerPB.Image = null; objectLayerPB.Location = new Point(0, 0); objectLayerPB.Size = new Size(256, 192); if (msgReceived.callout <= 3) { sendEnabled = false; curPreAnimTime = 0; curPreAnimTime = 0; curPreAnim = null; soundTime = 0; curSoundTime = 0; if (msgReceived.callout > 0) performCallout(); if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav")) { sfxPlayer.Stop(); wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); sfxPlayer.Initialize(wr); soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim); } /* if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & (File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav") | File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"))) { sfxPlayer.Stop(); if (File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav")) wr = new WaveFileReader("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); else wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); sfxPlayer.Initialize(wr); soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim); } */ if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5) ChangeSides(true); else ChangeSides(); //If there is no pre-animation if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5 | iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) == null | iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim) <= 0) { charLayerPB.Enabled = true; setCharSprite("base/characters/" + msgReceived.strName + "/(b)" + iniParser.GetAnim(msgReceived.strName, msgReceived.anim) + ".gif"); if (msgReceived.cmdCommand == Command.Present) { sfxPlayer.Stop(); wr = new WaveFileReader("base/sounds/general/sfx-shooop.wav"); sfxPlayer.Initialize(wr); if (!mute) sfxPlayer.Play(); switch (iniParser.GetSide(msgReceived.strName)) { case "def": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "pro": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(13, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "hld": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(13, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "hlp": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; default: testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; } msgReceived.strMessage = msgReceived.strMessage.Split('|')[0]; } prepWriteDispBoxes(msgReceived, msgReceived.textColor); } else //if there is a pre-animation { //charLayerPB.Enabled = false; setCharSprite("base/characters/" + msgReceived.strName + "/" + iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) + ".gif"); preAnimTime = iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim); curPreAnim = iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim); } //dispTextRedraw.Enabled = true; } else { performCallout(); } break; case Command.List: appendTxtLogSafe("<<<" + strName + " has entered the courtroom>>>\r\n"); break; case Command.DataInfo: //Do the stuff with the incoming server data here //The user has logged into the system so we now request the server to send //the names of all users who are in the chat room Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Login; msgToSend.strName = strName; byteData = new byte[1048576]; byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); byteData = new byte[1048576]; break; case Command.PacketSize: break; } if (msgReceived.strMessage != null & msgReceived.cmdCommand == Command.Message | msgReceived.cmdCommand == Command.Login | msgReceived.cmdCommand == Command.Logout) { if (msgReceived.callout <= 3) appendTxtLogSafe(msgReceived.strMessage + "\r\n"); } if (msgReceived.cmdCommand != Command.PacketSize) byteData = new byte[1048576]; else byteData = new byte[Convert.ToInt32(msgReceived.strMessage)]; } clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } catch (SocketException) { if (MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK) { Close(); } } catch (ObjectDisposedException) { } catch (Exception ex) { if (Program.debug) MessageBox.Show(ex.Message + ".\r\n" + ex.StackTrace.ToString(), "AODXClient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BufferProc(object o) { var resetEvent = o as EventWaitHandle; try { if (_stream == null || _stream.CanRead == false) throw new Exception("Mp3WebStream not initialized"); byte[] buffer = null; int read; Mp3Format format, prevFormat; Mp3Frame frame; read = ReadRawDataFromFrame(ref buffer, out frame); format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate); _buffer = new FixedSizeBuffer<byte>(format.BytesPerSecond * 2); do { _buffer.Write(buffer, 0, read); read = ReadRawDataFromFrame(ref buffer, out frame); prevFormat = format; format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate); } while (_buffer.Buffered < _buffer.Length / 10 || !format.Equals(prevFormat)); _decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false); if (resetEvent != null) resetEvent.Set(); do { if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing) Thread.Sleep(250); else { _buffer.Write(buffer, 0, read); read = ReadRawDataFromFrame(ref buffer, out frame); } } while (!_disposing); } finally { if (resetEvent != null) resetEvent.Set(); } }