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