util.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef __UTIL_H__
  2. #define __UTIL_H__
  3. #if defined(unix) || defined(__APPLE__)
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #define SOCKETTYPE long
  9. #define SOCKETFAIL(a) ((a) < 0)
  10. #define INVSOCK -1
  11. #define INVINETADDR -1
  12. #define CLOSESOCKET close
  13. #define SOCKERRMSG strerror(errno)
  14. static inline bool sock_blocks(void)
  15. {
  16. return (errno == EAGAIN || errno == EWOULDBLOCK);
  17. }
  18. #elif defined WIN32
  19. #include <ws2tcpip.h>
  20. #include <winsock2.h>
  21. #define SOCKETTYPE SOCKET
  22. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  23. #define INVSOCK INVALID_SOCKET
  24. #define INVINETADDR INADDR_NONE
  25. #define CLOSESOCKET closesocket
  26. extern char *WSAErrorMsg(void);
  27. #define SOCKERRMSG WSAErrorMsg()
  28. static inline bool sock_blocks(void)
  29. {
  30. return (WSAGetLastError() == WSAEWOULDBLOCK);
  31. }
  32. #ifndef SHUT_RDWR
  33. #define SHUT_RDWR SD_BOTH
  34. #endif
  35. #ifndef in_addr_t
  36. #define in_addr_t uint32_t
  37. #endif
  38. #endif
  39. #if JANSSON_MAJOR_VERSION >= 2
  40. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  41. #else
  42. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  43. #endif
  44. struct thr_info;
  45. struct pool;
  46. enum dev_reason;
  47. struct cgpu_info;
  48. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  49. void thr_info_freeze(struct thr_info *thr);
  50. void thr_info_cancel(struct thr_info *thr);
  51. void nmsleep(unsigned int msecs);
  52. void nusleep(unsigned int usecs);
  53. void cgtime(struct timeval *tv);
  54. void subtime(struct timeval *a, struct timeval *b);
  55. void addtime(struct timeval *a, struct timeval *b);
  56. bool time_more(struct timeval *a, struct timeval *b);
  57. bool time_less(struct timeval *a, struct timeval *b);
  58. void copy_time(struct timeval *dest, const struct timeval *src);
  59. double us_tdiff(struct timeval *end, struct timeval *start);
  60. double tdiff(struct timeval *end, struct timeval *start);
  61. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  62. bool sock_full(struct pool *pool);
  63. char *recv_line(struct pool *pool);
  64. bool parse_method(struct pool *pool, char *s);
  65. bool extract_sockaddr(struct pool *pool, char *url);
  66. bool auth_stratum(struct pool *pool);
  67. bool initiate_stratum(struct pool *pool);
  68. bool restart_stratum(struct pool *pool);
  69. void suspend_stratum(struct pool *pool);
  70. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  71. void *realloc_strcat(char *ptr, char *s);
  72. void *str_text(char *ptr);
  73. void RenameThread(const char* name);
  74. /* Align a size_t to 4 byte boundaries for fussy arches */
  75. static inline void align_len(size_t *len)
  76. {
  77. if (*len % 4)
  78. *len += 4 - (*len % 4);
  79. }
  80. #endif /* __UTIL_H__ */