Skip to content

Commit

Permalink
Make flags be a table/struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Jan 2, 2024
1 parent 6e785dc commit a1405e2
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 84 deletions.
26 changes: 12 additions & 14 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TransportListenInfo,
TransportListenIp,
TransportProtocol,
TransportSocketFlag
TransportSocketFlags
} from './Transport';
import { WebRtcTransport, WebRtcTransportOptions, parseWebRtcTransportDumpResponse } from './WebRtcTransport';
import { PlainTransport, PlainTransportOptions, parsePlainTransportDumpResponse } from './PlainTransport';
Expand Down Expand Up @@ -571,7 +571,7 @@ export class Router<RouterAppData extends AppData = AppData>
listenInfo.ip,
listenInfo.announcedIp,
listenInfo.port,
socketFlagsToInteger(listenInfo.flags),
socketFlagsToFbs(listenInfo.flags),
listenInfo.sendBufferSize,
listenInfo.recvBufferSize
));
Expand Down Expand Up @@ -751,7 +751,7 @@ export class Router<RouterAppData extends AppData = AppData>
listenInfo!.ip,
listenInfo!.announcedIp,
listenInfo!.port,
socketFlagsToInteger(listenInfo!.flags),
socketFlagsToFbs(listenInfo!.flags),
listenInfo!.sendBufferSize,
listenInfo!.recvBufferSize
),
Expand All @@ -762,7 +762,7 @@ export class Router<RouterAppData extends AppData = AppData>
rtcpListenInfo.ip,
rtcpListenInfo.announcedIp,
rtcpListenInfo.port,
socketFlagsToInteger(rtcpListenInfo.flags),
socketFlagsToFbs(rtcpListenInfo.flags),
rtcpListenInfo.sendBufferSize,
rtcpListenInfo.recvBufferSize
) : undefined,
Expand Down Expand Up @@ -901,7 +901,7 @@ export class Router<RouterAppData extends AppData = AppData>
listenInfo!.ip,
listenInfo!.announcedIp,
listenInfo!.port,
socketFlagsToInteger(listenInfo!.flags),
socketFlagsToFbs(listenInfo!.flags),
listenInfo!.sendBufferSize,
listenInfo!.recvBufferSize
),
Expand Down Expand Up @@ -1625,14 +1625,12 @@ export function parseRouterDumpResponse(
};
}

function socketFlagsToInteger(flags: TransportSocketFlag[] = []): number
export function socketFlagsToFbs(
flags: TransportSocketFlags = {}
): FbsTransport.SocketFlagsT
{
let flagsInteger = 0;

for (const flag of flags)
{
flagsInteger |= flag;
}

return flagsInteger;
return new FbsTransport.SocketFlagsT(
Boolean(flags.ipv6Only),
Boolean(flags.udpReusePort)
);
}
15 changes: 6 additions & 9 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type TransportListenInfo =
/**
* Socket flags.
*/
flags?: TransportSocketFlag[];
flags?: TransportSocketFlags;

/**
* Send buffer size (bytes).
Expand Down Expand Up @@ -113,23 +113,20 @@ export type TransportListenIp =
export type TransportProtocol = 'udp' | 'tcp';

/**
* UDP/TCP socket flag.
* UDP/TCP socket flags.
*/
// NOTE: ESLint absurdly complains about "'TransportSocketFlag' is already
// declared in the upper scope".
// eslint-disable-next-line no-shadow
export enum TransportSocketFlag
export type TransportSocketFlags =
{
/**
* Disable dual-stack support so only IPv6 is used (only if ip is IPv6).
*/
IPV6ONLY = FbsTransport.SocketFlag.IPV6ONLY,
ipv6Only?: boolean;
/**
* Make different transports bind to the same ip and port (only for UDP).
* Useful for multicast scenarios with plain transport. Use with caution.
*/
UDP_REUSEPORT = FbsTransport.SocketFlag.UDP_REUSEPORT
}
udpReusePort?: boolean;
};

export type TransportTuple =
{
Expand Down
3 changes: 2 additions & 1 deletion node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import * as ortc from './ortc';
import { Channel } from './Channel';
import { Router, RouterOptions } from './Router';
import { Router, RouterOptions, socketFlagsToFbs } from './Router';
import { WebRtcServer, WebRtcServerOptions } from './WebRtcServer';
import { RtpCodecCapability } from './RtpParameters';
import { AppData } from './types';
Expand Down Expand Up @@ -699,6 +699,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
listenInfo.ip,
listenInfo.announcedIp,
listenInfo.port,
socketFlagsToFbs(listenInfo.flags),
listenInfo.sendBufferSize,
listenInfo.recvBufferSize)
);
Expand Down
4 changes: 2 additions & 2 deletions node/src/tests/test-PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ if (!IS_WINDOWS)
protocol : 'udp',
ip : multicastIp,
port : port,
flags : [ mediasoup.types.TransportSocketFlag.UDP_REUSEPORT ]
flags : { udpReusePort: true }
}
});

Expand All @@ -349,7 +349,7 @@ if (!IS_WINDOWS)
protocol : 'udp',
ip : multicastIp,
port : port,
flags : [ mediasoup.types.TransportSocketFlag.UDP_REUSEPORT ]
flags : { udpReusePort: true }
}
});
}).not.toThrow();
Expand Down
8 changes: 4 additions & 4 deletions worker/fbs/transport.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ enum Protocol: uint8 {
TCP
}

enum SocketFlag: uint8 {
IPV6ONLY = 1,
UDP_REUSEPORT = 2
table SocketFlags {
ipv6_only: bool = false;
udp_reuse_port: bool = false;
}

table ListenInfo {
protocol: Protocol = UDP;
ip: string (required);
announced_ip: string;
port: uint16 = 0;
flags: uint8 = 0;
flags: SocketFlags;
send_buffer_size: uint32 = 0;
recv_buffer_size: uint32 = 0;
}
Expand Down
16 changes: 9 additions & 7 deletions worker/include/RTC/PortManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "common.hpp"
#include "Settings.hpp"
#include "RTC/Transport.hpp"
#include <uv.h>
#include <absl/container/flat_hash_map.h>
#include <string>
Expand All @@ -20,19 +21,19 @@ namespace RTC
};

public:
static uv_udp_t* BindUdp(std::string& ip, uint8_t flags)
static uv_udp_t* BindUdp(std::string& ip, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_udp_t*>(Bind(Transport::UDP, ip, flags));
}
static uv_udp_t* BindUdp(std::string& ip, uint16_t port, uint8_t flags)
static uv_udp_t* BindUdp(std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_udp_t*>(Bind(Transport::UDP, ip, port, flags));
}
static uv_tcp_t* BindTcp(std::string& ip, uint8_t flags)
static uv_tcp_t* BindTcp(std::string& ip, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_tcp_t*>(Bind(Transport::TCP, ip, flags));
}
static uv_tcp_t* BindTcp(std::string& ip, uint16_t port, uint8_t flags)
static uv_tcp_t* BindTcp(std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_tcp_t*>(Bind(Transport::TCP, ip, port, flags));
}
Expand All @@ -46,11 +47,12 @@ namespace RTC
}

private:
static uv_handle_t* Bind(Transport transport, std::string& ip, uint8_t flags);
static uv_handle_t* Bind(Transport transport, std::string& ip, uint16_t port, uint8_t flags);
static uv_handle_t* Bind(Transport transport, std::string& ip, RTC::Transport::SocketFlags& flags);
static uv_handle_t* Bind(
Transport transport, std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags);
static void Unbind(Transport transport, std::string& ip, uint16_t port);
static std::vector<bool>& GetPorts(Transport transport, const std::string& ip);
static uint8_t ConvertSocketFlags(uint8_t flags);
static uint8_t ConvertSocketFlags(RTC::Transport::SocketFlags& flags);

private:
thread_local static absl::flat_hash_map<std::string, std::vector<bool>> mapUdpIpPorts;
Expand Down
8 changes: 6 additions & 2 deletions worker/include/RTC/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "common.hpp"
#include "RTC/TcpConnection.hpp"
#include "RTC/Transport.hpp"
#include "handles/TcpConnectionHandle.hpp"
#include "handles/TcpServerHandle.hpp"
#include <string>
Expand All @@ -24,13 +25,16 @@ namespace RTC

public:
TcpServer(
Listener* listener, RTC::TcpConnection::Listener* connListener, std::string& ip, uint8_t flags);
Listener* listener,
RTC::TcpConnection::Listener* connListener,
std::string& ip,
RTC::Transport::SocketFlags& flags);
TcpServer(
Listener* listener,
RTC::TcpConnection::Listener* connListener,
std::string& ip,
uint16_t port,
uint8_t flags);
RTC::Transport::SocketFlags& flags);
~TcpServer() override;

/* Pure virtual methods inherited from ::TcpServerHandle. */
Expand Down
8 changes: 7 additions & 1 deletion worker/include/RTC/Transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,20 @@ namespace RTC
};

public:
struct SocketFlags
{
bool ipv6Only{ false };
bool udpReusePort{ false };
};

struct ListenInfo
{
std::string ip;
std::string announcedIp;
uint16_t port{ 0u };
SocketFlags flags;
uint32_t sendBufferSize{ 0u };
uint32_t recvBufferSize{ 0u };
uint8_t flags{ 0u };
};

private:
Expand Down
5 changes: 3 additions & 2 deletions worker/include/RTC/UdpSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MS_RTC_UDP_SOCKET_HPP

#include "common.hpp"
#include "RTC/Transport.hpp"
#include "handles/UdpSocketHandle.hpp"
#include <string>

Expand All @@ -21,8 +22,8 @@ namespace RTC
};

public:
UdpSocket(Listener* listener, std::string& ip, uint8_t flags);
UdpSocket(Listener* listener, std::string& ip, uint16_t port, uint8_t flags);
UdpSocket(Listener* listener, std::string& ip, RTC::Transport::SocketFlags& flags);
UdpSocket(Listener* listener, std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags);
~UdpSocket() override;

/* Pure virtual methods inherited from ::UdpSocketHandle. */
Expand Down
9 changes: 5 additions & 4 deletions worker/src/RTC/PipeTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ namespace RTC
this->listenInfo.announcedIp.assign(options->listenInfo()->announcedIp()->str());
}

this->listenInfo.port = options->listenInfo()->port();
this->listenInfo.sendBufferSize = options->listenInfo()->sendBufferSize();
this->listenInfo.recvBufferSize = options->listenInfo()->recvBufferSize();
this->listenInfo.flags = options->listenInfo()->flags();
this->listenInfo.port = options->listenInfo()->port();
this->listenInfo.sendBufferSize = options->listenInfo()->sendBufferSize();
this->listenInfo.recvBufferSize = options->listenInfo()->recvBufferSize();
this->listenInfo.flags.ipv6Only = options->listenInfo()->flags()->ipv6Only();
this->listenInfo.flags.udpReusePort = options->listenInfo()->flags()->udpReusePort();

this->rtx = options->enableRtx();

Expand Down
12 changes: 7 additions & 5 deletions worker/src/RTC/PlainTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ namespace RTC
this->listenInfo.announcedIp.assign(options->listenInfo()->announcedIp()->str());
}

this->listenInfo.port = options->listenInfo()->port();
this->listenInfo.sendBufferSize = options->listenInfo()->sendBufferSize();
this->listenInfo.recvBufferSize = options->listenInfo()->recvBufferSize();
this->listenInfo.flags = options->listenInfo()->flags();
this->listenInfo.port = options->listenInfo()->port();
this->listenInfo.sendBufferSize = options->listenInfo()->sendBufferSize();
this->listenInfo.recvBufferSize = options->listenInfo()->recvBufferSize();
this->listenInfo.flags.ipv6Only = options->listenInfo()->flags()->ipv6Only();
this->listenInfo.flags.udpReusePort = options->listenInfo()->flags()->udpReusePort();

this->rtcpMux = options->rtcpMux();
this->comedia = options->comedia();
Expand Down Expand Up @@ -84,7 +85,8 @@ namespace RTC
this->rtcpListenInfo.port = options->rtcpListenInfo()->port();
this->rtcpListenInfo.sendBufferSize = options->rtcpListenInfo()->sendBufferSize();
this->rtcpListenInfo.recvBufferSize = options->rtcpListenInfo()->recvBufferSize();
this->rtcpListenInfo.flags = options->rtcpListenInfo()->flags();
this->rtcpListenInfo.flags.ipv6Only = options->rtcpListenInfo()->flags()->ipv6Only();
this->rtcpListenInfo.flags.udpReusePort = options->rtcpListenInfo()->flags()->udpReusePort();
}
// If rtcpListenInfo is not given, just clone listenInfo.
else
Expand Down
Loading

0 comments on commit a1405e2

Please sign in to comment.