| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 7 | /* |
| 8 | Name |
| 9 | osnet-connect.cpp - Win32 command-line UI connection |
| 10 | Function |
| 11 | Launches a local Web browser to connect to the Web UI |
| 12 | Notes |
| 13 | |
| 14 | Modified |
| 15 | 05/12/10 MJRoberts - Creation |
| 16 | */ |
| 17 | |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include "t3std.h" |
| 21 | #include "os.h" |
| 22 | #include "osifcnet.h" |
| 23 | #include "vmnet.h" |
| 24 | #include "vmvsn.h" |
| 25 | #include "vmglob.h" |
| 26 | #include "vmimage.h" |
| 27 | #include "osnet-comm.h" |
| 28 | |
| 29 | |
| 30 | /* ------------------------------------------------------------------------ */ |
| 31 | /* |
| 32 | * The Web UI communicator thread - specialization for the VM side of the |
| 33 | * channel. |
| 34 | */ |
| 35 | class WebUICommThreadVM: public WebUICommThread |
| 36 | { |
| 37 | public: |
| 38 | WebUICommThreadVM(TadsMessageQueue *mq, HANDLE proc, HANDLE pipe) |
| 39 | : WebUICommThread(proc, pipe) |
| 40 | { |
| 41 | /* remember the message queue object */ |
| 42 | (msg_queue = mq)->add_ref(); |
| 43 | } |
| 44 | |
| 45 | ~WebUICommThreadVM() |
| 46 | { |
| 47 | /* release the message queue */ |
| 48 | msg_queue->release_ref(); |
| 49 | } |
| 50 | |
| 51 | virtual void process_request(int id, char *cmd) |
| 52 | { |
| 53 | /* check for close/disconnect messages */ |
| 54 | if (memcmp(cmd, "disconnect", 10) == 0 |
| 55 | || memcmp(cmd, "close", 5) == 0) |
| 56 | { |
| 57 | /* post a UI Close event */ |
| 58 | if (msg_queue != 0) |
| 59 | msg_queue->post(new TadsUICloseEvent(0)); |
| 60 | } |
| 61 | |
| 62 | /* inherit the base class handling */ |
| 63 | WebUICommThread::process_request(id, cmd); |
| 64 | } |
| 65 | |
| 66 | /* the application-wide network message queue */ |
| 67 | TadsMessageQueue *msg_queue; |
| 68 | }; |
| 69 | |
| 70 | static WebUICommThreadVM *comm_thread = 0; |
| 71 | |
| 72 | |
| 73 | /* ------------------------------------------------------------------------ */ |
| 74 | /* |
| 75 | * Launch a local "tadsweb" customized browser as a separate process, and |
| 76 | * navigate it to the given start page on our internal HTTP server. This |
| 77 | * handles the Web UI connection when we're running in the local |
| 78 | * stand-alone configuration, where the user launches the game from the |
| 79 | * Windows desktop or command line. |
| 80 | */ |
| 81 | static int launch_tadsweb(VMG_ const char *addr, int port, const char *path, |
| 82 | char **errmsg) |
| 83 | { |
| 84 | STARTUPINFO si; |
| 85 | PROCESS_INFORMATION pi; |
| 86 | char dir[OSFNMAX], exe[OSFNMAX]; |
| 87 | char *rootname; |
| 88 | int ok; |
| 89 | HANDLE pipe = INVALID_HANDLE_VALUE; |
| 90 | char pipe_name[128]; |
| 91 | char cmdline[1024]; |
| 92 | |
| 93 | /* presume failure, but we don't have an error message yet */ |
| 94 | ok = FALSE; |
| 95 | *errmsg = 0; |
| 96 | |
| 97 | /* initialize the process descriptor structures */ |
| 98 | memset(&si, 0, sizeof(si)); |
| 99 | memset(&pi, 0, sizeof(pi)); |
| 100 | si.cb = sizeof(si); |
| 101 | |
| 102 | /* if the URL path starts with a '/', skip it */ |
| 103 | if (path != 0 && *path == '/') |
| 104 | ++path; |
| 105 | |
| 106 | /* |
| 107 | * if there's already a process running, try connecting the current |
| 108 | * process to the new path |
| 109 | */ |
| 110 | if (comm_thread != 0) |
| 111 | { |
| 112 | /* try sending a 'connect' message */ |
| 113 | char *reply; |
| 114 | ok = (comm_thread->send_cmd( |
| 115 | 2500, reply, "connect http://%s:%d/%s", addr, port, path) |
| 116 | && strcmp(reply, "ok") == 0); |
| 117 | |
| 118 | /* free the reply message */ |
| 119 | lib_free_str(reply); |
| 120 | |
| 121 | /* check the result */ |
| 122 | if (ok) |
| 123 | { |
| 124 | /* success - we're now showing the new UI */ |
| 125 | return TRUE; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | /* |
| 130 | * the re-connect attempt failed; shut down the old UI and |
| 131 | * proceed to start a new one |
| 132 | */ |
| 133 | osnet_disconnect_webui(TRUE); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* get the current executable directory */ |
| 138 | GetModuleFileName(0, dir, sizeof(dir)); |
| 139 | |
| 140 | /* |
| 141 | * build the full path to the tadsweb executable, looking in the same |
| 142 | * directory that contains the current process's executable |
| 143 | */ |
| 144 | rootname = os_get_root_name(dir); |
| 145 | *(rootname != 0 ? rootname : dir) = '\0'; |
| 146 | os_build_full_path(exe, sizeof(exe), dir, "tadsweb.exe"); |
| 147 | |
| 148 | /* open a named pipe for communication with the server */ |
| 149 | sprintf(pipe_name, "\\\\.\\pipe\\tadsweb.%lx", |
| 150 | (long)GetCurrentProcessId()); |
| 151 | pipe = CreateNamedPipe( |
| 152 | pipe_name, |
| 153 | PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE |
| 154 | | FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH, |
| 155 | PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, |
| 156 | 1, 4096, 4096, 0, 0); |
| 157 | |
| 158 | /* check to make sure we created the pipe */ |
| 159 | if (pipe == INVALID_HANDLE_VALUE) |
| 160 | goto done; |
| 161 | |
| 162 | /* set up the command line */ |
| 163 | _snprintf(cmdline, sizeof(cmdline), |
| 164 | " url=http://%s:%d/%s pipe=%s ppid=%ld", |
| 165 | addr, port, path, pipe_name, (long)GetCurrentProcessId()); |
| 166 | |
| 167 | /* try launching the process */ |
| 168 | if (CreateProcess(exe, cmdline, 0, 0, TRUE, |
| 169 | 0, 0, 0, &si, &pi)) |
| 170 | { |
| 171 | char *reply = 0; |
| 172 | |
| 173 | /* |
| 174 | * success |
| 175 | */ |
| 176 | |
| 177 | /* set the success status */ |
| 178 | ok = TRUE; |
| 179 | |
| 180 | /* we don't need the webui's main thread handle */ |
| 181 | CloseHandle(pi.hThread); |
| 182 | |
| 183 | /* if there's a previous communicator thread, forget it */ |
| 184 | if (comm_thread != 0) |
| 185 | comm_thread->release_ref(); |
| 186 | |
| 187 | /* create a new communicator thread */ |
| 188 | comm_thread = new WebUICommThreadVM(G_net_queue, pi.hProcess, pipe); |
| 189 | |
| 190 | /* set up an overlapped I/O wait for the client to connect */ |
| 191 | OVERLAPPED ov; |
| 192 | memset(&ov, 0, sizeof(ov)); |
| 193 | ov.hEvent = CreateEvent(0, TRUE, FALSE, 0); |
| 194 | if (ConnectNamedPipe(pipe, &ov) |
| 195 | || GetLastError() == ERROR_PIPE_CONNECTED) |
| 196 | { |
| 197 | /* success/already connected */ |
| 198 | } |
| 199 | else if (GetLastError() == ERROR_IO_PENDING) |
| 200 | { |
| 201 | /* wait for that to complete, but not too long */ |
| 202 | ok = (WaitForSingleObject(ov.hEvent, 30000) == WAIT_OBJECT_0); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | /* failed */ |
| 207 | ok = FALSE; |
| 208 | } |
| 209 | |
| 210 | /* done with the overlapped I/O event */ |
| 211 | CloseHandle(ov.hEvent); |
| 212 | |
| 213 | /* if the client didn't start up, abort */ |
| 214 | if (!ok) |
| 215 | { |
| 216 | *errmsg = lib_copy_str("Timed out waiting for client to connect"); |
| 217 | goto done; |
| 218 | } |
| 219 | |
| 220 | /* launch the thread */ |
| 221 | if (!comm_thread->launch()) |
| 222 | { |
| 223 | /* couldn't launch the thread, so release the thread object */ |
| 224 | comm_thread->release_ref(); |
| 225 | comm_thread = 0; |
| 226 | |
| 227 | /* flag the error and abort */ |
| 228 | ok = FALSE; |
| 229 | *errmsg = lib_copy_str("Unable to launch monitor thread"); |
| 230 | goto done; |
| 231 | } |
| 232 | |
| 233 | /* send it the TADS version string */ |
| 234 | if (comm_thread->send_cmd(500, reply, "tadsver %s", T3VM_VSN_STRING)) |
| 235 | lib_free_str(reply); |
| 236 | |
| 237 | /* send the game directory */ |
| 238 | if (comm_thread->send_cmd(500, reply, |
| 239 | "gamedir %s", G_image_loader->get_path())) |
| 240 | lib_free_str(reply); |
| 241 | |
| 242 | /* send the default saved game extension, if any */ |
| 243 | const char *ext = os_get_save_ext(); |
| 244 | if (ext != 0 && comm_thread->send_cmd(500, reply, "saveext %s", ext)) |
| 245 | lib_free_str(reply); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | /* failed */ |
| 250 | *errmsg = lib_copy_str("Unable to open game window"); |
| 251 | } |
| 252 | |
| 253 | done: |
| 254 | /* if an error occurred, clean up partial resources */ |
| 255 | if (!ok) |
| 256 | { |
| 257 | /* if we opened the pipe, close it */ |
| 258 | if (pipe != INVALID_HANDLE_VALUE) |
| 259 | CloseHandle(pipe); |
| 260 | |
| 261 | /* if we created a process, close the handle */ |
| 262 | if (pi.hProcess != 0) |
| 263 | CloseHandle(pi.hProcess); |
| 264 | } |
| 265 | |
| 266 | /* return the status indication */ |
| 267 | return ok; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * Connect to the client UI. A Web-based game calls this after starting |
| 273 | * its internal HTTP server, to send instructions back to the client on how |
| 274 | * the client UI can connect to the game. |
| 275 | * |
| 276 | * There are two very different configurations we can run in: |
| 277 | * |
| 278 | * - Local, stand-alone mode. This is the "old fashioned" configuration |
| 279 | * where everything's installed on the local PC: the user has a copy of the |
| 280 | * TADS Player Kit installed, and has a downloaded copy of the game on the |
| 281 | * local hard disk. In this mode, everything should behave as much as |
| 282 | * possible like the conventional Windows version of TADS. To launch the |
| 283 | * game, the user double-clicks the game file on the Windows desktop, or |
| 284 | * enters a t3run command line. There's no browser involved in the launch |
| 285 | * process, so to connect to the UI, we actually have to create the UI. In |
| 286 | * principle, this *could* simply launch a regular Web browser, but that's |
| 287 | * not what our implementation does. Instead, to provide a more integrated |
| 288 | * appearance to the UI, we launch a special TADS window that's basically |
| 289 | * an IE control wrapped in a custom app frame. This lets us decorate the |
| 290 | * app frame so that it looks roughly like a traditional HTML TADS window. |
| 291 | * |
| 292 | * - Client/server mode. In this mode, the local machine is set up with |
| 293 | * t3run plus a conventional Web server (e.g., Apache), with the TADS web |
| 294 | * launch scripts installed. The user is on a remote machine. The user |
| 295 | * opens a Web browser and connects to our Apache server by requesting (via |
| 296 | * HTTP) our launch page. The launch page is typically our t3launch.php |
| 297 | * script. That page executes t3run (that's us) as a subprocess. Before |
| 298 | * t3launch.php sends a reply back to the client, it waits to hear from the |
| 299 | * t3run subprocess (that's us again): we have to send the game connection |
| 300 | * information, so that t3launch.php can relay the information back to the |
| 301 | * client as its reply to the launch page request. The php page sends the |
| 302 | * address back as an HTTP 301 redirect, so the client automatically |
| 303 | * navigates to the game's start page. |
| 304 | * |
| 305 | * We can tell which mode we're running in by checking for a "hostname" |
| 306 | * variable in the net config object. If there's no hostname variable |
| 307 | * setting, we're in local mode. If there's a hostname setting, we're in |
| 308 | * client/server mode, and the hostname gives the address of the network |
| 309 | * adapter our HTTP listener binds to. |
| 310 | */ |
| 311 | int osnet_connect_webui(VMG_ const char *addr, int port, const char *path, |
| 312 | char **errmsg) |
| 313 | { |
| 314 | /* get the host name from the network configuration, if any */ |
| 315 | const char *hostname = (G_net_config != 0 |
| 316 | ? G_net_config->get("hostname") : 0); |
| 317 | |
| 318 | /* |
| 319 | * If we have host name in the network configuration, we're running in |
| 320 | * Web server mode - the host name parameter means that we were |
| 321 | * launched by a web server in response to an incoming launch request |
| 322 | * from a remote client. In this mode, we need to send the connection |
| 323 | * information back to the web server's launcher, which is our parent |
| 324 | * process; we send the information back via stdout. |
| 325 | * |
| 326 | * If we don't have a host name, it means that the user must have |
| 327 | * launched the interpreter from the Windows desktop or command shell, |
| 328 | * in which case they want to run the game as a local, stand-alone |
| 329 | * application. In local mode, we have to display our own user |
| 330 | * interface. |
| 331 | */ |
| 332 | if (hostname != 0) |
| 333 | { |
| 334 | /* |
| 335 | * Web server mode. We were launched by the local Web server in |
| 336 | * response to a new game request from a Web client. Our parent |
| 337 | * process is the Web server running the php launch page. The php |
| 338 | * page has a pipe connection to our stdout. Send the start page |
| 339 | * information back to the php page simply by writing the |
| 340 | * information to stdout. |
| 341 | */ |
| 342 | printf("connectWebUI:http://%s:%d%s\n", addr, port, path); |
| 343 | fflush(stdout); |
| 344 | |
| 345 | #if 0 |
| 346 | // $$$ for testing only |
| 347 | if (strcmp(hostname, "localhost") == 0) |
| 348 | { |
| 349 | char buf[4096]; |
| 350 | _snprintf(buf, sizeof(buf), "http://%s:%d%s\n", addr, port, path); |
| 351 | ShellExecute(0, "open", buf, 0, 0, SW_SHOWNORMAL); |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | /* success */ |
| 356 | *errmsg = 0; |
| 357 | return TRUE; |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | /* |
| 362 | * Local stand-alone mode. In this mode, we have to launch our own |
| 363 | * user interface, since the user directly ran the TADS interpreter |
| 364 | * from the Windows desktop or command shell, and so far we haven't |
| 365 | * presented any UI. Our UI in this mode is the "tadsweb" custom |
| 366 | * browser. This is similar to an ordinary browser, but it has a |
| 367 | * customized frame (menus, toolbar, etc) that makes it look more |
| 368 | * like a regular HTML TADS window, and it has a special control |
| 369 | * channel (via a pipe) that tightly couples it to the interpreter. |
| 370 | */ |
| 371 | return launch_tadsweb(vmg_ addr, port, path, errmsg); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* ------------------------------------------------------------------------ */ |
| 377 | /* |
| 378 | * Disconnect from the web UI, in preparation for program termination |
| 379 | */ |
| 380 | void osnet_disconnect_webui(int close) |
| 381 | { |
| 382 | /* if the web ui process is running, send it a shutdown message */ |
| 383 | if (comm_thread != 0) |
| 384 | { |
| 385 | /* send a 'shutdown' message */ |
| 386 | comm_thread->disconnect(close); |
| 387 | |
| 388 | /* wait for the comm thread to finish (but not forever) */ |
| 389 | comm_thread->wait(15000); |
| 390 | |
| 391 | /* we're done with the thread */ |
| 392 | comm_thread->release_ref(); |
| 393 | comm_thread = 0; |
| 394 | } |
| 395 | |
| 396 | /* unload dynamically linked libraries */ |
| 397 | tads_unlink_user32(); |
| 398 | } |
| 399 | |
| 400 | /* ------------------------------------------------------------------------ */ |
| 401 | /* |
| 402 | * Negotiate foreground status for the Web UI |
| 403 | */ |
| 404 | |
| 405 | /* ask the Web UI to yield the foreground to the current process */ |
| 406 | void osnet_webui_yield_foreground() |
| 407 | { |
| 408 | if (comm_thread != 0) |
| 409 | { |
| 410 | /* send the request to yield the foreground to the current PID */ |
| 411 | char *reply; |
| 412 | if (comm_thread->send_cmd(2500, reply, |
| 413 | "yield-fg %ld", GetCurrentProcessId())) |
| 414 | lib_free_str(reply); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* bring the Web UI to the foreground */ |
| 419 | void osnet_webui_to_foreground() |
| 420 | { |
| 421 | if (comm_thread != 0) |
| 422 | { |
| 423 | /* explicitly yield the foreground to the webui process */ |
| 424 | tads_AllowSetForegroundWindow(GetProcessId(comm_thread->proc)); |
| 425 | |
| 426 | /* tell the web ui to set itself as the foreground window */ |
| 427 | char *reply; |
| 428 | if (comm_thread->send_cmd(1000, reply, "to-fg")) |
| 429 | lib_free_str(reply); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* ------------------------------------------------------------------------ */ |
| 434 | /* |
| 435 | * Quote the askfile prompt - escape commas and percent signs with '%' |
| 436 | */ |
| 437 | static size_t quote_askfile_prompt(char *dst, const char *src) |
| 438 | { |
| 439 | size_t len; |
| 440 | for (len = 0 ; *src != '\0' ; ++src) |
| 441 | { |
| 442 | switch (*src) |
| 443 | { |
| 444 | case ',': |
| 445 | case '%': |
| 446 | /* we need to quote these */ |
| 447 | len += 2; |
| 448 | if (dst != 0) |
| 449 | { |
| 450 | *dst++ = '%'; |
| 451 | *dst++ = *src; |
| 452 | } |
| 453 | break; |
| 454 | |
| 455 | default: |
| 456 | /* everything else goes as is */ |
| 457 | len += 1; |
| 458 | if (dst != 0) |
| 459 | *dst++ = *src; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* null-terminate the string */ |
| 465 | if (dst != 0) |
| 466 | *dst = '\0'; |
| 467 | |
| 468 | /* return the length */ |
| 469 | return len; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* ------------------------------------------------------------------------ */ |
| 474 | /* |
| 475 | * Run the askfile dialog in the Web UI process. This sends a pipe request |
| 476 | * to the Web UI process asking it to show the file selector, and returns |
| 477 | * the result. |
| 478 | */ |
| 479 | int osnet_askfile(const char *prompt, char *fname_buf, int fname_buf_len, |
| 480 | int prompt_type, int file_type) |
| 481 | { |
| 482 | /* presume failure */ |
| 483 | int ret = 1; |
| 484 | |
| 485 | if (comm_thread != 0) |
| 486 | { |
| 487 | /* allocate space for the quoted prompt, and quote it */ |
| 488 | size_t qplen = quote_askfile_prompt(0, prompt); |
| 489 | char *qprompt = lib_alloc_str(qplen); |
| 490 | quote_askfile_prompt(qprompt, prompt); |
| 491 | |
| 492 | /* send the request */ |
| 493 | char *reply; |
| 494 | if (comm_thread->send_cmd( |
| 495 | OS_FOREVER, reply, |
| 496 | "askfile %s,%d,%d", qprompt, prompt_type, file_type)) |
| 497 | { |
| 498 | /* |
| 499 | * Got a reply. The return code is the first character of the |
| 500 | * buffer, as a digit giving the OS_AFE_xxx value. |
| 501 | */ |
| 502 | ret = reply[0] - '0'; |
| 503 | |
| 504 | /* the rest is the filename string */ |
| 505 | lib_strcpy(fname_buf, fname_buf_len, reply + 1); |
| 506 | |
| 507 | /* free the reply */ |
| 508 | lib_free_str(reply); |
| 509 | } |
| 510 | |
| 511 | /* delete the allocated buffer */ |
| 512 | lib_free_str(qprompt); |
| 513 | } |
| 514 | |
| 515 | /* return the result */ |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 |