TCPSocket.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef BELL_BASIC_SOCKET_H
  2. #define BELL_BASIC_SOCKET_H
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "BellSocket.h"
  12. #ifdef _WIN32
  13. #include <winsock2.h>
  14. #include <ws2tcpip.h>
  15. #include "win32shim.h"
  16. #else
  17. #include <netdb.h>
  18. #include <netinet/in.h>
  19. #include <netinet/tcp.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include <unistd.h>
  23. #ifdef __sun
  24. #include <sys/filio.h>
  25. #endif
  26. #endif
  27. #include <BellLogger.h>
  28. #include <fstream>
  29. #include <sstream>
  30. namespace bell {
  31. class TCPSocket : public bell::Socket {
  32. private:
  33. int sockFd;
  34. bool isClosed = true;
  35. public:
  36. TCPSocket(){};
  37. ~TCPSocket() { close(); };
  38. int getFd() { return sockFd; }
  39. void open(const std::string& host, uint16_t port) {
  40. int err;
  41. int domain = AF_INET;
  42. int socketType = SOCK_STREAM;
  43. struct addrinfo hints {
  44. }, *addr;
  45. //fine-tune hints according to which socket you want to open
  46. hints.ai_family = domain;
  47. hints.ai_socktype = socketType;
  48. hints.ai_protocol =
  49. IPPROTO_IP; // no enum : possible value can be read in /etc/protocols
  50. hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
  51. // BELL_LOG(info, "http", "%s %d", host.c_str(), port);
  52. char portStr[6];
  53. sprintf(portStr, "%u", port);
  54. err = getaddrinfo(host.c_str(), portStr, &hints, &addr);
  55. if (err != 0) {
  56. throw std::runtime_error("Resolve failed");
  57. }
  58. sockFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  59. err = connect(sockFd, addr->ai_addr, addr->ai_addrlen);
  60. if (err < 0) {
  61. close();
  62. BELL_LOG(error, "http", "Could not connect to %s. Error %d", host.c_str(),
  63. errno);
  64. throw std::runtime_error("Resolve failed");
  65. }
  66. int flag = 1;
  67. setsockopt(sockFd, /* socket affected */
  68. IPPROTO_TCP, /* set option at TCP level */
  69. TCP_NODELAY, /* name of option */
  70. (char*)&flag, /* the cast is historical cruft */
  71. sizeof(int)); /* length of option value */
  72. freeaddrinfo(addr);
  73. isClosed = false;
  74. }
  75. size_t read(uint8_t* buf, size_t len) {
  76. return recv(sockFd, (char*)buf, len, 0);
  77. }
  78. size_t write(uint8_t* buf, size_t len) {
  79. return send(sockFd, (char*)buf, len, 0);
  80. }
  81. size_t poll() {
  82. #ifdef _WIN32
  83. unsigned long value;
  84. ioctlsocket(sockFd, FIONREAD, &value);
  85. #else
  86. int value;
  87. ioctl(sockFd, FIONREAD, &value);
  88. #endif
  89. return value;
  90. }
  91. bool isOpen() {
  92. return !isClosed;
  93. }
  94. void close() {
  95. if (!isClosed) {
  96. #ifdef _WIN32
  97. closesocket(sockFd);
  98. #else
  99. ::close(sockFd);
  100. #endif
  101. sockFd = -1;
  102. isClosed = true;
  103. }
  104. }
  105. };
  106. } // namespace bell
  107. #endif