My application produces a compressed (gzip) xml messages to transfer over https to a webserver. It receives up to 10,000 data packets/sec from client apps through TCP/IP. Size of each packet could be from 500 to 2000 bytes. It should add a received packet to a corresponding xml message in the collection based on the packet metadata. When the length of the particular message reaches the specified limit (1024Kb, for example) it should be saved to file in the specified folder and then sent from from there to a web server.
What is the correct/efficient approach to do this?
I am considering two ways:
1) GZipStream > FileStream Write each packet on-the-fly directly to the corresponding file and watch on its size. But I am not sure that so frequent writes is good and i would like to keep the file system workload as low as possible.
2) GZipStream > MemoryStream > File Use a memory as a buffer for messages. In this case i should create a MemoryStream for each message and keep it open while adding packets. But in this case i don't know how to get memory stream's current length without closing it.
Example: I can get the ms.Length only after ms.close() call.
var ms = new MemoryStream();
var gstream = new GZipStream(ms, CompressionMode.Compress);
var writer = XmlWriter.Create(gstream);
writer.WriteStartElement("x", "root", "123");
writer.WriteElementString("packet_ID", "some data");
writer.WriteEndElement();
writer.Close();
gstream.Close();
ms.Close();
Console.WriteLine("Memory size: " + ms.ToArray().Length);
Console.ReadLine();