| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2010 Michael J. Roberts. |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * This module defines the HTTPServer intrinsic class. This class is used |
| 10 | * to set up a network HTTP server, which has a number of potential |
| 11 | * applications: |
| 12 | * |
| 13 | * - Run a game in a client/server configuration, so that the user |
| 14 | * interface is running in a Web browser on one machine, and the game |
| 15 | * itself is running on a separate server machine on the network. This |
| 16 | * lets the game run on handheld devices that might be too slow to run the |
| 17 | * game directly, allows running the game without any software |
| 18 | * installation, and makes the game more portable by depending only on a |
| 19 | * browser being available rather than requiring a TADS interpreter. |
| 20 | * |
| 21 | * - Run a game in with a browser-based UI, so that it can take advantage |
| 22 | * of HTML DOM and Javascript. Modern browsers are more powerful than HTML |
| 23 | * TADS, so it's possible to create a more elaborate and customized game UI |
| 24 | * by running in a browser. |
| 25 | * |
| 26 | * - Create a multi-user game, by running the game on a server and writing |
| 27 | * the game to accept connections from more than one player. |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * HTTP Server Object. This implements a multi-threaded, background server |
| 32 | * that runs concurrently with the game program. The server listens for |
| 33 | * and accepts incoming connection requests from clients, and then handles |
| 34 | * HTTP protocol transactions with connected clients. Client requests are |
| 35 | * routed to the byte code program via network events, which the program |
| 36 | * can retrieve via the getNetEvent() function. |
| 37 | * |
| 38 | * Construction: to set up an HTTP server, simply create an HTTPServer |
| 39 | * object with 'new': |
| 40 | * |
| 41 | *. local srv = new HTTPServer(hostname, portnum?, maxUploadSize?); |
| 42 | * |
| 43 | * 'hostname' is a string giving the domain name or IP address that the |
| 44 | * server will bind to for accepting connections. For a server that |
| 45 | * accepts connections from separate client machines, this is simply the |
| 46 | * external IP address of the local machine. (This is specified as an |
| 47 | * argument because some machines have more than one network interface, and |
| 48 | * thus have more than one IP address or domain name.) |
| 49 | * |
| 50 | * 'portnum' is the TCP/IP port number wehre the server will listen for |
| 51 | * incoming connections. If this is omitted or nil, the operating system |
| 52 | * will automatically select an available port number and assign it to the |
| 53 | * server. Using a specific port number allows you to create a service on |
| 54 | * a "well known" port, which makes it easier for clients to find the |
| 55 | * service; but a given port can only be used by one server at a time, so |
| 56 | * using a pre-selected port number runs the risk that some other process |
| 57 | * will already be using the same port. |
| 58 | * |
| 59 | * 'maxUploadSize' is the maximum size in bytes for any single request's |
| 60 | * content. Content sizes over this limit will be rejected. Some HTTP |
| 61 | * requests, such as POST, can include uploaded content from the client, |
| 62 | * and the HTTP protocol itself supports essentially unlimited sizes for |
| 63 | * these objects. Uploads consume resources on the server, though, so it's |
| 64 | * often desirable to set a size limit to prevent errant or malicious |
| 65 | * clients from overwhelming the server with a very large upload. |
| 66 | * Depending on the specific function of your server, you might or might |
| 67 | * not wish to set a limit. If you omit this argument or set it to nil, |
| 68 | * unlimited upload sizes will be allowed. Note that this limit applies to |
| 69 | * each individual upload separately; it's not a lifetime limit for the |
| 70 | * server or for any session. |
| 71 | * |
| 72 | * Creating an HTTPServer object with 'new' automatically starts the |
| 73 | * server. The object will create a background thread that will listen for |
| 74 | * incoming connections on the given network address and port number, so |
| 75 | * the server is active as soon as the 'new' finishes. You can create any |
| 76 | * number of servers, as long as they have different port numbers. When a |
| 77 | * connection request is received, the server will accept the connection |
| 78 | * and automatically create another background thread to handle requests on |
| 79 | * that connection. Each incoming request will be forwarded to the game |
| 80 | * program to handle, via the network message queue. |
| 81 | */ |
| 82 | intrinsic class HTTPServer 'http-server/030000': Object |
| 83 | { |
| 84 | /* |
| 85 | * Shut down the server. This immediately disconnects the server from |
| 86 | * its network port; no further client connections will be accepted |
| 87 | * once the server shuts down. In addition, all of the server threads |
| 88 | * that were started by this server object will be notified to |
| 89 | * terminate. |
| 90 | * |
| 91 | * If 'wait' is omitted or is nil, the routine sends the shutdown |
| 92 | * notification to the main server and to its server threads, then |
| 93 | * immediately returns. This means that one or more of the server's |
| 94 | * background threads might continue to run for a while after |
| 95 | * shutdown() returns. The main practical consideration is that the |
| 96 | * port number used by the server might not be immediately available |
| 97 | * for use by a new server object, since the port won't be closed until |
| 98 | * the server actually exits. |
| 99 | * |
| 100 | * If 'wait' is true, this routine won't return until all of the server |
| 101 | * threads have actually terminated. |
| 102 | * |
| 103 | * The return value is true if all server threads have terminated, nil |
| 104 | * if any server threads are still running. It's legal to call this |
| 105 | * routine repeatedly, so you can make repeated calls to shutdown(nil) |
| 106 | * to poll for completion. This is useful if you need to wait until |
| 107 | * the server shuts down to move on to a next step, but you have other |
| 108 | * work you can perform in the meantime. If you don't have any other |
| 109 | * work, you can avoid burning CPU time by calling shutdown(true), |
| 110 | * which waits (without consuming CPU time) for the server to exit. |
| 111 | */ |
| 112 | shutdown(wait?); |
| 113 | |
| 114 | /* |
| 115 | * Get the listening address. This returns a string giving the |
| 116 | * original binding address specified when the object was constructed. |
| 117 | * This can contain either a host name or an IP address, since either |
| 118 | * form can be used in the constructor. |
| 119 | */ |
| 120 | getAddress(); |
| 121 | |
| 122 | /* |
| 123 | * Get the listening IP address. This returns the numerical IP address |
| 124 | * where the server is listening for connections. |
| 125 | */ |
| 126 | getIPAddress(); |
| 127 | |
| 128 | /* |
| 129 | * Get the port number. This returns an integer giving the TCP/IP |
| 130 | * network port number on which this server is listening for incoming |
| 131 | * connections. Clients connect to the port by including it in the |
| 132 | * HTTP URL, after the host name. For example, if the server is on |
| 133 | * port 10815, the client would connect to a URL of the form |
| 134 | * http://myserver.com:10815/index.htm. |
| 135 | */ |
| 136 | getPortNum(); |
| 137 | } |
| 138 |