r5sdk/r5dev/netconsole/netconsole.cpp

110 lines
2.9 KiB
C++
Raw Normal View History

//=====================================================================================//
//
// Purpose: Lightweight netconsole.
//
//=====================================================================================//
#include "core/stdafx.h"
#include "tier1/NetAdr2.h"
#include "tier2/socketcreator.h"
#include "netconsole/netconsole.h"
//-----------------------------------------------------------------------------
// purpose: send datagram
//-----------------------------------------------------------------------------
void CNetCon::Send(void)
{
2022-02-06 17:27:47 +01:00
static std::string svUserInput;
do
{
printf(">");
std::getline(std::cin, svUserInput);
svUserInput.append("\n\r");
int nSendResult = ::send(pSocket->GetAcceptedSocketData(0)->m_hSocket, svUserInput.c_str(), svUserInput.size(), MSG_NOSIGNAL);
} while (svUserInput.size() > 0);
}
//-----------------------------------------------------------------------------
// purpose: receive datagram
//-----------------------------------------------------------------------------
void CNetCon::Recv(void)
{
2022-02-06 17:27:47 +01:00
static char szRecvBuf[MAX_NETCONSOLE_INPUT_LEN]{};
for (;;)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2022-02-06 17:27:47 +01:00
int nRecvLen = ::recv(pSocket->GetAcceptedSocketData(0)->m_hSocket, szRecvBuf, sizeof(szRecvBuf), MSG_NOSIGNAL);
if (nRecvLen > 0 && nRecvLen < MAX_NETCONSOLE_INPUT_LEN - 1)
{
2022-02-06 17:27:47 +01:00
szRecvBuf[nRecvLen + 1] = '\0';
printf("%s\n", szRecvBuf);
}
}
}
//-----------------------------------------------------------------------------
// purpose: WSA and NETCON systems init
//-----------------------------------------------------------------------------
bool CNetCon::Init(void)
{
WSAData wsaData{};
int nError = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
if (nError != 0)
{
assert(nError != 0 && "Failed to start Winsock via WSAStartup.");
return false;
}
if (pSocket->ConnectSocket(*pNetAdr2, true) == SOCKET_ERROR)
{
assert(nError != 0 && "'pSocket->ConnectSocket()' returned 'SOCKET_ERROR'");
return false;
}
std::thread tRecv(&CNetCon::Recv, this);
tRecv.detach();
return true;
}
//-----------------------------------------------------------------------------
// purpose: WSA and NETCON systems shutdown
//-----------------------------------------------------------------------------
bool CNetCon::Shutdown(void)
{
pSocket->CloseAllAcceptedSockets();
int nError = ::WSACleanup();
if (nError != 0)
{
assert(nError != 0 && "Failed to stop winsock via WSACleanup.\n");
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// purpose: entrypoint
//-----------------------------------------------------------------------------
int main(void)
{
CNetCon* pNetCon = new CNetCon();
if (!pNetCon->Init())
{
return EXIT_FAILURE;
}
pNetCon->Send();
if (!pNetCon->Shutdown())
{
return EXIT_FAILURE;
}
return ERROR_SUCCESS;
}