TCPSocket.h 2.9 KB

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