util.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 cgtime(struct timeval *tv);
  53. void subtime(struct timeval *a, struct timeval *b);
  54. void addtime(struct timeval *a, struct timeval *b);
  55. bool time_more(struct timeval *a, struct timeval *b);
  56. bool time_less(struct timeval *a, struct timeval *b);
  57. void copy_time(struct timeval *dest, const struct timeval *src);
  58. double us_tdiff(struct timeval *end, struct timeval *start);
  59. double tdiff(struct timeval *end, struct timeval *start);
  60. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  61. bool sock_full(struct pool *pool);
  62. char *recv_line(struct pool *pool);
  63. bool parse_method(struct pool *pool, char *s);
  64. bool extract_sockaddr(struct pool *pool, char *url);
  65. bool auth_stratum(struct pool *pool);
  66. bool initiate_stratum(struct pool *pool);
  67. bool restart_stratum(struct pool *pool);
  68. void suspend_stratum(struct pool *pool);
  69. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  70. void *realloc_strcat(char *ptr, char *s);
  71. void *str_text(char *ptr);
  72. void RenameThread(const char* name);
  73. /* Align a size_t to 4 byte boundaries for fussy arches */
  74. static inline void align_len(size_t *len)
  75. {
  76. if (*len % 4)
  77. *len += 4 - (*len % 4);
  78. }
  79. #endif /* __UTIL_H__ */