File: udp_socket.h

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (74 lines) | stat: -rw-r--r-- 1,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * (c) 2025 by Mega Limited, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#ifndef MEGA_UDP_SOCKET_H
#define MEGA_UDP_SOCKET_H

#include <chrono>
#include <memory>
#include <stdint.h>
#include <string>
#include <variant>

struct sockaddr_in;
struct sockaddr_in6;
struct sockaddr;

namespace mega
{

class UdpSocket final
{
public:
    UdpSocket(const std::string& remoteIP, int remotePort);
    ~UdpSocket();
    bool isIPv4() const;

    struct Communication
    {
        int code{};
        std::string message;
    };

    Communication sendSyncMessage(const std::string& message);
    Communication receiveSyncMessage(const std::chrono::high_resolution_clock::time_point& timeout);

private:
    bool createRemoteAddress(const std::string& remoteIP, int remotePort);
    sockaddr* getSockaddr();

    int mInetType{};
    std::unique_ptr<std::variant<sockaddr_in, sockaddr_in6>> mRemoteAddress;
    int mSocket{};

    //
    // OS-specific implementations
    //
    bool initializeSocketSupport();
    void cleanupSocketSupport();
    bool openNonblockingSocket();
    intmax_t sendtoWrapper(const std::string& message);
    static bool noDataYet();
    void closeSocket();
    static Communication getSocketError();
    static int getSocketErrorCode();
};

} // namespace mega

#endif // MEGA_UDP_SOCKET_H