Slamtec / rplidar_sdk

Open source SDK for Slamtec RPLIDAR series products
BSD 2-Clause "Simplified" License
425 stars 253 forks source link

Missing clearRxCache Implementation in DGramSocketImpl for macOS Causes Compilation Error #122

Open Eotel opened 12 months ago

Eotel commented 12 months ago

Description

Compilation fails on macOS due to the DGramSocketImpl being an abstract class. The issue stems from the fact that the pure virtual function clearRxCache, declared in the base class DGramSocket, is not implemented in DGramSocketImpl for macOS. Interestingly, this method is implemented in the Linux and Windows versions (rplidar_sdk/sdk/src/arch/linux/net_socket.cpp and rplidar_sdk/sdk/src/arch/win32/net_socket.cpp), but is missing from the macOS version (rplidar_sdk/sdk/src/arch/macOS/net_socket.cpp).

Solution

Implement the clearRxCache method in DGramSocketImpl for macOS. The following code snippet clears the receive cache using select and recv system calls.

virtual u_result clearRxCache()
{
    timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    fd_set rdset;
    FD_ZERO(&rdset);
    FD_SET(_socket_fd, &rdset);

    int res = -1;
    char recv_data[2];
    memset(recv_data, 0, sizeof(recv_data));
    while (true) {
        res = select(FD_SETSIZE, &rdset, nullptr, nullptr, &tv);
        if (res == 0) break;
        recv(_socket_fd, recv_data, 1, 0);
    }
    return RESULT_OK;
}

With this implementation, the class is no longer abstract, and the code successfully compiles on macOS.

Additional Notes

If the method was deliberately not implemented for macOS, insights into the reasoning would be appreciated. This will help the community understand any platform-specific constraints or decisions.

JetForMe commented 11 months ago

I just ran into this too. Thanks for the fix!