| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #include <Atomic/Core/CoreEvents.h>//
- #include <Atomic/IO/IOEvents.h>
- #include <Atomic/Input/InputEvents.h>
- #include <Atomic/Graphics/Graphics.h>
- #include <Atomic/Graphics/GraphicsEvents.h>
- #include <Atomic/IPC/IPCEvents.h>
- #include <AtomicJS/Javascript/JSIPCEvents.h>
- #include "IPCPlayerAppEvents.h"
- #include <Atomic/Engine/Engine.h>
- #include <Atomic/IPC/IPC.h>
- #include <AtomicJS/Javascript/Javascript.h>
- #include <Atomic/UI/SystemUI/DebugHud.h>
- #include "IPCPlayerApp.h"
- #include <stdio.h>
- namespace Atomic
- {
- IPCPlayerApp *arobj = NULL;
- void do_reconnect() // hack to call object method
- {
- if (arobj)
- arobj->Reconnect();
- }
- #ifdef ATOMIC_PLATFORM_WINDOWS
- /*
- * Example debug transport using a Windows TCP socket
- *
- * Provides a TCP server socket which a debug client can connect to.
- * After that data is just passed through.
- *
- * https://msdn.microsoft.com/en-us/library/windows/desktop/ms737593(v=vs.85).aspx
- *
- * Compiling 'duk' with debugger support using MSVC (Visual Studio):
- *
- * > python2 tools\configure.py \
- * --output-directory prep
- * -DDUK_USE_DEBUGGER_SUPPORT -DDUK_USE_INTERRUPT_COUNTER
- * > cl /W3 /O2 /Feduk.exe \
- * /DDUK_CMDLINE_DEBUGGER_SUPPORT
- * /Iexamples\debug-trans-socket /Iprep
- * examples\cmdline\duk_cmdline.c
- * examples\debug-trans-socket\duk_trans_socket_windows.c
- * prep\duktape.c
- *
- * With MinGW:
- *
- * $ python2 tools\configure.py \
- * --output-directory prep
- * -DDUK_USE_DEBUGGER_SUPPORT -DDUK_USE_INTERRUPT_COUNTER
- * $ gcc -oduk.exe -Wall -O2 \
- * -DDUK_CMDLINE_DEBUGGER_SUPPORT \
- * -Iexamples/debug-trans-socket -Iprep \
- * examples/cmdline/duk_cmdline.c \
- * examples/debug-trans-socket/duk_trans_socket_windows.c \
- * prep/duktape.c -lm -lws2_32
- */
- #undef UNICODE
- #if !defined(WIN32_LEAN_AND_MEAN)
- #define WIN32_LEAN_AND_MEAN
- #endif
- /* MinGW workaround for missing getaddrinfo() etc:
- * http://programmingrants.blogspot.fi/2009/09/tips-on-undefined-reference-to.html
- */
- #if defined(__MINGW32__) || defined(__MINGW64__)
- #if !defined(_WIN32_WINNT)
- #define _WIN32_WINNT 0x0501
- #endif
- #endif
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdio.h>
- #include <string.h>
- #include "duktape.h"
- #if defined(_MSC_VER)
- #pragma comment (lib, "Ws2_32.lib")
- #endif
- #if !defined(DUK_DEBUG_PORT)
- #define DUK_DEBUG_PORT 9091
- #endif
- #if !defined(DUK_DEBUG_ADDRESS)
- #define DUK_DEBUG_ADDRESS "0.0.0.0"
- #endif
- #define DUK__STRINGIFY_HELPER(x) #x
- #define DUK__STRINGIFY(x) DUK__STRINGIFY_HELPER(x)
- #if 0
- #define DEBUG_PRINTS
- #endif
- static SOCKET server_sock = INVALID_SOCKET;
- static SOCKET client_sock = INVALID_SOCKET;
- static int wsa_inited = 0;
- /*
- * Transport init and finish
- */
- void duk_trans_socket_init(void) {
- WSADATA wsa_data;
- struct addrinfo hints;
- struct addrinfo *result = NULL;
- int rc;
- memset((void *) &wsa_data, 0, sizeof(wsa_data));
- memset((void *) &hints, 0, sizeof(hints));
- rc = WSAStartup(MAKEWORD(2, 2), &wsa_data);
- if (rc != 0) {
- fprintf(stderr, "%s: WSAStartup() failed: %d\n", __FILE__, rc);
- fflush(stderr);
- goto fail;
- }
- wsa_inited = 1;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- rc = getaddrinfo(DUK_DEBUG_ADDRESS, DUK__STRINGIFY(DUK_DEBUG_PORT), &hints, &result);
- if (rc != 0) {
- fprintf(stderr, "%s: getaddrinfo() failed: %d\n", __FILE__, rc);
- fflush(stderr);
- goto fail;
- }
- server_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (server_sock == INVALID_SOCKET) {
- fprintf(stderr, "%s: socket() failed with error: %ld\n",
- __FILE__, (long) WSAGetLastError());
- fflush(stderr);
- goto fail;
- }
- rc = bind(server_sock, result->ai_addr, (int) result->ai_addrlen);
- if (rc == SOCKET_ERROR) {
- fprintf(stderr, "%s: bind() failed with error: %ld\n",
- __FILE__, (long) WSAGetLastError());
- fflush(stderr);
- goto fail;
- }
- rc = listen(server_sock, SOMAXCONN);
- if (rc == SOCKET_ERROR) {
- fprintf(stderr, "%s: listen() failed with error: %ld\n",
- __FILE__, (long) WSAGetLastError());
- fflush(stderr);
- goto fail;
- }
- if (result != NULL) {
- freeaddrinfo(result);
- result = NULL;
- }
- return;
- fail:
- if (result != NULL) {
- freeaddrinfo(result);
- result = NULL;
- }
- if (server_sock != INVALID_SOCKET) {
- (void) closesocket(server_sock);
- server_sock = INVALID_SOCKET;
- }
- if (wsa_inited) {
- WSACleanup();
- wsa_inited = 0;
- }
- }
- void duk_trans_socket_finish(void) {
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(client_sock);
- client_sock = INVALID_SOCKET;
- }
- if (server_sock != INVALID_SOCKET) {
- (void) closesocket(server_sock);
- server_sock = INVALID_SOCKET;
- }
- if (wsa_inited) {
- WSACleanup();
- wsa_inited = 0;
- }
- // auto-restart
- do_reconnect();
- }
- void duk_trans_socket_waitconn(void) {
- if (server_sock == INVALID_SOCKET) {
- fprintf(stderr, "%s: no server socket, skip waiting for connection\n",
- __FILE__);
- fflush(stderr);
- return;
- }
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(client_sock);
- client_sock = INVALID_SOCKET;
- }
- fprintf(stderr, "Waiting for debug connection on port %d\n", (int) DUK_DEBUG_PORT);
- fflush(stderr);
- client_sock = accept(server_sock, NULL, NULL);
- if (client_sock == INVALID_SOCKET) {
- fprintf(stderr, "%s: accept() failed with error %ld, skip waiting for connection\n",
- __FILE__, (long) WSAGetLastError());
- fflush(stderr);
- goto fail;
- }
- fprintf(stderr, "Debug connection established\n");
- fflush(stderr);
- /* XXX: For now, close the listen socket because we won't accept new
- * connections anyway. A better implementation would allow multiple
- * debug attaches.
- */
- if (server_sock != INVALID_SOCKET) {
- (void) closesocket(server_sock);
- server_sock = INVALID_SOCKET;
- }
- return;
- fail:
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(client_sock);
- client_sock = INVALID_SOCKET;
- }
- }
- /*
- * Duktape callbacks
- */
- /* Duktape debug transport callback: (possibly partial) read. */
- duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) {
- int ret;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
- __FUNCTION__, (void *) udata, (void *) buffer, (long) length);
- fflush(stderr);
- #endif
- if (client_sock == INVALID_SOCKET) {
- return 0;
- }
- if (length == 0) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: read request length == 0, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- if (buffer == NULL) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: read request buffer == NULL, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- /* In a production quality implementation there would be a sanity
- * timeout here to recover from "black hole" disconnects.
- */
- ret = recv(client_sock, (void *) buffer, (int) length, 0);
- if (ret < 0) {
- fprintf(stderr, "%s: debug read failed, error %d, closing connection\n",
- __FILE__, ret);
- fflush(stderr);
- goto fail;
- } else if (ret == 0) {
- fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- } else if (ret > (int) length) {
- fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n",
- __FILE__, (long) ret, (long) length);
- fflush(stderr);
- goto fail;
- }
- return (duk_size_t) ret;
- fail:
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(client_sock);
- client_sock = INVALID_SOCKET;
- }
- return 0;
- }
- /* Duktape debug transport callback: (possibly partial) write. */
- duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length) {
- int ret;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
- __FUNCTION__, (void *) udata, (const void *) buffer, (long) length);
- fflush(stderr);
- #endif
- if (client_sock == INVALID_SOCKET) {
- return 0;
- }
- if (length == 0) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: write request length == 0, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- if (buffer == NULL) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: write request buffer == NULL, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- /* In a production quality implementation there would be a sanity
- * timeout here to recover from "black hole" disconnects.
- */
- ret = send(client_sock, (const void *) buffer, (int) length, 0);
- if (ret <= 0 || ret > (int) length) {
- fprintf(stderr, "%s: debug write failed, ret %d, closing connection\n",
- __FILE__, ret);
- fflush(stderr);
- goto fail;
- }
- return (duk_size_t) ret;
- fail:
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(INVALID_SOCKET);
- client_sock = INVALID_SOCKET;
- }
- return 0;
- }
- duk_size_t duk_trans_socket_peek_cb(void *udata) {
- u_long avail;
- int rc;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata);
- fflush(stderr);
- #endif
- if (client_sock == INVALID_SOCKET) {
- return 0;
- }
- avail = 0;
- rc = ioctlsocket(client_sock, FIONREAD, &avail);
- if (rc != 0) {
- fprintf(stderr, "%s: ioctlsocket() returned %d, closing connection\n",
- __FILE__, rc);
- fflush(stderr);
- goto fail; /* also returns 0, which is correct */
- } else {
- if (avail == 0) {
- return 0; /* nothing to read */
- } else {
- return 1; /* something to read */
- }
- }
- /* never here */
- fail:
- if (client_sock != INVALID_SOCKET) {
- (void) closesocket(client_sock);
- client_sock = INVALID_SOCKET;
- }
- return 0;
- }
- void duk_trans_socket_read_flush_cb(void *udata) {
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata);
- fflush(stderr);
- #endif
- /* Read flush: Duktape may not be making any more read calls at this
- * time. If the transport maintains a receive window, it can use a
- * read flush as a signal to update the window status to the remote
- * peer. A read flush is guaranteed to occur before Duktape stops
- * reading for a while; it may occur in other situations as well so
- * it's not a 100% reliable indication.
- */
- /* This TCP transport requires no read flush handling so ignore.
- * You can also pass a NULL to duk_debugger_attach() and not
- * implement this callback at all.
- */
- }
- void duk_trans_socket_write_flush_cb(void *udata) {
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata);
- fflush(stderr);
- #endif
- /* Write flush. If the transport combines multiple writes
- * before actually sending, a write flush is an indication
- * to write out any pending bytes: Duktape may not be doing
- * any more writes on this occasion.
- */
- /* This TCP transport requires no write flush handling so ignore.
- * You can also pass a NULL to duk_debugger_attach() and not
- * implement this callback at all.
- */
- return;
- }
- #undef DUK__STRINGIFY_HELPER
- #undef DUK__STRINGIFY
- #else // OSX ad LINUX
- /*
- * Example debug transport using a Linux/Unix TCP socket
- *
- * Provides a TCP server socket which a debug client can connect to.
- * After that data is just passed through.
- */
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <poll.h>
- #include <errno.h>
- #include <ThirdParty/Duktape/duktape.h>
- #if !defined(DUK_DEBUG_PORT)
- #define DUK_DEBUG_PORT 9091
- #endif
- #if 0
- #define DEBUG_PRINTS
- #endif
- static int server_sock = -1;
- static int client_sock = -1;
- /*
- * Transport init and finish
- */
- void duk_trans_socket_init(void) {
- struct sockaddr_in addr;
- int on;
- server_sock = socket(AF_INET, SOCK_STREAM, 0);
- if (server_sock < 0) {
- fprintf(stderr, "%s: failed to create server socket: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- }
- on = 1;
- if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on)) < 0) {
- fprintf(stderr, "%s: failed to set SO_REUSEADDR for server socket: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- }
- memset((void *) &addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = INADDR_ANY;
- addr.sin_port = htons(DUK_DEBUG_PORT);
- if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- fprintf(stderr, "%s: failed to bind server socket: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- }
- listen(server_sock, 1 /*backlog*/);
- return;
- fail:
- if (server_sock >= 0) {
- (void) close(server_sock);
- server_sock = -1;
- }
- }
- void duk_trans_socket_waitconn(void) {
- struct sockaddr_in addr;
- socklen_t sz;
- if (server_sock < 0) {
- fprintf(stderr, "%s: no server socket, skip waiting for connection\n",
- __FILE__);
- fflush(stderr);
- return;
- }
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- fprintf(stderr, "Waiting for debug connection on port %d\n", (int) DUK_DEBUG_PORT);
- fflush(stderr);
- sz = (socklen_t) sizeof(addr);
- client_sock = accept(server_sock, (struct sockaddr *) &addr, &sz);
- if (client_sock < 0) {
- fprintf(stderr, "%s: accept() failed, skip waiting for connection: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- }
- fprintf(stderr, "Debug connection established\n");
- fflush(stderr);
- /* XXX: For now, close the listen socket because we won't accept new
- * connections anyway. A better implementation would allow multiple
- * debug attaches.
- */
- if (server_sock >= 0) {
- (void) close(server_sock);
- server_sock = -1;
- }
- return;
- fail:
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- }
- void duk_trans_socket_finish(void) {
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- if (server_sock >= 0) {
- (void) close(server_sock);
- server_sock = -1;
- }
- //
- // auto-restart
- do_reconnect();
- }
- /*
- * Duktape callbacks
- */
- /* Duktape debug transport callback: (possibly partial) read. */
- duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) {
- ssize_t ret;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
- __func__, (void *) udata, (void *) buffer, (long) length);
- fflush(stderr);
- #endif
- if (client_sock < 0) {
- return 0;
- }
- if (length == 0) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: read request length == 0, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- if (buffer == NULL) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: read request buffer == NULL, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- /* In a production quality implementation there would be a sanity
- * timeout here to recover from "black hole" disconnects.
- */
- ret = read(client_sock, (void *) buffer, (size_t) length);
- if (ret < 0) {
- fprintf(stderr, "%s: debug read failed, closing connection: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- } else if (ret == 0) {
- fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- } else if (ret > (ssize_t) length) {
- fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n",
- __FILE__, (long) ret, (long) length);
- fflush(stderr);
- goto fail;
- }
- return (duk_size_t) ret;
- fail:
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- return 0;
- }
- /* Duktape debug transport callback: (possibly partial) write. */
- duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length) {
- ssize_t ret;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
- __func__, (void *) udata, (const void *) buffer, (long) length);
- fflush(stderr);
- #endif
- if (client_sock < 0) {
- return 0;
- }
- if (length == 0) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: write request length == 0, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- if (buffer == NULL) {
- /* This shouldn't happen. */
- fprintf(stderr, "%s: write request buffer == NULL, closing connection\n",
- __FILE__);
- fflush(stderr);
- goto fail;
- }
- /* In a production quality implementation there would be a sanity
- * timeout here to recover from "black hole" disconnects.
- */
- ret = write(client_sock, (const void *) buffer, (size_t) length);
- if (ret <= 0 || ret > (ssize_t) length) {
- fprintf(stderr, "%s: debug write failed, closing connection: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail;
- }
- return (duk_size_t) ret;
- fail:
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- return 0;
- }
- duk_size_t duk_trans_socket_peek_cb(void *udata) {
- struct pollfd fds[1];
- int poll_rc;
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
- fflush(stderr);
- #endif
- if (client_sock < 0) {
- return 0;
- }
- fds[0].fd = client_sock;
- fds[0].events = POLLIN;
- fds[0].revents = 0;
- poll_rc = poll(fds, 1, 0);
- if (poll_rc < 0) {
- fprintf(stderr, "%s: poll returned < 0, closing connection: %s\n",
- __FILE__, strerror(errno));
- fflush(stderr);
- goto fail; /* also returns 0, which is correct */
- } else if (poll_rc > 1) {
- fprintf(stderr, "%s: poll returned > 1, treating like 1\n",
- __FILE__);
- fflush(stderr);
- return 1; /* should never happen */
- } else if (poll_rc == 0) {
- return 0; /* nothing to read */
- } else {
- return 1; /* something to read */
- }
- fail:
- if (client_sock >= 0) {
- (void) close(client_sock);
- client_sock = -1;
- }
- return 0;
- }
- void duk_trans_socket_read_flush_cb(void *udata) {
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
- fflush(stderr);
- #endif
- /* Read flush: Duktape may not be making any more read calls at this
- * time. If the transport maintains a receive window, it can use a
- * read flush as a signal to update the window status to the remote
- * peer. A read flush is guaranteed to occur before Duktape stops
- * reading for a while; it may occur in other situations as well so
- * it's not a 100% reliable indication.
- */
- /* This TCP transport requires no read flush handling so ignore.
- * You can also pass a NULL to duk_debugger_attach() and not
- * implement this callback at all.
- */
- }
- void duk_trans_socket_write_flush_cb(void *udata) {
- (void) udata; /* not needed by the example */
- #if defined(DEBUG_PRINTS)
- fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
- fflush(stderr);
- #endif
- /* Write flush. If the transport combines multiple writes
- * before actually sending, a write flush is an indication
- * to write out any pending bytes: Duktape may not be doing
- * any more writes on this occasion.
- */
- /* This TCP transport requires no write flush handling so ignore.
- * You can also pass a NULL to duk_debugger_attach() and not
- * implement this callback at all.
- */
- return;
- }
- #endif // OSX ad LINUX
- void debugger_detached (duk_context* ctx, void *udata) {
- fflush(stderr);
- /* Ensure socket is closed even when detach is initiated by Duktape
- * rather than debug client.
- */
- duk_trans_socket_finish();
- }
- // hack around oo programming, so a function can call a class method.
- void set_autoreconn ( IPCPlayerApp *someptr )
- {
- arobj = someptr;
- }
- IPCPlayerApp::IPCPlayerApp(Context* context) :
- PlayerApp(context),
- subprocess_(false),
- debugPlayer_(false),
- brokerActive_(false)
- {
- fd_[0] = INVALID_IPCHANDLE_VALUE;
- fd_[1] = INVALID_IPCHANDLE_VALUE;
- }
- IPCPlayerApp::~IPCPlayerApp()
- {
- }
- void IPCPlayerApp::Setup()
- {
- PlayerApp::Setup();
- // This should be configurable
- engineParameters_.InsertNew("LogLevel", LOG_DEBUG);
- ipc_ = new IPC(context_);
- context_->RegisterSubsystem(ipc_);
- }
- void IPCPlayerApp::ProcessArguments()
- {
- PlayerApp::ProcessArguments();
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if (!fileSystem)
- {
- ErrorExit("IPCPlayerApp::ProcessArguments FileSystem subsystem does not exist");
- }
- String resourcePrefix;
- engineParameters_["ResourcePrefixPaths"] = "";
- for (unsigned i = 0; i < arguments_.Size(); ++i)
- {
- if (arguments_[i].Length() > 1)
- {
- String argument = arguments_[i].ToLower();
- String value = i + 1 < arguments_.Size() ? arguments_[i + 1] : String::EMPTY;
- if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
- {
- subprocess_ = true;
- }
- else if (argument == "--debug")
- {
- debugPlayer_ = true;
- }
- else if (argument == "--resourceprefix" && value.Length())
- {
- resourcePrefix = value;
- engineParameters_["ResourcePrefixPaths"] = resourcePrefix;
- }
- else if (argument == "--project" && value.Length())
- {
- value = AddTrailingSlash(value);
- AddEngineConfigSearchPath(value + "Settings/");
- // check that cache exists
- if (!fileSystem->DirExists(value + "Cache"))
- {
- ErrorExit("Project cache folder does not exist, projects must be loaded into the Atomic Editor at least once before using the --player command line mode");
- return;
- }
- #ifdef ATOMIC_DEV_BUILD
- String resourcePaths = ToString("%s/Resources/CoreData;%s/Resources/PlayerData;%sResources;%s;%sCache",
- ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR, value.CString(), value.CString(), value.CString());
- #else
- #ifdef __APPLE__
- if (!resourcePrefix.Length())
- {
- engineParameters_["ResourcePrefixPaths"] = fileSystem->GetProgramDir() + "../Resources";
- }
- #else
- if (!resourcePrefix.Length())
- {
- engineParameters_["ResourcePrefixPaths"] = fileSystem->GetProgramDir() + "Resources";
- }
- #endif
- String resourcePaths = ToString("CoreData;PlayerData;%s/;%s/Resources;%s;%sCache",
- value.CString(), value.CString(), value.CString(), value.CString());
- #endif
- ATOMIC_LOGINFOF("Adding ResourcePaths: %s", resourcePaths.CString());
- engineParameters_["ResourcePaths"] = resourcePaths;
- }
- }
- }
- // IPC client player should not auto exit if a subprocess
- if (subprocess_)
- engine_->SetAutoExit(false);
- }
- void IPCPlayerApp::Start()
- {
- if (subprocess_)
- {
- // do not execute main in the player app
- executeJSMain_ = false;
- }
- PlayerApp::Start();
- int id = -1;
- if (IPC::ProcessArguments(arguments_, id, fd_[0], fd_[1]))
- {
- SubscribeToEvent(E_IPCINITIALIZE, ATOMIC_HANDLER(IPCPlayerApp, HandleIPCInitialize));
- SubscribeToEvent(E_LOGMESSAGE, ATOMIC_HANDLER(IPCPlayerApp, HandleLogMessage));
- SubscribeToEvent(E_JSERROR, ATOMIC_HANDLER(IPCPlayerApp, HandleJSError));
- SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(IPCPlayerApp, HandleExitRequest));
- SubscribeToEvent(E_SCREENMODE, ATOMIC_HANDLER(IPCPlayerApp, HandlePlayerWindowChanged));
- SubscribeToEvent(E_WINDOWPOS, ATOMIC_HANDLER(IPCPlayerApp, HandlePlayerWindowChanged));
- SubscribeToEvent(E_UPDATESPAUSEDRESUMED, ATOMIC_HANDLER(IPCPlayerApp, HandleUpdatesPausedResumed));
- if (ipc_->InitWorker((unsigned)id, fd_[0], fd_[1]))
- {
- brokerActive_ = true;
- }
- else if (subprocess_)
- {
- ATOMIC_LOGERROR("IPCPlayerApp::Start() - Unable to initialize IPC Worker");
- }
- }
- if (subprocess_)
- {
- JSVM* vm = JSVM::GetJSVM(0);
- if (!vm->ExecuteMain())
- {
- SendEvent(E_EXITREQUESTED);
- }
- SubscribeToEvent(E_PLAYERQUIT, ATOMIC_HANDLER(IPCPlayerApp, HandleQuit));
- set_autoreconn (this); // for auto-reconnect
- }
- GetSubsystem<Graphics>()->RaiseWindow();
- if (debugPlayer_)
- {
- ATOMIC_LOGERROR("Starting Debugger");
- set_autoreconn (this); // for auto-reconnect
- Reconnect();
- }
- }
- void IPCPlayerApp::Reconnect()
- {
- duk_trans_socket_init();
- duk_trans_socket_waitconn();
- JSVM* vm = JSVM::GetJSVM(0);
- if(!vm) return;
- duk_context *ctx_ = vm->GetJSContext();
- if(!ctx_) return;
- duk_debugger_attach(ctx_,
- duk_trans_socket_read_cb,
- duk_trans_socket_write_cb,
- duk_trans_socket_peek_cb,
- duk_trans_socket_read_flush_cb,
- duk_trans_socket_write_flush_cb,
- NULL, // duk_debug_request_function - no custom application specific request functions
- debugger_detached,
- NULL);
- }
- void IPCPlayerApp::HandleQuit(StringHash eventType, VariantMap& eventData)
- {
- engine_->Exit();
- }
- void IPCPlayerApp::Stop()
- {
- PlayerApp::Stop();
- }
- void IPCPlayerApp::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
- {
- brokerActive_ = true;
- // If the parent application has a profile mode up, sync
- SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
- if (debugHud)
- {
- unsigned mode = eventData["debugHudMode"].GetUInt();
- // Only set if we haven't set the mode in player code
- if (mode && !debugHud->GetMode())
- {
- debugHud->SetMode(mode);
- debugHud->SetProfilerMode((DebugHudProfileMode)eventData["debugHudProfilerMode"].GetUInt());
- }
- }
- }
- void IPCPlayerApp::HandleJSError(StringHash eventType, VariantMap& eventData)
- {
- if (brokerActive_)
- {
- if (ipc_.Null())
- return;
- String errName = eventData[JSError::P_ERRORNAME].GetString();
- String errStack = eventData[JSError::P_ERRORSTACK].GetString();
- String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
- String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
- int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
- VariantMap ipcErrorData;
- ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
- ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
- ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
- ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
- ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
- ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
- ATOMIC_LOGERROR("SENDING E_IPCJSERROR");
- }
- }
- void IPCPlayerApp::HandlePlayerWindowChanged(StringHash eventType, VariantMap& eventData)
- {
- Graphics* graphics = GetSubsystem<Graphics>();
- using namespace IPCPlayerWindowChanged;
- VariantMap data;
- data[P_POSX] = graphics->GetWindowPosition().x_;
- data[P_POSY] = graphics->GetWindowPosition().y_;
- data[P_WIDTH] = graphics->GetWidth();
- data[P_HEIGHT] = graphics->GetHeight();
- data[P_MONITOR] = graphics->GetCurrentMonitor();
- data[P_MAXIMIZED] = graphics->GetMaximized();
- ipc_->SendEventToBroker(E_IPCPLAYERWINDOWCHANGED, data);
- }
- void IPCPlayerApp::HandleUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
- {
- ipc_->SendEventToBroker(E_IPCPLAYERUPDATESPAUSEDRESUMED, eventData);
- }
- void IPCPlayerApp::HandleExitRequest(StringHash eventType, VariantMap& eventData)
- {
- // we are exiting, so let's clear out the debugger object
- arobj = NULL;
- UnsubscribeFromEvent(E_LOGMESSAGE);
- SendEvent(E_PLAYERQUIT);
- }
- void IPCPlayerApp::HandleLogMessage(StringHash eventType, VariantMap& eventData)
- {
- using namespace LogMessage;
- if (brokerActive_)
- {
- if (ipc_.Null())
- return;
- VariantMap logEvent;
- logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
- logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
- ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
- }
- }
- }
|