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