mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-12-02 22:18:09 +01:00
* First time ever messing with sockets, but i think it should be good ? * Note: I put the definition alongside Windows64 but it may be more suited under a Linux.cmake file... No reason for the Windows64 group to be in Common though
288 lines
9.1 KiB
C++
288 lines
9.1 KiB
C++
#include "stdafx.h"
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include "NetworkPlayerLinux.h"
|
|
#include "..\..\..\Minecraft.World\Socket.h"
|
|
#include "..\Windows64_Xuid.h"
|
|
#include <qnet.h>
|
|
#include <algorithm>
|
|
|
|
NetworkPlayerLinux::NetworkPlayerLinux(int userIndex, unsigned char smallId, bool isLocal, bool isHost, const std::wstring& name) {
|
|
m_name = name;
|
|
m_userIndex = userIndex;
|
|
m_smallId = smallId;
|
|
m_uid = INVALID_XUID;
|
|
m_hasResolvedUID = false;
|
|
m_lastChunkPacketTick = 0;
|
|
|
|
m_isLocal = isLocal;
|
|
m_isHost = isHost;
|
|
m_isHostTransport = isHost;
|
|
m_isFakeHost = false;
|
|
|
|
m_sessionIndex = -1;
|
|
m_remoteAddress = 0;
|
|
m_sendQueueOffset = 0;
|
|
m_sendQueueBytes = 0;
|
|
m_gameSocket = nullptr;
|
|
m_connected = true;
|
|
m_transportSocket = INVALID_SOCKET;
|
|
RefreshUID();
|
|
}
|
|
|
|
NetworkPlayerLinux::~NetworkPlayerLinux() {
|
|
CloseTransport(m_isHost);
|
|
}
|
|
|
|
unsigned char NetworkPlayerLinux::GetSmallId() { return m_smallId; }
|
|
|
|
NetworkPlayerLinux* NetworkPlayerLinux::GetTransportPlayer(INetworkPlayer* player) {
|
|
if (m_isHostTransport) {
|
|
return dynamic_cast<NetworkPlayerLinux*>(player);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
void NetworkPlayerLinux::SendData(INetworkPlayer* player, const void* pvData, int dataSize, bool lowPriority, bool ack) {
|
|
(void)ack;
|
|
NetworkPlayerLinux* transportPlayer = GetTransportPlayer(player);
|
|
if (transportPlayer == nullptr || pvData == nullptr || dataSize <= 0) return;
|
|
|
|
if (transportPlayer->m_transportSocket.load() == INVALID_SOCKET || !transportPlayer->m_connected.load()) {
|
|
app.DebugPrintf("Windows network send skipped: player has no active TCP connection.");
|
|
return;
|
|
}
|
|
|
|
std::vector<char> message(dataSize);
|
|
memcpy(message.data(), pvData, dataSize);
|
|
|
|
std::lock_guard<std::mutex> lock(transportPlayer->m_sendQueueMutex);
|
|
transportPlayer->m_sendQueueBytes += message.size();
|
|
transportPlayer->m_sendQueue.push_back(std::move(message));
|
|
}
|
|
|
|
bool NetworkPlayerLinux::IsSameSystem(INetworkPlayer* player) {
|
|
NetworkPlayerLinux* other = dynamic_cast<NetworkPlayerLinux*>(player);
|
|
return other != nullptr && (this == other || m_remoteAddress == other->m_remoteAddress);
|
|
}
|
|
|
|
int NetworkPlayerLinux::GetOutstandingAckCount() { return 0; }
|
|
|
|
int NetworkPlayerLinux::GetQueueSizeBytes() {
|
|
std::lock_guard<std::mutex> lock(m_sendQueueMutex);
|
|
return static_cast<int>(std::min<size_t>(m_sendQueueBytes, INT_MAX));
|
|
}
|
|
|
|
int NetworkPlayerLinux::GetQueueSizeMessages() {
|
|
std::lock_guard<std::mutex> lock(m_sendQueueMutex);
|
|
return static_cast<int>(std::min<size_t>(m_sendQueue.size(), INT_MAX));
|
|
}
|
|
|
|
int NetworkPlayerLinux::GetSendQueueSizeBytes(INetworkPlayer* player, bool lowPriority) {
|
|
NetworkPlayerLinux* transportPlayer = GetTransportPlayer(player);
|
|
if (transportPlayer == nullptr) return 0;
|
|
|
|
return transportPlayer->GetQueueSizeBytes();
|
|
}
|
|
|
|
int NetworkPlayerLinux::GetSendQueueSizeMessages(INetworkPlayer* player, bool lowPriority) {
|
|
NetworkPlayerLinux* transportPlayer = GetTransportPlayer(player);
|
|
if (transportPlayer == nullptr) return 0;
|
|
|
|
return transportPlayer->GetQueueSizeMessages();
|
|
}
|
|
|
|
int NetworkPlayerLinux::GetCurrentRtt() { return 0; }
|
|
|
|
bool NetworkPlayerLinux::IsHost() { return m_isHost; }
|
|
bool NetworkPlayerLinux::IsFakeHost() { return m_isFakeHost; }
|
|
bool NetworkPlayerLinux::IsGuest() { return false; }
|
|
bool NetworkPlayerLinux::IsLocal() { return m_isLocal; }
|
|
int NetworkPlayerLinux::GetSessionIndex() { return m_sessionIndex; }
|
|
|
|
bool NetworkPlayerLinux::IsTalking() { return false; }
|
|
bool NetworkPlayerLinux::IsMutedByLocalUser(int userIndex) { return false; }
|
|
bool NetworkPlayerLinux::HasVoice() { return false; }
|
|
bool NetworkPlayerLinux::HasCamera() { return false; }
|
|
|
|
int NetworkPlayerLinux::GetUserIndex() { return m_userIndex; }
|
|
|
|
void NetworkPlayerLinux::SetSocket(Socket* pSocket) { m_gameSocket = pSocket; }
|
|
Socket* NetworkPlayerLinux::GetSocket() { return m_gameSocket; }
|
|
|
|
const wchar_t* NetworkPlayerLinux::GetOnlineName() { return m_name.c_str(); }
|
|
std::wstring NetworkPlayerLinux::GetDisplayName() { return m_name; }
|
|
|
|
PlayerUID NetworkPlayerLinux::GetUID() { return m_uid; }
|
|
|
|
void NetworkPlayerLinux::SentChunkPacket() { m_lastChunkPacketTick = GetTickCount64(); }
|
|
int NetworkPlayerLinux::GetTimeSinceLastChunkPacket_ms() {
|
|
if (m_lastChunkPacketTick == 0) return INT_MAX;
|
|
ULONGLONG elapsed = GetTickCount64() - m_lastChunkPacketTick;
|
|
return elapsed > static_cast<ULONGLONG>(INT_MAX) ? INT_MAX : static_cast<int>(elapsed);
|
|
}
|
|
|
|
void NetworkPlayerLinux::SetHost(bool isHost) {
|
|
m_isHost = isHost;
|
|
if (!m_hasResolvedUID) RefreshUID();
|
|
if (m_smallId < MINECRAFT_NET_MAX_PLAYERS) {
|
|
IQNet::m_player[m_smallId].m_isHostPlayer = isHost;
|
|
IQNet::m_player[m_smallId].m_resolvedXuid = m_uid;
|
|
}
|
|
}
|
|
void NetworkPlayerLinux::SetHostTransport(bool isServer) { m_isHostTransport = isServer; }
|
|
void NetworkPlayerLinux::SetFakeHost(bool isFakeHost) { m_isFakeHost = isFakeHost; }
|
|
void NetworkPlayerLinux::SetSmallId(unsigned char smallId) {
|
|
m_smallId = smallId;
|
|
if (!m_hasResolvedUID) RefreshUID();
|
|
if (m_smallId < MINECRAFT_NET_MAX_PLAYERS) {
|
|
IQNet::m_player[m_smallId].m_smallId = m_smallId;
|
|
IQNet::m_player[m_smallId].m_resolvedXuid = m_uid;
|
|
}
|
|
}
|
|
void NetworkPlayerLinux::SetUID(PlayerUID uid) {
|
|
m_uid = uid;
|
|
m_hasResolvedUID = true;
|
|
if (m_smallId < MINECRAFT_NET_MAX_PLAYERS)
|
|
IQNet::m_player[m_smallId].m_resolvedXuid = uid;
|
|
}
|
|
void NetworkPlayerLinux::SetDisplayName(const std::wstring& name) {
|
|
m_name = name;
|
|
if (m_smallId < MINECRAFT_NET_MAX_PLAYERS)
|
|
wcsncpy_s(IQNet::m_player[m_smallId].m_gamertag, 32, name.c_str(), _TRUNCATE);
|
|
}
|
|
|
|
void NetworkPlayerLinux::SetSessionIndex(int index) { m_sessionIndex = index; }
|
|
|
|
void NetworkPlayerLinux::ResetForSession(bool isHost, unsigned char smallId) {
|
|
m_isHost = isHost;
|
|
m_isHostTransport = isHost;
|
|
m_isFakeHost = false;
|
|
SetSmallId(smallId);
|
|
m_gameSocket = nullptr;
|
|
m_remoteAddress = 0;
|
|
m_connected.store(true);
|
|
std::lock_guard<std::mutex> lock(m_sendQueueMutex);
|
|
m_sendQueue.clear();
|
|
m_sendQueueOffset = 0;
|
|
m_sendQueueBytes = 0;
|
|
}
|
|
|
|
void NetworkPlayerLinux::SetTransportSocket(SOCKET socket, unsigned long remoteAddress) {
|
|
std::lock_guard<std::mutex> lock(m_transportMutex);
|
|
m_transportSocket.store(socket);
|
|
m_remoteAddress = remoteAddress;
|
|
m_connected.store(socket != INVALID_SOCKET);
|
|
}
|
|
|
|
SOCKET NetworkPlayerLinux::GetTransportSocket() const { return m_transportSocket.load(); }
|
|
|
|
bool NetworkPlayerLinux::IsConnected() const { return m_connected.load(); }
|
|
|
|
bool NetworkPlayerLinux::ReceivePendingData(bool hostIsServer) {
|
|
char buffer[8192];
|
|
while (true) {
|
|
int bytesReceived;
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_transportMutex);
|
|
SOCKET socket = m_transportSocket.load();
|
|
if (socket == INVALID_SOCKET || !m_connected.load()) {
|
|
return false;
|
|
}
|
|
bytesReceived = recv(socket, buffer, sizeof(buffer), 0);
|
|
}
|
|
|
|
if (bytesReceived > 0) {
|
|
if (m_gameSocket == nullptr) {
|
|
app.DebugPrintf("Windows network data received before the game socket was created.");
|
|
return false;
|
|
}
|
|
m_gameSocket->pushDataToQueue(reinterpret_cast<const BYTE*>(buffer), bytesReceived, !hostIsServer);
|
|
continue;
|
|
}
|
|
|
|
if (bytesReceived == 0) return false;
|
|
|
|
int error = WSAGetLastError();
|
|
if (error == WSAEWOULDBLOCK) return true;
|
|
if (error == WSAEINTR) continue;
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool NetworkPlayerLinux::FlushSendQueue()
|
|
{
|
|
std::lock_guard<std::mutex> transportLock(m_transportMutex);
|
|
SOCKET socket = m_transportSocket.load();
|
|
if (socket == INVALID_SOCKET || !m_connected.load()) return false;
|
|
|
|
std::lock_guard<std::mutex> queueLock(m_sendQueueMutex);
|
|
while (!m_sendQueue.empty()) {
|
|
std::vector<char>& message = m_sendQueue.front();
|
|
int remaining = static_cast<int>(message.size() - m_sendQueueOffset);
|
|
int bytesSent = send(socket, message.data() + m_sendQueueOffset, remaining, 0);
|
|
if (bytesSent > 0) {
|
|
m_sendQueueOffset += bytesSent;
|
|
m_sendQueueBytes -= bytesSent;
|
|
if (m_sendQueueOffset == message.size()) {
|
|
m_sendQueue.pop_front();
|
|
m_sendQueueOffset = 0;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (bytesSent == 0) return false;
|
|
|
|
int error = WSAGetLastError();
|
|
if (error == WSAEWOULDBLOCK) return true;
|
|
if (error == WSAEINTR) continue;
|
|
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void NetworkPlayerLinux::CloseTransport(bool isServerConnection) {
|
|
FlushSendQueue();
|
|
|
|
Socket* gameSocket = nullptr;
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_transportMutex);
|
|
SOCKET socket = m_transportSocket.exchange(INVALID_SOCKET);
|
|
bool wasConnected = m_connected.exchange(false);
|
|
if (socket != INVALID_SOCKET) {
|
|
shutdown(socket, SD_BOTH);
|
|
closesocket(socket);
|
|
}
|
|
|
|
if (wasConnected) {
|
|
gameSocket = m_gameSocket;
|
|
m_gameSocket = nullptr;
|
|
}
|
|
}
|
|
|
|
if (gameSocket != nullptr) {
|
|
InputStream* input = gameSocket->getInputStream(isServerConnection);
|
|
Socket::SocketOutputStream* output = gameSocket->getOutputStream(isServerConnection);
|
|
if (input != nullptr) input->close();
|
|
if (output != nullptr) output->close();
|
|
gameSocket->close(isServerConnection);
|
|
}
|
|
}
|
|
|
|
void NetworkPlayerLinux::RefreshUID() {
|
|
if (m_isHost) {
|
|
m_uid = Win64Xuid::GetLegacyEmbeddedHostXuid();
|
|
} else if (m_userIndex >= 0 && m_userIndex < XUSER_MAX_COUNT) {
|
|
m_uid = Win64Xuid::DeriveXuidForPad(Win64_GetActiveXuid(), m_userIndex);
|
|
} else {
|
|
m_uid = Win64Xuid::GetLegacyEmbeddedBaseXuid() + m_smallId;
|
|
}
|
|
if (m_smallId < MINECRAFT_NET_MAX_PLAYERS)
|
|
IQNet::m_player[m_smallId].m_resolvedXuid = m_uid;
|
|
}
|
|
|
|
#endif
|