Panda3D
winStats.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file winStats.cxx
10  * @author drose
11  * @date 2003-12-02
12  */
13 
14 #include "pandatoolbase.h"
15 
16 #include "winStatsServer.h"
17 #include "config_pstatclient.h"
18 
19 #ifndef WIN32_LEAN_AND_MEAN
20 #define WIN32_LEAN_AND_MEAN 1
21 #endif
22 #include <windows.h>
23 
24 static const char *toplevel_class_name = "pstats";
25 static WinStatsServer *server = nullptr;
26 
27 /**
28  *
29  */
30 static LONG WINAPI
31 toplevel_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
32  switch (msg) {
33  case WM_TIMER:
34  server->poll();
35  break;
36 
37  case WM_DESTROY:
38  PostQuitMessage(0);
39  break;
40 
41  default:
42  break;
43  }
44 
45  return DefWindowProc(hwnd, msg, wparam, lparam);
46 }
47 
48 
49 /**
50  * Creates the initial, toplevel window for the application.
51  */
52 static HWND
53 create_toplevel_window(HINSTANCE application) {
54  WNDCLASS wc;
55 
56  ZeroMemory(&wc, sizeof(WNDCLASS));
57  wc.lpfnWndProc = (WNDPROC)toplevel_window_proc;
58  wc.hInstance = application;
59  wc.lpszClassName = toplevel_class_name;
60 
61  if (!RegisterClass(&wc)) {
62  nout << "Could not register window class!\n";
63  exit(1);
64  }
65 
66  DWORD window_style = WS_POPUP | WS_SYSMENU | WS_ICONIC;
67 
68  std::ostringstream strm;
69  strm << "PStats " << pstats_port;
70  std::string window_name = strm.str();
71 
72  HWND toplevel_window =
73  CreateWindow(toplevel_class_name, window_name.c_str(), window_style,
74  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
75  nullptr, nullptr, application, 0);
76  if (!toplevel_window) {
77  nout << "Could not create toplevel window!\n";
78  exit(1);
79  }
80 
81  return toplevel_window;
82 }
83 
84 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
85  HINSTANCE application = GetModuleHandle(nullptr);
86  HWND toplevel_window = create_toplevel_window(application);
87 
88  ShowWindow(toplevel_window, SW_SHOWMINIMIZED);
89 
90  // Create the server object.
91  server = new WinStatsServer;
92  if (!server->listen()) {
93  std::ostringstream stream;
94  stream
95  << "Unable to open port " << pstats_port
96  << ". Try specifying a different\n"
97  << "port number using pstats-port in your Config file.";
98  std::string str = stream.str();
99  MessageBox(toplevel_window, str.c_str(), "PStats error",
100  MB_OK | MB_ICONEXCLAMATION);
101  exit(1);
102  }
103 
104  // Set up a timer to poll the pstats every so often.
105  SetTimer(toplevel_window, 1, 200, nullptr);
106 
107  // Now get lost in the Windows message loop.
108  MSG msg;
109  int retval;
110  retval = GetMessage(&msg, nullptr, 0, 0);
111  while (retval != 0) {
112  if (retval == -1) {
113  nout << "Error processing message queue.\n";
114  exit(1);
115  }
116  TranslateMessage(&msg);
117  DispatchMessage(&msg);
118  retval = GetMessage(&msg, nullptr, 0, 0);
119  }
120 
121  return (0);
122 }
123 
124 // WinMain() is the correct way to start a Windows-only application, but it is
125 // sometimes more convenient during development to use main() instead, which
126 // doesn't squelch the stderr output.
127 
128 int main(int argc, char *argv[]) {
129  return WinMain(nullptr, nullptr, nullptr, 0);
130 }
PStatServer::poll
void poll()
Checks for any network activity and handles it, if appropriate, and then returns.
Definition: pStatServer.cxx:82
WinStatsServer
The class that owns the main loop, waiting for client connections.
Definition: winStatsServer.h:23
config_pstatclient.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PStatServer::listen
bool listen(int port=-1)
Establishes a port number that the manager will listen on for TCP connections.
Definition: pStatServer.cxx:49
winStatsServer.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandatoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.