public byte[] ReadBytes() { byte[] buffer = null; if (_isOpen) { buffer = new byte[_sharedBuffer.Count]; _sharedBuffer.CopyTo(buffer); } return(buffer); }
public byte[] ReadBytes() { byte[] ret = null; if (_isOpen) { ret = new byte[_sharedBuf.Count]; _sharedBuf.CopyTo(ret); //_sharedBuf. } return(ret); }
public void CopyTo_NullArray_ThrowsException() { var name = Guid.NewGuid().ToString(); using (var sma = new SharedArray <int>(name, 10)) { bool exceptionThrown = false; try { sma.CopyTo(null); } catch (ArgumentNullException) { exceptionThrown = true; } Assert.IsTrue(exceptionThrown, "null buffer should result in ArgumentNullException"); } }
private void Application_Idle(object sender, EventArgs e) { //Render bitmap // MessageBox.Show("RENDER"); byte[] _read = new byte[arr.Length]; arr.CopyTo(_read, 0); Rectangle rect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); BitmapData bmpData = _texture.LockBits(rect, ImageLockMode.WriteOnly, _texture.PixelFormat); IntPtr ptr = bmpData.Scan0; System.Runtime.InteropServices.Marshal.Copy(_read, 0, ptr, _read.Length); _texture.UnlockBits(bmpData); pictureBox1.Image = _texture; //Query message //clientSocket.Receive() CheckMessage(); }
//////////Added////////// public void UpdateInitialized() { if (Initialized) { SendPing(); if (_bufferBytes == null) { long arraySize = _mainTexArray.Length; Debug.Log("Memory array size:" + arraySize); _bufferBytes = new byte[arraySize]; } _mainTexArray.CopyTo(_bufferBytes, 0); lock (sPixelLock) { BrowserTexture.LoadRawTextureData(_bufferBytes); BrowserTexture.Apply(); } } }
public void AcquireWriteLock_ReadWrite_LocksCorrectly() { var name = Guid.NewGuid().ToString(); Random r = new Random(); int bufSize = 1024; byte[] data = new byte[bufSize]; byte[] readBuf = new byte[bufSize]; bool readIsFirst = false; bool readBlocked = false; int syncValue = 0; // Fill with random data r.NextBytes(data); using (var sma = new SharedArray <byte>(name, bufSize)) { // Acquire write lock early sma.AcquireWriteLock(); using (var smr = new SharedArray <byte>(name)) { var t1 = Task.Factory.StartNew(() => { if (System.Threading.Interlocked.Exchange(ref syncValue, 1) == 0) { readIsFirst = true; } // Should block until write lock is released smr.AcquireReadLock(); if (System.Threading.Interlocked.Exchange(ref syncValue, 3) == 4) { readBlocked = true; } smr.CopyTo(readBuf); smr.ReleaseReadLock(); }); System.Threading.Thread.Sleep(10); var t2 = Task.Factory.StartNew(() => { var val = System.Threading.Interlocked.Exchange(ref syncValue, 2); if (val == 0) { readIsFirst = false; } else if (val == 3) { readBlocked = false; } System.Threading.Thread.Sleep(10); sma.Write(data); System.Threading.Interlocked.Exchange(ref syncValue, 4); sma.ReleaseWriteLock(); }); Task.WaitAll(t1, t2); Assert.IsTrue(readIsFirst, "The read thread did not enter first."); Assert.IsTrue(readBlocked, "The read thread did not block."); // Check data was written before read for (var i = 0; i < readBuf.Length; i++) { Assert.AreEqual(data[i], readBuf[i]); } } } }
public void UpdateTexture() { if (Initialized) { if (_bufferBytes == null) { long arraySize = _mainTexArray.Length; //Debug.Log("Memory array size:" + arraySize); _bufferBytes = new byte[arraySize]; } _mainTexArray.CopyTo(_bufferBytes, 0); lock (sPixelLock) { BrowserTexture.LoadRawTextureData(_bufferBytes); BrowserTexture.Apply(); } SendPing(); //execute run-once functions if (_needToRunOnce) { SendExecuteJSEvent(_runOnceJS); _needToRunOnce = false; } } else { try { //GetProcesses does not work for x86. //so we have just to wait while the plugin started. //string processName = _pluginProcess.ProcessName;//could be InvalidOperationException //foreach (System.Diagnostics.Process clsProcess in System.Diagnostics.Process.GetProcesses()) //{ // if (clsProcess.ProcessName.Contains(processName)) //HACK: on x86 we have: <process name> (32 bit) { Thread.Sleep(200); //give it some time to initialize try { //Connect // _clientSocket = new TcpClient("127.0.0.1", _port); //start listen _clientSocket.GetStream() .BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(StreamReceiver), null); //init memory file _mainTexArray = new SharedArray <byte>(_sharedFileName); Initialized = true; } catch (Exception ex) { //SharedMem and TCP exceptions Debug.Log("Exception on init:" + ex.Message + ".Waiting for plugin server"); } } //} } catch (Exception) { //InvalidOperationException } } }