Files

464 lines
25 KiB
C

/* Own PE bridge: Wine Unix-call by default, explicit native Windows backend build. */
#include <windows.h>
#include <string.h>
#ifndef GW_NATIVE_WINDOWS
#include <unixlib.h>
#else
#include <winternl.h>
#endif
#include "wine_wire_abi.h"
#include "frame_timing.h"
#include "wine_session_retirement.h"
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
#include "priority_route_slot.h"
#endif
#include "wine_preopen_hook.private.h"
#include "wire_realm_observe.private.h"
#include "realm_diag_admission.private.h"
#include "wire_profile5_auto_realm.private.h"
#ifdef GW_ENABLE_SELF_LOCATING_BUNDLE
#include "wine_bundle_paths.h"
#endif
LONG WINAPI gw_wire_account_active(void);
LONG WINAPI gw_wire_account_start(void);
LONG WINAPI gw_wire_account_start_owned(void* owner);
void WINAPI gw_wire_account_ui_activate(void);
NTSTATUS WINAPI gw_wire_account_transport(struct gw_wire_call*);
void WINAPI gw_wire_engine_realm_open(void*);
extern struct gw_engine_realm_control gw_wire_realm_data;
extern volatile LONG gw_wire_realm_entered_original;
extern volatile LONG gw_wire_realm_exit_gate;
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
extern struct gw_wire_auth_control gw_wire_auth_data;
extern struct gw_wire_auth_credentials gw_wire_auth_credentials_data;
extern struct gw_wire_endpoint_profile gw_wire_endpoint_profile_data;
LONG WINAPI gw_source_profile_kind(void);
#endif
#if defined(GW_NATIVE_OUT_OF_PROCESS) && (!defined(GW_NATIVE_WINDOWS) || defined(GW_NATIVE_LINKED_BACKEND))
#error "Owned IPC frontend requires native Windows and no linked backend"
#endif
#ifdef GW_NATIVE_WINDOWS
#ifdef GW_NATIVE_OUT_OF_PROCESS
NTSTATUS WINAPI gw_windows_backend_ipc_initialize(const WCHAR*);
NTSTATUS WINAPI gw_windows_backend_ipc_dispatch(struct gw_wire_call*);
NTSTATUS WINAPI gw_windows_backend_ipc_shutdown(void);
static NTSTATUS backend_call(struct gw_wire_call* call){return gw_windows_backend_ipc_dispatch(call);}
static NTSTATUS backend_unload(void){return gw_windows_backend_ipc_shutdown();}
#else
/* Native Windows owns a normal PE backend; never resolve Wine private APIs. */
typedef NTSTATUS (WINAPI *NativeWireDispatch)(void*);
static HMODULE module;
static NativeWireDispatch native_dispatch;
static NTSTATUS backend_call(struct gw_wire_call* call){return native_dispatch(call);}
static NTSTATUS backend_unload(void){
#ifndef GW_NATIVE_LINKED_BACKEND
if(!FreeLibrary(module))return (NTSTATUS)0xc0000001;
#endif
module=NULL;native_dispatch=NULL;return 0;
}
#endif
#else
static unixlib_module_t module;
static unixlib_handle_t handle;
static NTSTATUS backend_call(struct gw_wire_call* call){return __wine_unix_call(handle,0,call);}
static NTSTATUS backend_unload(void){NTSTATUS status=__wine_unload_unix_lib(module);if(!status)module=handle=0;return status;}
#endif
#ifdef GW_NATIVE_LINKED_BACKEND
__declspec(dllimport) NTSTATUS WINAPI gw_native_wire_dispatch(void*);
#endif
#if defined(GW_NATIVE_LINKED_BACKEND) || defined(GW_NATIVE_OUT_OF_PROCESS)
__declspec(dllimport) unsigned int WINAPI gw_bridge_abi(void);
#endif
static LONG state;
#ifdef GW_NATIVE_WINDOWS
static volatile LONG native_loader_error;
#endif
__declspec(dllexport) unsigned int WINAPI gw_wire_abi(void){return GW_WINE_WIRE_ABI;}
__declspec(dllexport) NTSTATUS WINAPI gw_wire_initialize(const WCHAR* filename){
#ifndef GW_NATIVE_WINDOWS
UNICODE_STRING name;
#endif
NTSTATUS status;unsigned int length=0;LONG previous;
struct gw_wire_call check={0};
if(!filename)return (NTSTATUS)0xc000000d;
previous=InterlockedCompareExchange(&state,1,0);
if(previous==2)return 0;
if(previous)return (NTSTATUS)0x80000011;
while(length<4096 && filename[length])++length;
if(!length || length==4096){state=0;return (NTSTATUS)0xc000000d;}
#ifdef GW_NATIVE_WINDOWS
/* Absolute backend path only. Dependency search is confined to the backend
* directory and standard Windows safe directories, never the process cwd. */
if(!((length>=3 && ((filename[0]>=L'A' && filename[0]<=L'Z') || (filename[0]>=L'a' && filename[0]<=L'z')) && filename[1]==L':' && (filename[2]==L'\\' || filename[2]==L'/')) ||
(length>=3 && filename[0]==L'\\' && filename[1]==L'\\'))){state=0;return (NTSTATUS)0xc000000d;}
#ifdef GW_NATIVE_OUT_OF_PROCESS
if(gw_bridge_abi()!=1){state=0;return (NTSTATUS)0xc000000d;}
status=gw_windows_backend_ipc_initialize(filename);
if(status){native_loader_error=(LONG)GetLastError();state=0;return status;}
native_loader_error=0;
#else
#ifdef GW_NATIVE_LINKED_BACKEND
if(gw_bridge_abi()!=1){state=0;return (NTSTATUS)0xc000000d;}
module=GetModuleHandleW(L"GWNativeBackend.dll");
#else
module=LoadLibraryExW(filename,NULL,0x00000100u|0x00001000u);
#endif
if(!module){native_loader_error=(LONG)GetLastError();state=0;return (NTSTATUS)0xc0000135;}
native_loader_error=0;
#ifdef GW_NATIVE_LINKED_BACKEND
native_dispatch=gw_native_wire_dispatch;
#else
FARPROC symbol=GetProcAddress(module,"gw_native_wire_dispatch");
_Static_assert(sizeof(symbol)==sizeof(native_dispatch),"Native backend function pointer ABI differs");
memcpy(&native_dispatch,&symbol,sizeof(native_dispatch));
if(!native_dispatch){FreeLibrary(module);module=NULL;state=0;return (NTSTATUS)0xc0000139;}
#endif
#endif
#else
name.Buffer=(WCHAR*)filename;name.Length=(USHORT)(length*2);name.MaximumLength=(USHORT)(name.Length+2);
/* Explicit library load. __wine_init_unix_call is for a pre-associated
* builtin module; this normal PE DLL intentionally loads its owned peer. */
status=__wine_load_unix_lib(&name,&module,&handle);
if(status || !handle){state=0;return status?status:(NTSTATUS)0xc000000d;}
#endif
check.abi=GW_WINE_WIRE_ABI;check.size=sizeof(check);check.operation=GW_WIRE_PING;
status=backend_call(&check);
if(status || check.result || check.value!=GW_NATIVE_ABI){
#ifdef GW_NATIVE_OUT_OF_PROCESS
if(status)native_loader_error=(LONG)GetLastError();
#endif
backend_unload();state=0;return status?status:(NTSTATUS)0xc000000d;
}
InterlockedExchange(&state,2);return 0;
}
__declspec(dllexport) NTSTATUS WINAPI gw_wire_dispatch(struct gw_wire_call* call){
if(!call || call->abi!=GW_WINE_WIRE_ABI || call->size!=sizeof(*call))return (NTSTATUS)0xc000000d;
if(InterlockedCompareExchange(&state,2,2)!=2)return (NTSTATUS)0xc0000184;
return backend_call(call);
}
/* Idle caller only: restore engine bindings/stop callbacks before unloading. */
__declspec(dllexport) NTSTATUS WINAPI gw_wire_shutdown(void){
struct gw_wire_call check={0};NTSTATUS status;
if(gw_wire_account_active())return (NTSTATUS)0x80000011;
if(InterlockedCompareExchange(&state,3,2)!=2)return (NTSTATUS)0xc0000184;
check.abi=GW_WINE_WIRE_ABI;check.size=sizeof(check);check.operation=GW_WIRE_COUNTS;
status=backend_call(&check);
if(status || check.result || check.value || check.consumed){InterlockedExchange(&state,2);return status?status:(NTSTATUS)0x80000011;}
status=backend_unload();
if(status){InterlockedExchange(&state,2);return status;}
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
/* A rejected shutdown leaves the live route intact. The browser worker
* holds a Wire module pin and cancellation never joins it. */
gw_priority_slot_shutdown();
#endif
InterlockedExchange(&state,0);return 0;
}
#ifdef GW_ENABLE_SELF_LOCATING_BUNDLE
/* Executable-relative bundle fallback for an already-authorized frame. The
* explicit public initializer remains unchanged. This only locates modules;
* the existing backend ABI ping supplies compatibility, not file identity. */
static NTSTATUS gw_wire_initialize_bundle(void) {
struct gw_bundle_paths *paths;
NTSTATUS result;
paths=(struct gw_bundle_paths*)HeapAlloc(GetProcessHeap(),0,sizeof(*paths));
if(!paths)return (NTSTATUS)0xc0000017;
#ifdef GW_NATIVE_WINDOWS
if(gw_bundle_paths_current(GW_BUNDLE_NATIVE_WINDOWS,paths))result=(NTSTATUS)0xc000000d;
else result=gw_wire_initialize(paths->backend);
#else
if(gw_bundle_paths_current(GW_BUNDLE_WINE,paths))result=(NTSTATUS)0xc000000d;
else result=gw_wire_initialize(paths->wine_loader_name);
#endif
HeapFree(GetProcessHeap(),0,paths);
return result;
}
#endif
extern void WINAPI gw_wire_engine_session_poll(void*);
extern void WINAPI gw_wire_channel_poll(void*);
/* Optional own-module control at the already verified NetClient poll boundary.
* This probe performs only one scalar Unix-call check; no authentication, packet
* injection, connection changes or Lua. Original engine polling is forwarded.
*/
struct WirePollHost {
unsigned int abi,size,stage,calls,thread,error;
unsigned long long owner,original_vtable,original_poll;
unsigned long long methods[129];
};
struct WireFrameData {
unsigned int abi,size;
volatile LONG command,completed,calls,thread,error,initialize_status,call_status,result,echoed,elapsed_ms;
struct WirePollHost* host;
WCHAR filename[512];
};
_Static_assert(sizeof(struct WirePollHost)==1080,"Poll host ABI changed");
_Static_assert(sizeof(struct WireFrameData)==1080,"Wire frame ABI changed");
__declspec(dllexport) struct WireFrameData gw_wire_frame_data={.abi=1,.size=sizeof(struct WireFrameData)};
__declspec(dllexport) uint32_t WINAPI gw_wire_pipe_generation_export(void){return gw_wire_pipe_generation();}
/* Published by the in-process Bridge only after owner and frame admission.
* This is an opt-in precondition for the existing command-7 OpenRealm call. */
__declspec(dllexport) struct GwWirePreOpenHook gw_wire_preopen_hook_data={.abi=1,.size=sizeof(struct GwWirePreOpenHook)};
/* Additive opt-in: older Bridge builds leave ready=0 and take the exact
* existing post-OpenRealm path. A hardware return lease requires this callback
* immediately after the original call, before any session/channel poll. */
__declspec(dllexport) struct GwWirePreOpenHook gw_wire_postopen_hook_data={.abi=1,.size=sizeof(struct GwWirePreOpenHook)};
static volatile LONG realm_after_note_once,realm_enter_note_once;
static void *realm_retry_owner;
static DWORD realm_retry_thread;
static ULONGLONG realm_retry_start,realm_retry_next;
static uint32_t realm_retry_attempts;
static void realm_retry_retire_generation(void){
realm_retry_owner=0;realm_retry_thread=0;
realm_retry_start=realm_retry_next=0;realm_retry_attempts=0;
InterlockedExchange(&realm_after_note_once,0);
InterlockedExchange(&realm_enter_note_once,0);
}
static void wire_frame_retire_generation(void){
realm_retry_retire_generation();
gw_wire_frame_data.error=0;
gw_wire_frame_data.call_status=0;
gw_wire_frame_data.result=0;
gw_wire_frame_data.echoed=0;
gw_wire_frame_data.elapsed_ms=0;
InterlockedExchange(&gw_wire_frame_data.command,2);
}
static void wire_frame_retirement_poll(void* owner,LONG state){
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
/* Failed -12/temporary -23 attempts stay idle until the original connection and
* every existing account/holder/queue guard are terminal. The request
* transition is one-shot; no retirement is replayed while blocked. */
if(state==2 && gw_wire_frame_data.completed==1 &&
gw_wire_frame_data.initialize_status==0 &&
((gw_wire_frame_data.command>=4 && gw_wire_frame_data.command<=8) ||
gw_wire_frame_data.command==12 ||
(gw_wire_frame_data.command==10 &&
gw_wire_session_retirement_data.state==3 &&
(gw_wire_session_retirement_data.result==-12 || gw_wire_session_retirement_data.result==-23))) &&
gw_wire_session_retirement_request_if_terminal(owner))
InterlockedExchange(&gw_wire_frame_data.command,9);
#else
(void)state;
#endif
if(InterlockedCompareExchange(&gw_wire_frame_data.command,10,9)==9){
gw_wire_session_retire(owner,gw_wire_frame_data.error);
if(gw_wire_session_retirement_data.state==2)
wire_frame_retire_generation();
}
}
static char *realm_diag_put(char *at,const char *text){while(*text)*at++=*text++;return at;}
static char *realm_diag_number(char *at,uint64_t value){
char digits[24];unsigned int count=0;
do{digits[count++]=(char)('0'+value%10);value/=10;}while(value);
while(count)*at++=digits[--count];
return at;
}
/* Durable nonsecret numerics in the existing owned startup log. Each call
* closes the file before a possible hardware or postopen fail-closed exit. */
static void realm_diag_log(uint32_t event,uint32_t query,uint32_t state,
uint32_t protect,uint32_t rpm,uint32_t match,
uint32_t got,uint32_t error){
char path[1024],line[320],*at=line;DWORD written=0,length;
HANDLE file;
length=GetEnvironmentVariableA("GW_NATIVE_STARTUP_LOG",path,sizeof(path));
if(!length||length>=sizeof(path))return;
at=realm_diag_put(at,"GW_LOGIN_SITE event=");at=realm_diag_number(at,event);
at=realm_diag_put(at," tid=");at=realm_diag_number(at,GetCurrentThreadId());
at=realm_diag_put(at," pid=");at=realm_diag_number(at,GetCurrentProcessId());
at=realm_diag_put(at," query=");at=realm_diag_number(at,query);
at=realm_diag_put(at," state=");at=realm_diag_number(at,state);
at=realm_diag_put(at," protect=");at=realm_diag_number(at,protect);
at=realm_diag_put(at," rpm=");at=realm_diag_number(at,rpm);
at=realm_diag_put(at," match=");at=realm_diag_number(at,match);
at=realm_diag_put(at," got=");at=realm_diag_number(at,got);
at=realm_diag_put(at," error=");at=realm_diag_number(at,error);
*at++='\n';
file=CreateFileA(path,FILE_APPEND_DATA,FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(file==INVALID_HANDLE_VALUE)return;
WriteFile(file,line,(DWORD)(at-line),&written,NULL);
CloseHandle(file);
}
/* Numeric-only account identity receipt at the authenticated Source dispatch.
* No account bytes, password bytes or digest enter the durable log. */
void gw_wire_account_identity_note(uint32_t generation,uint32_t observed_bytes,
uint32_t expected_bytes,uint32_t comparison){
realm_diag_log(25,(generation<<8)|observed_bytes,expected_bytes,
0,0,comparison,0,0);
}
void gw_wire_realm_note_entered_original(void){
if(!gw_realm_diag_admitted(&gw_wire_preopen_hook_data,&gw_wire_realm_data,
(void*)(uintptr_t)gw_wire_realm_data.owner,
GetCurrentThreadId()) ||
!gw_realm_diag_once(&realm_enter_note_once))return;
realm_diag_log(21,1,(uint32_t)gw_wire_realm_data.stage,
(uint32_t)gw_wire_realm_data.state_before,0,1,7,0);
}
static void realm_after_call(const struct GwWireRealmObservation *seen){
if(!seen || !gw_realm_diag_admitted(&gw_wire_preopen_hook_data,
&gw_wire_realm_data,
(void*)(uintptr_t)gw_wire_realm_data.owner,
GetCurrentThreadId()) ||
!gw_realm_diag_once(&realm_after_note_once))return;
realm_diag_log(20,(uint32_t)seen->stage_after_call,
(uint32_t)seen->state_before,
(uint32_t)seen->state_after,
(uint32_t)seen->stage_before_call,
(uint32_t)seen->entered_original,
(uint32_t)seen->exit_gate,(uint32_t)seen->error);
}
static void WINAPI realm_open_observed(void *owner){
gw_wire_realm_open_observed(owner,gw_wire_engine_realm_open,
&gw_wire_realm_data,&gw_wire_realm_entered_original,
&gw_wire_realm_exit_gate,
gw_realm_diag_admitted(&gw_wire_preopen_hook_data,&gw_wire_realm_data,
owner,GetCurrentThreadId())
? realm_after_call : NULL);
}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
#include "private_account_switch_gate.h"
__declspec(dllexport) struct gw_private_account_switch_gate gw_wire_private_account_switch_gate={.abi=1,.size=sizeof(struct gw_private_account_switch_gate)};
int gw_wire_ui_chosen_ready(void);
static int profile5_auto_admit(void *owner,DWORD thread){
char diagnostic[2]={0};
/* Hold the realm open until the login screen has seen the chosen realm. */
if(!gw_wire_ui_chosen_ready())return 0;
DWORD bytes=GetEnvironmentVariableA("GW_PRIVATE_ACCOUNT_SWITCH_DIAGNOSTIC",diagnostic,sizeof(diagnostic));
int enabled=bytes==1 && diagnostic[0]=='1';
if(!gw_wire_profile5_auto_realm_admit(
&gw_wire_auth_data,&gw_wire_auth_credentials_data,
&gw_wire_endpoint_profile_data,&gw_wire_session_retirement_data,
&gw_wire_preopen_hook_data,owner,*(void**)0x145e7a098ULL,
thread,gw_source_profile_kind()))return 0;
return gw_private_account_switch_realm_allowed(&gw_wire_private_account_switch_gate,
enabled,gw_wire_session_retirement_data.next_credentials_generation,
(uint64_t)(uintptr_t)owner);
}
#endif
__declspec(dllexport) struct gw_frame_timing gw_wire_frame_timing_data={.abi=1,.size=sizeof(struct gw_frame_timing)};
static uint64_t frame_clock_us(void){
LARGE_INTEGER frequency,ticks;
if(!QueryPerformanceFrequency(&frequency)||!frequency.QuadPart||!QueryPerformanceCounter(&ticks))return 0;
return gw_frame_ticks_us((uint64_t)ticks.QuadPart,(uint64_t)frequency.QuadPart);
}
__declspec(dllexport) void WINAPI gw_wire_frame_callback(void* owner){
uint64_t timing_start=frame_clock_us(),timing_gateway_end;int timing_owned=0;
struct WirePollHost* host=gw_wire_frame_data.host;
DWORD thread=GetCurrentThreadId();
if(!host || host->abi!=1 || host->size!=sizeof(*host) || !host->original_poll){gw_wire_frame_data.error=1;return;}
if(host->owner!=(unsigned long long)owner)gw_wire_frame_data.error=2;
else if(host->thread && host->thread!=thread)gw_wire_frame_data.error=3;
else{
timing_owned=1;
host->thread=thread;++host->calls;host->stage=3;
gw_wire_frame_data.thread=(LONG)thread;++gw_wire_frame_data.calls;
#if defined(GW_ENABLE_SELF_LOCATING_BUNDLE) && defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
/* Once the genuine owned frame is installed, a local profile may
* initialize its bundled backend without an external command. Only
* an untouched empty-path frame is claimed; explicit commands and
* completion (including failure) remain one-shot. */
if(!gw_wire_frame_data.filename[0] &&
!InterlockedCompareExchange(&gw_wire_frame_data.completed,0,0))
InterlockedCompareExchange(&gw_wire_frame_data.command,1,0);
#endif
if(InterlockedCompareExchange(&gw_wire_frame_data.command,2,1)==1){
struct gw_wire_call call={0};ULONGLONG start=GetTickCount64();
#ifdef GW_ENABLE_SELF_LOCATING_BUNDLE
gw_wire_frame_data.initialize_status=gw_wire_frame_data.filename[0]
? gw_wire_initialize(gw_wire_frame_data.filename) : gw_wire_initialize_bundle();
#else
gw_wire_frame_data.initialize_status=gw_wire_initialize(gw_wire_frame_data.filename);
#endif
if(!gw_wire_frame_data.initialize_status){
call.abi=GW_WINE_WIRE_ABI;call.size=sizeof(call);call.operation=GW_WIRE_PING;
gw_wire_frame_data.call_status=gw_wire_dispatch(&call);
gw_wire_frame_data.result=call.result;gw_wire_frame_data.echoed=(LONG)call.value;
}
#ifdef GW_NATIVE_WINDOWS
if(gw_wire_frame_data.initialize_status && native_loader_error)gw_wire_frame_data.result=-native_loader_error;
#endif
gw_wire_frame_data.elapsed_ms=(LONG)(GetTickCount64()-start);
InterlockedExchange(&gw_wire_frame_data.completed,1);
}
if(InterlockedCompareExchange(&gw_wire_frame_data.command,4,3)==3){
if(InterlockedCompareExchange(&state,2,2)!=2)gw_wire_frame_data.error=4;
else if(gw_wire_account_start_owned(owner))gw_wire_frame_data.error=5;
}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
/* Genuine singleton local proof advances the existing owned frame once.
* Explicit commands and all other profiles keep their prior behavior. */
/* Public Source waits for the user's realm choice; without an external
* host the owned frame arms the in-process realm-screen UI itself.
* LOCAL (profile 5) does the same once realmd lists several realms
* (#129: normal, Hardcore, PvP); a single realm keeps the auto-admit. */
if(InterlockedCompareExchange(&gw_wire_frame_data.command,4,4)==4 &&
gw_wire_auth_data.state==5 && InterlockedCompareExchange(&state,2,2)==2 &&
(GW_SOURCE_ONLY_PROFILE == 2 || gw_wire_auth_data.realm_count>1))
gw_wire_account_ui_activate();
if(InterlockedCompareExchange(&gw_wire_frame_data.command,4,4)==4 &&
gw_wire_auth_data.state==2 && profile5_auto_admit(owner,thread))
InterlockedCompareExchange(&gw_wire_frame_data.command,5,4);
#endif
if(InterlockedCompareExchange(&gw_wire_frame_data.command,6,5)==5){
struct gw_wire_call call={0};
call.abi=GW_WINE_WIRE_ABI;call.size=sizeof(call);call.operation=GW_WIRE_PORT;
gw_wire_frame_data.call_status=gw_wire_account_transport(&call);
gw_wire_frame_data.result=call.result;gw_wire_frame_data.echoed=(LONG)call.value;
}
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
if(InterlockedCompareExchange(&gw_wire_frame_data.command,6,6)==6 &&
gw_wire_auth_data.state==2 && profile5_auto_admit(owner,thread)){
if(!gw_wire_frame_data.call_status && !gw_wire_frame_data.result &&
gw_wire_frame_data.echoed>0 && gw_wire_frame_data.echoed<=65535)
InterlockedCompareExchange(&gw_wire_frame_data.command,7,6);
else gw_wire_frame_data.error=13;
}
#endif
if(InterlockedCompareExchange(&gw_wire_frame_data.command,7,7)==7){
ULONGLONG now=GetTickCount64();
if(!realm_retry_owner){
realm_retry_owner=owner;realm_retry_thread=thread;
realm_retry_start=now;realm_retry_next=now;
}
if(realm_retry_owner!=owner || realm_retry_thread!=thread ||
now-realm_retry_start>=20000 || realm_retry_attempts>=400){
if(InterlockedCompareExchange(&gw_wire_frame_data.command,8,7)==7){
gw_wire_frame_data.error=16;
realm_diag_log(36,realm_retry_attempts,7,0,0,0,0,16);
}
}else if(now>=realm_retry_next){
HMODULE callback_module=0;
int opened;
realm_retry_next=now+50;
++realm_retry_attempts;
if(gw_wire_preopen_hook_data.callback)
GetModuleHandleExW(0x00000006u,
(LPCWSTR)(uintptr_t)gw_wire_preopen_hook_data.callback,
&callback_module);
opened=gw_wire_preopen_admit(&gw_wire_preopen_hook_data,owner,
thread,callback_module);
if(opened!=2 &&
InterlockedCompareExchange(&gw_wire_frame_data.command,8,7)==7){
if(opened==1){realm_diag_log(130,realm_retry_attempts,7,0,0,0,0,0);realm_open_observed(owner);}
else gw_wire_frame_data.error=12;
}
}
}
/* Explicit owned-host opt-in, after both published UI consumers and
* the strict ABI2 facade are ready. A read-only snapshot never arms it.
* Publish the acknowledgment only after activation has returned. */
if(InterlockedCompareExchange(&gw_wire_frame_data.command,-11,11)==11){
if(InterlockedCompareExchange(&state,2,2)!=2)gw_wire_frame_data.error=4;
else gw_wire_account_ui_activate();
InterlockedExchange(&gw_wire_frame_data.command,12);
}
gw_wire_engine_session_poll(owner);
gw_wire_channel_poll(owner);
wire_frame_retirement_poll(owner,state);
}
timing_gateway_end=frame_clock_us();
((void (WINAPI *)(void*))host->original_poll)(owner);
if(timing_owned && timing_start && timing_gateway_end)gw_frame_timing_record(&gw_wire_frame_timing_data,timing_start,timing_gateway_end,frame_clock_us());
}