private void outputWorker() { List <byte> dataToSend = new List <byte>(); int sent = 0; int packetSizeCount = 0; int packetSizeSum = 0; int packetSizeAvg = 0; while (ShouldBeRunning) { Thread.Sleep(1); if (!socket.Connected) { break; } dataToSend.Clear(); packetSizeSum = 0; packetSizeCount = 0; while (DataToSendQueue.myCount > 0) { //packetSizeCount++; //packetSizeSum += DataToSendQueue.Peek().Length; dataToSend.AddRange(DataToSendQueue.DeQ()); } if (dataToSend.Count == 0) { continue; } //packetSizeAvg = packetSizeSum / packetSizeCount; try { //DateTime pre = DateTime.Now; //Send ALL bytes at once instead of "per packet", should be better int r = socket.Send(dataToSend.ToArray()); //DateTime post = DateTime.Now; sent += r; //double ts = (post - pre).TotalSeconds; //Trace.WriteLine("SocketComm output bytes actually sent: " + sent + " took " +ts + " seconds, Avg Packet Size:" + packetSizeAvg+ ", Packet Count"+packetSizeCount + " Rate="+(float)r/ts +"Bps"); //Trace.WriteLine("SocketComm output bytes actually sent: " + sent ); sent = 0; } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.StackTrace); } } }
private void inputProcWorker() { byte[] aPacket; byte[] lenBytes = new byte[4]; byte[] data = new byte[0]; int packetLength = -1; byte[] oldData = new byte[0]; while (ShouldBeRunning) { while (DataReceived.myCount > 0) { try { byte[] inData = DataReceived.DeQ() as byte[]; int requiredLength = oldData.Length + inData.Length; //if (data.Length < requiredLength) data = new byte[requiredLength]; // make data large enough to contain old and new Array.Copy(oldData, 0, data, 0, oldData.Length); // copy the existing data into the resized array, at the front Array.Copy(inData, 0, data, oldData.Length, inData.Length); // copy the newly dequeued data into the resized array, at the end. if (packetLength == -1 && // if the length of the next packet is unknown, and data.Length >= 4) // we have that much data on hand, { Array.Copy(data, lenBytes, 4); // copy packetLength = BitConverter.ToInt32(lenBytes, 0); if (packetLength <= 0 || packetLength > 5000) { throw new FormatException("fast packet length " + packetLength + " is unreasonably long."); } } if (packetLength != -1 && // we know how much data goes in the next packet, and data.Length >= packetLength + 4) // we have that much data on hand, { //Trace.WriteLine("fast Processing " + packetLength + " / " + data.Length); aPacket = new byte[packetLength]; // make a packet buffer Array.Copy(data, 4, aPacket, 0, packetLength); // copy the data for this packet into the packet buffer CallPacketReceived(aPacket); // send this packet off for processing int unCopiedBytes = data.Length - (packetLength + 4); // calculate bytes that were not in that packet (length of packet + 4 byte length header) oldData = new byte[unCopiedBytes]; Array.Copy(data, packetLength + 4, oldData, 0, unCopiedBytes); // shift the uncopied bytes up to the front (for queue-like behavior) packetLength = -1; // signal the unknown length of the next packet } } catch (Exception E) { Trace.WriteLine(E.Message); } } //Trace.WriteLine("sleeping"); Thread.Sleep(10); } }