2
0

TCPSocket.h 2.6 KB

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