You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1 KiB

4 months ago
  1. /*
  2. * IXSocket.h
  3. * Author: Benjamin Sergeant
  4. * Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
  5. */
  6. #pragma once
  7. #include <atomic>
  8. #include <functional>
  9. #include <memory>
  10. #include <mutex>
  11. #include <string>
  12. #ifdef _WIN32
  13. #include <basetsd.h>
  14. #ifdef _MSC_VER
  15. typedef SSIZE_T ssize_t;
  16. #endif
  17. #endif
  18. #include "IXCancellationRequest.h"
  19. #include "IXProgressCallback.h"
  20. #include "IXSelectInterrupt.h"
  21. namespace ix
  22. {
  23. enum class PollResultType
  24. {
  25. ReadyForRead = 0,
  26. ReadyForWrite = 1,
  27. Timeout = 2,
  28. Error = 3,
  29. SendRequest = 4,
  30. CloseRequest = 5
  31. };
  32. class Socket
  33. {
  34. public:
  35. Socket(int fd = -1);
  36. virtual ~Socket();
  37. bool init(std::string& errorMsg);
  38. // Functions to check whether there is activity on the socket
  39. PollResultType poll(int timeoutMs = kDefaultPollTimeout);
  40. bool wakeUpFromPoll(uint64_t wakeUpCode);
  41. bool isWakeUpFromPollSupported();
  42. PollResultType isReadyToWrite(int timeoutMs);
  43. PollResultType isReadyToRead(int timeoutMs);
  44. // Virtual methods
  45. virtual bool accept(std::string& errMsg);
  46. virtual bool connect(const std::string& host,
  47. int port,
  48. std::string& errMsg,
  49. const CancellationRequest& isCancellationRequested);
  50. virtual void close();
  51. virtual ssize_t send(char* buffer, size_t length);
  52. ssize_t send(const std::string& buffer);
  53. virtual ssize_t recv(void* buffer, size_t length);
  54. // Blocking and cancellable versions, working with socket that can be set
  55. // to non blocking mode. Used during HTTP upgrade.
  56. bool readByte(void* buffer, const CancellationRequest& isCancellationRequested);
  57. bool writeBytes(const std::string& str, const CancellationRequest& isCancellationRequested);
  58. std::pair<bool, std::string> readLine(const CancellationRequest& isCancellationRequested);
  59. std::pair<bool, std::string> readBytes(size_t length,
  60. const OnProgressCallback& onProgressCallback,
  61. const OnChunkCallback& onChunkCallback,
  62. const CancellationRequest& isCancellationRequested);
  63. static int getErrno();
  64. static bool isWaitNeeded();
  65. static void closeSocket(int fd);
  66. static PollResultType poll(bool readyToRead,
  67. int timeoutMs,
  68. int sockfd,
  69. const SelectInterruptPtr& selectInterrupt);
  70. protected:
  71. std::atomic<int> _sockfd;
  72. std::mutex _socketMutex;
  73. static bool readSelectInterruptRequest(const SelectInterruptPtr& selectInterrupt,
  74. PollResultType* pollResult);
  75. private:
  76. static const int kDefaultPollTimeout;
  77. static const int kDefaultPollNoTimeout;
  78. SelectInterruptPtr _selectInterrupt;
  79. };
  80. } // namespace ix