diff --git a/direct/src/directd/directd.cxx b/direct/src/directd/directd.cxx index 5be663db9d..9203ec5a0b 100644 --- a/direct/src/directd/directd.cxx +++ b/direct/src/directd/directd.cxx @@ -17,15 +17,15 @@ // //////////////////////////////////////////////////////////////////// -/*#include "directd.h" -#include "pandaFramework.h" +#include "directd.h" +/*#include "pandaFramework.h" #include "queuedConnectionManager.h"*/ -#include -#include -#include "pandabase.h" +//#include +//#include +//#include "pandabase.h" -#include "queuedConnectionManager.h" +//#include "queuedConnectionManager.h" #include "queuedConnectionListener.h" #include "queuedConnectionReader.h" #include "connectionWriter.h" @@ -46,7 +46,8 @@ namespace { #define TA_SUCCESS_KILL 2 #define TA_SUCCESS_16 3 - BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam) { + BOOL CALLBACK + TerminateAppEnum(HWND hwnd, LPARAM lParam) { DWORD dwID; GetWindowThreadProcessId(hwnd, &dwID); if(dwID == (DWORD)lParam) { @@ -59,7 +60,7 @@ namespace { DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout) Purpose: - Shut down a 32-Bit Process (or 16-bit process under Windows 95) + Shut down a 32-Bit Process Parameters: dwPID @@ -73,7 +74,6 @@ namespace { TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE. TA_SUCCESS_KILL - if the process was shut down with TerminateProcess(). - NOTE: See header for these defines. */ DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout) { @@ -89,7 +89,7 @@ namespace { // TerminateAppEnum() posts WM_CLOSE to all windows whose PID // matches your process's. - EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID); + EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)dwPID); // Wait on the handle. If it signals, great. If it times out, // then you kill it. @@ -103,40 +103,32 @@ namespace { return dwRet; } + /* + returns process id. + */ + DWORD + StartApp(const string& cmd) { + DWORD pid=0; + STARTUPINFO si; + PROCESS_INFORMATION pi; + ZeroMemory(&si, sizeof(STARTUPINFO)); + si.cb = sizeof(STARTUPINFO); + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); + if (CreateProcess(NULL, (char*)cmd.c_str(), + 0, 0, 1, NORMAL_PRIORITY_CLASS, + 0, 0, &si, &pi)) { + pid=pi.dwProcessId; + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + } else { + cerr<<"CreateProcess failed: "<get_address() << "\n"; + if (_verbose) { + nout << "Lost connection from " + << connection->get_address() << "\n"; + } clients.erase(connection); _cm.close_connection(connection); } @@ -274,10 +232,20 @@ DirectD::run_server() { while (reader.data_available()) { NetDatagram datagram; if (reader.get_data(datagram)) { - nout << "Got datagram " /*<< datagram <<*/ "from " - << datagram.get_address() << ", sending to " - << clients.size() << " clients.\n"; - if (_verbose) datagram.dump_hex(nout); + if (_verbose) { + nout << "Got datagram " /*<< datagram <<*/ "from " + << datagram.get_address() << ", sending to " + << clients.size() << " clients.\n"; + if (_verbose) datagram.dump_hex(nout); + } + DatagramIterator di(datagram); + string cmd=di.get_string(); + if (_verbose) { + cerr<<"server: "<get_address() << "\n"; - clients.erase(connection); - _cm.close_connection(connection); - nout << "Closed " << connection << "\n"; - } - */ - - // No, an empty datagram means to shut down the server. + // An empty datagram, shut down the server: shutdown = true; } } @@ -348,6 +302,11 @@ DirectD::run_client() { datagram.add_string(d); // Send the datagram. writer.send(datagram, c); + if (d[0] == 'q' && d.size()==1) { + // ...user entered quit command. + _cm.close_connection(c); + return; + } } // Check for a lost connection. diff --git a/direct/src/directd/directd.h b/direct/src/directd/directd.h index 929400646c..8a29d6508b 100644 --- a/direct/src/directd/directd.h +++ b/direct/src/directd/directd.h @@ -1 +1,54 @@ -// directd.h +// Filename: directd.h +// Created by: skyler 2002.04.08 +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + + +#include +#include +#include "pandabase.h" +#include "queuedConnectionManager.h" + + +class DirectD { +public: + DirectD(); + ~DirectD(); + + void set_host_name(const string& host_name); + void set_port(int port); + void spawn_background_server(); + + void run_server(); + void run_client(); + void run_controller(); + + void send_start_app(const string& cmd); + void start_app(const string& cmd); + + void send_kill_app(const string& pid); + void kill_app(); + +protected: + QueuedConnectionManager _cm; + string _host_name; + int _port; + intptr_t _app_pid; + bool _controller; + bool _verbose; +}; + +