Telegram-Android/TMessagesProj/jni/tgnet/ConnectionSocket.h

96 lines
2.7 KiB
C
Raw Permalink Normal View History

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