ConnectionSocket.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #ifndef CONNECTIONSOCKET_H
  9. #define CONNECTIONSOCKET_H
  10. #include <sys/epoll.h>
  11. #include <netinet/in.h>
  12. #include <string>
  13. class NativeByteBuffer;
  14. class ConnectionsManager;
  15. class ByteStream;
  16. class EventObject;
  17. class ByteArray;
  18. class ConnectionSocket {
  19. public:
  20. ConnectionSocket(int32_t instance);
  21. virtual ~ConnectionSocket();
  22. void writeBuffer(uint8_t *data, uint32_t size);
  23. void writeBuffer(NativeByteBuffer *buffer);
  24. void openConnection(std::string address, uint16_t port, std::string secret, bool ipv6, int32_t networkType);
  25. void setTimeout(time_t timeout);
  26. time_t getTimeout();
  27. bool isDisconnected();
  28. void dropConnection();
  29. void setOverrideProxy(std::string address, uint16_t port, std::string username, std::string password, std::string secret);
  30. void onHostNameResolved(std::string host, std::string ip, bool ipv6);
  31. protected:
  32. int32_t instanceNum;
  33. void onEvent(uint32_t events);
  34. bool checkTimeout(int64_t now);
  35. void resetLastEventTime();
  36. bool hasTlsHashMismatch();
  37. virtual void onReceivedData(NativeByteBuffer *buffer) = 0;
  38. virtual void onDisconnected(int32_t reason, int32_t error) = 0;
  39. virtual void onConnected() = 0;
  40. virtual bool hasPendingRequests() = 0;
  41. std::string overrideProxyUser = "";
  42. std::string overrideProxyPassword = "";
  43. std::string overrideProxyAddress = "";
  44. std::string overrideProxySecret = "";
  45. uint16_t overrideProxyPort = 1080;
  46. private:
  47. ByteStream *outgoingByteStream = nullptr;
  48. struct epoll_event eventMask;
  49. struct sockaddr_in socketAddress;
  50. struct sockaddr_in6 socketAddress6;
  51. int socketFd = -1;
  52. time_t timeout = 12;
  53. bool onConnectedSent = false;
  54. int64_t lastEventTime = 0;
  55. EventObject *eventObject;
  56. int32_t currentNetworkType;
  57. bool isIpv6;
  58. std::string currentAddress;
  59. uint16_t currentPort;
  60. std::string waitingForHostResolve;
  61. bool adjustWriteOpAfterResolve;
  62. std::string currentSecret;
  63. std::string currentSecretDomain;
  64. bool tlsHashMismatch = false;
  65. bool tlsBufferSized = true;
  66. NativeByteBuffer *tlsBuffer = nullptr;
  67. ByteArray *tempBuffer = nullptr;
  68. size_t bytesRead = 0;
  69. int8_t tlsState = 0;
  70. uint8_t proxyAuthState;
  71. int32_t checkSocketError(int32_t *error);
  72. void closeSocket(int32_t reason, int32_t error);
  73. void openConnectionInternal(bool ipv6);
  74. void adjustWriteOp();
  75. friend class EventObject;
  76. friend class ConnectionsManager;
  77. friend class Connection;
  78. };
  79. #endif