From 94255e8ad7cf93ee330e614bafc7706538afd290 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 19:52:59 +0100 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8=E9=80=80=E5=87=BA?= =?UTF-8?q?=E5=90=8E=E6=94=BE=E6=8E=89=E6=88=AA=E5=9B=BE=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=86=85=E5=AD=98=EF=BC=8C=E4=B8=8B=E4=B8=80=E5=8F=B0=E8=B5=B7?= =?UTF-8?q?=E6=9D=A5=E6=97=B6=E8=87=AA=E5=B7=B1=E9=87=8D=E6=96=B0=E6=98=A0?= =?UTF-8?q?=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 截图共享内存的名字只按控制台端口取。句柄一直映射着旧的那块,模拟器退出了也不放; 同一端口的下一台模拟器(比如改成 1080p 后)再建这块共享内存时,拿回来的还是旧的、 更小的那块,映射失败,截图共享就开不起来(模拟器那边还会因此卡死,另修)。 Windows 上: - 打开时找出监听控制台端口的进程(就是模拟器),等它退出时(线程池等待)关掉映射。 抓图时如果映射已经没了,最多每秒试一次映射当前这台模拟器的共享内存,没有就返回 MODAVD_E_NOT_FOUND。抓图和关映射之间用读写锁隔开。 - 普通打开时,共享内存在、控制台端口却没人监听,说明是别的程序留着的旧的:不映射, 返回 MODAVD_E_NOT_FOUND。带 MODAVD_OPEN_NO_START 的照旧信任现有映射。 - MuMu 形状的外壳也走同一条抓图路径。 测试:test_core 加两条(另起一个进程当模拟器,杀掉后句柄放掉映射、名字随之消失, 换更大的帧重新起来后抓到新尺寸;没人监听的旧共享内存不映射);test_shim 的假帧配上 假的控制台监听,再加一条旧共享内存连不上。去掉退出时关映射的那一行,新测试失败 2 处。 Refs ModAVD/ModAVD#210 --- CMakeLists.txt | 2 +- include/modavd_extras.h | 11 +++- shim/mumu_shim.c | 4 +- src/console.c | 34 ++++++++++ src/core.c | 130 +++++++++++++++++++++++++++++++++++- src/mx_internal.h | 25 +++++++ tests/fake_frame.h | 35 ++++++++++ tests/test_core.c | 142 +++++++++++++++++++++++++++++++++++++++- tests/test_shim.c | 24 ++++++- 9 files changed, 398 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7d96af..b380958 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ endif() function(mx_link_system target) if(WIN32) - target_link_libraries(${target} PRIVATE ws2_32) + target_link_libraries(${target} PRIVATE ws2_32 iphlpapi) else() target_link_libraries(${target} PRIVATE Threads::Threads) if(CMAKE_SYSTEM_NAME STREQUAL "Linux") diff --git a/include/modavd_extras.h b/include/modavd_extras.h index e4cbb90..083bd37 100644 --- a/include/modavd_extras.h +++ b/include/modavd_extras.h @@ -136,7 +136,13 @@ MODAVD_API int modavd_parse_console_port(const char* adb_address); * opening the mapping: named regions can survive an emulator restart. Console * auth/command failures return MODAVD_E_IO, even if an old mapping exists. * The first frame may not have arrived yet; capture then returns NOT_READY. - * MODAVD_E_NOT_FOUND: frame sharing is off and nothing answers on the console port. + * On Windows the handle holds the mapping only while the emulator listening on + * the console port runs: when it exits the mapping is released, so the next + * emulator on the port can share frames at its own size (ModAVD/ModAVD#210). + * MODAVD_E_NOT_FOUND: frame sharing is off and nothing answers on the console + * port; on Windows also a region with no emulator on the + * port (left over, held by another program; unless + * MODAVD_OPEN_NO_START is set). * MODAVD_E_NOT_READY: the console answered (or MODAVD_OPEN_NO_START is set) but * frame sharing did not come up in time. */ MODAVD_API int modavd_open(const modavd_open_params* params, modavd_handle* out); @@ -152,6 +158,9 @@ MODAVD_API int modavd_console_port(modavd_handle h); /* > 0, or MODAVD_E_IN * MODAVD_E_NOT_READY: no frame yet, or the emulator published a new frame * during each of the copies, so buf may mix two frames; * call again. MODAVD_E_RESIZED: reopen the handle. + * MODAVD_E_NOT_FOUND (Windows): the emulator exited and no new one shares + * frames on the port yet; call again later, the handle + * maps the next emulator's region by itself. * MODAVD_E_BUFFER: buf_size < info->stride * info->height. */ MODAVD_API int modavd_capture(modavd_handle h, int display, uint32_t format, uint32_t flags, void* buf, size_t buf_size, modavd_frame_info* info); diff --git a/shim/mumu_shim.c b/shim/mumu_shim.c index c25718a..da0b60b 100644 --- a/shim/mumu_shim.c +++ b/shim/mumu_shim.c @@ -269,8 +269,8 @@ EXPORT int nemu_capture_display(int handle, unsigned int display_id, int buffer_ * the last copy over when a new frame arrived during every copy, instead * of failing (MaaCore would give up on this path at connect, or drop to its * screencap speed test again). */ - int rc = mx_copy_frame(&h->shm, MODAVD_FMT_RGBA, MODAVD_CAPTURE_BOTTOM_UP | MX_CAPTURE_UNPUBLISHED, h->simd, - size ? pixels : NULL, size, &info); + int rc = mx_capture_frame(h, MODAVD_FMT_RGBA, MODAVD_CAPTURE_BOTTOM_UP | MX_CAPTURE_UNPUBLISHED, + size ? pixels : NULL, size, &info); if (rc == MX_TORN) { rc = MODAVD_OK; } diff --git a/src/console.c b/src/console.c index 69ff3ca..ab5544c 100644 --- a/src/console.c +++ b/src/console.c @@ -16,6 +16,7 @@ #define WIN32_LEAN_AND_MEAN #include #include +#include typedef SOCKET mx_socket; #define MX_BAD_SOCKET INVALID_SOCKET #define mx_close_socket closesocket @@ -190,6 +191,39 @@ int mx_console_query(mx_console* c, const char* command, char* reply, size_t cap int mx_console_send(mx_console* c, const char* command) { return mx_console_query(c, command, NULL, 0); } +#ifdef _WIN32 +unsigned long mx_console_owner_pid(int port) +{ + /* The listener table, filtered by Windows itself: no need to know what + * value this Windows build uses for the listening state. */ + ULONG size = 0; + MIB_TCPTABLE_OWNER_PID* table = NULL; + DWORD rc = ERROR_INSUFFICIENT_BUFFER; + for (int attempt = 0; attempt < 4 && rc == ERROR_INSUFFICIENT_BUFFER; ++attempt) { + free(table); + table = size ? (MIB_TCPTABLE_OWNER_PID*)malloc(size) : NULL; + if (size && !table) { + return 0; + } + rc = GetExtendedTcpTable(table, &size, FALSE, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0); + } + unsigned long pid = 0; + if (rc == NO_ERROR && table) { + for (DWORD i = 0; i < table->dwNumEntries; ++i) { + const MIB_TCPROW_OWNER_PID* row = &table->table[i]; + DWORD addr = ntohl(row->dwLocalAddr); + if (ntohs((u_short)row->dwLocalPort) == (u_short)port && + (addr == INADDR_LOOPBACK || addr == INADDR_ANY)) { + pid = row->dwOwningPid; + break; + } + } + } + free(table); + return pid; +} +#endif + void mx_console_close(mx_console* c) { if (c->sock == (intptr_t)MX_BAD_SOCKET) { diff --git a/src/core.c b/src/core.c index b4e0788..df2dd88 100644 --- a/src/core.c +++ b/src/core.c @@ -38,6 +38,81 @@ static void sleep_ms(int ms) #define MODAVD_EXTRAS_VERSION "0.0.0" #endif +#ifdef _WIN32 +/* Thread pool callback: the watched emulator exited. Drops the mapping so the + * next emulator on this port can create its region at its own size. */ +static void CALLBACK emulator_exited(PVOID ctx, BOOLEAN timed_out) +{ + (void)timed_out; + struct modavd_instance* h = (struct modavd_instance*)ctx; + AcquireSRWLockExclusive((PSRWLOCK)&h->shm_lock); + if (h->shm.view) { + mx_log(MODAVD_LOG_INFO, "port=%d: the emulator exited, released its shared memory", h->console_port); + mx_shm_close(&h->shm); + } + ReleaseSRWLockExclusive((PSRWLOCK)&h->shm_lock); +} + +static int watch_emulator(struct modavd_instance* h, unsigned long pid) +{ + HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, (DWORD)pid); + if (!process) { + return MODAVD_E_IO; + } + HANDLE wait = NULL; + if (!RegisterWaitForSingleObject(&wait, process, emulator_exited, h, INFINITE, WT_EXECUTEONLYONCE)) { + CloseHandle(process); + return MODAVD_E_IO; + } + h->emu_process = process; + h->emu_wait = wait; + return MODAVD_OK; +} + +/* block: wait for a running callback to return. Only without shm_lock held, + * since the callback takes it. */ +static void unwatch_emulator(struct modavd_instance* h, int block) +{ + if (h->emu_wait) { + UnregisterWaitEx((HANDLE)h->emu_wait, block ? INVALID_HANDLE_VALUE : NULL); + h->emu_wait = NULL; + } + if (h->emu_process) { + CloseHandle((HANDLE)h->emu_process); + h->emu_process = NULL; + } +} + +/* With shm_lock held exclusively and no mapping: maps the region of the + * emulator now on the port, at most once a second. */ +static void reattach(struct modavd_instance* h) +{ + ULONGLONG now = GetTickCount64(); + if (h->reattach_ms && now - h->reattach_ms < 1000) { + return; + } + h->reattach_ms = now; + /* The old wait has fired (that is what dropped the mapping) and its + * callback is done with the lock, so this does not need to block. */ + unwatch_emulator(h, 0); + unsigned long pid = mx_console_owner_pid(h->console_port); + if (!pid) { + return; + } + mx_shm shm = { 0 }; + if (mx_shm_open(h->console_port, &shm) != MODAVD_OK) { + return; + } + if (watch_emulator(h, pid) != MODAVD_OK) { + mx_log(MODAVD_LOG_WARN, "port=%d: cannot watch emulator process %lu", h->console_port, pid); + } + h->shm = shm; + const volatile mx_video_info* vi = (const volatile mx_video_info*)shm.view; + mx_log(MODAVD_LOG_INFO, "port=%d: mapped the shared memory of the new emulator, %ux%u size=%zu", h->console_port, + vi->width, vi->height, shm.size); +} +#endif + uint32_t modavd_api_version(void) { return MODAVD_API_VERSION; } const char* modavd_version_string(void) { return MODAVD_EXTRAS_VERSION; } @@ -132,6 +207,20 @@ int modavd_open(const modavd_open_params* params, modavd_handle* out) return rc; } +#ifdef _WIN32 + /* The emulator whose region this is: the program on the console port. */ + unsigned long pid = mx_console_owner_pid(port); + if (!pid && !(params->flags & MODAVD_OPEN_NO_START)) { + /* A region with no emulator behind it is left over from an emulator + * that exited while another program still held it. Holding it too + * would keep it alive for the next emulator on this port. */ + mx_log(MODAVD_LOG_WARN, "port=%d: shared memory is there but no emulator listens on the console port", + port); + mx_shm_close(&shm); + return MODAVD_E_NOT_FOUND; + } +#endif + struct modavd_instance* inst = (struct modavd_instance*)calloc(1, sizeof(*inst)); struct mx_input* input = mx_input_create(port); if (!inst || !input) { @@ -147,6 +236,14 @@ int modavd_open(const modavd_open_params* params, modavd_handle* out) const volatile mx_video_info* vi = (const volatile mx_video_info*)shm.view; mx_log(MODAVD_LOG_INFO, "open port=%d %ux%u fps=%u frame=%u size=%zu client=%s", port, vi->width, vi->height, vi->fps, vi->frame_number, shm.size, params->client_name ? params->client_name : "-"); +#ifdef _WIN32 + InitializeSRWLock((PSRWLOCK)&inst->shm_lock); + /* NO_START with no console trusts the mapping as it is, unwatched. */ + if (pid && watch_emulator(inst, pid) != MODAVD_OK) { + mx_log(MODAVD_LOG_WARN, "port=%d: cannot watch emulator process %lu; its shared memory stays mapped", port, + pid); + } +#endif *out = inst; return MODAVD_OK; } @@ -156,6 +253,10 @@ void modavd_close(modavd_handle h) if (!h) { return; } +#ifdef _WIN32 + /* Waits for a running emulator_exited() before the handle goes away. */ + unwatch_emulator(h, 1); +#endif /* Lifts the contacts this handle left down. */ mx_input_destroy(h->input); mx_shm_close(&h->shm); @@ -359,6 +460,33 @@ int mx_copy_frame(const mx_shm* shm, uint32_t format, uint32_t flags, int simd, return MX_TORN; } +int mx_capture_frame(struct modavd_instance* h, uint32_t format, uint32_t flags, void* buf, size_t buf_size, + modavd_frame_info* info) +{ +#ifdef _WIN32 + PSRWLOCK lock = (PSRWLOCK)&h->shm_lock; + AcquireSRWLockShared(lock); + if (!h->shm.view) { + ReleaseSRWLockShared(lock); + AcquireSRWLockExclusive(lock); + if (!h->shm.view) { + reattach(h); + } + ReleaseSRWLockExclusive(lock); + AcquireSRWLockShared(lock); + if (!h->shm.view) { + ReleaseSRWLockShared(lock); + return MODAVD_E_NOT_FOUND; + } + } + int rc = mx_copy_frame(&h->shm, format, flags, h->simd, buf, buf_size, info); + ReleaseSRWLockShared(lock); + return rc; +#else + return mx_copy_frame(&h->shm, format, flags, h->simd, buf, buf_size, info); +#endif +} + int modavd_capture(modavd_handle h, int display, uint32_t format, uint32_t flags, void* buf, size_t buf_size, modavd_frame_info* info) { @@ -368,7 +496,7 @@ int modavd_capture(modavd_handle h, int display, uint32_t format, uint32_t flags if (display != 0) { return MODAVD_E_UNSUPPORTED; } - int rc = mx_copy_frame(&h->shm, format, flags & ~MX_CAPTURE_UNPUBLISHED, h->simd, buf, buf_size, info); + int rc = mx_capture_frame(h, format, flags & ~MX_CAPTURE_UNPUBLISHED, buf, buf_size, info); if (rc == MX_TORN) { mx_log(MODAVD_LOG_WARN, "port=%d: a new frame arrived during each of %d copies", h->console_port, MX_MAX_COPY_ATTEMPTS); diff --git a/src/mx_internal.h b/src/mx_internal.h index c1e9a4f..6994edc 100644 --- a/src/mx_internal.h +++ b/src/mx_internal.h @@ -144,8 +144,33 @@ struct modavd_instance { int simd; /* mx_cpu_simd() at open */ mx_shm shm; struct mx_input* input; +#ifdef _WIN32 + /* The emulator owns the name of the shared frame, but a mapping stays + * alive while anyone holds it. If this handle kept the old mapping after + * the emulator exited, the next emulator on the same port would get the + * old, possibly smaller region back and could not share frames (ModAVD#210). + * So the handle watches the emulator process (the one listening on the + * console port) and drops the mapping when it exits; the next capture maps + * the new emulator's region. shm_lock (an SRWLOCK) guards shm: shared + * while copying, exclusive while mapping or unmapping. */ + void* shm_lock; + void* emu_process; /* HANDLE of the watched emulator, NULL when none */ + void* emu_wait; /* its RegisterWaitForSingleObject wait, NULL when none */ + uint64_t reattach_ms; /* GetTickCount64() of the last attempt to map again */ +#endif }; +/* Copies one frame of the handle, as mx_copy_frame() does on its mapping. + * Windows: when the emulator exited the mapping is gone; this maps the region + * of the emulator now on the port (at most once a second) and otherwise + * returns MODAVD_E_NOT_FOUND. */ +int mx_capture_frame(struct modavd_instance* h, uint32_t format, uint32_t flags, void* buf, size_t buf_size, + modavd_frame_info* info); + +/* Windows: process id of the program listening on 127.0.0.1:port (or any + * address) over TCP, 0 when none. */ +unsigned long mx_console_owner_pid(int port); + /* 1 when this CPU runs the vector row conversion (SSSE3 on x86, NEON on * ARM64), else 0. */ int mx_cpu_simd(void); diff --git a/tests/fake_frame.h b/tests/fake_frame.h index dba1904..baefd3c 100644 --- a/tests/fake_frame.h +++ b/tests/fake_frame.h @@ -15,6 +15,7 @@ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN +#include #include #else #include @@ -110,4 +111,38 @@ static void fake_frame_destroy(fake_frame* f) memset(f, 0, sizeof(*f)); } +#ifdef _WIN32 +/* Listens on 127.0.0.1:port without ever answering, so the library sees an + * emulator (this process) on that console port. INVALID_SOCKET on failure. */ +static SOCKET fake_listen(int port) +{ + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { + return INVALID_SOCKET; + } + SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + struct sockaddr_in a; + memset(&a, 0, sizeof(a)); + a.sin_family = AF_INET; + a.sin_port = htons((u_short)port); + a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (s == INVALID_SOCKET || bind(s, (struct sockaddr*)&a, sizeof(a)) != 0 || listen(s, 4) != 0) { + if (s != INVALID_SOCKET) { + closesocket(s); + } + WSACleanup(); + return INVALID_SOCKET; + } + return s; +} + +static void fake_unlisten(SOCKET s) +{ + if (s != INVALID_SOCKET) { + closesocket(s); + WSACleanup(); + } +} +#endif + #endif /* FAKE_FRAME_H */ diff --git a/tests/test_core.c b/tests/test_core.c index 509c926..488d8b7 100644 --- a/tests/test_core.c +++ b/tests/test_core.c @@ -468,8 +468,144 @@ static void test_log_callback(void) modavd_set_log(NULL, NULL); } -int main(void) +#ifdef _WIN32 +/* Another process standing in for the emulator: this test listening on the + * console port until it is killed (or 60 s pass). */ +static HANDLE spawn_listener(int port, DWORD* pid) { + char exe[MAX_PATH], cmd[MAX_PATH + 32]; + GetModuleFileNameA(NULL, exe, MAX_PATH); + snprintf(cmd, sizeof(cmd), "\"%s\" --listen %d", exe, port); + STARTUPINFOA si; + PROCESS_INFORMATION pi; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + if (!CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { + return NULL; + } + CloseHandle(pi.hThread); + *pid = pi.dwProcessId; + for (int i = 0; i < 100 && mx_console_owner_pid(port) != pi.dwProcessId; ++i) { + Sleep(50); + } + return pi.hProcess; +} + +static int capture_until(modavd_handle h, int want, uint32_t* width) +{ + modavd_frame_info info; + int rc = MODAVD_E_IO; + for (int i = 0; i < 60; ++i) { + memset(&info, 0, sizeof(info)); + info.size = sizeof(info); + rc = modavd_capture(h, 0, MODAVD_FMT_BGRA, 0, NULL, 0, &info); + if (rc == want) { + break; + } + Sleep(50); + } + if (width) { + *width = (uint32_t)info.width; + } + return rc; +} + +/* ModAVD#210: the emulator exits while a client keeps its handle; the next + * emulator on the port shares frames at a bigger size. The handle must not + * keep the old region alive, and must pick up the new one. */ +static void test_release_when_emulator_exits(void) +{ + const int port = 65015; + fake_frame f; + DWORD pid = 0; + if (fake_frame_create(&f, port, 16, 8, 16, 8) != 0) { + CHECK(!"cannot create the fake frame"); + return; + } + HANDLE emu = spawn_listener(port, &pid); + CHECK(emu != NULL); + CHECK_EQ(mx_console_owner_pid(port), pid); + modavd_open_params p; + memset(&p, 0, sizeof(p)); + p.size = sizeof(p); + p.console_port = port; + p.flags = MODAVD_OPEN_NO_START; + modavd_handle h = NULL; + CHECK_EQ(modavd_open(&p, &h), MODAVD_OK); + uint32_t width = 0; + CHECK_EQ(capture_until(h, MODAVD_OK, &width), MODAVD_OK); + CHECK_EQ(width, 16); + + /* The emulator exits: the handle lets go of its region. */ + TerminateProcess(emu, 1); + WaitForSingleObject(emu, 5000); + CloseHandle(emu); + CHECK_EQ(capture_until(h, MODAVD_E_NOT_FOUND, NULL), MODAVD_E_NOT_FOUND); + fake_frame_destroy(&f); + HANDLE left = OpenFileMappingA(FILE_MAP_READ, FALSE, "SHM_videmulator65015"); + CHECK(left == NULL); + if (left) { + CloseHandle(left); + } + + /* The next emulator on the port, at a bigger size. */ + if (fake_frame_create(&f, port, 32, 16, 32, 16) != 0) { + CHECK(!"cannot create the second fake frame"); + modavd_close(h); + return; + } + CHECK_EQ(f.size, MX_HEADER_SIZE + 32 * 16 * 4); + emu = spawn_listener(port, &pid); + CHECK(emu != NULL); + CHECK_EQ(capture_until(h, MODAVD_OK, &width), MODAVD_OK); + CHECK_EQ(width, 32); + modavd_close(h); + if (emu) { + TerminateProcess(emu, 1); + WaitForSingleObject(emu, 5000); + CloseHandle(emu); + } + fake_frame_destroy(&f); +} + +/* A region with no emulator on the port (left over, held by someone else): + * an ordinary open does not map it. */ +static void test_open_leftover_region(void) +{ + fake_frame f; + if (fake_frame_create(&f, 65016, 8, 4, 8, 4) != 0) { + CHECK(!"cannot create the fake frame"); + return; + } + modavd_open_params p; + memset(&p, 0, sizeof(p)); + p.size = sizeof(p); + p.console_port = 65016; + modavd_handle h = NULL; + CHECK_EQ(modavd_open(&p, &h), MODAVD_E_NOT_FOUND); + CHECK(h == NULL); + fake_frame_destroy(&f); + HANDLE left = OpenFileMappingA(FILE_MAP_READ, FALSE, "SHM_videmulator65016"); + CHECK(left == NULL); + if (left) { + CloseHandle(left); + } +} +#endif + +int main(int argc, char** argv) +{ +#ifdef _WIN32 + if (argc == 3 && strcmp(argv[1], "--listen") == 0) { + SOCKET s = fake_listen(atoi(argv[2])); + Sleep(60000); + fake_unlisten(s); + return 0; + } +#else + (void)argc; + (void)argv; +#endif test_parse_console_port(); test_version_and_errors(); test_open_errors(); @@ -479,6 +615,10 @@ int main(void) test_capture_odd_sizes(); test_convert_row(); test_log_callback(); +#ifdef _WIN32 + test_release_when_emulator_exits(); + test_open_leftover_region(); +#endif if (g_failures) { fprintf(stderr, "%d check(s) failed\n", g_failures); return 1; diff --git a/tests/test_shim.c b/tests/test_shim.c index 3282aaf..9499a97 100644 --- a/tests/test_shim.c +++ b/tests/test_shim.c @@ -53,7 +53,8 @@ int main(int argc, char** argv) const int port = 65003; const uint32_t w = 48, h = 20; fake_frame f; - if (fake_frame_create(&f, port, w, h, w, h) != 0) { + SOCKET console = fake_listen(port); /* the shell only maps a frame an emulator is behind */ + if (fake_frame_create(&f, port, w, h, w, h) != 0 || console == INVALID_SOCKET) { fprintf(stderr, "cannot create the fake frame\n"); return 1; } @@ -122,7 +123,8 @@ int main(int argc, char** argv) { const int base = 1100, index = 3000; fake_frame g; - if (fake_frame_create(&g, base + 10 * index, w, h, w, h) != 0) { + SOCKET gl = fake_listen(base + 10 * index); + if (fake_frame_create(&g, base + 10 * index, w, h, w, h) != 0 || gl == INVALID_SOCKET) { fprintf(stderr, "cannot create the second fake frame\n"); return 1; } @@ -160,8 +162,10 @@ int main(int argc, char** argv) /* A portBase ModAVD does not accept is not taken: the plan falls back to 20000 (port 50000, no frame). * Frames wait where taking 1150 or 1000 would land, so taking them shows. */ fake_frame g1150, g1000; + SOCKET l1150 = fake_listen(1150 + 10 * index), l1000 = fake_listen(1000 + 10 * index); if (fake_frame_create(&g1150, 1150 + 10 * index, w, h, w, h) != 0 || - fake_frame_create(&g1000, 1000 + 10 * index, w, h, w, h) != 0) { + fake_frame_create(&g1000, 1000 + 10 * index, w, h, w, h) != 0 || l1150 == INVALID_SOCKET || + l1000 == INVALID_SOCKET) { fprintf(stderr, "cannot create the fake frames for refused plans\n"); return 1; } @@ -185,6 +189,9 @@ int main(int argc, char** argv) fake_frame_destroy(&g1150); fake_frame_destroy(&g1000); fake_frame_destroy(&g); + fake_unlisten(l1150); + fake_unlisten(l1000); + fake_unlisten(gl); remove(json); RemoveDirectoryA(dir); RemoveDirectoryA(parent); @@ -201,7 +208,18 @@ int main(int argc, char** argv) CHECK(strstr(text, "connect index=65021 port=65021: shared memory unavailable") != NULL); } + /* A frame left over with no emulator behind it: not mapped, so it can go. */ + { + fake_frame s; + if (fake_frame_create(&s, 65022, w, h, w, h) == 0) { + CHECK(nemu_connect(scratch, 65022) == 0); + fake_frame_destroy(&s); + CHECK(OpenFileMappingA(FILE_MAP_READ, FALSE, "SHM_videmulator65022") == NULL); + } + } + fake_frame_destroy(&f); + fake_unlisten(console); FreeLibrary(dll); if (g_failures) { fprintf(stderr, "%d check(s) failed\n", g_failures);