public T read() { if (mapFile == null) { try { mapFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, fileName); } catch { mapFile = null; throw new Exception("File " + fileName + " is not opened yet."); } } reader = mapFile.MapView(MapAccess.FileMapRead, 0, 8 * 1024); try { mutex = Mutex.OpenExisting("MyMutex"); } catch { mutex = new Mutex(false, "MyMutex", out mutexCreated); } if (mutexCreated == false) { Console.WriteLine("Mutex error"); //return; } return(getObjectFromMMF()); }
public virtual void CustomAwake(MemoryBridge memoryBridge, string limbName, VesselControl vesselControl) { Debug.Log("Custom Awake Limb : " + limbName); this.memoryBridge = memoryBridge; this.vesselControl = vesselControl; limbFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, limbName); float byteCount; float servoCount; using (Stream mapStream = limbFile.MapView(MapAccess.FileMapAllAccess, 0, 16)) { var floatBuffer = new byte[4]; mapStream.Read(floatBuffer, 0, 4); byteCount = BitConverter.ToSingle(floatBuffer, 0); mapStream.Read(floatBuffer, 0, 4); servoCount = BitConverter.ToSingle(floatBuffer, 0); } var servosMirror = new List <RoboticServoMirror>(); ground = Instantiate(GameObject.Find("Ground")).transform; using (Stream mapStream = limbFile.MapView(MapAccess.FileMapAllAccess, 0, (int)byteCount)) { mapStream.Position = 16; for (int i = 0; i < servoCount; i++) { //servo name var floatBuffer = new byte[4]; mapStream.Read(floatBuffer, 0, 4); var stringByteLength = BitConverter.ToSingle(floatBuffer, 0); var stringBuffer = new byte[(int)stringByteLength]; mapStream.Read(stringBuffer, 0, stringBuffer.Length); string servoName = ASCIIEncoding.ASCII.GetString(stringBuffer); //servo parent id mapStream.Read(floatBuffer, 0, 4); var partID = BitConverter.ToSingle(floatBuffer, 0); mapStream.Read(floatBuffer, 0, 4); var parentID = BitConverter.ToSingle(floatBuffer, 0); foreach (var part in vesselControl.vessel.parts) { if (part.ID == partID) { var newServo = part.gameObject.AddComponent(typeof(RoboticServoMirror)) as RoboticServoMirror; newServo.CustomStart(servoName, memoryBridge, this, (int)parentID); servosMirror.Add(newServo); } } } } foreach (var servo in servosMirror) { servo.CreateBaseAnchor(); } CreateRoboticLegs(servosMirror); }
public void RemoveObject(string ObjName) { //get the main mmf table and remove the key MemoryMappedFile map = new MemoryMappedFile(); if (map.OpenEx(ObjectNamesMMF + ".nat", MapProtection.PageReadWrite, ObjectNamesMMF, MapAccess.FileMapAllAccess)) { BinaryFormatter bf = new BinaryFormatter(); MapViewStream mmfStream = map.MapView(MapAccess.FileMapRead, 0, 0, ""); mmfStream.Position = 0; Hashtable oFilesMap = bf.Deserialize(mmfStream) as Hashtable; oFilesMap.Remove(ObjName); mmfStream.Close(); //update the main file bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, oFilesMap); MapViewStream stream = map.MapView(MapAccess.FileMapAllAccess, 0, (int)0, ""); stream.Position = 0; stream.Write(ms.GetBuffer(), 0, (int)ms.Length); stream.Flush(); stream.Close(); //delete the map of the object MemoryMappedFile oMMf = new MemoryMappedFile(); if (oMMf.Open(MapAccess.FileMapAllAccess, ObjName)) { oMMf.Close(); oMMf.Dispose(); } if (System.IO.File.Exists(map.GetMMFDir() + ObjName + ".nat")) { System.IO.File.Delete(map.GetMMFDir() + ObjName + ".nat"); } } }
void GetFloatKeys(float keysToRead, float byteCount) { Debug.Log("Get Float Keys"); if (fileKeyFloat == null) { //Debug.Log("Open key file"); fileKeyFloat = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "FileKeysFloat3"); } keysFloat = new List <string>(); valueAddressFloat = new Dictionary <string, int>(); using (var keyReader = fileKeyFloat.MapView(MapAccess.FileMapAllAccess, 0, 100000)) { for (int i = 0; i < keysToRead; i++) { keyReader.Read(floatBuffer, 0, 4); var stringLength = BitConverter.ToSingle(floatBuffer, 0); var stringBuffer = new Byte[(int)stringLength]; keyReader.Read(stringBuffer, 0, (int)stringLength); var key = Encoding.ASCII.GetString(stringBuffer); //Debug.Log(key + " added from server"); keysFloat.Add(key); valueAddressFloat.Add(key, keysFloat.Count - 1); } } streamFloatValue = fileValueFloat.MapView(MapAccess.FileMapAllAccess, 0, (int)keysFloat.Count * 4); // EditorApplication.isPaused = true; }
public static void Initialize() { try { mappedFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "Local\\BROKENGEKI_SHARED_BUFFER"); NekoClient.Logging.Log.Info("Brokengeki shared memory exists, opening..."); } catch (FileMapIOException) { NekoClient.Logging.Log.Info("Brokengeki shared memory does not exist, creating..."); mappedFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, 256, "Local\\BROKENGEKI_SHARED_BUFFER"); } mapViewStream = (MapViewStream)mappedFile.MapView(MapAccess.FileMapAllAccess, 0, 256); }
void NotifyFinish() { if (currentWrite != null) { file.EndWrite(currentWrite); currentWrite = null; } file.Flush(); if (streamInfos.Count > 0) { mmf = MemoryMappedFile.Open(file); //Name, FileAccess.Read, FileShare.ReadWrite); } }
public static void StartMMF() { var filename = @"sensordata.bin"; var mmf = new MemoryMappedFile<Data>(filename, 1024); mmf.Open(); //var StructSize = mmf.StructSize; //var x1 = mmf.Serialize(new myData() { data1 = 12, data2 = -12 }); //var x2 = mmf.Serialize(new myData() { data1 = 13, data2 = -13 }); //using (var view = mmf.CreateViewAccessor(0, 1024, MemoryMappedFileAccess.Write)) //{ // //view.WriteArray(0, whiteRow, 0, whiteRow.Length); //} }
public void TestNamedMMF() { try { const string NAME = "MyMappedFile"; MemoryMappedFile map = MemoryMappedFile.Create(MapProtection.PageReadWrite, MAX_BYTES, NAME); MemoryMappedFile map2 = MemoryMappedFile.Open(MapAccess.FileMapRead, NAME); map2.Close(); map.Close(); } catch (FileMapIOException e) { Assert.Fail("Failed Named MMF: " + e); } }
T openMemoryFileAndReadData() { if (mapFile == null) { try { mapFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, fileName); UnityEngine.Debug.Log("read mutex e dhukse"); } catch { UnityEngine.Debug.Log("mmf read exception"); mapFile = null; throw new Exception("File " + fileName + " is not opened yet."); } } reader = mapFile.MapView(MapAccess.FileMapRead, 0, 8 * 1024); UnityEngine.Debug.Log("mmf read"); return(getObjectFromMMF()); }
// List<LimbControllersIK> IKlimbs; public void CustomAwake(MemoryBridge memoryBridge, VesselControl vesselControl, ref List <LimbController> limbs) { IRFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "IRFile" + memoryBridge.fileName); limbs = new List <LimbController>(); float byteCount; float limbCount; using (Stream mapStream = IRFile.MapView(MapAccess.FileMapAllAccess, 0, 16)) { var floatBuffer = new byte[4]; mapStream.Read(floatBuffer, 0, 4); byteCount = BitConverter.ToInt32(floatBuffer, 0); mapStream.Read(floatBuffer, 0, 4); limbCount = BitConverter.ToInt32(floatBuffer, 0); } Debug.Log("vessel has limbs " + limbCount); using (Stream mapStream = IRFile.MapView(MapAccess.FileMapAllAccess, 0, (int)byteCount)) { mapStream.Position = 16; for (int i = 0; i < limbCount; i++) { var floatBuffer = new byte[4]; mapStream.Read(floatBuffer, 0, 4); var stringByteLength = BitConverter.ToSingle(floatBuffer, 0); var stringBuffer = new byte[(int)stringByteLength]; mapStream.Read(stringBuffer, 0, stringBuffer.Length); string limbName = ASCIIEncoding.ASCII.GetString(stringBuffer); var newLimbObject = new GameObject(); newLimbObject.name = limbName; newLimbObject.transform.SetParent(vesselControl.vessel.transform); var newLimb = newLimbObject.AddComponent(typeof(LimbController)) as LimbController; newLimb.CustomAwake(memoryBridge, limbName, vesselControl); limbs.Add(newLimb); // Debug.Log("Add limb " + limbName); } } }
// Use this for initialization void Start1() { //MemoryMappedFile memMap = MemoryMappedFile.Create(MapProtection.PageReadWrite, 512, "TestMemMap3"); //if (memMap == null) // Debug.Log("streamwriter null"); //Debug.Log(memMap); //using (Stream mapStream = memMap.MapView(MapAccess.FileMapAllAccess, 0, 512)) //{ // Debug.Log(mapStream); // // StreamReader streamReader = new StreamReader(mapStream); // StreamWriter streamWriter = new StreamWriter(mapStream); // if (streamWriter == null) // Debug.Log("streamwriter null"); // streamWriter.WriteLine("yoo"); // streamWriter.Flush(); // mapStream.Flush(); // Debug.Log("can write " + mapStream.CanWrite); //} MemoryMappedFile memFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "TestMemMap3"); if (memFile == null) { Debug.Log("memfile null"); } using (Stream mapStream1 = memFile.MapView(MapAccess.FileMapAllAccess, 0, 512)) { Debug.Log("can read " + mapStream1.CanRead); StreamReader streamReader = new StreamReader(mapStream1); if (streamReader == null) { Debug.Log("streamReader null"); } Debug.Log("line " + streamReader.ReadToEnd()); //var bytes = mapStream1.BeginRead() } memFile.Dispose(); }
private void openOrCreateMemoryFileAndWriteData(T data) { try { mapFile = MemoryMappedFile.Open(MapAccess.FileMapWrite, fileName); } catch { mapFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, 8 * 1024, fileName); } char[] charArrayOfObject = serializeToXml <T>(data).ToCharArray(); Stream writer = mapFile.MapView(MapAccess.FileMapWrite, 0, charArrayOfObject.Length); for (int i = 0; i < charArrayOfObject.Length; i++) { writer.WriteByte((byte)charArrayOfObject[i]); } writer.Flush(); writer.Close(); }
void Update() { if (!receivingResponce) //IS THE CLIENT RESPONDING { Debug.Log("client file empty"); //camFeedFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, textureByte.Length, "CamFeedFile" + fileName + this.memoryBridge.cameraFeeds.Count); camFeedFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "CamFeedFile" + fileName + memoryBridge.cameraFeeds.Count); if (camFeedFile != null) { _receivingResponce = true; BuildCamera(); } } else { Debug.Log("cam feed update"); memoryBridge.SetVector3("CamLocalPos" + fileName + memoryBridge.cameraFeeds.Count, cameraRig.localPosition); memoryBridge.SetVector3("CamLocalEuler" + fileName + memoryBridge.cameraFeeds.Count, cameraRig.localEulerAngles); /////WORKS//// using (Stream camStream = camFeedFile.MapView(MapAccess.FileMapAllAccess, 0, feedByteCount)) { byte[] buffer = new byte[feedByteCount]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = camStream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } var byteArray = ms.ToArray(); readTex.LoadRawTextureData(byteArray); } } readTex.Apply(); meshRenderer.material.SetTexture("_MainTex", readTex); } }
public void WriteAndReadFromBufferUsingMemoryMappedFile() { using (MemoryMappedFile mmf1 = MemoryMappedFile.CreateSharedMemory("Local\\TestMemory", 1024)) { using (UnmanagedMemory view1 = mmf1.MapView(0, 1024)) { using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(view1.Start, (int)view1.Length)) { using (Stream ws = ncb.CreateWritingStream()) { ws.WriteByte(0x42); } using (MemoryMappedFile mmf2 = MemoryMappedFile.Open("Local\\TestMemory")) { using (UnmanagedMemory view2 = mmf1.MapView(0, 1024)) { Assert.AreNotEqual(view1.Start, view2.Start); using (UnmanagedCircularBuffer ncb2 = UnmanagedCircularBuffer.Open(view2.Start)) { using (Stream rs = ncb2.CreateReadingStream()) { Assert.AreEqual(0x42, rs.ReadByte()); } } } } } } } }
IEnumerator TryConnectToServer() { Application.runInBackground = true; keysFloat = new List <string>(); valueAddressFloat = new Dictionary <string, int>(); keysToAdd = new Dictionary <string, float>(); do { // fileHeader = null; //Debug.Log("checking for header file"); fileHeader = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "FileHeader"); if (fileHeader != null) { //Debug.Log("header file not null"); // fileKeyFloat = MemoryMappedFile.Create(MapProtection.PageReadWrite, byteCountKeyFloat + byteCountKeyFloatNew, "FileKeysFloat"); // fileKeyFloat = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "FileKeysFloat"); fileValueFloat = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "FileValuesFloat3"); connected = true; //// if (fileKeyFloat == null) // fileKeyFloat = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "FileKeysFloat"); ReadHeaderFile(); //var fileKeyFloat1 = MemoryMappedFile.Create(MapProtection.PageReadWrite, 50000, "FileKeysFloat1"); //using (var keyWriter = fileKeyFloat1.MapView(MapAccess.FileMapAllAccess, 0, 50000)) //{ // keyWriter.Position = 4000; //} //fileKeyFloat1.Close(); yield break; } yield return(null); } while (!connected); }
public VesselControl.Vessel DrawVessel(MemoryBridge memoryBridge, Material drawMat, string vesselName) { VesselControl.Vessel newVessel; this.memoryBridge = memoryBridge; this.vesselName = vesselName; var binFormatter = new BinaryFormatter(); var mStream = new MemoryStream(); meshList = new List <Mesh>(); parts = new List <Part>(); // partList = new List<VesselPart>(); var vesselObject = new GameObject(); vesselObject.name = vesselName; vessel = vesselObject.transform; vessel.SetParent(transform); vessel.position = Vector3.zero; vessel.localEulerAngles = Vector3.zero; //DebugVector.DrawVector(vessel); var modelObject = new GameObject(); modelObject.name = "Vessel Offset"; vesselOffset = modelObject.transform; vesselOffset.SetParent(vessel); vesselOffset.position = Vector3.zero; vesselOffset.localEulerAngles = Vector3.zero; if (vesselFile == null) { vesselFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "VesselFile" + memoryBridge.fileName); } if (vesselFile == null) { Debug.Log("file is null"); #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #endif } int byteCount = 0; int partCount = 0; using (Stream mapStream = vesselFile.MapView(MapAccess.FileMapAllAccess, 0, 8)) { var totalByteBuffer = new byte[4]; var partCountBuffer = new byte[4]; mapStream.Read(totalByteBuffer, 0, 4); mapStream.Read(partCountBuffer, 0, 4); byteCount = BitConverter.ToInt32(totalByteBuffer, 0); partCount = BitConverter.ToInt32(partCountBuffer, 0); } using (Stream mapStream = vesselFile.MapView(MapAccess.FileMapAllAccess, 0, byteCount)) { mapStream.Position = 8; for (int i = 0; i < partCount; i++) { List <MeshRenderer> meshRenderers = new List <MeshRenderer>(); //part unique id var totalpartBuffer = new byte[4]; mapStream.Read(totalpartBuffer, 0, 4); var uniqueID = BitConverter.ToSingle(totalpartBuffer, 0); //parent unique id totalpartBuffer = new byte[4]; mapStream.Read(totalpartBuffer, 0, 4); var parentUniqueID = BitConverter.ToSingle(totalpartBuffer, 0); //filter count totalpartBuffer = new byte[4]; mapStream.Read(totalpartBuffer, 0, 4); var filterCount = BitConverter.ToSingle(totalpartBuffer, 0); GameObject newPart = new GameObject(); var newPartObject = newPart.AddComponent(typeof(Part)) as Part; Vector3 vesselPartOffset = Vector3.zero; var tempBuffer = new Byte[4]; mapStream.Read(tempBuffer, 0, 4); vesselPartOffset.x = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); vesselPartOffset.y = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); vesselPartOffset.z = BitConverter.ToSingle(tempBuffer, 0); newPart.transform.localPosition = vesselPartOffset; //rot offset Vector3 vesselPartRotOffset = Vector3.zero; tempBuffer = new Byte[4]; mapStream.Read(tempBuffer, 0, 4); vesselPartRotOffset.x = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); vesselPartRotOffset.y = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); vesselPartRotOffset.z = BitConverter.ToSingle(tempBuffer, 0); newPart.transform.localEulerAngles = vesselPartRotOffset; //part name mapStream.Read(tempBuffer, 0, 4); var stringByteLength = BitConverter.ToSingle(tempBuffer, 0); var stringBuffer = new byte[(int)stringByteLength]; mapStream.Read(stringBuffer, 0, stringBuffer.Length); newPartObject.kspPartName = ASCIIEncoding.ASCII.GetString(stringBuffer); GameObject meshObjects = new GameObject(); meshObjects.name = "Mesh Objects"; newPartObject.partMeshes = meshObjects; meshObjects.transform.SetParent(newPart.transform); meshObjects.transform.localEulerAngles = Vector3.zero; for (int k = 0; k < filterCount; k++) { //Debug.Log("start writing filter at " + mapStream.Position); var newMesh = Instantiate(Resources.Load("BlankPart", typeof(GameObject))) as GameObject; newMesh.transform.SetParent(newPart.transform); meshRenderers.Add(newMesh.GetComponent <MeshRenderer>()); newMesh.GetComponent <MeshRenderer>().material = drawMat; Vector3 posVector = Vector3.zero; tempBuffer = new Byte[4]; mapStream.Read(tempBuffer, 0, 4); posVector.x = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); posVector.y = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); posVector.z = BitConverter.ToSingle(tempBuffer, 0); newMesh.transform.localPosition = posVector; var localEuler = Vector3.zero; // tempBuffer = new Byte[4]; mapStream.Read(tempBuffer, 0, 4); localEuler.x = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); localEuler.y = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); localEuler.z = BitConverter.ToSingle(tempBuffer, 0); newMesh.transform.localEulerAngles = localEuler; Vector3 scaleVector = Vector3.zero; // tempBuffer = new Byte[4]; mapStream.Read(tempBuffer, 0, 4); scaleVector.x = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); scaleVector.y = BitConverter.ToSingle(tempBuffer, 0); mapStream.Read(tempBuffer, 0, 4); scaleVector.z = BitConverter.ToSingle(tempBuffer, 0); newMesh.transform.localScale = scaleVector; var lengthBuffer = new Byte[4]; mapStream.Read(lengthBuffer, 0, 4); var byteMeshLength = BitConverter.ToSingle(lengthBuffer, 0); var meshBuffer = new Byte[(int)byteMeshLength]; mapStream.Read(meshBuffer, 0, (int)byteMeshLength); newMesh.GetComponent <MeshFilter>().mesh = MeshSerializer.ReadMesh(meshBuffer); //not sure why this doesnt work up top, but too lazy to figure it out newMesh.transform.SetParent(meshObjects.transform); } newPartObject.CustomAwake((int)uniqueID, (int)parentUniqueID, vesselName, meshRenderers); parts.Add(newPartObject); } } newVessel.transform = vessel; newVessel.parts = parts; newVessel.meshOffset = vesselOffset; foreach (var part in newVessel.parts) { part.vessel = newVessel; part.FindParent(); } return(newVessel); }