2
0

TCPSocket.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. void open(const std::string& host, uint16_t port) {
  39. int err;
  40. int domain = AF_INET;
  41. int socketType = SOCK_STREAM;
  42. struct addrinfo hints {
  43. }, *addr;
  44. //fine-tune hints according to which socket you want to open
  45. hints.ai_family = domain;
  46. hints.ai_socktype = socketType;
  47. hints.ai_protocol =
  48. IPPROTO_IP; // no enum : possible value can be read in /etc/protocols
  49. hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
  50. // BELL_LOG(info, "http", "%s %d", host.c_str(), port);
  51. char portStr[6];
  52. sprintf(portStr, "%u", port);
  53. err = getaddrinfo(host.c_str(), portStr, &hints, &addr);
  54. if (err != 0) {
  55. throw std::runtime_error("Resolve failed");
  56. }
  57. sockFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  58. err = connect(sockFd, addr->ai_addr, addr->ai_addrlen);
  59. if (err < 0) {
  60. close();
  61. BELL_LOG(error, "http", "Could not connect to %s. Error %d", host.c_str(),
  62. errno);
  63. throw std::runtime_error("Resolve failed");
  64. }
  65. int flag = 1;
  66. setsockopt(sockFd, /* socket affected */
  67. IPPROTO_TCP, /* set option at TCP level */
  68. TCP_NODELAY, /* name of option */
  69. (char*)&flag, /* the cast is historical cruft */
  70. sizeof(int)); /* length of option value */
  71. freeaddrinfo(addr);
  72. isClosed = false;
  73. }
  74. size_t read(uint8_t* buf, size_t len) {
  75. return recv(sockFd, (char*)buf, len, 0);
  76. }
  77. size_t write(uint8_t* buf, size_t len) {
  78. return send(sockFd, (char*)buf, len, 0);
  79. }
  80. size_t poll() {
  81. #ifdef _WIN32
  82. unsigned long value;
  83. ioctlsocket(sockFd, FIONREAD, &value);
  84. #else
  85. int value;
  86. ioctl(sockFd, FIONREAD, &value);
  87. #endif
  88. return value;
  89. }
  90. bool isOpen() { return !isClosed; }
  91. void close() {
  92. if (!isClosed) {
  93. #ifdef _WIN32
  94. closesocket(sockFd);
  95. #else
  96. ::close(sockFd);
  97. #endif
  98. sockFd = -1;
  99. isClosed = true;
  100. }
  101. }
  102. };
  103. } // namespace bell
  104. #endif