Nagram/TMessagesProj/jni/tgnet/ConnectionSocket.h

96 lines
2.7 KiB
C
Raw Normal View History

2015-09-24 20:52:02 +00:00
/*
2018-07-30 02:07:02 +00:00
* This is the source code of tgnet library v. 1.1
2015-09-24 20:52:02 +00:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 02:07:02 +00:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 20:52:02 +00:00
*/
#ifndef CONNECTIONSOCKET_H
#define CONNECTIONSOCKET_H
#include <sys/epoll.h>
#include <netinet/in.h>
#include <string>
class NativeByteBuffer;
class ConnectionsManager;
class ByteStream;
class EventObject;
2019-08-21 23:53:26 +00:00
class ByteArray;
2015-09-24 20:52:02 +00:00
class ConnectionSocket {
public:
2018-07-30 02:07:02 +00:00
ConnectionSocket(int32_t instance);
2015-09-24 20:52:02 +00:00
virtual ~ConnectionSocket();
2017-07-08 16:32:04 +00:00
void writeBuffer(uint8_t *data, uint32_t size);
2015-09-24 20:52:02 +00:00
void writeBuffer(NativeByteBuffer *buffer);
2019-08-21 23:53:26 +00:00
void openConnection(std::string address, uint16_t port, std::string secret, bool ipv6, int32_t networkType);
2015-09-24 20:52:02 +00:00
void setTimeout(time_t timeout);
2017-07-08 16:32:04 +00:00
time_t getTimeout();
2015-09-24 20:52:02 +00:00
bool isDisconnected();
void dropConnection();
2018-07-30 02:07:02 +00:00
void setOverrideProxy(std::string address, uint16_t port, std::string username, std::string password, std::string secret);
2019-05-14 12:08:05 +00:00
void onHostNameResolved(std::string host, std::string ip, bool ipv6);
2015-09-24 20:52:02 +00:00
protected:
2018-07-30 02:07:02 +00:00
int32_t instanceNum;
2015-09-24 20:52:02 +00:00
void onEvent(uint32_t events);
2020-01-05 11:50:11 +00:00
bool checkTimeout(int64_t now);
2019-05-14 12:08:05 +00:00
void resetLastEventTime();
2019-08-21 23:53:26 +00:00
bool hasTlsHashMismatch();
2015-09-24 20:52:02 +00:00
virtual void onReceivedData(NativeByteBuffer *buffer) = 0;
2018-07-30 02:07:02 +00:00
virtual void onDisconnected(int32_t reason, int32_t error) = 0;
2015-09-24 20:52:02 +00:00
virtual void onConnected() = 0;
2019-01-23 17:03:33 +00:00
virtual bool hasPendingRequests() = 0;
2015-09-24 20:52:02 +00:00
2021-10-31 13:09:32 +00:00
std::string overrideProxyUser = "";
std::string overrideProxyPassword = "";
std::string overrideProxyAddress = "";
std::string overrideProxySecret = "";
uint16_t overrideProxyPort = 1080;
2015-09-24 20:52:02 +00:00
private:
ByteStream *outgoingByteStream = nullptr;
struct epoll_event eventMask;
struct sockaddr_in socketAddress;
struct sockaddr_in6 socketAddress6;
int socketFd = -1;
2017-07-08 16:32:04 +00:00
time_t timeout = 12;
2015-09-24 20:52:02 +00:00
bool onConnectedSent = false;
int64_t lastEventTime = 0;
EventObject *eventObject;
2017-03-30 23:58:05 +00:00
int32_t currentNetworkType;
2017-07-08 16:32:04 +00:00
bool isIpv6;
std::string currentAddress;
uint16_t currentPort;
2019-05-14 12:08:05 +00:00
std::string waitingForHostResolve;
bool adjustWriteOpAfterResolve;
2019-08-21 23:53:26 +00:00
std::string currentSecret;
std::string currentSecretDomain;
bool tlsHashMismatch = false;
2019-09-10 10:56:11 +00:00
bool tlsBufferSized = true;
2019-08-21 23:53:26 +00:00
NativeByteBuffer *tlsBuffer = nullptr;
ByteArray *tempBuffer = nullptr;
size_t bytesRead = 0;
int8_t tlsState = 0;
2018-07-30 02:07:02 +00:00
2017-07-08 16:32:04 +00:00
uint8_t proxyAuthState;
2015-09-24 20:52:02 +00:00
2018-07-30 02:07:02 +00:00
int32_t checkSocketError(int32_t *error);
void closeSocket(int32_t reason, int32_t error);
2019-05-14 12:08:05 +00:00
void openConnectionInternal(bool ipv6);
2015-09-24 20:52:02 +00:00
void adjustWriteOp();
friend class EventObject;
friend class ConnectionsManager;
2018-07-30 02:07:02 +00:00
friend class Connection;
2015-09-24 20:52:02 +00:00
};
#endif