ice_socket.h 3.11 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
3
 *
4 5 6 7 8 9
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
10
 *
11 12 13 14 15 16 17 18 19
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
20 21 22
#pragma once

#include "generic_io.h"
23 24

#include <memory>
25
#include <functional>
26

27 28 29
#if defined(_MSC_VER)
#include <BaseTsd.h>
using ssize_t = SSIZE_T;
30 31
#endif

Adrien Béraud's avatar
Adrien Béraud committed
32
namespace jami {
33 34

class IceTransport;
35
using IceRecvCb = std::function<ssize_t(unsigned char* buf, size_t len)>;
36 37 38 39 40 41 42 43 44

class IceSocket
{
    private:
        std::shared_ptr<IceTransport> ice_transport_ {};
        int compId_ = -1;

    public:
        IceSocket(std::shared_ptr<IceTransport> iceTransport, int compId)
45
            : ice_transport_(std::move(iceTransport)), compId_(compId) {}
46 47 48

        void close();
        ssize_t send(const unsigned char* buf, size_t len);
49
        ssize_t waitForData(std::chrono::milliseconds timeout);
50
        void setOnRecv(IceRecvCb cb);
51
        uint16_t getTransportOverhead();
52 53
};

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/// ICE transport as a GenericSocket.
///
/// \warning Simplified version where we assume that ICE protocol
/// always use UDP over IP over ETHERNET, and doesn't add more header to the UDP payload.
///
class IceSocketTransport final : public GenericSocket<uint8_t>
{
public:
    using SocketType = GenericSocket<uint8_t>;

    static constexpr uint16_t STANDARD_MTU_SIZE = 1280; // Size in bytes of MTU for IPv6 capable networks
    static constexpr uint16_t IPV6_HEADER_SIZE = 40; // Size in bytes of IPv6 packet header
    static constexpr uint16_t IPV4_HEADER_SIZE = 20; // Size in bytes of IPv4 packet header
    static constexpr uint16_t UDP_HEADER_SIZE = 8; // Size in bytes of UDP header

69
    IceSocketTransport(std::shared_ptr<IceTransport>& ice, int comp_id, bool reliable = false)
70
        : compId_ {comp_id}
71 72
        , ice_ {ice}
        , reliable_ {reliable} {}
73 74

    bool isReliable() const override {
75
        return reliable_;
76 77
    }

78 79
    void shutdown() override;

80 81 82 83
    bool isInitiator() const override;

    int maxPayload() const override;

84
    int waitForData(std::chrono::milliseconds timeout, std::error_code& ec) const override;
85 86 87 88 89 90 91

    std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override;

    std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override;

    void setOnRecv(RecvCb&& cb) override;

92 93 94 95
    IpAddr localAddr() const override;

    IpAddr remoteAddr() const override;

96 97 98
private:
    const int compId_;
    std::shared_ptr<IceTransport> ice_;
99
    bool reliable_;
100 101
};

102
};