public static extern IntPtr MapViewOfFileEx( FileMappingHandle hFileMappingObject, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, ulong dwNumberOfBytesToMap, ulong lpBaseAddress);
public async Task Attach(CancellationToken cancellationToken) { System.Diagnostics.Debug.Assert(CurrentState == State.Idle); SocketPermission permission = new SocketPermission( NetworkAccess.Connect, TransportType.Tcp, kServerHostname, kServerPort); permission.Demand(); IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, kServerPort); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Blocking = false; socket.NoDelay = true; socket.ReceiveBufferSize = 1024 * 1024; socket.SendBufferSize = 1024 * 1024; socket.ReceiveTimeout = 0; socket.SendTimeout = 0; OnStateChanged(State.Attaching); while (true) { Task task = Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, ipEndPoint, null); try { await task.WithCancellation(cancellationToken); } catch (OperationCanceledException) { socket.Close(); socket = null; OnStateChanged(State.Idle); return; } catch (SocketException e) { if (e.SocketErrorCode == SocketError.ConnectionRefused) { // Not found - emulator may still be starting. System.Diagnostics.Debug.WriteLine("Connection refused; trying again..."); continue; } OnStateChanged(State.Idle); return; } break; } // Start recv pump. Dispatch.Issue(() => ReceivePump()); var fbb = BeginRequest(); AttachRequest.StartAttachRequest(fbb); int requestDataOffset = AttachRequest.EndAttachRequest(fbb); var response = await CommitRequest(fbb, RequestData.AttachRequest, requestDataOffset); System.Diagnostics.Debug.Assert(response.ResponseDataType == ResponseData.AttachResponse); var attachResponse = new AttachResponse(); response.GetResponseData(attachResponse); // Open mmap to share memory. memoryHandle = FileMapping.OpenFileMapping( FileMapAccess.FILE_MAP_ALL_ACCESS, false, attachResponse.MemoryFile); if (memoryHandle.IsInvalid) { System.Diagnostics.Debug.Fail("Unable to open target memory"); Detach(); return; } // Open mmap to code cache. codeCacheHandle = FileMapping.OpenFileMapping(FileMapAccess.FILE_MAP_ALL_ACCESS, false, attachResponse.CodeCacheFile); if (codeCacheHandle.IsInvalid) { System.Diagnostics.Debug.Fail("Unable to open target code cache"); Detach(); return; } codeCachePtr = FileMapping.MapViewOfFileEx( codeCacheHandle, FileMapAccess.FILE_MAP_ALL_ACCESS, 0, 0, attachResponse.CodeCacheSize, attachResponse.CodeCacheBase); // Setup the memory system. This maps the emulator memory into our address // space. if (!Memory.InitializeMapping(memoryHandle)) { Detach(); return; } OnStateChanged(State.Attached); }
public void Detach() { if (CurrentState == State.Idle || CurrentState == State.Detached) { return; } Memory.UninitializeMapping(); if (codeCacheHandle != null) { FileMapping.UnmapViewOfFile(codeCachePtr); codeCacheHandle.Close(); codeCacheHandle = null; } if (memoryHandle != null) { memoryHandle.Close(); memoryHandle = null; } if (socket != null) { socket.Close(); socket = null; } OnStateChanged(State.Detached); }