diff --git a/source/core/Core.cpp b/source/core/Core.cpp index d2d56b5b0916d4d288298145329f9866dc1dbd7f..f47153f650e00a051c83223b2fec2f9a7b5d10b7 100644 --- a/source/core/Core.cpp +++ b/source/core/Core.cpp @@ -30,7 +30,7 @@ CCore::CCore(const char *szName) ConsoleConnect(szName); ConsoleRegisterCmds(); CvarInitSystem(this); - + m_pConsole = new CConsole(); Core_0RegisterCVarBool("g_time_run", true, "Запущено ли игровое время?", FCVAR_NOTIFY_OLD); diff --git a/source/core/concmd.cpp b/source/core/concmd.cpp index ea874861762619dd796b547bc8dff6eab6f155d4..48d562cff395c2bbf72bf9ab516574b757bd2167 100644 --- a/source/core/concmd.cpp +++ b/source/core/concmd.cpp @@ -29,11 +29,10 @@ See the license in LICENSE AssotiativeArray<String, ConCmd> g_mCmds; -SOCKET ConnectSocket = INVALID_SOCKET; -SOCKET CommandSocket = INVALID_SOCKET; +SOCKET g_iSendSocket = INVALID_SOCKET; +SOCKET g_iRecvSocket = INVALID_SOCKET; #define CONSOLE_PORT g_szServerPort /*!< Стандартный порт для подключения консоли */ -#define COMMAND_PORT g_szClientPort /*!< Стандартный порт для команд */ bool g_bRunning = false; bool g_bRunningCmd = false; @@ -43,11 +42,9 @@ typedef std::mutex Mutex; CommandBuffer g_vCommandBuffer; Stack<CommandBuffer> g_cbufStack; -char g_szServerPort[8]; -char g_szClientPort[8]; +char g_szServerPort[8] = "59705"; +char g_szServerAddr[255] = "127.0.0.1"; -bool CommandConnect(); -void CommandDisconnect(); SX_LIB_API XDEPRECATED void Core_0RegisterConcmd(const char * name, SXCONCMD cmd, const char * desc) { @@ -744,48 +741,116 @@ void ConsoleRegisterCmds() SX_LIB_API UINT_PTR Core_ConsoleGetOutHandler() { - return(ConnectSocket); + return(g_iSendSocket); } -/*! -@TODO: Handle this with TaskManager -*/ -void ConsoleRecvTask() +SOCKET Connect(const char *szAddr, const char *szPort) { - char recvbuf[2048]; int iResult; - int recvbuflen = sizeof(recvbuf); + struct addrinfo *result = NULL, + *ptr = NULL, + hints; + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; - iResult = recv(CommandSocket, recvbuf, recvbuflen - 1, 0); - if(iResult > 0) + // Resolve the server address and port + iResult = getaddrinfo(szAddr, szPort, &hints, &result); + if(iResult != 0) { - recvbuf[iResult] = 0; - Core_0ConsoleExecCmd("%s", recvbuf); + printf("getaddrinfo failed with error: %d\n", iResult); + return(INVALID_SOCKET); } - else if(iResult == 0) + + SOCKET iSocket = INVALID_SOCKET; + + // Attempt to connect to an address until one succeeds + for(ptr = result; ptr != NULL; ptr = ptr->ai_next) { - //printf("Connection closed\n"); - return; + + // Create a SOCKET for connecting to server + iSocket = WSASocketW(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol, NULL, 0, 0); + if(iSocket == INVALID_SOCKET) + { + printf("socket failed with error: %ld\n", WSAGetLastError()); + return(iSocket); + } + + // Connect to server. + iResult = connect(iSocket, ptr->ai_addr, (int)ptr->ai_addrlen); + if(iResult == SOCKET_ERROR) + { + closesocket(iSocket); + iSocket = INVALID_SOCKET; + continue; + } + break; } - //else - //printf("recv failed with error: %d\n", WSAGetLastError()); + + freeaddrinfo(result); + + return(iSocket); } void ConsoleRecv(void*) { - while(g_bRunningCmd) + int iResult; + g_iRecvSocket = Connect(g_szServerAddr, CONSOLE_PORT); + + if(g_iRecvSocket == INVALID_SOCKET) { - ConsoleRecvTask(); + printf("Unable to connect to console command channel!\n"); + return; } -} + char recvbuf[2048]; + int recvbuflen = sizeof(recvbuf); -int hOut; -FILE * fOut = NULL; + while(g_bRunning) + { + iResult = recv(g_iRecvSocket, recvbuf, recvbuflen - 1, 0); + if(iResult > 0) + { + recvbuf[iResult] = 0; + Core_0ConsoleExecCmd("%s", recvbuf); + } + else if(iResult == 0) + { + break; + } + } + + iResult = shutdown(g_iRecvSocket, SD_SEND); + if(iResult == SOCKET_ERROR) + { + printf("shutdown failed with error: %d\n", WSAGetLastError()); + } + closesocket(g_iRecvSocket); + g_iRecvSocket = INVALID_SOCKET; +} bool ConsoleConnect(const char *szName, bool bNewInstance) { - char szNameConsole[64]; + { + const char *szConsole = Core_0GetCommandLineArg("console", "127.0.0.1:59705"); + + char *str = strdupa(szConsole); + char *parts[2]; + if(parse_str(str, parts, 2, ':') == 2) + { + strcpy(CONSOLE_PORT, parts[1]); + } + strcpy(g_szServerAddr, parts[0]); + if(!strcmp(parts[0], "localhost")) + { + strcpy(g_szServerAddr, "127.0.0.1"); + } + } + + char szConsoleArgs[64]; + szConsoleArgs[0] = 0; char str[MAX_PATH]; GetModuleFileNameA(NULL, str, MAX_PATH); @@ -795,26 +860,19 @@ bool ConsoleConnect(const char *szName, bool bNewInstance) srand((UINT)time(NULL)); int port = (rand() % (65536 - 2048) | 1) + 2048; sprintf(g_szServerPort, "%d", port); - sprintf(g_szClientPort, "%d", port + 1); - sprintf(szNameConsole, "%s %s", g_szServerPort, szName); + sprintf(szConsoleArgs, "-port %s -exit-on-disconnect 1", g_szServerPort); } - else + else if(!strcmp(g_szServerAddr, "127.0.0.1") && strcmp(g_szServerPort, "59705")) { - int port = 59705; - sprintf(g_szServerPort, "%d", port); - sprintf(g_szClientPort, "%d", port + 1); - - sprintf(szNameConsole, "0 %s", szName); + sprintf(szConsoleArgs, "-port %s -exit-on-disconnect 1", g_szServerPort); } if (bNewInstance || !Core_0IsProcessRun("sxconsole.exe")) - ShellExecuteA(0, "open", "sxconsole.exe", szNameConsole, dirname(str), SW_SHOWNORMAL); + ShellExecuteA(0, "open", "sxconsole.exe", szConsoleArgs, dirname(str), SW_SHOWNORMAL); WSADATA wsaData; - struct addrinfo *result = NULL, - *ptr = NULL, - hints; + char *sendbuf = "Console initialized...\n"; //char recvbuf[2048]; int iResult; @@ -828,184 +886,68 @@ bool ConsoleConnect(const char *szName, bool bNewInstance) return(false); } - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; + g_iSendSocket = Connect(g_szServerAddr, CONSOLE_PORT); - // Resolve the server address and port - iResult = getaddrinfo("127.0.0.1", CONSOLE_PORT, &hints, &result); - if(iResult != 0) + if(g_iSendSocket == INVALID_SOCKET) { - printf("getaddrinfo failed with error: %d\n", iResult); + printf("Unable to connect to console!\n"); WSACleanup(); - return(false); - } - - // Attempt to connect to an address until one succeeds - for(ptr = result; ptr != NULL; ptr = ptr->ai_next) - { - - // Create a SOCKET for connecting to server - ConnectSocket = WSASocketW(ptr->ai_family, ptr->ai_socktype, - ptr->ai_protocol, NULL, 0, 0); - if(ConnectSocket == INVALID_SOCKET) + const char *szConsoleAddr = Core_0GetCommandLineArg("console", NULL); + if(szConsoleAddr) { - printf("socket failed with error: %ld\n", WSAGetLastError()); - WSACleanup(); + AllocConsole(); + freopen("CONOUT$", "wt", stdout); + freopen("CONOUT$", "wt", stderr); + printf("Unable to connect to %s!\n", szConsoleAddr); return(false); } - - // Connect to server. - iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); - if(iResult == SOCKET_ERROR) - { - closesocket(ConnectSocket); - ConnectSocket = INVALID_SOCKET; - continue; - } - break; - } - - freeaddrinfo(result); - - if(ConnectSocket == INVALID_SOCKET) - { - printf("Unable to connect to console!\n"); - WSACleanup(); return(ConsoleConnect(szName, true)); //return(false); } // Send an initial buffer - iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); + iResult = send(g_iSendSocket, sendbuf, (int)strlen(sendbuf), 0); if(iResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); - closesocket(ConnectSocket); - ConnectSocket = INVALID_SOCKET; - WSACleanup(); - return(false); + closesocket(g_iSendSocket); + g_iSendSocket = INVALID_SOCKET; + goto end; } //connected? reopen streams + // unsigned long block = 1;//+ + // ioctlsocket(g_iSendSocket, FIONBIO, &block);//+ + FreeConsole(); Core_SetOutPtr(); g_bRunning = true; - //_beginthread(ConsoleRecv, 0, 0); - CommandConnect(); + _beginthread(ConsoleRecv, 0, 0); return(true); + +end: + WSACleanup(); + return(false); } void ConsoleDisconnect() { g_bRunning = false; - int iResult = shutdown(ConnectSocket, SD_SEND); + int iResult = shutdown(g_iSendSocket, SD_SEND); if(iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); goto end; } end: - closesocket(ConnectSocket); + closesocket(g_iSendSocket); WSACleanup(); //Sleep(1000); - CommandDisconnect(); -} - -bool CommandConnect() -{ - WSADATA wsaData; - struct addrinfo *result = NULL, - *ptr = NULL, - hints; - - int iResult; - - - // Initialize Winsock - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if(iResult != 0) - { - printf("WSAStartup failed with error: %d\n", iResult); - return(false); - } - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - // Resolve the server address and port - iResult = getaddrinfo("127.0.0.1", COMMAND_PORT, &hints, &result); - if(iResult != 0) - { - printf("getaddrinfo failed with error: %d\n", iResult); - WSACleanup(); - return(false); - } - - // Attempt to connect to an address until one succeeds - for(ptr = result; ptr != NULL; ptr = ptr->ai_next) - { - - // Create a SOCKET for connecting to server - CommandSocket = WSASocketW(ptr->ai_family, ptr->ai_socktype, - ptr->ai_protocol, NULL, 0, 0); - if(CommandSocket == INVALID_SOCKET) - { - printf("socket failed with error: %ld\n", WSAGetLastError()); - WSACleanup(); - return(false); - } - - // Connect to server. - iResult = connect(CommandSocket, ptr->ai_addr, (int)ptr->ai_addrlen); - if(iResult == SOCKET_ERROR) - { - closesocket(CommandSocket); - CommandSocket = INVALID_SOCKET; - continue; - } - break; - } - - freeaddrinfo(result); - - if(CommandSocket == INVALID_SOCKET) - { - printf("Unable to connect to console!\n"); - WSACleanup(); - return(false); - } - - g_bRunningCmd = true; - - //Core_MTaskAdd(ConsoleRecvTask, CORE_TASK_FLAG_BACKGROUND_REPEATING); - _beginthread(ConsoleRecv, 0, 0); - - return(true); + //CommandDisconnect(); } -void CommandDisconnect() -{ - g_bRunningCmd = false; - - int iResult = shutdown(CommandSocket, SD_BOTH); - if(iResult == SOCKET_ERROR) - { - printf("shutdown failed with error: %d\n", WSAGetLastError()); - closesocket(CommandSocket); - WSACleanup(); - return; - } - // cleanup - closesocket(CommandSocket); - //WSACleanup(); -} - diff --git a/source/core/sxcore.h b/source/core/sxcore.h index 16329686fe886647e8cc185b706e93fa6aecc03d..e5531a31e612f66fba6a529a62af7a92e87df675 100644 --- a/source/core/sxcore.h +++ b/source/core/sxcore.h @@ -520,7 +520,10 @@ class COutPtr } ~COutPtr() { - fclose(m_fOut); + if(m_fOut) + { + fclose(m_fOut); + } } FILE *m_fOut; diff --git a/source/gdefines.h b/source/gdefines.h index 5972803b31371f0a888f707880101e2402fd2fff..f0dc5fd815bf5296104369d0cb727bb35e2cd408 100644 --- a/source/gdefines.h +++ b/source/gdefines.h @@ -260,6 +260,9 @@ typedef void(*report_func) (int iLevel, const char *szLibName, const char *szMes //! @} +#define CONSOLE_TITLE "\033]0;" +#define CONSOLE_TITLE_END "\a" + #ifndef DEFAULT_FUNCTION_REPORT #define DEFAULT_FUNCTION_REPORT diff --git a/source/sxconsole/sxconsole.cpp b/source/sxconsole/sxconsole.cpp index 35f6f832c322f5f17288f57f301838e82cbc0766..f8e41072174354de9bc077681e8a18d27ae3b52d 100644 --- a/source/sxconsole/sxconsole.cpp +++ b/source/sxconsole/sxconsole.cpp @@ -39,20 +39,21 @@ HANDLE g_hStdOut = NULL; COORD g_iOldOutPos; ColorPrint * g_pColor; -BOOL g_bRunning = 1; -SOCKET ClientSocket = INVALID_SOCKET; +bool g_bRunning = true; +SOCKET g_iListenSocket = INVALID_SOCKET; +SOCKET g_iRecvSocket = INVALID_SOCKET; +SOCKET g_iSendSocket = INVALID_SOCKET; AnsiColor g_iCurColorFG; AnsiColor g_iCurColorBG; +char g_szServerBind[64] = "127.0.0.1"; int g_iServerPort = 59705; char g_szServerPort[8]; -char g_szClientPort[8]; bool g_bExitOnDisconnect = false; #define DEFAULT_PORT g_szServerPort -#define COMMAND_PORT g_szClientPort Mutex mx; @@ -236,7 +237,6 @@ void WriteColored(char ** _buf) int clr; bool done = false; - while(!done) { esc = strstr(buf, "\033"); @@ -246,6 +246,25 @@ void WriteColored(char ** _buf) printf("%s", buf); *esc = '\033'; ++esc; + + if(esc[0] == ']' && esc[1] == '0' && esc[2] == ';') + { + char *szEnd = strstr(esc, "\a"); + if(!szEnd) + { + *_buf = esc - 1; + goto end; + } + else + { + *szEnd = 0; + buf = szEnd + 1; + esc += 3; + SetConsoleTitleA(esc); + continue; + } + } + if(!strstr(esc, "m") && strlen(esc) < 20) { *_buf = esc - 1; @@ -387,34 +406,67 @@ end: mx.unlock(); } -bool g_bConnected = true; +bool g_bConnected = false; + +int iScreenWidth = 0, iScreenHeight = 0; + +void threadCommander(void*) +{ + int iResult = 0; + g_iSendSocket = accept(g_iListenSocket, NULL, NULL); + if(g_iSendSocket == INVALID_SOCKET) + { + SetColor(ANSI_RED); + WriteOutput("accept failed with error: %d\n", WSAGetLastError()); + SetColor(g_pColor->getDefaultFG()); + return; + } + closesocket(g_iListenSocket); -SOCKET ListenSocket = INVALID_SOCKET; + while(g_bConnected) + { + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(g_hStdOut, &csbi); + int iWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1; + int iHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; + if(iWidth != iScreenWidth) + { + char buf[64]; + sprintf(buf, "con_width %d\n", iWidth); + send(g_iSendSocket, buf, strlen(buf), 0); + iScreenWidth = iWidth; + } + if(iHeight != iScreenHeight) + { + char buf[64]; + sprintf(buf, "con_height %d\n", iHeight); + send(g_iSendSocket, buf, strlen(buf), 0); + iScreenHeight = iHeight; + } + Sleep(10); + } + + iResult = shutdown(g_iSendSocket, SD_SEND); + if(iResult == SOCKET_ERROR) + { + SetColor(ANSI_RED); + WriteOutput("shutdown failed with error: %d\n", WSAGetLastError()); + SetColor(g_pColor->getDefaultFG()); + } + closesocket(g_iSendSocket); + g_iSendSocket = INVALID_SOCKET; +} void threadServer(void*) { - WSADATA wsaData; int iResult; - SOCKET ListenSocket = INVALID_SOCKET; - SOCKET _ClientSocket = INVALID_SOCKET; - struct addrinfo *result = NULL; struct addrinfo hints; char recvbuf[DEFAULT_BUFLEN]; int recvbuflen = DEFAULT_BUFLEN; - - // Initialize Winsock - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if(iResult != 0) - { - SetColor(ANSI_RED); - WriteOutput("WSAStartup failed with error: %d\n", iResult); - SetColor(g_pColor->getDefaultFG()); - return; - } - + ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; @@ -422,51 +474,47 @@ void threadServer(void*) hints.ai_flags = AI_PASSIVE; // Resolve the server address and port - iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + iResult = getaddrinfo(g_szServerBind, DEFAULT_PORT, &hints, &result); if(iResult != 0) { SetColor(ANSI_RED); WriteOutput("getaddrinfo failed with error: %d\n", iResult); SetColor(g_pColor->getDefaultFG()); - WSACleanup(); return; } // Create a SOCKET for connecting to server - ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if(ListenSocket == INVALID_SOCKET) + g_iListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if(g_iListenSocket == INVALID_SOCKET) { SetColor(ANSI_RED); - WriteOutput("socket failed with error: %ld\n", WSAGetLastError()); + WriteOutput("socket failed with error: %d\n", WSAGetLastError()); SetColor(g_pColor->getDefaultFG()); freeaddrinfo(result); - WSACleanup(); return; } // Setup the TCP listening socket - iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + iResult = bind(g_iListenSocket, result->ai_addr, (int)result->ai_addrlen); if(iResult == SOCKET_ERROR) { SetColor(ANSI_RED); WriteOutput("bind failed with error: %d\n", WSAGetLastError()); SetColor(g_pColor->getDefaultFG()); freeaddrinfo(result); - closesocket(ListenSocket); - WSACleanup(); + closesocket(g_iListenSocket); return; } freeaddrinfo(result); - iResult = listen(ListenSocket, SOMAXCONN); + iResult = listen(g_iListenSocket, SOMAXCONN); if(iResult == SOCKET_ERROR) { SetColor(ANSI_RED); WriteOutput("listen failed with error: %d\n", WSAGetLastError()); SetColor(g_pColor->getDefaultFG()); - closesocket(ListenSocket); - WSACleanup(); + closesocket(g_iListenSocket); return; } SetColor(ANSI_LCYAN); @@ -477,8 +525,8 @@ void threadServer(void*) int offset = 0; while(g_bRunning) { - _ClientSocket = accept(ListenSocket, NULL, NULL); - if(_ClientSocket == INVALID_SOCKET) + g_iRecvSocket = accept(g_iListenSocket, NULL, NULL); + if(g_iRecvSocket == INVALID_SOCKET) { SetColor(ANSI_RED); WriteOutput("accept failed with error: %d\n", WSAGetLastError()); @@ -486,19 +534,20 @@ void threadServer(void*) continue; } - - // No longer need server socket - closesocket(ListenSocket); + g_bConnected = true; + + _beginthread(threadCommander, 0, 0); ClearAll(); SetColor(ANSI_LGREEN); WriteOutput("Connected!\n"); SetColor(g_pColor->getDefaultFG()); + iScreenWidth = iScreenHeight = 0; + do { - - iResult = recv(_ClientSocket, recvbuf + offset, recvbuflen - 1 - offset, 0); + iResult = recv(g_iRecvSocket, recvbuf + offset, recvbuflen - 1 - offset, 0); if(iResult > 0) { recvbuf[iResult + offset] = 0; @@ -521,45 +570,33 @@ void threadServer(void*) SetColor(ANSI_RED); WriteOutput("recv failed with error: %d\n", WSAGetLastError()); SetColor(g_pColor->getDefaultFG()); - closesocket(_ClientSocket); - _ClientSocket = INVALID_SOCKET; + closesocket(g_iRecvSocket); + g_iRecvSocket = INVALID_SOCKET; continue; } } while(iResult > 0); + g_bConnected = false; // shutdown the connection since we're done - iResult = shutdown(_ClientSocket, SD_SEND); + iResult = shutdown(g_iRecvSocket, SD_SEND); if(iResult == SOCKET_ERROR) { SetColor(ANSI_RED); WriteOutput("shutdown failed with error: %d\n", WSAGetLastError()); SetColor(g_pColor->getDefaultFG()); - closesocket(_ClientSocket); - _ClientSocket = INVALID_SOCKET; - break; - continue; } - _ClientSocket = INVALID_SOCKET; - iResult = shutdown(ClientSocket, SD_SEND); - if(iResult == SOCKET_ERROR) - { - SetColor(ANSI_RED); - WriteOutput("shutdown failed with error: %d\n", WSAGetLastError()); - SetColor(g_pColor->getDefaultFG()); - closesocket(ClientSocket); - ClientSocket = INVALID_SOCKET; - break; - continue; - } - ClientSocket = INVALID_SOCKET; break; } + closesocket(g_iRecvSocket); + g_iRecvSocket = INVALID_SOCKET; - closesocket(_ClientSocket); - WSACleanup(); + if(g_iListenSocket != INVALID_SOCKET) + { + closesocket(g_iListenSocket); + } if(g_bExitOnDisconnect) { @@ -571,197 +608,15 @@ void threadServer(void*) } } -void InitCommandChannel(void*) -{ - WSADATA wsaData; - int iResult; - - - struct addrinfo *result = NULL; - struct addrinfo hints; - -// char recvbuf[DEFAULT_BUFLEN]; -// int recvbuflen = DEFAULT_BUFLEN; - - // Initialize Winsock - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if(iResult != 0) - { - SetColor(ANSI_RED); - WriteOutput("WSAStartup failed with error: %d\n", iResult); - SetColor(g_pColor->getDefaultFG()); - return; - } - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE; - - // Resolve the server address and port - iResult = getaddrinfo(NULL, COMMAND_PORT, &hints, &result); - if(iResult != 0) - { - SetColor(ANSI_RED); - WriteOutput("getaddrinfo failed with error: %d\n", iResult); - SetColor(g_pColor->getDefaultFG()); - WSACleanup(); - return; - } - - // Create a SOCKET for connecting to server - ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if(ListenSocket == INVALID_SOCKET) - { - SetColor(ANSI_RED); - WriteOutput("socket failed with error: %ld\n", WSAGetLastError()); - SetColor(g_pColor->getDefaultFG()); - freeaddrinfo(result); - WSACleanup(); - return; - } - - // Setup the TCP listening socket - iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); - if(iResult == SOCKET_ERROR) - { - SetColor(ANSI_RED); - WriteOutput("bind failed with error: %d\n", WSAGetLastError()); - SetColor(g_pColor->getDefaultFG()); - freeaddrinfo(result); - closesocket(ListenSocket); - WSACleanup(); - return; - } - - freeaddrinfo(result); - - iResult = listen(ListenSocket, SOMAXCONN); - if(iResult == SOCKET_ERROR) - { - SetColor(ANSI_RED); - WriteOutput("listen failed with error: %d\n", WSAGetLastError()); - SetColor(g_pColor->getDefaultFG()); - closesocket(ListenSocket); - WSACleanup(); - return; - } - //SetColor(ANSI_LCYAN); - //WriteOutput("\n"); - //SetColor(g_pColor->getDefaultFG()); - // Accept a client socket - - int iScreenWidth, iScreenHeight; - - int offset = 0; - while(g_bRunning) - { - ClientSocket = accept(ListenSocket, NULL, NULL); - if(ClientSocket == INVALID_SOCKET) - { - SetColor(ANSI_RED); - //WriteOutput("accept failed with error: %d\n", WSAGetLastError()); - //SetColor(g_pColor->getDefaultFG()); - continue; - } - g_bConnected = true; - //SetColor(ANSI_LGREEN); - //WriteOutput("Connected!\n"); - //SetColor(g_pColor->getDefaultFG()); - - iScreenWidth = iScreenHeight = 0; - - while(g_bConnected) - { - CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(g_hStdOut, &csbi); - int iWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1; - int iHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; - if(iWidth != iScreenWidth) - { - char buf[64]; - sprintf(buf, "con_width %d\n", iWidth); - send(ClientSocket, buf, strlen(buf), 0); - iScreenWidth = iWidth; - } - if(iHeight != iScreenHeight) - { - char buf[64]; - sprintf(buf, "con_height %d\n", iHeight); - send(ClientSocket, buf, strlen(buf), 0); - iScreenHeight = iHeight; - } - Sleep(10); - } - - /*do - { - - iResult = recv(ClientSocket, recvbuf + offset, recvbuflen - 1 - offset, 0); - if(iResult > 0) - { - recvbuf[iResult + offset] = 0; - char * buf = recvbuf; - WriteColored(&buf); - offset = 0; - if(buf) // not fully parsed - { - memmove(recvbuf, buf, (offset = strlen(buf)) + 1); - } - } - else if(iResult == 0) - { - // SetColor(ANSI_RED); - // WriteOutput("Connection closed.\n"); - // SetColor(g_pColor->getDefaultFG()); - } - else - { - // SetColor(ANSI_RED); - // WriteOutput("recv failed with error: %d\n", WSAGetLastError()); - // SetColor(g_pColor->getDefaultFG()); - closesocket(ClientSocket); - ClientSocket = INVALID_SOCKET; - continue; - } - - } - while(iResult > 0);*/ - - // shutdown the connection since we're done - iResult = shutdown(ClientSocket, SD_SEND); - if(iResult == SOCKET_ERROR) - { - SetColor(ANSI_RED); - //WriteOutput("shutdown failed with error: %d\n", WSAGetLastError()); - SetColor(g_pColor->getDefaultFG()); - closesocket(ClientSocket); - ClientSocket = INVALID_SOCKET; - continue; - } - ClientSocket = INVALID_SOCKET; - } -} -void CloseCommandChannel() -{ - // No longer need server socket - closesocket(ListenSocket); - - - closesocket(ClientSocket); - WSACleanup(); -} - BOOL WINAPI HandlerRoutine( _In_ DWORD dwCtrlType ) { if(CTRL_CLOSE_EVENT == dwCtrlType) { - if(ClientSocket != INVALID_SOCKET) + if(g_iSendSocket != INVALID_SOCKET) { - send(ClientSocket, "exit\n", strlen("exit\n"), 0); + send(g_iSendSocket, "exit\n", strlen("exit\n"), 0); } } return(FALSE); @@ -785,31 +640,56 @@ int main(int argc, char ** argv) WriteOutput("SkyXEngine Console\n"); SetColor(g_pColor->getDefaultFG()); + + WSADATA wsaData; + int iResult; + // Initialize Winsock + iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if(iResult != 0) + { + SetColor(ANSI_RED); + WriteOutput("WSAStartup failed with error: %d\n", iResult); + SetColor(g_pColor->getDefaultFG()); + return(1); + } + + g_iHistoryPointer = 0; g_vHistory.push_back({NULL, NULL}); - char szTitle[64]; - if (argc >= 2) + + for(int i = 1; i < argc - 1; ++i) { - int iPort = 0; - sscanf(argv[1], "%d", &iPort); - if (iPort > 0) + if(!strcmp(argv[i], "-bind")) { - g_bExitOnDisconnect = true; - g_iServerPort = iPort; + strcpy(g_szServerBind, argv[i + 1]); + } + else if(!strcmp(argv[i], "-port")) + { + int iPort = 0; + sscanf(argv[i + 1], "%d", &iPort); + if(iPort > 0) + { + g_iServerPort = iPort; + } + } + else if(!strcmp(argv[i], "-exit-on-disconnect")) + { + int iVal = 0; + sscanf(argv[i + 1], "%d", &iVal); + if(iVal > 0) + { + g_bExitOnDisconnect = true; + } } } - sprintf(szTitle, "sxconsole - %s", argc >= 3 ? argv[2] : ""); - SetConsoleTitleA(szTitle); - + sprintf(g_szServerPort, "%d", g_iServerPort); - sprintf(g_szClientPort, "%d", g_iServerPort + 1); SetConsoleCtrlHandler(HandlerRoutine, TRUE); _beginthread(threadServer, 0, 0); - _beginthread(InitCommandChannel, 0, 0); int i = 0; @@ -850,9 +730,9 @@ int main(int argc, char ** argv) putch(g_szUserInp[i] = ch); g_szUserInp[i] = '\n'; g_szUserInp[++i] = 0; - if(ClientSocket != INVALID_SOCKET) + if(g_iSendSocket != INVALID_SOCKET) { - send(ClientSocket, g_szUserInp, strlen(g_szUserInp), 0); + send(g_iSendSocket, g_szUserInp, strlen(g_szUserInp), 0); WriteOutput("] %s", g_szUserInp); PutHistory(); } @@ -904,8 +784,8 @@ int main(int argc, char ** argv) //g_szUserInp } - - CloseCommandChannel(); + + WSACleanup(); delete g_pColor; return(0); diff --git a/source/xEngine/Engine.cpp b/source/xEngine/Engine.cpp index 1a3e9087da2d5e3432b18f11a1f982e2ca82d7cd..f12f97802c4579e35fec2a49fa234b46516e2a15 100644 --- a/source/xEngine/Engine.cpp +++ b/source/xEngine/Engine.cpp @@ -126,6 +126,8 @@ CEngine::CEngine(int argc, char **argv, const char *szName) INIT_OUTPUT_STREAM(m_pCore); LibReport(REPORT_MSG_LEVEL_NOTICE, "LIB core initialized\n"); + printf(CONSOLE_TITLE "SkyXEngine %s version " SKYXENGINE_VERSION CONSOLE_TITLE_END, szName); + m_pObserverChangedEventChannel = m_pCore->getEventChannel<XEventObserverChanged>(EVENT_OBSERVER_CHANGED_GUID); Core_0RegisterCVarString("engine_version", SKYXENGINE_VERSION, "Текущая версия движка", FCVAR_READONLY); diff --git a/source/xcommon/IXCore.h b/source/xcommon/IXCore.h index 1f541ed8257e958c2d3e67e72e88918dc4d945f4..ac558821fac2677099e41d79f657523ee5709b25 100644 --- a/source/xcommon/IXCore.h +++ b/source/xcommon/IXCore.h @@ -103,7 +103,10 @@ class CCoreOutPtr } ~CCoreOutPtr() { - fclose(m_fOut); + if(m_fOut) + { + fclose(m_fOut); + } } FILE *m_fOut;