plugins/consoles/rcons/server/win32/console_host.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // console_host.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "stdafx.h"
       
    14 #include "resource.h"
       
    15 #include "Misc.h"
       
    16 #include "Window.h"
       
    17 #include "ConsoleWindow.h"
       
    18 #include <winsock2.h>
       
    19 #include "Server.h"
       
    20 
       
    21 void InitWinSock()
       
    22 	{
       
    23 	WORD version = MAKEWORD (2, 2);
       
    24 	WSADATA wsaData;
       
    25 
       
    26 	if (WSAStartup(version, &wsaData) == SOCKET_ERROR)
       
    27 		{
       
    28 		throw KExceptionWinSockStartupFailed;
       
    29 		}
       
    30 
       
    31 	if (version != wsaData.wVersion)
       
    32 		{
       
    33 		throw KExceptionWinSockVersionInvalid;
       
    34 		}
       
    35 	}
       
    36 
       
    37 int APIENTRY WinMain(HINSTANCE hInstance,
       
    38                      HINSTANCE hPrevInstance,
       
    39                      LPSTR     lpCmdLine,
       
    40                      int       nCmdShow)
       
    41 	{
       
    42 	MSG msg;
       
    43 	HACCEL hAccelTable;
       
    44 	CServer* server;
       
    45 	try
       
    46 		{
       
    47 		hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CONSOLE_HOST);
       
    48 		InitWinSock();
       
    49 		server = CServer::New(hInstance);
       
    50 		}
       
    51 	catch (TException aException)
       
    52 		{
       
    53 		LPCTSTR errorTitle = TEXT("Remote Console Server Initialization Error");
       
    54 		LPCTSTR errorText = NULL;
       
    55 		switch (aException)
       
    56 			{
       
    57 			case KExceptionNoMemory:
       
    58 				errorText = TEXT("Not enough memory to initialize server.");
       
    59 				break;
       
    60 			case KExceptionWindowConstructFailed:
       
    61 				errorText = TEXT("Failed to construct server window.");
       
    62 				break;
       
    63 			case KExceptionWinSockStartupFailed:
       
    64 				errorText = TEXT("Failed to initialize WINSOCK.");
       
    65 				break;
       
    66 			case KExceptionWinSockVersionInvalid:
       
    67 				errorText = TEXT("Invalid WINSOCK version.");
       
    68 				break;
       
    69 			case KExceptionSocketConstructFailed:
       
    70 				errorText = TEXT("Failed to construct server socket.");
       
    71 				break;
       
    72 			default:
       
    73 				errorText = TEXT("Unknown error.");
       
    74 				break;
       
    75 			}
       
    76 		MessageBox(NULL, errorText, errorTitle, MB_OK);
       
    77 		WSACleanup();
       
    78 		return 0;
       
    79 		}
       
    80 
       
    81 	while (GetMessage(&msg, NULL, 0, 0)) 
       
    82 		{
       
    83 		try
       
    84 			{
       
    85 			if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
       
    86 				{
       
    87 				TranslateMessage(&msg);
       
    88 				DispatchMessage(&msg);
       
    89 				}
       
    90 			}
       
    91 		catch (TException aException)
       
    92 			{
       
    93 			server->HandleException(aException);
       
    94 			}
       
    95 		}
       
    96 
       
    97 	delete server;
       
    98 	WSACleanup();
       
    99 
       
   100 	return msg.wParam;
       
   101 	}