| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 4 | /* |
| 5 | Name |
| 6 | osnet-comm.h - Win32 Web UI communicator thread |
| 7 | Function |
| 8 | Interprocess communication server for the Web UI |
| 9 | Notes |
| 10 | This module defines some helper classes for interprocess communications |
| 11 | between the TADS VM and the Web UI. These two parts of the system run as |
| 12 | separate processes, so they need a way to coordinate their activities. For |
| 13 | the most part, the browser control (in the Web UI) and the byte code |
| 14 | program (on the VM side) communicate via HTTP to create the presentation |
| 15 | and handle user input events. However, the VM and the Web UI need to talk |
| 16 | directly, at a "meta" level, to coordinate certain application-level |
| 17 | activities between the two processes. We do this via a pair of pipes that |
| 18 | the VM creates and hands to the UI process via handle inheritance. This |
| 19 | module implements a message-based protocol on the pipes, and provides a |
| 20 | convenient, high-level API to send and receive these messages. |
| 21 | Modified |
| 22 | 05/26/10 MJRoberts - Creation |
| 23 | */ |
| 24 | |
| 25 | #ifndef OSNET_COMM_H |
| 26 | #define OSNET_COMM_H |
| 27 | |
| 28 | #include "osifcnet.h" |
| 29 | #include "vmnet.h" |
| 30 | |
| 31 | |
| 32 | /* ------------------------------------------------------------------------ */ |
| 33 | /* |
| 34 | * Web UI message. This message represents a request from this process to |
| 35 | * our peer process; we queue these to the web UI communicator thread, |
| 36 | * which sends them across the pipe and reads the replies. |
| 37 | */ |
| 38 | class WebUIMessage: public TadsMessage |
| 39 | { |
| 40 | public: |
| 41 | WebUIMessage(int serial, char *msg) : TadsMessage("WebUIMessage", 0) |
| 42 | { |
| 43 | /* take ownership of the command message */ |
| 44 | this->msg = msg; |
| 45 | |
| 46 | /* no reply yet */ |
| 47 | reply = 0; |
| 48 | |
| 49 | /* set our serial number */ |
| 50 | this->serial = serial; |
| 51 | } |
| 52 | |
| 53 | ~WebUIMessage() |
| 54 | { |
| 55 | /* done with the command message and reply */ |
| 56 | lib_free_str(msg); |
| 57 | lib_free_str(reply); |
| 58 | } |
| 59 | |
| 60 | /* the request message */ |
| 61 | char *msg; |
| 62 | |
| 63 | /* the reply from the webui process */ |
| 64 | char *reply; |
| 65 | |
| 66 | /* the message serial number, for matching it up with the reply */ |
| 67 | int serial; |
| 68 | }; |
| 69 | |
| 70 | /* ------------------------------------------------------------------------ */ |
| 71 | /* |
| 72 | * Web UI communications thread. This thread monitors the incoming pipe |
| 73 | * for requests from the webui to self. |
| 74 | */ |
| 75 | class WebUICommThread: public OS_Thread |
| 76 | { |
| 77 | public: |
| 78 | WebUICommThread(HANDLE proc, HANDLE pipe) |
| 79 | { |
| 80 | /* remember the various handles */ |
| 81 | this->proc = proc; |
| 82 | this->pipe = pipe; |
| 83 | |
| 84 | /* create our message queue */ |
| 85 | queue = new TadsMessageQueue(0); |
| 86 | |
| 87 | /* create the 'quit' event */ |
| 88 | quit = new OS_Event(TRUE); |
| 89 | |
| 90 | /* set the initial message serial number */ |
| 91 | wmsg_serial = 0; |
| 92 | |
| 93 | /* nothing in the pending reply queue yet */ |
| 94 | msg_pending = 0; |
| 95 | |
| 96 | /* initialize the overlapped read structure for the pipe */ |
| 97 | memset(&pipe_ov, 0, sizeof(pipe_ov)); |
| 98 | pipe_ov.hEvent = CreateEvent(0, TRUE, FALSE, 0); |
| 99 | |
| 100 | /* not yet disconnecting */ |
| 101 | disconnecting = FALSE; |
| 102 | } |
| 103 | |
| 104 | virtual ~WebUICommThread() |
| 105 | { |
| 106 | /* |
| 107 | * flush the outgoing pipe to allow the client to read any pending |
| 108 | * data before we disconnect |
| 109 | */ |
| 110 | FlushFileBuffers(pipe); |
| 111 | DisconnectNamedPipe(pipe); |
| 112 | |
| 113 | /* cancel any outstanding read request */ |
| 114 | CancelIo(pipe); |
| 115 | |
| 116 | /* close our handles */ |
| 117 | CloseHandle(proc); |
| 118 | CloseHandle(pipe); |
| 119 | CloseHandle(pipe_ov.hEvent); |
| 120 | |
| 121 | /* clear and release the queue */ |
| 122 | queue->flush(); |
| 123 | queue->release_ref(); |
| 124 | |
| 125 | /* release the quit event */ |
| 126 | quit->release_ref(); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Disconnect |
| 131 | */ |
| 132 | void disconnect(int close_ui) |
| 133 | { |
| 134 | /* |
| 135 | * If we've already received a disconnect request from the other |
| 136 | * side, there's no need to echo it back, and in fact it's |
| 137 | * problematic to do so, since the other side is busy waiting for |
| 138 | * us to reply to its request. |
| 139 | */ |
| 140 | if (disconnecting) |
| 141 | return; |
| 142 | |
| 143 | /* send a "disconnect" request */ |
| 144 | char *reply; |
| 145 | send_cmd(10000, reply, close_ui ? "close" : "disconnect"); |
| 146 | lib_free_str(reply); |
| 147 | |
| 148 | /* signal the 'quit' event */ |
| 149 | quit->signal(); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Process a request. This must be overridden in each subclass to |
| 154 | * handle the specific request types for this end of the pipe. |
| 155 | * |
| 156 | * "id" is an integer giving the request ID. This is an arbitrary |
| 157 | * serial number that we must send back to the peer with the reply; |
| 158 | * this allows the peer to match up the reply to its original request. |
| 159 | * |
| 160 | * "cmd" is the command text, as specified by the client. The syntax |
| 161 | * is up to each subclass to define. For convenience, this routine may |
| 162 | * modify the command buffer in place, provided it doesn't add bytes |
| 163 | * past the end of the existing string. |
| 164 | */ |
| 165 | virtual void process_request(int id, char *cmd) |
| 166 | { |
| 167 | /* check for messages we handle in the base class */ |
| 168 | if (strcmp(cmd, "disconnect") == 0 || strcmp(cmd, "close") == 0) |
| 169 | { |
| 170 | /* disconnect - note that we've received this request */ |
| 171 | disconnecting = TRUE; |
| 172 | |
| 173 | /* send an acknowledgment */ |
| 174 | send_reply(id, "ok"); |
| 175 | |
| 176 | /* signal the 'quit' event to shut ourselves down */ |
| 177 | quit->signal(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* main thread entrypoint */ |
| 182 | void thread_main() |
| 183 | { |
| 184 | /* start the first non-blocking read on the pipe */ |
| 185 | start_read_pipe(); |
| 186 | |
| 187 | /* keep going until the process terminates */ |
| 188 | for (int done = FALSE ; !done ; ) |
| 189 | { |
| 190 | /* wait for the read pipe or process termination */ |
| 191 | HANDLE hlst[] = { |
| 192 | proc, |
| 193 | queue->get_event_obj()->get_handle(), |
| 194 | quit->get_handle(), |
| 195 | pipe_ov.hEvent |
| 196 | }; |
| 197 | switch (WaitForMultipleObjects(4, hlst, FALSE, INFINITE)) |
| 198 | { |
| 199 | case WAIT_OBJECT_0: |
| 200 | /* the process has terminated */ |
| 201 | done = TRUE; |
| 202 | break; |
| 203 | |
| 204 | case WAIT_OBJECT_0 + 1: |
| 205 | /* there's a message in our request queue */ |
| 206 | process_message(); |
| 207 | break; |
| 208 | |
| 209 | case WAIT_OBJECT_0 + 2: |
| 210 | /* quit event */ |
| 211 | done = TRUE; |
| 212 | break; |
| 213 | |
| 214 | case WAIT_OBJECT_0 + 3: |
| 215 | /* the pipe is ready - go read it */ |
| 216 | finish_read_pipe(); |
| 217 | |
| 218 | /* schedule the next read */ |
| 219 | start_read_pipe(); |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | /* anything else is an error - abort */ |
| 224 | flag_error("error waiting in main monitor thread"); |
| 225 | done = TRUE; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* flag an error */ |
| 232 | void flag_error(const char *msg) |
| 233 | { |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Send a command to the peer and await the reply |
| 238 | */ |
| 239 | int send_cmd(unsigned long timeout, char *&reply, const char *msg, ...) |
| 240 | { |
| 241 | /* presume there will be no reply */ |
| 242 | reply = 0; |
| 243 | |
| 244 | /* if the peer isn't running, don't bother */ |
| 245 | if (!peer_running()) |
| 246 | return FALSE; |
| 247 | |
| 248 | /* format the message */ |
| 249 | va_list args; |
| 250 | va_start(args, msg); |
| 251 | char *buf = t3vsprintf_alloc(msg, args); |
| 252 | va_end(args); |
| 253 | |
| 254 | /* queue the message (the message object takes over the buffer) */ |
| 255 | WebUIMessage *m = new WebUIMessage(++wmsg_serial, buf); |
| 256 | int ok = queue->send(m, timeout); |
| 257 | |
| 258 | /* check what happened */ |
| 259 | if (ok) |
| 260 | { |
| 261 | /* success - make a copy of the reply for the caller */ |
| 262 | reply = lib_copy_str(m->reply); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | /* |
| 267 | * failed - notify the comm thread that we're no longer |
| 268 | * interested in the message, so that it can release any |
| 269 | * references it has to the message object |
| 270 | */ |
| 271 | abandon(m); |
| 272 | } |
| 273 | |
| 274 | /* done with the message */ |
| 275 | m->release_ref(); |
| 276 | |
| 277 | /* return the status indication */ |
| 278 | return ok; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Send a reply to a request from the peer |
| 283 | */ |
| 284 | int send_reply(int id, const char *reply, ...) |
| 285 | { |
| 286 | /* if the peer isn't running, don't bother */ |
| 287 | if (!peer_running()) |
| 288 | return FALSE; |
| 289 | |
| 290 | /* format the message */ |
| 291 | va_list args; |
| 292 | va_start(args, reply); |
| 293 | char *mbuf = t3vsprintf_alloc(reply, args); |
| 294 | va_end(args); |
| 295 | |
| 296 | /* format the overall message: "reply ID <text>" */ |
| 297 | char *buf = t3sprintf_alloc("reply %d %s", id, mbuf); |
| 298 | size_t buflen = strlen(buf); |
| 299 | |
| 300 | /* write the reply to the pipe */ |
| 301 | DWORD actual; |
| 302 | int ok = (WriteFile(pipe, buf, buflen, &actual, 0) |
| 303 | && actual == buflen); |
| 304 | |
| 305 | /* done with the formatted message buffers */ |
| 306 | t3free(buf); |
| 307 | t3free(mbuf); |
| 308 | |
| 309 | /* return the status indication */ |
| 310 | return ok; |
| 311 | } |
| 312 | |
| 313 | /* is the peer process running? */ |
| 314 | int peer_running() |
| 315 | { |
| 316 | /* it's running if the process handle isn't signaled yet */ |
| 317 | return proc != INVALID_HANDLE_VALUE |
| 318 | && WaitForSingleObject(proc, 0) == WAIT_TIMEOUT; |
| 319 | } |
| 320 | |
| 321 | /* process a message */ |
| 322 | void process_message() |
| 323 | { |
| 324 | WebUIMessage *uimsg; |
| 325 | |
| 326 | /* retrieve the message */ |
| 327 | TadsMessage *msg = queue->get(); |
| 328 | |
| 329 | /* check the type */ |
| 330 | if ((uimsg = cast_tads_message(WebUIMessage, msg)) != 0) |
| 331 | { |
| 332 | /* build the full request: "serial command" */ |
| 333 | char *req = t3sprintf_alloc("%d %s", uimsg->serial, uimsg->msg); |
| 334 | size_t reqlen = strlen(req); |
| 335 | |
| 336 | /* write the request to the pipe */ |
| 337 | DWORD actual; |
| 338 | if (WriteFile(pipe, req, reqlen, &actual, FALSE) |
| 339 | && actual == reqlen) |
| 340 | { |
| 341 | /* |
| 342 | * We successfully wrote the request to the pipe. Now we |
| 343 | * just sit back and wait for the other side to send back a |
| 344 | * reply tagged with this message ID. In order to find the |
| 345 | * message object later, though, we need to add it to our |
| 346 | * list of messages pending replies. |
| 347 | */ |
| 348 | uimsg->nxt = msg_pending; |
| 349 | msg_pending = uimsg; |
| 350 | |
| 351 | /* add a reference for the reply list */ |
| 352 | uimsg->add_ref(); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | /* |
| 357 | * failed to write the request - send back a failure reply |
| 358 | * immediately |
| 359 | */ |
| 360 | flag_error("error in pipe send"); |
| 361 | uimsg->reply = lib_copy_str("error in pipe send"); |
| 362 | uimsg->complete(); |
| 363 | } |
| 364 | |
| 365 | /* done with the request text */ |
| 366 | t3free(req); |
| 367 | } |
| 368 | |
| 369 | /* we're done with the message */ |
| 370 | if (msg != 0) |
| 371 | msg->release_ref(); |
| 372 | } |
| 373 | |
| 374 | /* schedule a read on the pipe */ |
| 375 | void start_read_pipe() |
| 376 | { |
| 377 | /* reset the pipe read event */ |
| 378 | ResetEvent(pipe_ov.hEvent); |
| 379 | |
| 380 | /* do the read */ |
| 381 | if (!ReadFile(pipe, pipe_in_buf, sizeof(pipe_in_buf)-1, 0, &pipe_ov) |
| 382 | && GetLastError() != ERROR_IO_PENDING) |
| 383 | { |
| 384 | /* read failed - signal the quit event */ |
| 385 | DWORD err = GetLastError(); |
| 386 | flag_error("error in pipe read"); |
| 387 | quit->signal(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* complete a read on the pipe */ |
| 392 | void finish_read_pipe() |
| 393 | { |
| 394 | /* get the result information */ |
| 395 | DWORD actual; |
| 396 | GetOverlappedResult(pipe, &pipe_ov, &actual, TRUE); |
| 397 | |
| 398 | /* null-terminate the message */ |
| 399 | char *msg = pipe_in_buf; |
| 400 | msg[actual] = '\0'; |
| 401 | |
| 402 | /* check the message syntax */ |
| 403 | if (memcmp(msg, "reply ", 6) == 0) |
| 404 | { |
| 405 | /* get the ID */ |
| 406 | int id = atoi(msg + 6); |
| 407 | |
| 408 | /* find the reply text, after the ID */ |
| 409 | char *p; |
| 410 | for (p = msg + 6 ; *p != '\0' && !isspace(*p) ; ++p) ; |
| 411 | for ( ; isspace(*p) ; ++p) ; |
| 412 | |
| 413 | /* it's a reply to a message we sent - process it */ |
| 414 | process_reply(id, p); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | /* |
| 419 | * It's a request message. It has the syntax "ID command", |
| 420 | * where "ID" is the serial number of the message, which we |
| 421 | * have to send back in the reply to allow our peer to match it |
| 422 | * up with the request; and "command" is the command text for |
| 423 | * the request. |
| 424 | */ |
| 425 | |
| 426 | /* get the ID */ |
| 427 | int id = atoi(msg); |
| 428 | |
| 429 | /* find the command, which follows a space after the ID */ |
| 430 | char *cmd; |
| 431 | for (cmd = msg ; *cmd != '\0' && !isspace(*cmd) ; ++cmd) ; |
| 432 | for ( ; isspace(*cmd) ; ++cmd) ; |
| 433 | |
| 434 | /* process the request */ |
| 435 | process_request(id, cmd); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /* process a reply to a message we sent */ |
| 440 | void process_reply(int id, char *txt) |
| 441 | { |
| 442 | /* find the message in the pending reply list (removing it) */ |
| 443 | WebUIMessage *msg = remove_pending(id); |
| 444 | |
| 445 | /* if we found it, send the reply */ |
| 446 | if (msg != 0) |
| 447 | { |
| 448 | /* copy the reply text into the message */ |
| 449 | msg->reply = lib_copy_str(txt); |
| 450 | |
| 451 | /* signal it as completed */ |
| 452 | msg->complete(); |
| 453 | |
| 454 | /* we're done with the message */ |
| 455 | msg->release_ref(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* abandon a message */ |
| 460 | void abandon(TadsMessage *msg) |
| 461 | { |
| 462 | /* remove the message from the reply wait list */ |
| 463 | WebUIMessage *uimsg = cast_tads_message(WebUIMessage, msg); |
| 464 | if (uimsg != 0) |
| 465 | { |
| 466 | /* remove it from the pending list */ |
| 467 | if (remove_pending(uimsg->serial) != 0) |
| 468 | { |
| 469 | /* it was in the list, so remove the list's reference */ |
| 470 | uimsg->release_ref(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* remove the message from the queue as well */ |
| 475 | queue->abandon(msg); |
| 476 | } |
| 477 | |
| 478 | /* remove a message from the pending reply list */ |
| 479 | WebUIMessage *remove_pending(int id) |
| 480 | { |
| 481 | /* find the message in our pending list */ |
| 482 | WebUIMessage *msg, *prv; |
| 483 | for (prv = 0, msg = msg_pending ; msg != 0 ; |
| 484 | prv = msg, msg = (WebUIMessage *)msg->nxt) |
| 485 | { |
| 486 | /* check the ID */ |
| 487 | if (msg->serial == id) |
| 488 | { |
| 489 | /* found it - unlink it from the pending list */ |
| 490 | if (prv != 0) |
| 491 | prv->nxt = msg->nxt; |
| 492 | else |
| 493 | msg_pending = (WebUIMessage *)msg->nxt; |
| 494 | |
| 495 | /* it's no longer in any list */ |
| 496 | msg->nxt = 0; |
| 497 | |
| 498 | /* return it */ |
| 499 | return msg; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* didn't find it */ |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* webui process handle */ |
| 508 | HANDLE proc; |
| 509 | |
| 510 | /* pipe for communicating with peer */ |
| 511 | HANDLE pipe; |
| 512 | |
| 513 | /* pipe overlapped read structure */ |
| 514 | OVERLAPPED pipe_ov; |
| 515 | |
| 516 | /* input pipe read buffer */ |
| 517 | char pipe_in_buf[4096]; |
| 518 | |
| 519 | /* self->webui request message queue */ |
| 520 | TadsMessageQueue *queue; |
| 521 | |
| 522 | /* list of messages pending replies */ |
| 523 | WebUIMessage *msg_pending; |
| 524 | |
| 525 | /* 'quit' event */ |
| 526 | OS_Event *quit; |
| 527 | |
| 528 | /* WebUIMessage serial number */ |
| 529 | int wmsg_serial; |
| 530 | |
| 531 | /* flag: we've received a disconnect request from our peer */ |
| 532 | int disconnecting; |
| 533 | }; |
| 534 | |
| 535 | #endif /* OSNET_COMM_H */ |
| 536 |