File: RiceUtilities.h

package info (click to toggle)
wpewebkit 2.52.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488,880 kB
  • sloc: cpp: 3,911,108; javascript: 197,735; ansic: 163,103; python: 53,139; asm: 22,290; ruby: 18,114; perl: 17,149; xml: 4,631; yacc: 2,394; sh: 2,310; java: 2,032; lex: 1,358; pascal: 372; makefile: 88
file content (118 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *  Copyright (C) 2025 Igalia S.L. All rights reserved.
 *  Copyright (C) 2025 Metrological Group B.V.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#pragma once

#if USE(LIBRICE)

#include "GUniquePtrRice.h"
#include "SharedMemory.h"
#include <wtf/HexNumber.h>
#include <wtf/Scope.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>

#ifndef RICE_CHECK_VERSION
#define RICE_CHECK_VERSION(major, minor, patch) \
    (RICE_PROTO_MAJOR > (major) || \
    (RICE_PROTO_MAJOR == (major) && RICE_PROTO_MINOR > (minor)) || \
    (RICE_PROTO_MAJOR == (major) && RICE_PROTO_MINOR == (minor) && RICE_PROTO_PATCH >= (patch)))
#endif

namespace WebCore {

static inline String riceAddressToString(const RiceAddress* address, bool includePort = true)
{
    std::array<uint8_t, 16> bytes;
    auto size = rice_address_get_address_bytes(address, bytes.data());
    StringBuilder builder;

    switch (rice_address_get_family(address)) {
    case RICE_ADDRESS_FAMILY_IPV4:
        // Maximum capacity for 255.255.255.255:65535
        builder.reserveCapacity(includePort ? 21 : 15);
        for (unsigned i = 0; i < size; i++) {
            if (i)
                builder.append('.');
            builder.append(static_cast<int>(bytes[i]));
        }
        break;
    case RICE_ADDRESS_FAMILY_IPV6:
        builder.reserveCapacity(includePort ? 47 : 39);
        if (includePort)
            builder.append('[');
        for (unsigned i = 0; i < size; i++) {
            if (i && !(i % 2))
                builder.append(':');
            builder.append(hex(bytes[i], 2));
        }
        if (includePort)
            builder.append(']');
        break;
    }
    if (includePort)
        builder.append(':', rice_address_get_port(address));
    builder.shrinkToFit();
    return builder.toString();
}

static inline GUniquePtr<RiceAddress> riceAddressFromString(const String& address)
{
    GUniquePtr<RiceAddress> result(rice_address_new_from_string(address.ascii().data()));
    return result;
}

static inline std::optional<SharedMemory::Handle> riceTransmitToSharedMemoryHandle(RiceTransmit* /* transfer full */ transmit)
{
    auto scopeExit = makeScopeExit([&] {
        rice_transmit_clear(transmit);
    });

    return SharedMemoryHandle::createCopy(unsafeMakeSpan(transmit->data.ptr, transmit->data.size), SharedMemoryProtection::ReadOnly);
}


static inline RiceTransportType fromRTCIceProtocol(RTCIceProtocol protocol)
{
    switch (protocol) {
    case RTCIceProtocol::Tcp:
        return RICE_TRANSPORT_TYPE_TCP;
    case WebCore::RTCIceProtocol::Udp:
        return RICE_TRANSPORT_TYPE_UDP;
    };
    return RICE_TRANSPORT_TYPE_UDP;
}

static inline RTCIceProtocol riceTransmitTransportToIceProtocol(const RiceTransmit& transmit)
{
    switch (transmit.transport) {
    case RICE_TRANSPORT_TYPE_TCP:
        return RTCIceProtocol::Tcp;
    case RICE_TRANSPORT_TYPE_UDP:
        return RTCIceProtocol::Udp;
    }

    return RTCIceProtocol::Udp;
}


} // namespace WebCore

#endif // USE(LIBRICE)