2024-12-03 10:36:06 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/**
|
|
|
|
Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Frame.h"
|
|
|
|
#include "wrap_socket.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
2024-12-09 09:41:04 +08:00
|
|
|
namespace Beckhoff
|
2024-12-03 10:36:06 +08:00
|
|
|
{
|
|
|
|
using AddressList = std::unique_ptr<struct addrinfo, void (*)(struct addrinfo*)>;
|
|
|
|
/**
|
|
|
|
* Splits the provided host string into host and port. If no port was found
|
|
|
|
* in the host string, the provided default port is used.
|
|
|
|
*/
|
2024-12-09 09:41:04 +08:00
|
|
|
AddressList GetHostAddresses(const std::string& host, const std::string& port = {}, bool* found = nullptr);
|
2024-12-03 10:36:06 +08:00
|
|
|
|
|
|
|
struct IpV4 {
|
|
|
|
const uint32_t value;
|
|
|
|
IpV4(const std::string& addr);
|
|
|
|
IpV4(uint32_t __val);
|
|
|
|
bool operator<(const IpV4& ref) const;
|
|
|
|
bool operator==(const IpV4& ref) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Socket {
|
2024-12-09 09:41:04 +08:00
|
|
|
Frame& read(Frame& frame, timeval* timeout);
|
|
|
|
size_t read(uint8_t* buffer, size_t maxBytes, timeval* timeout);
|
|
|
|
size_t write(const Frame& frame);
|
2024-12-03 10:36:06 +08:00
|
|
|
void Shutdown();
|
2024-12-09 09:41:04 +08:00
|
|
|
bool IsValid() const;
|
|
|
|
bool IsConnected() const;
|
|
|
|
int GetError() const;
|
2024-12-03 10:36:06 +08:00
|
|
|
|
|
|
|
struct TimeoutEx : std::runtime_error {
|
|
|
|
TimeoutEx(const char* _Message) : std::runtime_error(_Message)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2024-12-09 09:41:04 +08:00
|
|
|
/**
|
|
|
|
* Confirm if this Socket is connected to one of the target addresses.
|
|
|
|
* @param[in] targetAddresses pointer to a previously allocated list of
|
|
|
|
* "struct addrinfo" returned by getaddrinfo(3).
|
|
|
|
* @return true, this connection can be used to reach one of the targetAddresses.
|
|
|
|
*/
|
|
|
|
bool IsConnectedTo(const struct addrinfo* targetAddresses) const;
|
|
|
|
|
2024-12-03 10:36:06 +08:00
|
|
|
protected:
|
2024-12-11 13:33:15 +08:00
|
|
|
bool m_Connected;
|
2024-12-03 10:36:06 +08:00
|
|
|
int m_WSAInitialized;
|
2024-12-09 09:41:04 +08:00
|
|
|
int m_LastError;
|
2024-12-03 10:36:06 +08:00
|
|
|
SOCKET m_Socket;
|
|
|
|
sockaddr_storage m_SockAddress;
|
|
|
|
const sockaddr* const m_DestAddr;
|
|
|
|
socklen_t m_DestAddrLen;
|
2024-12-09 09:41:04 +08:00
|
|
|
sockaddr_in m_HostAddr;
|
2024-12-10 10:14:40 +08:00
|
|
|
int m_type;
|
2024-12-03 10:36:06 +08:00
|
|
|
|
|
|
|
Socket(const struct addrinfo* host, int type);
|
|
|
|
~Socket();
|
2024-12-09 09:41:04 +08:00
|
|
|
bool Select(timeval* timeout);
|
2024-12-03 10:36:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TcpSocket : Socket {
|
|
|
|
TcpSocket(const struct addrinfo* host);
|
2024-12-09 09:41:04 +08:00
|
|
|
uint32_t GetLocalSockAddr() const;
|
|
|
|
uint32_t GetHostSockAddr() const;
|
2024-12-03 10:36:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UdpSocket : Socket {
|
|
|
|
UdpSocket(const struct addrinfo* host);
|
|
|
|
};
|
2024-12-09 09:41:04 +08:00
|
|
|
}
|