socket_lidar.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #ifdef WIN32
  3. #ifndef WIN32_LEAN_AND_MEAN
  4. #define WIN32_LEAN_AND_MEAN
  5. #endif
  6. #include <WS2tcpip.h>
  7. #include <WinSock2.h>
  8. #define socketerrno WSAGetLastError()
  9. #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS
  10. #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
  11. #ifndef _SSIZE_T_DEFINED
  12. typedef int ssize_t;
  13. #define _SSIZE_T_DEFINED
  14. #endif
  15. #ifndef _SOCKET_T_DEFINED
  16. typedef SOCKET socket_t;
  17. #define _SOCKET_T_DEFINED
  18. #endif
  19. #else
  20. #include <unistd.h>
  21. #include <arpa/inet.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netinet/tcp.h>
  26. #include <fcntl.h>
  27. #define socketerrno errno
  28. #define SOCKET_EAGAIN_EINPROGRESS EAGAIN
  29. #define SOCKET_EWOULDBLOCK EWOULDBLOCK
  30. #define INVALID_SOCKET -1
  31. #define SOCKET_ERROR -1
  32. #ifndef _SOCKET_T_DEFINED
  33. typedef int socket_t;
  34. #define _SOCKET_T_DEFINED
  35. #endif
  36. #endif
  37. #include <thread>
  38. #include <mutex>
  39. #include <unordered_map>
  40. #include "../common/comm.h"
  41. #include "../common/radar_deal.h"
  42. class CMessageQueue;
  43. class SocketLidar
  44. {
  45. public:
  46. SocketLidar(CMessageQueue* n);
  47. bool Start(int32_t port);
  48. void Run();
  49. void Stop();
  50. RadarDeal radarDeal;
  51. private:
  52. socket_t _fd;
  53. std::thread _thread;
  54. bool _run;
  55. std::mutex _lock;
  56. std::unordered_map<int32_t, cannet_frame> _message;
  57. CMessageQueue* _canNotify;
  58. sockaddr_in _canaddr;
  59. };