public static List <byte[]> PackageData(int id, byte[] rawData) { //Determin the amount of parts needed int envelopeSize = (1024 - 16); // 16 bit header int parts = rawData.Length / envelopeSize; if (rawData.Length % envelopeSize > 0) { parts++; } // Create a temp output object List <byte[]> outPut = new List <byte[]>(); for (int i = 0; i < parts; i++) { // Check how much data is left // More than env Size, trim to size // Less than or equal int sizeOfDataToWrite = rawData.Length - (i * envelopeSize); if (sizeOfDataToWrite > envelopeSize) { sizeOfDataToWrite = envelopeSize; } // Copy the array chunk to a holder, // create and add the data to output byte[] dataHolder = new byte[envelopeSize]; Array.ConstrainedCopy(rawData, i * envelopeSize, dataHolder, 0, sizeOfDataToWrite); NetDataEnvelope bit = new NetDataEnvelope(id, i, parts, dataHolder); outPut.Add(bit.rawdata); } return(outPut); }
public DataGroup(NetDataEnvelope firstMember, string owner_id) { message_owner = owner_id; heldMembers = new List <NetDataEnvelope>(); heldMembers.Add(new NetDataEnvelope( firstMember.Ident, firstMember.PartNo, firstMember.PartTotal, firstMember.DataInsideEnvelope.ToArray() )); Ident = firstMember.Ident; recTime = DateTime.Now; }
/// <summary> /// Returns true if the message data is complete /// out data is the internal data, header is stripped /// </summary> /// <param name="msg">byte array from the network stream</param> /// <param name="internalDataAsBytes">output when available</param> /// <returns>true if data is available on the 'out'</returns> public static bool ReceiveData(string owner_id, byte[] msg, out byte[] internalDataAsBytes) { //This could have its own thread.... timeoutHandle(); //convert the data NetDataEnvelope env = new NetDataEnvelope(msg); // see if multipart or not // handle multipart buffer if (env.PartTotal == 1) { // is not multipart // return the internal data straight from the message internalDataAsBytes = env.DataInsideEnvelope; return(true); } else if (env.PartTotal > 1) { DataGroup group = DataGroup.Combine(owner_id, env.Ident, dataGroups); if (group == null) { dataGroups.Add(new DataGroup(env, owner_id)); internalDataAsBytes = new byte[0]; return(false); } else { if (group.addPart(env)) { // is done internalDataAsBytes = group.AllInternalData; // Set the return data dataGroups.Remove(group); // We are done with this group, remove it return(true); // return true so system knows to read data } else { // Update recTime so does not timeout group.recTime = DateTime.Now; } internalDataAsBytes = new byte[0]; return(false); } } //ERROR part total less than 1 ??? internalDataAsBytes = new byte[0]; return(false); }
public bool addPart(NetDataEnvelope partToAdd) { //Add it and return true if done //heldMembers.Add(partToAdd); heldMembers.Add(new NetDataEnvelope( partToAdd.Ident, partToAdd.PartNo, partToAdd.PartTotal, partToAdd.DataInsideEnvelope.ToArray() )); if (heldMembers.Count == partToAdd.PartTotal) { return(true); } return(false); }