/// <summary> /// Asynchronously read from network stream /// </summary> /// <param name="ar">Ar.</param> private void OnReadCompletedCallback(IAsyncResult ar) { StreamStruct streamStruct = (StreamStruct)ar.AsyncState; int mycount = streamStruct.stream.EndRead(ar); // Translate data bytes to a ASCII string. String data = System.Text.Encoding.ASCII.GetString(streamStruct.data, 0, mycount); Debug.Log(string.Format("Received: {0}", data)); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. use begin write to avoid blocking // the main unity thread Debug.Log(string.Format("Sending: {0} ...", data)); streamStruct.stream.BeginWrite(msg, 0, msg.Length, myWriteCallBack, streamStruct.stream); //queue up another read SendMSGBack(streamStruct.stream); }
private Dictionary <ENodeType, StreamStruct> CreateStreams(EDeflateCompression CompressMode) { if (IsFileWrite) { return(CreateFileStreams(CompressMode)); } WebRequests.Clear(); Dictionary <ENodeType, StreamStruct> Streams = new Dictionary <ENodeType, StreamStruct>(); EProcessedFileType HierarchyFileType = EProcessedFileType.HIERARCHY_RAF; EProcessedFileType GeometryFileType = EProcessedFileType.GEOMETRY_RAF; EProcessedFileType MetadateFileType = EProcessedFileType.METADATA_RAF; if (CompressMode == EDeflateCompression.Compress) { HierarchyFileType = EProcessedFileType.HIERARCHY_CF; GeometryFileType = EProcessedFileType.GEOMETRY_CF; MetadateFileType = EProcessedFileType.METADATA_CF; } HttpWebRequest HierarchyRequest = CreateWebRequest(UploadUrls[HierarchyFileType]); lock (WebRequests) { WebRequests.Add(HierarchyRequest); } Stream WriteHierarchyStream = HierarchyRequest.GetRequestStream(); StreamStruct HierarchyStreamStruct = new StreamStruct(WriteHierarchyStream, CompressMode); HttpWebRequest GeometryRequest = CreateWebRequest(UploadUrls[GeometryFileType]); lock (WebRequests) { WebRequests.Add(GeometryRequest); } Stream WriteGeometryStream = GeometryRequest.GetRequestStream(); StreamStruct GeometryStreamStruct = new StreamStruct(WriteGeometryStream, CompressMode); HttpWebRequest MetadataRequest = CreateWebRequest(UploadUrls[MetadateFileType]); lock (WebRequests) { WebRequests.Add(MetadataRequest); } Stream WriteMetadataStream = MetadataRequest.GetRequestStream(); StreamStruct MetadataStreamStruct = new StreamStruct(WriteMetadataStream, CompressMode); Streams.Add(ENodeType.Hierarchy, HierarchyStreamStruct); Streams.Add(ENodeType.Geometry, GeometryStreamStruct); Streams.Add(ENodeType.Metadata, MetadataStreamStruct); return(Streams); }
private void ReadCompletedCallback(IAsyncResult ar) { StreamStruct streamStruct = (StreamStruct)ar.AsyncState; int mycount = streamStruct.stream.EndRead(ar); Debug.Log("Completing read " + mycount + ": " + System.Text.Encoding.ASCII.GetString(streamStruct.data, 0, mycount)); // Close everything. stream.Close(); client.Close(); }
private Dictionary <ENodeType, StreamStruct> CreateFileStreams(EDeflateCompression CompressMode) { WriteStreams.Clear(); Dictionary <ENodeType, StreamStruct> Streams = new Dictionary <ENodeType, StreamStruct>(); EProcessedFileType HierarchyFileType = EProcessedFileType.HIERARCHY_RAF; EProcessedFileType GeometryFileType = EProcessedFileType.GEOMETRY_RAF; EProcessedFileType MetadateFileType = EProcessedFileType.METADATA_RAF; string Ext = "x3dp_"; if (CompressMode == EDeflateCompression.Compress) { Ext = "x3dc_"; GeometryFileType = EProcessedFileType.GEOMETRY_CF; HierarchyFileType = EProcessedFileType.HIERARCHY_CF; MetadateFileType = EProcessedFileType.METADATA_CF; } Stream WriteHierarchyStream = new FileStream($"{UploadUrls[HierarchyFileType]}-{CurrentFileSet}.{Ext}h", FileMode.Create); lock (WriteStreams) { WriteStreams.Add(WriteHierarchyStream); } StreamStruct HierarchyStreamStruct = new StreamStruct(WriteHierarchyStream, CompressMode); Stream WriteGeometryStream = new FileStream($"{UploadUrls[GeometryFileType]}-{CurrentFileSet}.{Ext}g", FileMode.Create); lock (WriteStreams) { WriteStreams.Add(WriteGeometryStream); } StreamStruct GeometryStreamStruct = new StreamStruct(WriteGeometryStream, CompressMode); Stream WriteMetadataStream = new FileStream($"{UploadUrls[MetadateFileType]}-{CurrentFileSet}.{Ext}m", FileMode.Create); lock (WriteStreams) { WriteStreams.Add(WriteMetadataStream); } StreamStruct MetadataStreamStruct = new StreamStruct(WriteMetadataStream, CompressMode); Streams.Add(ENodeType.Hierarchy, HierarchyStreamStruct); Streams.Add(ENodeType.Geometry, GeometryStreamStruct); Streams.Add(ENodeType.Metadata, MetadataStreamStruct); return(Streams); }
public static void SimpleReadWriteTest(bool Compress = true) { string[][] LodList = new string[][] { new string[] { "Content/Models/monkey.obj", "Content/Models/monkeyLod1.obj", "Content/Models/monkeyLod2.obj" }, new string[] { "Content/Models/squishymonkey.obj", "Content/Models/squishymonkeyLod1.obj" }, new string[] { "Content/Models/HDmonkey.obj", "Content/Models/HDMonkeyLod1.obj", "Content/Models/HDMonkeyLod2.obj", "Content/Models/HDMonkeyLod3.obj" } }; string[] MetadataStrings = new string[] { "Name:Monkey", "Name:Squishymonkey", "Name:HDmonkey" }; string Ext = ""; EDeflateCompression CompressEnum; if (Compress) { CompressEnum = EDeflateCompression.Compress; Ext = "x3_c_"; } else { CompressEnum = EDeflateCompression.DoNotCompress; Ext = "x3_p_"; } List <HierarchyNode> WriteHNodes = new List <HierarchyNode>(); List <GeometryNode> WritePolygons = new List <GeometryNode>(); List <MetadataNode> WriteMNodes = new List <MetadataNode>(); Random Rand = new Random(); for (int i = 0; i < 3; ++i) { HierarchyNode HNode = new HierarchyNode(); HNode.UniqueID = GetId(); MetadataNode Meta = new MetadataNode(); Meta.Metadata = MetadataStrings[i]; Meta.UniqueID = GetId(); var Polygon = new GeometryNode(); for (int f = 0; f < LodList[i].Length; ++f) { Obj ObjFile = new Obj(); ObjFile.LoadObj(LodList[i][f]); SetPolygonData(Polygon, ObjFile, f); } Polygon.UniqueID = GetId(); HierarchyNode.GeometryPart Part = CreateGeometryPart(Rand, i, Polygon); HNode.GeometryParts.Add(Part); HNode.MetadataID = Meta.UniqueID; if (i != 0) { HNode.ParentID = WriteHNodes[i - 1].UniqueID; WriteHNodes[i - 1].ChildNodes.Add(HNode.UniqueID); //HierarchyNode.GeometryPart InstancePart = CreateGeometryPart(Rand, i, WritePolygons[i - 1]); //HNode.GeometryParts.Add(InstancePart); } WriteHNodes.Add(HNode); WritePolygons.Add(Polygon); WriteMNodes.Add(Meta); } Dictionary <ENodeType, StreamStruct> Streams = new Dictionary <ENodeType, StreamStruct>(); FileStream WriteFileStreamH = new FileStream($"m.{Ext}h", FileMode.Create); StreamStruct StreamStructH = new StreamStruct(WriteFileStreamH, CompressEnum); FileStream WriteFileStreamG = new FileStream($"m.{Ext}g", FileMode.Create); StreamStruct StreamStructG = new StreamStruct(WriteFileStreamG, CompressEnum); FileStream FileStreamM = new FileStream($"m.{Ext}m", FileMode.Create); StreamStruct StreamStructM = new StreamStruct(FileStreamM, CompressEnum); Streams.Add(ENodeType.Hierarchy, StreamStructH); Streams.Add(ENodeType.Geometry, StreamStructG); Streams.Add(ENodeType.Metadata, StreamStructM); Console.WriteLine("Writing"); using (XStreamWriter writer = new XStreamWriter(Streams)) { for (int i = 0; i < WriteHNodes.Count; ++i) { writer.Write(WriteHNodes[i]); } for (int i = 0; i < WritePolygons.Count; ++i) { writer.Write(WritePolygons[i]); } for (int i = 0; i < WriteMNodes.Count; ++i) { writer.Write(WriteMNodes[i]); } } //Dispose of stream here Streams[ENodeType.Hierarchy].IOStream.Dispose(); Streams[ENodeType.Metadata].IOStream.Dispose(); Streams[ENodeType.Geometry].IOStream.Dispose(); Console.WriteLine("Done writing"); Console.WriteLine("Reading"); List <GeometryNode> ReadPolygons = new List <GeometryNode>(); List <HierarchyNode> ReadHNodes = new List <HierarchyNode>(); List <MetadataNode> ReadMNodes = new List <MetadataNode>(); ManualResetEvent WaitReads = new ManualResetEvent(false); int ReadsLeft = WriteHNodes.Count + WriteMNodes.Count + WritePolygons.Count; FileStream ReadFileStreamH = new FileStream($"m.{Ext}h", FileMode.Open); FileStream ReadFileStreamG = new FileStream($"m.{Ext}g", FileMode.Open); FileStream ReadFileStreamM = new FileStream($"m.{Ext}m", FileMode.Open); using (XStreamReader ReaderH = new XStreamReader(ENodeType.Hierarchy, ReadFileStreamH, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadHNodes) { ReadHNodes.Add((HierarchyNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderH.Process(Console.WriteLine)) { throw new Exception("Failed"); } } using (XStreamReader ReaderM = new XStreamReader(ENodeType.Metadata, ReadFileStreamM, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadMNodes) { ReadMNodes.Add((MetadataNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderM.Process(Console.WriteLine)) { throw new Exception("Failed"); } } using (XStreamReader ReaderG = new XStreamReader(ENodeType.Geometry, ReadFileStreamG, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadPolygons) { ReadPolygons.Add((GeometryNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderG.Process(Console.WriteLine)) { throw new Exception("Failed"); } } WaitReads.WaitOne(); Console.WriteLine("Done Reading"); WritePolygons = WritePolygons.OrderBy(x => x.UniqueID).ToList(); ReadPolygons = ReadPolygons.OrderBy(x => x.UniqueID).ToList(); WriteMNodes = WriteMNodes.OrderBy(x => x.UniqueID).ToList(); ReadMNodes = ReadMNodes.OrderBy(x => x.UniqueID).ToList(); WriteHNodes = WriteHNodes.OrderBy(x => x.UniqueID).ToList(); ReadHNodes = ReadHNodes.OrderBy(x => x.UniqueID).ToList(); CheckInputVsOutput(WritePolygons, WriteHNodes, WriteMNodes, ReadPolygons, ReadHNodes, ReadMNodes); }