public byte[] GetDataQ() { SVSEResponse response = (SVSEResponse)GoodQ.Dequeue();//There should be one response byte[] rawData = new byte[BytesExpected]; long allPos = 0; //Keep track of position while (DataQ.Count > 0) //Copy all data into one array { byte[] bytes = (byte[])DataQ.Dequeue(); bytes.CopyTo(rawData, allPos); allPos += bytes.Length; } //first 4 have the length byte[] data = new byte[rawData.Length - 4]; for (int i = 0; i < data.Length; i++) { data[i] = rawData[i + 4]; } return(data); }
void socketComm_OnDataIn(object sender, byte[] data) { string text = Encoding.ASCII.GetString(data); _RESPONSE.Text = text.Substring(0, 16);//remove the CRLF log.InfoFormat("Rcvd: {0}", _RESPONSE.Text); if (_RESPONSE.Handle != null) //LOGIN, OPENDATA, HARTBEAT { _eventRESPONSE.Set(); //Execute the waiting method } else if (text.StartsWith("FAIL")) //Capture Failed { if (_POLLDATA.HandleWAIT != null) { _POLLDATA.HandleWAIT.Unregister(null); _POLLDATA.HandleWAIT = null; } // Stop future execution of the callback method if (_POLLDATA.HandleDONE != null) { _POLLDATA.HandleDONE.Unregister(null); _POLLDATA.HandleDONE = null; } // Stop future execution of the callback method _POLLDATA.ClearQs(); string description = "Capture failed! "; switch (text) { case "FAILTRACTDNZFAIL": description += "The buffer is not full."; break; default: description += text; break; } if (OnError.GetInvocationList().Length > 0) { OnError(this, new Exception(description)); } } else { //Capture Succeeded if (text.Substring(8, 4) == "DONE") { // The server has finished sending data but it is possible that we // have not received it all on the data port, so this will cause the // callback method to run periodically until all the data is received _POLLDATA.HandleWAIT = ThreadPool.RegisterWaitForSingleObject( _eventCHECKDATA, //Register Wait Handle new WaitOrTimerCallback(CallbackCHECKDATA), //Delegate/method to call when signaled null, //Object passed to the delegate msRecheckData, //Timeout false); _eventCHECKDATA.Set();//Check now } else { //Store the response SVSEResponse response = new SVSEResponse(); response.Text = _currCommand.ToString(); response.Count = Convert.ToInt32(text.Substring(4, 4), 10); response.Length = Convert.ToInt32(text.Substring(8, 4), 10); _POLLDATA.GoodQ.Enqueue(response); } } }