1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-03 10:07:45 +01:00
libtgvoip/os/posix/NetworkSocketPosix.h

84 lines
2.7 KiB
C
Raw Normal View History

//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#ifndef LIBTGVOIP_NETWORKSOCKETPOSIX_H
#define LIBTGVOIP_NETWORKSOCKETPOSIX_H
2020-01-22 12:43:51 +01:00
#include "../../controller/net/NetworkSocket.h"
#include "../../tools/Buffers.h"
#include <vector>
2020-03-10 20:31:58 +01:00
#include <mutex>
#include <sys/select.h>
#include <pthread.h>
2020-01-24 20:26:34 +01:00
namespace tgvoip
{
class SocketSelectCancellerPosix : public SocketSelectCanceller
{
friend class NetworkSocketPosix;
public:
SocketSelectCancellerPosix();
virtual ~SocketSelectCancellerPosix();
virtual void CancelSelect() override;
2020-01-24 20:26:34 +01:00
private:
int pipeRead;
int pipeWrite;
};
2020-01-24 20:26:34 +01:00
class NetworkSocketPosix : public NetworkSocket
{
public:
NetworkSocketPosix(NetworkProtocol protocol);
2020-03-10 20:31:58 +01:00
virtual ~NetworkSocketPosix() override;
2020-03-24 12:15:04 +01:00
virtual void Send(NetworkPacket &&packet) override;
virtual NetworkPacket Receive(size_t maxLen) override;
virtual void Open() override;
virtual void Close() override;
virtual void Connect(const NetworkAddress address, uint16_t port) override;
2020-01-24 20:26:34 +01:00
virtual std::string GetLocalInterfaceInfo(NetworkAddress *v4addr, NetworkAddress *v6addr) override;
virtual void OnActiveInterfaceChanged() override;
virtual uint16_t GetLocalPort() override;
static std::string V4AddressToString(uint32_t address);
static std::string V6AddressToString(const unsigned char address[16]);
static uint32_t StringToV4Address(std::string address);
2020-01-24 20:26:34 +01:00
static void StringToV6Address(std::string address, unsigned char *out);
static NetworkAddress ResolveDomainName(std::string name);
2020-01-24 20:26:34 +01:00
static bool Select(std::vector<std::shared_ptr<NetworkSocket>> &readFds, std::vector<std::shared_ptr<NetworkSocket>> &writeFds, std::vector<std::shared_ptr<NetworkSocket>> &errorFds, const std::unique_ptr<SocketSelectCanceller> &canceller);
virtual NetworkAddress GetConnectedAddress() override;
virtual uint16_t GetConnectedPort() override;
virtual void SetTimeouts(int sendTimeout, int recvTimeout) override;
virtual bool OnReadyToSend() override;
protected:
virtual void SetMaxPriority() override;
private:
2020-01-24 20:26:34 +01:00
static int GetDescriptorFromSocket(NetworkSocket *socket);
static int GetDescriptorFromSocket(const std::shared_ptr<NetworkSocket> &socket);
std::atomic<int> fd;
2020-03-10 20:31:58 +01:00
std::mutex m_fd;
bool needUpdateNat64Prefix;
bool nat64Present;
double switchToV6at;
2020-03-10 20:31:58 +01:00
std::atomic<bool> isV4Available;
std::atomic<bool> closing;
2020-01-24 20:26:34 +01:00
NetworkAddress tcpConnectedAddress = NetworkAddress::Empty();
uint16_t tcpConnectedPort;
2020-01-24 20:26:34 +01:00
NetworkPacket pendingOutgoingPacket = NetworkPacket::Empty();
Buffer recvBuffer = Buffer(2048);
};
2020-01-24 20:26:34 +01:00
} // namespace tgvoip
#endif //LIBTGVOIP_NETWORKSOCKETPOSIX_H