Files
SimpleRemoter/client/IOCPUDPClient.cpp
T
yuanyuanxianganddeepseek-flash 4456467c50 Fix(client): build on macOS after the pthread_t cast
The macOS client failed to compile at client/IOCPClient.cpp:788:
static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'uintptr_t'
is not allowed. Darwin defines pthread_t as a pointer, while Linux and
Android define it as an integer, so routing it through uintptr_t refused
to build there.

On POSIX the stored value is only a non-null "a worker thread exists"
marker: CloseHandle and SAFE_CLOSE_HANDLE are no-ops, and nothing ever
dereferences it. reinterpret_cast<HANDLE> accepts both shapes — integer
to pointer on Linux/Android, pointer to pointer on macOS — so the same
line now compiles everywhere. IOCPUDPClient.cpp had the identical cast
and got the same one-line change; it is not in any current build list,
but it would hit the same error if it were.

Verified with a green Linux client rebuild (integral pthread_t). Not yet
built on a real Mac.

Co-Authored-By: deepseek-flash
2026-11-14 12:19:44 +01:00

76 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "IOCPUDPClient.h"
BOOL IOCPUDPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
{
if (szServerIP != NULL && uPort != 0) {
SetServerAddress(szServerIP, uPort);
}
m_sCurIP = m_Domain.SelectIP();
unsigned short port = m_nHostPort;
// 创建 UDP socket
m_sClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_sClientSocket == INVALID_SOCKET) {
Mprintf("Failed to create UDP socket\n");
return FALSE;
}
// 初始化服务器地址结构
memset(&m_ServerAddr, 0, sizeof(m_ServerAddr));
m_ServerAddr.sin_family = AF_INET;
m_ServerAddr.sin_port = htons(port);
#ifdef _WIN32
m_ServerAddr.sin_addr.S_un.S_addr = inet_addr(m_sCurIP.c_str());
#else
if (inet_pton(AF_INET, m_sCurIP.c_str(), &m_ServerAddr.sin_addr) <= 0) {
Mprintf("Invalid address or address not supported\n");
closesocket(m_sClientSocket);
m_sClientSocket = INVALID_SOCKET;
return FALSE;
}
#endif
// UDP不调用 connect(),也不设置 TCP keep-alive 相关选项
Mprintf("UDP client socket created and ready to send.\n");
m_bConnected = TRUE;
// 创建工作线程(如果需要)
if (m_hWorkThread == NULL) {
#ifdef _WIN32
m_bIsRunning = TRUE;
m_hWorkThread = (HANDLE)__CreateThread(NULL, 0, WorkThreadProc, (LPVOID)this, 0, NULL);
m_bWorkThread = m_hWorkThread ? S_RUN : S_STOP;
m_bIsRunning = m_hWorkThread ? TRUE : FALSE;
#else
pthread_t id = 0;
int ret = (HANDLE)pthread_create(&id, nullptr, (void* (*)(void*))IOCPClient::WorkThreadProc, this);
if (ret == 0) {
m_bWorkThread = S_RUN;
m_bIsRunning = TRUE;
// 同 IOCPClient:macOS 的 pthread_t 是 _opaque_pthread_t*,不能经
// static_cast<uintptr_t> 中转,reinterpret_cast 对整型/指针两种实现都成立。
m_hWorkThread = reinterpret_cast<HANDLE>(id);
}
#endif
}
return TRUE;
}
int IOCPUDPClient::ReceiveData(char* buffer, int bufSize, int flags)
{
sockaddr_in fromAddr;
int fromLen = sizeof(fromAddr);
return recvfrom(m_sClientSocket, buffer, bufSize - 1, flags, (sockaddr*)&fromAddr, &fromLen);
}
int IOCPUDPClient::SendTo(const char* buf, int len, int flags)
{
if (len > 1200) {
Mprintf("UDP large packet may lost: %d bytes\n", len);
}
return ::sendto(m_sClientSocket, buf, len, flags, (sockaddr*)&m_ServerAddr, sizeof(m_ServerAddr));
}