| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1999, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3 |
| 8 | * |
| 9 | * This header defines the tads-net intrinsic function set. |
| 10 | * |
| 11 | * The tads-net function set provides network input/output. In particular, |
| 12 | * this implements an HTTP server that can be used to implement a |
| 13 | * browser-based user interface. The network layer could also be used for |
| 14 | * other purposes, such as implementing a multi-user game server. |
| 15 | * |
| 16 | * The first step in setting up a network server is to create a suitable |
| 17 | * server object, such as an HTTPServer. The server opens a network port |
| 18 | * on the local machine and waits for incoming connection requests from |
| 19 | * clients, such as from Web browsers on remote machines. When a client |
| 20 | * connects to the server's port, the server creates a separate thread to |
| 21 | * handle the new connection, then goes back to listening for additional |
| 22 | * connections. The connection thread handles network communications with |
| 23 | * the client; when the client sends a request to the server, the thread |
| 24 | * posts an event message to the network message queue. |
| 25 | * |
| 26 | * The next step for the program, then, is simply to go into a loop reading |
| 27 | * and handling incoming network messages. Once the server is set up, the |
| 28 | * program is basically driven by the network client. Call netEvent() to |
| 29 | * read the next message, then interpret the contents, process the message, |
| 30 | * and send a reply. Once that's done, go back to the start of the loop to |
| 31 | * handle the next request message. The program continues running until a |
| 32 | * suitable network event occurs to terminate it, such as the user typing |
| 33 | * QUIT into the UI. |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | * tads-net - the TADS Network function set |
| 39 | */ |
| 40 | intrinsic 'tads-net/030001' |
| 41 | { |
| 42 | /* |
| 43 | * Connect to the Web UI client. This connects the Web browser client |
| 44 | * to the game's HTTP server. 'server' is the HTTPServer object where |
| 45 | * the Web UI is found, and 'path' is the URL path of the game's main |
| 46 | * UI start page. This will tell the client to navigate to the given |
| 47 | * start path on the given server. |
| 48 | * |
| 49 | * Web-based games must call this after starting the HTTP server that |
| 50 | * provides the game's Web UI. This should be called as quickly as |
| 51 | * possible after starting up, because clients might time out and miss |
| 52 | * the connection data if it takes too long to get this back to them. |
| 53 | */ |
| 54 | connectWebUI(server, path); |
| 55 | |
| 56 | /* |
| 57 | * Read an event from the network message queue. When a listener |
| 58 | * receives a connection request from a client, it creates a network |
| 59 | * server thread to handle the connection; then when the server thread |
| 60 | * receives data from the client, it packages the data into an event |
| 61 | * message and places it in the queue. This function retrieves the |
| 62 | * next message from the queue. |
| 63 | * |
| 64 | * If 'timeout' is omitted or nil, the function waits indefinitely; it |
| 65 | * doesn't return until a network event occurs. If a timeout is given, |
| 66 | * it gives the maximum time in milliseconds that the function should |
| 67 | * wait for an event; if the timeout expires before any events arrive, |
| 68 | * the function returns a timeout event. |
| 69 | * |
| 70 | * The return value is a NetEvent instance describing the event. |
| 71 | */ |
| 72 | getNetEvent(timeout?); |
| 73 | |
| 74 | /* |
| 75 | * Get the local network host name. This is the name (or a name) that |
| 76 | * other computers can use to connect to this computer across the |
| 77 | * network. |
| 78 | * |
| 79 | * Some computers have multiple host names, since a single machine can |
| 80 | * have more than one network adapter. If this is the case, this |
| 81 | * function returns the default host name if there is such a concept, |
| 82 | * otherwise it just picks one of the names arbitrarily. |
| 83 | * |
| 84 | * If there's no host name at all, this returns nil. |
| 85 | * |
| 86 | * The host name returned here isn't usually a "global" name for the |
| 87 | * computer, but is a name that the local operating system recognizes |
| 88 | * as itself for networking purposes. In particular, this usually |
| 89 | * won't be a full internet "x.y.com" domain name. This name thus |
| 90 | * isn't something you can advertise to arbitrary other machines across |
| 91 | * the Internet and expect them to be able to connect. Instead, this |
| 92 | * name is primarily useful for telling the operating system which |
| 93 | * network adapter to use when opening a listening port. For example, |
| 94 | * you can use this name in the HTTPServer constructor. |
| 95 | */ |
| 96 | getHostName(); |
| 97 | |
| 98 | /* |
| 99 | * Get the local host's IP address. This is the IP address (or an IP |
| 100 | * address) that other computers can use to connect to this computer |
| 101 | * via the network. IP addresses are the basic addressing scheme of |
| 102 | * the Internet. |
| 103 | * |
| 104 | * Some computers have multiple IP addresses, since a single machine |
| 105 | * can have more than one network adapter. If this is the case, this |
| 106 | * function returns the default IP address if there is one, otherwise |
| 107 | * it selects one of the machine's IP addresses arbitrarily. |
| 108 | * |
| 109 | * If the IP address can't be retrieved (for example, because the |
| 110 | * machine has no network adapter installed), this returns nil. |
| 111 | */ |
| 112 | getLocalIP(); |
| 113 | |
| 114 | /* |
| 115 | * Get the URL to the storage server. When running in Web mode, the |
| 116 | * interpreter is generally configured to store files on a separate |
| 117 | * "storage server" rather than on the game server. This function |
| 118 | * retrieves the interpreter's configuration data and returns the |
| 119 | * storage server URL. |
| 120 | * |
| 121 | * 'resource' is a string giving the resource (page) name on the server |
| 122 | * that you wish to access. This can contain query parameters |
| 123 | * (introduced by a "?" symbol), if desired. The return value is the |
| 124 | * full URL to the requested page on the server. |
| 125 | */ |
| 126 | getNetStorageURL(resource); |
| 127 | |
| 128 | /* |
| 129 | * Get the host address that the user used to launch the game. For |
| 130 | * standard client/server TADS Web play, this is the network address |
| 131 | * that you should specify as the listening address when setting up the |
| 132 | * HTTPServer object. |
| 133 | * |
| 134 | * When we're operating in client/server mode, the interpreter is |
| 135 | * launched by an external Web server, in response to an incoming |
| 136 | * request from a client. In order to establish our own connection |
| 137 | * with the client, we need to know the address that the client used to |
| 138 | * connect to the external Web server in the first place. The Web |
| 139 | * server passes this address to the interpreter as part of the launch |
| 140 | * information, and the interpreter makes the address available to the |
| 141 | * TADS program here. |
| 142 | * |
| 143 | * If there's no launch address, this returns nil. This means that the |
| 144 | * user launched the interpreter directly, from the local desktop or |
| 145 | * command shell. In this case, it means that the user wants to run |
| 146 | * the game locally, rather than from a remote client machine. Simply |
| 147 | * use "localhost" as the networking binding address in this case. |
| 148 | */ |
| 149 | getLaunchHostAddr(); |
| 150 | |
| 151 | /* |
| 152 | * Send a network request to a remote server. This initiates |
| 153 | * processing the request and immediately returns; the process of |
| 154 | * setting up the network connection to the remote server, sending the |
| 155 | * request data, and receiving the reply proceeds asynchronously while |
| 156 | * the program continues running. When the request completes (or fails |
| 157 | * due to an error), a NetEvent of type NetEvReply is queued. |
| 158 | * |
| 159 | * 'id' is a user-defined identifier for the request. This can be any |
| 160 | * value, but is typically an object that you create to keep track of |
| 161 | * the request and process the reply. TADS doesn't use this value |
| 162 | * itself, but simply hangs onto it while the request is being |
| 163 | * processed, and then stores it in the NetEvent object generated when |
| 164 | * the request completes. This lets you relate the reply event back to |
| 165 | * the request, so that you know which request it applies to. |
| 166 | * |
| 167 | * 'url' is a string giving the URL of the resource. This starts with |
| 168 | * a protocol name that specifies which protocol to use. The possible |
| 169 | * protocols are: |
| 170 | * |
| 171 | * - HTTP: the URL has the form 'http://server:port/resource'. It can |
| 172 | * also start with 'https://' for a secure HTTP connection. The port |
| 173 | * number is optional; if omitted, the default port is 80 for regular |
| 174 | * HTTP, or 443 for HTTPS. |
| 175 | * |
| 176 | * Currently, the only protocol supported is HTTP. An error occurs if |
| 177 | * another protocol is specified. |
| 178 | * |
| 179 | * Additional parameters depend on the protocol. |
| 180 | * |
| 181 | * HTTP Additional Parameters: |
| 182 | * |
| 183 | * 'verb' - a string giving the HTTP verb for the request (GET, POST, |
| 184 | * HEAD, PUT, etc). |
| 185 | * |
| 186 | * 'options' - optional; a bitwise combination of NetReqXXX option |
| 187 | * flags specifying special settings. Omit this argument or pass 0 to |
| 188 | * use the default settings. |
| 189 | * |
| 190 | * 'headers' - optional; a string giving any custom headers to include |
| 191 | * with the request. The system automatically generates any required |
| 192 | * headers for the type of request, but you can add your own custom |
| 193 | * headers with this parameter. The headers must be specified using |
| 194 | * the standard 'name: value' format. Use '\r\n' to separate multiple |
| 195 | * headers. If you don't need to specify any custom headers, pass nil |
| 196 | * or simply omit this argument. |
| 197 | * |
| 198 | * 'body' - optional; a string or ByteArray giving the content body of |
| 199 | * the request, if any. This is suitable for verbs such as PUT and |
| 200 | * POST. For verbs that don't send any content with the request, pass |
| 201 | * nil or simply omit the argument. |
| 202 | * |
| 203 | * 'bodyType' - optional; a string giving the MIME type of the content |
| 204 | * body. If this is omitted, a default MIME type is assumed according |
| 205 | * to the type of 'body': for a string, "text/plain; charset=utf-8"; |
| 206 | * for a ByteArray, "appliation/octet-stream". |
| 207 | * |
| 208 | * This routine has no return value, since the request is processed |
| 209 | * asynchronously. The result can't be determined until the |
| 210 | * corresponding NetEvReqReply event occurs, at which point you can |
| 211 | * inspect that NetEvent object to find out if the request was |
| 212 | * successful, and if so retrieve the reply data. |
| 213 | */ |
| 214 | sendNetRequest(id, url, ...); |
| 215 | } |
| 216 | |
| 217 | /* ------------------------------------------------------------------------ */ |
| 218 | /* |
| 219 | * sendNetRequest option flags for HTTP requests. |
| 220 | */ |
| 221 | |
| 222 | /* |
| 223 | * DO NOT follow "redirect" (301) results. By default (i.e., without this |
| 224 | * option flag), if the server sends back a 301 HTTP status code |
| 225 | * ("permanently moved"), we'll automatically follow the link to the new |
| 226 | * location and return the result from that new request. This is |
| 227 | * transparent to the caller; the caller just sees the final result from |
| 228 | * the final server we're redirected to. If this flag is specified, we |
| 229 | * DON'T follow a redirection, but instead simply stop and return the 301 |
| 230 | * result. The caller can inspect the reply headers to get the redirection |
| 231 | * link. |
| 232 | */ |
| 233 | #define NetReqNoRedirect 0x0001 |
| 234 | |
| 235 | |
| 236 | /* ------------------------------------------------------------------------ */ |
| 237 | /* |
| 238 | * Network event types. This value is used in the evType property of |
| 239 | * NetEvent objects to indicate the type of the event. |
| 240 | */ |
| 241 | |
| 242 | /* |
| 243 | * Request event. This type of event contains a request from the network |
| 244 | * client. For example, for an HTTP client, the event contains an HTTP |
| 245 | * request, such as a GET. The request is represented as an object; the |
| 246 | * class depends on the type of server and request. |
| 247 | */ |
| 248 | #define NetEvRequest 1 |
| 249 | |
| 250 | /* |
| 251 | * Timeout. netEvent() returns this type of event when the interval |
| 252 | * expires before any actual network events occur. |
| 253 | */ |
| 254 | #define NetEvTimeout 2 |
| 255 | |
| 256 | /* |
| 257 | * Debugger interrupt. netEvent() returns this type of event when the user |
| 258 | * commands the debugger to interrupt the program and take control. If the |
| 259 | * program is waiting for a network event, this causes the netEvent() call |
| 260 | * to return with this type of event. |
| 261 | * |
| 262 | * In a normal event-loop type of program, you can simply ignore this event |
| 263 | * and loop back for a new event immediately. The purpose of this event |
| 264 | * type is to force netEvent() to stop waiting and return to the byte-code |
| 265 | * caller, so that the debugger can pause execution at a valid byte-code |
| 266 | * program location. Once that's accomplished, there's nothing more to do |
| 267 | * with this event type; simply discard it and get the next event when the |
| 268 | * user resumes execution. |
| 269 | */ |
| 270 | #define NetEvDebugBreak 3 |
| 271 | |
| 272 | /* |
| 273 | * UI Closed. This type of event occurs when the user manually closes the |
| 274 | * Web UI, ONLY in a stand-alone local configuration. This is the special |
| 275 | * configuration where the browser UI is integrated into the interpreter |
| 276 | * application, simulating the traditional TADS interpreter environment |
| 277 | * with the Web UI, and everything's running on a single machine. When the |
| 278 | * user closes the Web UI in this environment, the game should usually just |
| 279 | * terminate, since the user has effectively dismissed the application. |
| 280 | * |
| 281 | * In the true client/server configuration, where the user is running an |
| 282 | * ordinary Web browser on a separate machine, we have no way of knowing |
| 283 | * that the user has closed the browser window. The best we can detect is |
| 284 | * a dropped network connection, but we can't assume this is due to the |
| 285 | * window closing, because it could happen for other reasons (such as a |
| 286 | * temporary network interruption). In the full client/server |
| 287 | * configuration, we have to be more subtle in determining that the user |
| 288 | * intends to quit the application, generally with inactivity timers. |
| 289 | */ |
| 290 | #define NetEvUIClose 4 |
| 291 | |
| 292 | /* |
| 293 | * Network reply. This type of event occurs when a network request |
| 294 | * initiated by sendNetRequest() completes, or fails with an error. The |
| 295 | * event object contains the status of the request and, if successful, the |
| 296 | * result information sent back from the server. |
| 297 | */ |
| 298 | #define NetEvReply 5 |
| 299 | |
| 300 | /* |
| 301 | * Reply data transfer finished. This type of event occurs when an |
| 302 | * asynchronous reply to a request is completed. An asynchronous HTTP |
| 303 | * request reply can be sent with HTTPRequest.sendReplyAsync(). The event |
| 304 | * object contains information on whether or not the reply was successfully |
| 305 | * sent to the client. For HTTP transactions, this is largely advisory, |
| 306 | * since there's no recovery action the server can take when an error |
| 307 | * occurs sending a reply; there's no provision in HTTP for a server to |
| 308 | * initiate contact with a client, so it's up to the client to handle it, |
| 309 | * such as by retrying the request. |
| 310 | */ |
| 311 | #define NetEvReplyDone 6 |
| 312 |