提交 767e7e76 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Work in ICE and TransportTuple.

上级 779269a9
......@@ -28,7 +28,15 @@ class Peer extends EventEmitter
// Closed flag
this._closed = false;
// TODO: subscribe to channel notifications from the worker
// Subscribe to notifications
this._channel.on(this._internal.peerId, (event) =>
{
switch (event)
{
default:
logger.error('ignoring unknown event "%s"', event);
}
});
}
get closed()
......@@ -51,6 +59,9 @@ class Peer extends EventEmitter
this._closed = true;
// Remove notification subscriptions (wait a bit so closure events arrive)
setTimeout(() => this._channel.removeAllListeners(this._internal.peerId), 1000);
// Close every Transport
this._transports.forEach((transport) => transport.close(undefined, true));
......
......@@ -27,8 +27,6 @@ class Room extends EventEmitter
// Closed flag
this._closed = false;
// TODO: subscribe to channel notifications from the worker
}
get closed()
......
......@@ -35,10 +35,8 @@ class Transport extends EventEmitter
case 'dtlsstatechange':
{
this._data.dtlsState = data.dtlsState;
// Emit it to the app
this.emit(event, data);
break;
}
......@@ -93,9 +91,8 @@ class Transport extends EventEmitter
this._closed = true;
// Remove notification subscriptions (do it in the next iteration to
// ensure the user receives closure events)
setImmediate(() => this._channel.removeAllListeners(this._internal.transportId));
// Remove notification subscriptions (wait a bit so closure events arrive)
setTimeout(() => this._channel.removeAllListeners(this._internal.transportId), 1000);
if (!dontSendChannel)
{
......
......@@ -2,8 +2,8 @@
'use strict';
// process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester*';
process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester* -mediasoup:mediasoup-worker*';
process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester*';
// process.env.DEBUG = '*ERROR* *WARN* mediasoup* mediasoup-tester* -mediasoup:mediasoup-worker*';
const http = require('http');
const url = require('url');
......@@ -169,7 +169,7 @@ app.put('/test-transport', function(req)
// Create transports promises
for (let i = 0; i < numTransports; i++)
{
let promise = mediaPeer.createTransport({ udp: true, tcp: false });
let promise = mediaPeer.createTransport({ udp: true, tcp: true });
// Set transport event listeners
promise.then((transport) =>
......@@ -183,7 +183,7 @@ app.put('/test-transport', function(req)
{
debug('transport "dtlsstatechange" event [data.dtlsState:%s, transport.dtlsState:%s]', data.dtlsState, transport.dtlsState);
});
})
});
// Add the transport promise to the array of promises
transportPromises.push(promise);
......@@ -267,7 +267,7 @@ app.put('/test-transport', function(req)
// Create a promise
let promise = transport.setRemoteDtlsParameters(
{
role : 'client', // SDP offer MUST always have a=setup:actpass so we can choose whatever we want
role : 'auto', // SDP offer MUST always have a=setup:actpass so we can choose whatever we want
fingerprint :
{
algorithm : remoteFingerprint.type,
......
......@@ -2,16 +2,23 @@
#define MS_RTC_ICE_SERVER_H
#include "common.h"
#include "RTC/TransportSource.h"
#include "RTC/STUNMessage.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPConnection.h"
#include "RTC/TransportTuple.h"
#include <string>
#include <list>
namespace RTC
{
class IceServer
{
public:
enum class IceState
{
NEW = 1,
CONNECTED,
COMPLETED
};
public:
class Listener
{
......@@ -20,17 +27,37 @@ namespace RTC
* These callbacks are guaranteed to be called before ProcessSTUNMessage()
* returns, so the given pointers are still usable.
*/
virtual void onOutgoingSTUNMessage(IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportSource* source) = 0;
virtual void onICEValidPair(IceServer* iceServer, RTC::TransportSource* source, bool has_use_candidate) = 0;
virtual void onOutgoingSTUNMessage(IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportTuple* tuple) = 0;
virtual void onICESelectedTuple(IceServer* iceServer, RTC::TransportTuple* tuple) = 0;
virtual void onICEConnected(IceServer* iceServer) = 0;
virtual void onICECompleted(IceServer* iceServer) = 0;
};
public:
IceServer(Listener* listener, const std::string& usernameFragment, const std::string password);
void Close();
void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source);
void ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportTuple* tuple);
std::string& GetUsernameFragment();
std::string& GetPassword();
IceState GetState();
bool IsValidTuple(RTC::TransportTuple* tuple);
private:
void HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate);
/**
* Store the given tuple and return its stored address.
*/
RTC::TransportTuple* AddTuple(RTC::TransportTuple* tuple);
/**
* If the given tuple exists return its stored address, nullptr otherwise.
*/
RTC::TransportTuple* HasTuple(RTC::TransportTuple* tuple);
/**
* Set the given tuple as the selected tuple.
* NOTE: The given tuple MUST be already stored within the list.
*/
void SetSelectedTuple(RTC::TransportTuple* stored_tuple);
private:
// Passed by argument.
......@@ -38,6 +65,9 @@ namespace RTC
// Others.
std::string usernameFragment;
std::string password;
IceState state = IceState::NEW;
std::list<RTC::TransportTuple> tuples;
RTC::TransportTuple* selectedTuple = nullptr;
};
/* Inline methods. */
......@@ -53,6 +83,12 @@ namespace RTC
{
return this->password;
}
inline
IceServer::IceState IceServer::GetState()
{
return this->state;
}
}
#endif
......@@ -2,18 +2,17 @@
#define MS_RTC_TRANSPORT_H
#include "common.h"
#include "RTC/IceCandidate.h"
#include "RTC/IceServer.h"
#include "RTC/DTLSTransport.h"
#include "RTC/STUNMessage.h"
#include "RTC/TransportSource.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPServer.h"
#include "RTC/TCPConnection.h"
#include "RTC/IceCandidate.h"
#include "RTC/IceServer.h"
#include "RTC/STUNMessage.h"
#include "RTC/TransportTuple.h"
#include "RTC/DTLSTransport.h"
#include "Channel/Notifier.h"
#include <string>
#include <vector>
#include <list>
#include <json/json.h>
namespace RTC
......@@ -25,13 +24,6 @@ namespace RTC
public RTC::IceServer::Listener,
public RTC::DTLSTransport::Listener
{
public:
class Listener
{
public:
virtual void onTransportClosed(RTC::Transport* transport) = 0;
};
public:
enum class IceComponent
{
......@@ -49,8 +41,15 @@ namespace RTC
FAILED
};
public:
class Listener
{
public:
virtual void onTransportClosed(RTC::Transport* transport) = 0;
};
private:
static size_t maxSources;
static size_t maxTuples;
public:
Transport(Listener* listener, Channel::Notifier* notifier, unsigned int transportId, Json::Value& data, Transport* rtpTransport = nullptr);
......@@ -66,17 +65,14 @@ namespace RTC
private:
void Terminate();
bool IsAlive();
int SetSendingSource(RTC::TransportSource* source);
int IsValidSource(RTC::TransportSource* source);
bool RemoveSource(RTC::TransportSource* source);
void MayRunDTLSTransport();
/* Private methods to unify UDP and TCP behavior. */
private:
void onSTUNDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onDTLSDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onRTPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onRTCPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len);
void onSTUNDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len);
void onDTLSDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len);
void onRTPDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len);
void onRTCPDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len);
/* Pure virtual methods inherited from RTC::UDPSocket::Listener. */
public:
......@@ -98,8 +94,10 @@ namespace RTC
/* Pure virtual methods inherited from RTC::IceServer::Listener. */
public:
virtual void onOutgoingSTUNMessage(RTC::IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportSource* source) override;
virtual void onICEValidPair(RTC::IceServer* iceServer, RTC::TransportSource* source, bool has_use_candidate) override;
virtual void onOutgoingSTUNMessage(RTC::IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportTuple* tuple) override;
virtual void onICESelectedTuple(IceServer* iceServer, RTC::TransportTuple* tuple) override;
virtual void onICEConnected(IceServer* iceServer) override;
virtual void onICECompleted(IceServer* iceServer) override;
/* Pure virtual methods inherited from RTC::DTLSTransport::Listener. */
public:
......@@ -136,10 +134,7 @@ namespace RTC
// Others (ICE).
RTC::Transport::IceComponent iceComponent;
std::vector<IceCandidate> iceLocalCandidates;
std::list<RTC::TransportSource> sources;
RTC::TransportSource* sendingSource = nullptr;
bool icePaired = false;
bool icePairedWithUseCandidate = false;
RTC::TransportTuple* selectedTuple = nullptr;
// Others (DTLS).
DtlsTransportState dtlsState = DtlsTransportState::NEW;
bool remoteDtlsParametersGiven = false;
......@@ -171,139 +166,6 @@ namespace RTC
{
return this->iceServer->GetPassword();
}
/**
* Set the given source as a valid one and mark it as the sending source for
* outgoing data.
*
* @param source: A RTC::TransportSource (which is a UDP tuple or TCP connection).
* @return: true if the given source was not an already valid source,
* false otherwise.
* @return: 1 if it's the current sending source, 2 if it's another
* valid source, 0 if it's not a valid source.
*/
inline
int Transport::SetSendingSource(RTC::TransportSource* source)
{
// Check whether this is already a valid source.
// NOTE: The IsValidSource() method will also mark it as sending source.
switch (IsValidSource(source))
{
// This is already the sending source.
case 1:
return 1;
// This is another valid source.
case 2:
return 2;
}
// If there are Transport::maxSources sources then close and remove the last one.
if (this->sources.size() == Transport::maxSources)
{
RTC::TransportSource* last_source = &(*this->sources.end());
last_source->Close();
this->sources.pop_back();
}
// Add the new source at the beginning.
this->sources.push_front(*source);
// Set it as sending source (make it point to the first source in the list).
this->sendingSource = &(*this->sources.begin());
// If it is UDP then we must store the remote address (until now it is
// just a pointer that will be freed soon).
if (this->sendingSource->IsUDP())
this->sendingSource->StoreUdpRemoteAddress();
return 0;
}
/**
* Check whether the given source is contained in the list of valid sources for
* this transport. In case it is, the given source is also marked as sending
* source for the outgoing data.
*
* @param source: The RTC::TransportSource to check.
* @return: 1 if it's the current sending source, 2 if it's another
* valid source, 0 if it's not a valid source.
*/
inline
int Transport::IsValidSource(RTC::TransportSource* source)
{
// If there is no sending source yet then we know that the sources list
// is empty, so return 0.
if (!this->sendingSource)
return 0;
// First check the current sending source. If it matches then return 1.
if (this->sendingSource->Compare(source))
return 1;
// Otherwise check other valid source in the sources list.
for (auto it = this->sources.begin(); it != this->sources.end(); ++it)
{
RTC::TransportSource* valid_source = &(*it);
// If found set it as sending source and return 2.
if (valid_source->Compare(source))
{
this->sendingSource = valid_source;
return 2;
}
}
// Otherwise it is not a valid source so return 0.
return 0;
}
/**
* Remove the given source from the list of valid sources. Also update the
* sending source if the given source was the sending source and make it point
* to the next valid source (if any).
*
* @param source: The RTC::TransportSource to remove.
* @return: true if the given source was present in the list of valid
* sources, false otherwise.
*/
inline
bool Transport::RemoveSource(RTC::TransportSource* source)
{
for (auto it = this->sources.begin(); it != this->sources.end(); ++it)
{
RTC::TransportSource* valid_source = &(*it);
// If the source was a valid source then remove it.
if (valid_source->Compare(source))
{
this->sources.erase(it);
// If it was the sending source then unset it and set the sending
// source to the first valid source (if any).
if (this->sendingSource == valid_source)
{
if (this->sources.begin() != this->sources.end())
{
this->sendingSource = &(*this->sources.begin());
}
else
{
this->sendingSource = nullptr;
// This is just useful is there are just TCP sources.
this->icePaired = false;
this->icePairedWithUseCandidate = false;
// TODO: This should fire a 'icefailed' event.
}
}
return true;
}
}
return false;
}
}
#endif
#ifndef MS_RTC_TRANSPORT_SOURCE_H
#define MS_RTC_TRANSPORT_SOURCE_H
#ifndef MS_RTC_TRANSPORT_TUPLE_H
#define MS_RTC_TRANSPORT_TUPLE_H
#include "common.h"
#include "RTC/UDPSocket.h"
#include "RTC/TCPConnection.h"
#include "Channel/Request.h"
#include "Utils.h"
#include <string>
namespace RTC
{
class TransportSource
class TransportTuple
{
public:
enum class Type
......@@ -20,15 +18,15 @@ namespace RTC
};
public:
TransportSource(RTC::UDPSocket* udpSocket, const struct sockaddr* udpRemoteAddr);
TransportSource(RTC::TCPConnection* tcpConnection);
TransportTuple(RTC::UDPSocket* udpSocket, const struct sockaddr* udpRemoteAddr);
TransportTuple(RTC::TCPConnection* tcpConnection);
void Close();
void Dump();
void StoreUdpRemoteAddress();
bool IsUDP();
bool IsTCP();
bool Compare(TransportSource* source);
bool Compare(TransportTuple* tuple);
void Send(const MS_BYTE* data, size_t len);
const struct sockaddr* GetRemoteAddress();
......@@ -45,27 +43,27 @@ namespace RTC
/* Inline methods. */
inline
void TransportSource::Close()
void TransportTuple::Close()
{
if (this->type == Type::TCP)
this->tcpConnection->Close();
}
inline
TransportSource::TransportSource(RTC::UDPSocket* udpSocket, const struct sockaddr* udpRemoteAddr) :
TransportTuple::TransportTuple(RTC::UDPSocket* udpSocket, const struct sockaddr* udpRemoteAddr) :
udpSocket(udpSocket),
udpRemoteAddr((struct sockaddr*)udpRemoteAddr),
type(Type::UDP)
{}
inline
TransportSource::TransportSource(RTC::TCPConnection* tcpConnection) :
TransportTuple::TransportTuple(RTC::TCPConnection* tcpConnection) :
tcpConnection(tcpConnection),
type(Type::TCP)
{}
inline
void TransportSource::StoreUdpRemoteAddress()
void TransportTuple::StoreUdpRemoteAddress()
{
// Clone the given address into our address storage and make the sockaddr
// pointer point to it.
......@@ -74,27 +72,27 @@ namespace RTC
}
inline
bool TransportSource::IsUDP()
bool TransportTuple::IsUDP()
{
return (this->type == Type::UDP);
}
inline
bool TransportSource::IsTCP()
bool TransportTuple::IsTCP()
{
return (this->type == Type::TCP);
}
inline
bool TransportSource::Compare(TransportSource* source)
bool TransportTuple::Compare(TransportTuple* tuple)
{
if (this->type == Type::UDP && source->IsUDP())
if (this->type == Type::UDP && tuple->IsUDP())
{
return (this->udpSocket == source->udpSocket && Utils::IP::CompareAddresses(this->udpRemoteAddr, source->GetRemoteAddress()));
return (this->udpSocket == tuple->udpSocket && Utils::IP::CompareAddresses(this->udpRemoteAddr, tuple->GetRemoteAddress()));
}
else if (this->type == Type::TCP && source->IsTCP())
else if (this->type == Type::TCP && tuple->IsTCP())
{
return (this->tcpConnection == source->tcpConnection);
return (this->tcpConnection == tuple->tcpConnection);
}
else
{
......@@ -103,7 +101,7 @@ namespace RTC
}
inline
void TransportSource::Send(const MS_BYTE* data, size_t len)
void TransportTuple::Send(const MS_BYTE* data, size_t len)
{
if (this->type == Type::UDP)
this->udpSocket->Send(data, len, this->udpRemoteAddr);
......@@ -112,7 +110,7 @@ namespace RTC
}
inline
const struct sockaddr* TransportSource::GetRemoteAddress()
const struct sockaddr* TransportTuple::GetRemoteAddress()
{
if (this->type == Type::UDP)
return (const struct sockaddr*)this->udpRemoteAddr;
......
......@@ -24,7 +24,7 @@ namespace RTC
delete this;
}
void IceServer::ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportSource* source)
void IceServer::ProcessSTUNMessage(RTC::STUNMessage* msg, RTC::TransportTuple* tuple)
{
MS_TRACE();
......@@ -38,7 +38,7 @@ namespace RTC
// Reply 400.
RTC::STUNMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
}
else
......@@ -59,7 +59,7 @@ namespace RTC
// Reply 400.
RTC::STUNMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
}
else
......@@ -82,7 +82,7 @@ namespace RTC
// Reply 400.
RTC::STUNMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
return;
......@@ -101,7 +101,7 @@ namespace RTC
// Reply 401.
RTC::STUNMessage* response = msg->CreateErrorResponse(401);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
return;
......@@ -114,7 +114,7 @@ namespace RTC
// Reply 400.
RTC::STUNMessage* response = msg->CreateErrorResponse(400);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
return;
......@@ -129,7 +129,7 @@ namespace RTC
// Reply 487 (Role Conflict).
RTC::STUNMessage* response = msg->CreateErrorResponse(487);
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
return;
......@@ -141,18 +141,18 @@ namespace RTC
RTC::STUNMessage* response = msg->CreateSuccessResponse();
// Add XOR-MAPPED-ADDRESS.
response->SetXorMappedAddress(source->GetRemoteAddress());
response->SetXorMappedAddress(tuple->GetRemoteAddress());
// Authenticate the response.
response->Authenticate(this->password);
// Send back.
response->Serialize();
this->listener->onOutgoingSTUNMessage(this, response, source);
this->listener->onOutgoingSTUNMessage(this, response, tuple);
delete response;
// Notify the listener with the valid pair.
this->listener->onICEValidPair(this, source, msg->HasUseCandidate());
// Handle the tuple.
HandleTuple(tuple, msg->HasUseCandidate());
break;
}
......@@ -160,20 +160,245 @@ namespace RTC
case RTC::STUNMessage::Class::Indication:
{
MS_DEBUG("STUN Binding Indication processed");
break;
}
case RTC::STUNMessage::Class::SuccessResponse:
{
MS_DEBUG("STUN Binding Success Response processed");
break;
}
case RTC::STUNMessage::Class::ErrorResponse:
{
MS_DEBUG("STUN Binding Error Response processed");
break;
}
}
}
bool IceServer::IsValidTuple(RTC::TransportTuple* tuple)
{
MS_TRACE();
if (HasTuple(tuple))
return true;
else
return false;
}
void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool has_use_candidate)
{
MS_TRACE();
switch (this->state)
{
case IceState::NEW:
{
// There should be no tuples.
MS_ASSERT(this->tuples.size() == 0, "state is 'new' but there are %zu tuples", this->tuples.size());
// There shouldn't be a selected tuple.
MS_ASSERT(this->selectedTuple == nullptr, "state is 'new' but there is selected tuple");
if (!has_use_candidate)
{
MS_DEBUG("transition from state 'new' to 'connected'");
// Store the tuple.
auto stored_tuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Update state.
this->state = IceState::CONNECTED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICEConnected(this);
}
else
{
MS_DEBUG("transition from state 'new' to 'completed'");
// Store the tuple.
auto stored_tuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Update state.
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICECompleted(this);
}
break;
}
case IceState::CONNECTED:
{
// There should be some tuples.
MS_ASSERT(this->tuples.size() > 0, "state is 'connected' but there are no tuples");
// There should be a selected tuple.
MS_ASSERT(this->selectedTuple != nullptr, "state is 'connected' but there is not selected tuple");
if (!has_use_candidate)
{
// If a new tuple store it.
if (!HasTuple(tuple))
AddTuple(tuple);
}
else
{
MS_DEBUG("transition from state 'connected' to 'completed'");
auto stored_tuple = HasTuple(tuple);
// If a new tuple store it.
if (!stored_tuple)
stored_tuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Update state.
this->state = IceState::COMPLETED;
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
this->listener->onICECompleted(this);
}
break;
}
case IceState::COMPLETED:
{
// There should be some tuples.
MS_ASSERT(this->tuples.size() > 0, "state is 'completed' but there are no tuples");
// There should be a selected tuple.
MS_ASSERT(this->selectedTuple != nullptr, "state is 'completed' but there is not selected tuple");
if (!has_use_candidate)
{
// If a new tuple store it.
if (!HasTuple(tuple))
AddTuple(tuple);
}
else
{
auto stored_tuple = HasTuple(tuple);
// If a new tuple store it.
if (!stored_tuple)
stored_tuple = AddTuple(tuple);
// Mark it as selected tuple.
SetSelectedTuple(stored_tuple);
// Notify the listener.
this->listener->onICESelectedTuple(this, stored_tuple);
}
break;
}
}
}
RTC::TransportTuple* IceServer::AddTuple(RTC::TransportTuple* tuple)
{
MS_TRACE();
// Add the new tuple at the beginning of the list.
this->tuples.push_front(*tuple);
auto stored_tuple = &(*this->tuples.begin());
// If it is UDP then we must store the remote address (until now it is
// just a pointer that will be freed soon).
if (stored_tuple->IsUDP())
stored_tuple->StoreUdpRemoteAddress();
// Return the address of the inserted tuple.
return stored_tuple;
}
RTC::TransportTuple* IceServer::HasTuple(RTC::TransportTuple* tuple)
{
MS_TRACE();
// If there is no selected tuple yet then we know that the tuples list
// is empty.
if (!this->selectedTuple)
return nullptr;
// Check the current selected tuple.
if (this->selectedTuple->Compare(tuple))
return this->selectedTuple;
// Otherwise check other stored tuples.
for (auto it = this->tuples.begin(); it != this->tuples.end(); ++it)
{
RTC::TransportTuple* stored_tuple = &(*it);
if (stored_tuple->Compare(tuple))
return stored_tuple;
}
return nullptr;
}
void IceServer::SetSelectedTuple(RTC::TransportTuple* stored_tuple)
{
MS_TRACE();
this->selectedTuple = stored_tuple;
}
// TODO
// bool Transport::RemoveTuple(RTC::TransportTuple* tuple)
// {
// for (auto it = this->tuples.begin(); it != this->tuples.end(); ++it)
// {
// RTC::TransportTuple* valid_tuple = &(*it);
// // If the tuple was a valid tuple then remove it.
// if (valid_tuple->Compare(tuple))
// {
// this->tuples.erase(it);
// // If it was the media tuple then unset it and set the media
// // tuple to the first valid tuple (if any).
// if (this->mediaTuple == valid_tuple)
// {
// if (this->tuples.begin() != this->tuples.end())
// {
// this->mediaTuple = &(*this->tuples.begin());
// }
// else
// {
// this->mediaTuple = nullptr;
// // This is just useful is there are just TCP tuples.
// this->icePaired = false;
// this->icePairedWithUseCandidate = false;
// // TODO: This should fire a 'icefailed' event.
// }
// }
// return true;
// }
// }
// return false;
// }
}
......@@ -28,7 +28,7 @@ namespace RTC
{
/* Class variables. */
size_t Transport::maxSources = 8;
size_t Transport::maxTuples = 8;
/* Instance methods. */
......@@ -513,10 +513,11 @@ namespace RTC
// Check our local DTLS role.
switch (this->dtlsLocalRole)
{
// If still 'auto' then transition to 'server' if ICE is complete (with or
// without UseCandidate).
// If still 'auto' then transition to 'server' if ICE is 'connected' or
// 'completed'.
case RTC::DTLSTransport::Role::AUTO:
if (this->icePaired)
if (this->iceServer->GetState() == RTC::IceServer::IceState::CONNECTED ||
this->iceServer->GetState() == RTC::IceServer::IceState::COMPLETED)
{
MS_DEBUG("transition from local 'auto' role to 'server' and running DTLS transport");
......@@ -527,9 +528,9 @@ namespace RTC
// 'client' is only set if a 'setRemoteDtlsParameters' request was previously
// received with remote DTLS role 'server'.
// If 'client' then wait for the first Binding with USE-CANDIDATE.
// If 'client' then wait for ICE to be 'completed'.
case RTC::DTLSTransport::Role::CLIENT:
if (this->icePairedWithUseCandidate)
if (this->iceServer->GetState() == RTC::IceServer::IceState::COMPLETED)
{
MS_DEBUG("running DTLS transport in 'client' role");
......@@ -537,9 +538,11 @@ namespace RTC
}
break;
// If 'server' then run the DTLS handler upon first valid ICE pair.
// If 'server' then run the DTLS handler if ICE is 'connected' or
// 'completed'.
case RTC::DTLSTransport::Role::SERVER:
if (this->icePaired)
if (this->iceServer->GetState() == RTC::IceServer::IceState::CONNECTED ||
this->iceServer->GetState() == RTC::IceServer::IceState::COMPLETED)
{
MS_DEBUG("running DTLS transport in 'server' role");
......@@ -553,7 +556,7 @@ namespace RTC
}
inline
void Transport::onSTUNDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
void Transport::onSTUNDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{
MS_TRACE();
......@@ -566,19 +569,19 @@ namespace RTC
}
// Pass it to the IceServer.
this->iceServer->ProcessSTUNMessage(msg, source);
this->iceServer->ProcessSTUNMessage(msg, tuple);
delete msg;
}
inline
void Transport::onDTLSDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
void Transport::onDTLSDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{
MS_TRACE();
if (!IsValidSource(source))
if (!this->iceServer->IsValidTuple(tuple))
{
MS_DEBUG("ignoring DTLS data coming from an invalid source");
MS_DEBUG("ignoring DTLS data coming from an invalid tuple");
return;
}
......@@ -600,7 +603,7 @@ namespace RTC
}
inline
void Transport::onRTPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
void Transport::onRTPDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{
MS_TRACE();
......@@ -608,7 +611,7 @@ namespace RTC
}
inline
void Transport::onRTCPDataRecv(RTC::TransportSource* source, const MS_BYTE* data, size_t len)
void Transport::onRTCPDataRecv(RTC::TransportTuple* tuple, const MS_BYTE* data, size_t len)
{
MS_TRACE();
......@@ -622,8 +625,8 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(socket, remote_addr);
onSTUNDataRecv(&source, data, len);
RTC::TransportTuple tuple(socket, remote_addr);
onSTUNDataRecv(&tuple, data, len);
}
void Transport::onDTLSDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
......@@ -633,8 +636,8 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(socket, remote_addr);
onDTLSDataRecv(&source, data, len);
RTC::TransportTuple tuple(socket, remote_addr);
onDTLSDataRecv(&tuple, data, len);
}
void Transport::onRTPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
......@@ -644,8 +647,8 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(socket, remote_addr);
onRTPDataRecv(&source, data, len);
RTC::TransportTuple tuple(socket, remote_addr);
onRTPDataRecv(&tuple, data, len);
}
void Transport::onRTCPDataRecv(RTC::UDPSocket *socket, const MS_BYTE* data, size_t len, const struct sockaddr* remote_addr)
......@@ -655,8 +658,8 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(socket, remote_addr);
onRTCPDataRecv(&source, data, len);
RTC::TransportTuple tuple(socket, remote_addr);
onRTCPDataRecv(&tuple, data, len);
}
void Transport::onRTCTCPConnectionClosed(RTC::TCPServer* tcpServer, RTC::TCPConnection* connection, bool is_closed_by_peer)
......@@ -666,12 +669,14 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(connection);
// TODO
if (RemoveSource(&source))
{
MS_DEBUG("valid source %s:", is_closed_by_peer ? "closed by peer" : "locally closed");
}
// RTC::TransportTuple tuple(connection);
// if (RemoveTuple(&tuple))
// {
// MS_DEBUG("valid tuple %s:", is_closed_by_peer ? "closed by peer" : "locally closed");
// }
}
void Transport::onSTUNDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
......@@ -681,8 +686,8 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(connection);
onSTUNDataRecv(&source, data, len);
RTC::TransportTuple tuple(connection);
onSTUNDataRecv(&tuple, data, len);
}
void Transport::onDTLSDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
......@@ -692,16 +697,16 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(connection);
onDTLSDataRecv(&source, data, len);
RTC::TransportTuple tuple(connection);
onDTLSDataRecv(&tuple, data, len);
}
void Transport::onRTPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
{
MS_TRACE();
RTC::TransportSource source(connection);
onRTPDataRecv(&source, data, len);
RTC::TransportTuple tuple(connection);
onRTPDataRecv(&tuple, data, len);
}
void Transport::onRTCPDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
......@@ -711,28 +716,30 @@ namespace RTC
if (!IsAlive())
return;
RTC::TransportSource source(connection);
onRTCPDataRecv(&source, data, len);
RTC::TransportTuple tuple(connection);
onRTCPDataRecv(&tuple, data, len);
}
void Transport::onOutgoingSTUNMessage(RTC::IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportSource* source)
void Transport::onOutgoingSTUNMessage(RTC::IceServer* iceServer, RTC::STUNMessage* msg, RTC::TransportTuple* tuple)
{
MS_TRACE();
if (!IsAlive())
return;
// Send the STUN response over the same transport source.
source->Send(msg->GetRaw(), msg->GetLength());
// Send the STUN response over the same transport tuple.
tuple->Send(msg->GetRaw(), msg->GetLength());
}
void Transport::onICEValidPair(RTC::IceServer* iceServer, RTC::TransportSource* source, bool has_use_candidate)
void Transport::onICESelectedTuple(RTC::IceServer* iceServer, RTC::TransportTuple* tuple)
{
MS_TRACE();
if (!IsAlive())
return;
this->selectedTuple = tuple;
/*
* RFC 5245 section 11.2 "Receiving Media":
*
......@@ -740,29 +747,22 @@ namespace RTC
* on any candidates provided for that component.
*/
this->icePaired = true;
if (has_use_candidate)
this->icePairedWithUseCandidate = true;
// If ready, run the DTLS handler.
MayRunDTLSTransport();
}
switch (SetSendingSource(source))
void Transport::onICEConnected(RTC::IceServer* iceServer)
{
// This is a new source.
case 0:
MS_DEBUG("got a new ICE valid pair [UseCandidate:%s]", has_use_candidate ? "true" : "false");
break;
// This is already the sending source.
case 1:
break;
MS_TRACE();
// This is another valid source.
case 2:
MS_DEBUG("got a previous ICE valid pair [UseCandidate:%s]", has_use_candidate ? "true" : "false");
break;
MS_DEBUG("ICE connected");
}
// If ready, run the DTLS handler.
MayRunDTLSTransport();
void Transport::onICECompleted(RTC::IceServer* iceServer)
{
MS_TRACE();
MS_DEBUG("ICE completed");
}
void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport)
......@@ -880,17 +880,17 @@ namespace RTC
{
MS_TRACE();
if (!this->sendingSource)
if (!this->selectedTuple)
{
MS_DEBUG("no sending address set, cannot send DTLS packet");
MS_DEBUG("no media address set, cannot send DTLS packet");
return;
}
// TODO: remove
MS_DEBUG("sending DTLS data to to:");
this->sendingSource->Dump();
MS_DEBUG("media DTLS data to to:");
this->selectedTuple->Dump();
this->sendingSource->Send(data, len);
this->selectedTuple->Send(data, len);
}
void Transport::onDTLSApplicationData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len)
......
#define MS_CLASS "RTC::TransportSource"
#define MS_CLASS "RTC::TransportTuple"
#include "RTC/TransportSource.h"
#include "RTC/TransportTuple.h"
#include "Logger.h"
#include <string>
namespace RTC
{
/* Instance methods. */
void TransportSource::Dump()
void TransportTuple::Dump()
{
MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论