提交 9f73fee8 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

C++ cleanup and add ICE state "disconnected".

上级 4743f0e4
...@@ -77,6 +77,11 @@ class Transport extends EventEmitter ...@@ -77,6 +77,11 @@ class Transport extends EventEmitter
case 'icestatechange': case 'icestatechange':
{ {
this._data.iceState = data.iceState; this._data.iceState = data.iceState;
// Unset .iceSelectedTuple if required
if (data.iceState === 'disconnected')
{
delete this._data.iceSelectedTuple;
}
// Emit it to the app // Emit it to the app
this.emit(event, data); this.emit(event, data);
break; break;
...@@ -157,6 +162,8 @@ class Transport extends EventEmitter ...@@ -157,6 +162,8 @@ class Transport extends EventEmitter
// We won't receive events anymore, so be nice and manually update // We won't receive events anymore, so be nice and manually update
// some fields and emit events before firing 'close' // some fields and emit events before firing 'close'
delete this._data.iceSelectedTuple;
if (this._data.iceState !== 'closed') if (this._data.iceState !== 'closed')
{ {
this._data.iceState = 'closed'; this._data.iceState = 'closed';
......
...@@ -7,7 +7,7 @@ debugerror.log = console.error.bind(console); ...@@ -7,7 +7,7 @@ debugerror.log = console.error.bind(console);
const domready = require('domready'); const domready = require('domready');
const rtcninja = require('rtcninja'); const rtcninja = require('rtcninja');
// const randomString = require('random-string'); const randomString = require('random-string');
const Peer = require('./Peer'); const Peer = require('./Peer');
let protooWsUrl; let protooWsUrl;
...@@ -33,10 +33,10 @@ function run() ...@@ -33,10 +33,10 @@ function run()
{ {
debug('run()'); debug('run()');
// let username = randomString(); let username = randomString();
// let uuid = randomString(); let uuid = randomString();
let username = 'alice'; // let username = 'alice';
let uuid = 'aaaa'; // let uuid = 'aaaa';
let peer = new Peer(protooWsUrl, username, uuid); let peer = new Peer(protooWsUrl, username, uuid);
......
...@@ -171,7 +171,7 @@ app.put('/test-transport', function(req) ...@@ -171,7 +171,7 @@ app.put('/test-transport', function(req)
{ {
let promise = mediaPeer.createTransport( let promise = mediaPeer.createTransport(
{ {
udp : true, udp : false,
tcp : true, tcp : true,
preferIPv4 : true, preferIPv4 : true,
preferUdp : true preferUdp : true
......
...@@ -20,7 +20,7 @@ public: ...@@ -20,7 +20,7 @@ public:
private: private:
void Close(); void Close();
RTC::Room* GetRoomFromRequest(Channel::Request* request, unsigned int* roomId = nullptr); RTC::Room* GetRoomFromRequest(Channel::Request* request, uint32_t* roomId = nullptr);
/* Methods inherited from SignalsHandler::Listener. */ /* Methods inherited from SignalsHandler::Listener. */
public: public:
...@@ -43,7 +43,7 @@ private: ...@@ -43,7 +43,7 @@ private:
SignalsHandler* signalsHandler = nullptr; SignalsHandler* signalsHandler = nullptr;
// Others. // Others.
bool closed = false; bool closed = false;
std::unordered_map<unsigned int, RTC::Room*> rooms; std::unordered_map<uint32_t, RTC::Room*> rooms;
}; };
#endif #endif
...@@ -23,7 +23,8 @@ namespace RTC ...@@ -23,7 +23,8 @@ namespace RTC
{ {
NEW = 1, NEW = 1,
CONNECTED, CONNECTED,
COMPLETED COMPLETED,
DISCONNECTED
}; };
public: public:
...@@ -38,6 +39,7 @@ namespace RTC ...@@ -38,6 +39,7 @@ namespace RTC
virtual void onICESelectedTuple(ICEServer* iceServer, RTC::TransportTuple* tuple) = 0; virtual void onICESelectedTuple(ICEServer* iceServer, RTC::TransportTuple* tuple) = 0;
virtual void onICEConnected(ICEServer* iceServer) = 0; virtual void onICEConnected(ICEServer* iceServer) = 0;
virtual void onICECompleted(ICEServer* iceServer) = 0; virtual void onICECompleted(ICEServer* iceServer) = 0;
virtual void onICEDisconnected(ICEServer* iceServer) = 0;
}; };
public: public:
...@@ -50,6 +52,7 @@ namespace RTC ...@@ -50,6 +52,7 @@ namespace RTC
std::string& GetPassword(); std::string& GetPassword();
IceState GetState(); IceState GetState();
bool IsValidTuple(RTC::TransportTuple* tuple); bool IsValidTuple(RTC::TransportTuple* tuple);
void RemoveTuple(RTC::TransportTuple* tuple);
// This should be just called in 'connected' or completed' state // This should be just called in 'connected' or completed' state
// and the given tuple must be an already valid tuple. // and the given tuple must be an already valid tuple.
void ForceSelectedTuple(RTC::TransportTuple* tuple); void ForceSelectedTuple(RTC::TransportTuple* tuple);
......
...@@ -31,15 +31,15 @@ namespace RTC ...@@ -31,15 +31,15 @@ namespace RTC
}; };
public: public:
IceCandidate(RTC::UDPSocket* udpSocket, uint64_t priority); IceCandidate(RTC::UDPSocket* udpSocket, uint32_t priority);
IceCandidate(RTC::TCPServer* tcpServer, uint64_t priority); IceCandidate(RTC::TCPServer* tcpServer, uint32_t priority);
Json::Value toJson(); Json::Value toJson();
private: private:
// Others. // Others.
std::string foundation; std::string foundation;
uint64_t priority; uint32_t priority;
std::string ip; std::string ip;
Protocol protocol; Protocol protocol;
MS_PORT port; MS_PORT port;
......
#ifndef MS_RTC_PEER_H #ifndef MS_RTC_PEER_H
#define MS_RTC_PEER_H #define MS_RTC_PEER_H
#include "common.h"
#include "RTC/Transport.h" #include "RTC/Transport.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include "Channel/Notifier.h" #include "Channel/Notifier.h"
...@@ -21,7 +22,7 @@ namespace RTC ...@@ -21,7 +22,7 @@ namespace RTC
}; };
public: public:
Peer(Listener* listener, Channel::Notifier* notifier, unsigned int peerId, std::string& peerName); Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName);
virtual ~Peer(); virtual ~Peer();
void Close(); void Close();
...@@ -29,7 +30,7 @@ namespace RTC ...@@ -29,7 +30,7 @@ namespace RTC
void HandleRequest(Channel::Request* request); void HandleRequest(Channel::Request* request);
private: private:
RTC::Transport* GetTransportFromRequest(Channel::Request* request, unsigned int* iceTransportId = nullptr); RTC::Transport* GetTransportFromRequest(Channel::Request* request, uint32_t* iceTransportId = nullptr);
/* Pure virtual methods inherited from RTC::Transport::Listener. */ /* Pure virtual methods inherited from RTC::Transport::Listener. */
public: public:
...@@ -37,7 +38,7 @@ namespace RTC ...@@ -37,7 +38,7 @@ namespace RTC
public: public:
// Passed by argument. // Passed by argument.
unsigned int peerId; uint32_t peerId;
std::string peerName; std::string peerName;
private: private:
...@@ -45,7 +46,7 @@ namespace RTC ...@@ -45,7 +46,7 @@ namespace RTC
Listener* listener = nullptr; Listener* listener = nullptr;
Channel::Notifier* notifier = nullptr; Channel::Notifier* notifier = nullptr;
// Others. // Others.
std::unordered_map<unsigned int, RTC::Transport*> transports; std::unordered_map<uint32_t, RTC::Transport*> transports;
}; };
} }
......
...@@ -31,11 +31,8 @@ namespace RTC ...@@ -31,11 +31,8 @@ namespace RTC
virtual void onTransportClosed(RTC::Transport* transport) = 0; virtual void onTransportClosed(RTC::Transport* transport) = 0;
}; };
private:
static size_t maxTuples;
public: public:
Transport(Listener* listener, Channel::Notifier* notifier, unsigned int transportId, Json::Value& data, Transport* rtpTransport = nullptr); Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data, Transport* rtpTransport = nullptr);
virtual ~Transport(); virtual ~Transport();
void Close(); void Close();
...@@ -43,7 +40,7 @@ namespace RTC ...@@ -43,7 +40,7 @@ namespace RTC
void HandleRequest(Channel::Request* request); void HandleRequest(Channel::Request* request);
std::string& GetIceUsernameFragment(); std::string& GetIceUsernameFragment();
std::string& GetIcePassword(); std::string& GetIcePassword();
Transport* CreateAssociatedTransport(unsigned int transportId); Transport* CreateAssociatedTransport(uint32_t transportId);
private: private:
void ClosePorts(); void ClosePorts();
...@@ -81,6 +78,7 @@ namespace RTC ...@@ -81,6 +78,7 @@ namespace RTC
virtual void onICESelectedTuple(ICEServer* iceServer, RTC::TransportTuple* tuple) override; virtual void onICESelectedTuple(ICEServer* iceServer, RTC::TransportTuple* tuple) override;
virtual void onICEConnected(ICEServer* iceServer) override; virtual void onICEConnected(ICEServer* iceServer) override;
virtual void onICECompleted(ICEServer* iceServer) override; virtual void onICECompleted(ICEServer* iceServer) override;
virtual void onICEDisconnected(ICEServer* iceServer) override;
/* Pure virtual methods inherited from RTC::DTLSTransport::Listener. */ /* Pure virtual methods inherited from RTC::DTLSTransport::Listener. */
public: public:
...@@ -93,7 +91,7 @@ namespace RTC ...@@ -93,7 +91,7 @@ namespace RTC
public: public:
// Passed by argument. // Passed by argument.
unsigned int transportId; uint32_t transportId;
protected: protected:
// Others. // Others.
......
...@@ -199,14 +199,13 @@ namespace Utils ...@@ -199,14 +199,13 @@ namespace Utils
public: public:
static void ClassInit(); static void ClassInit();
static void ClassDestroy(); static void ClassDestroy();
static unsigned int GetRandomUInt(unsigned int min, unsigned int max); static uint32_t GetRandomUInt(uint32_t min, uint32_t max);
static const std::string GetRandomString(size_t len); static const std::string GetRandomString(size_t len);
// static const char* GetRandomHexString(char* str, size_t len);
static MS_4BYTES GetCRC32(const MS_BYTE* data, size_t size); static MS_4BYTES GetCRC32(const MS_BYTE* data, size_t size);
static const MS_BYTE* GetHMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len); static const MS_BYTE* GetHMAC_SHA1(const std::string &key, const MS_BYTE* data, size_t len);
private: private:
static unsigned int seed; static uint32_t seed;
static HMAC_CTX hmacSha1Ctx; static HMAC_CTX hmacSha1Ctx;
static MS_BYTE hmacSha1Buffer[]; static MS_BYTE hmacSha1Buffer[];
static const MS_4BYTES crc32Table[256]; static const MS_4BYTES crc32Table[256];
...@@ -215,14 +214,14 @@ namespace Utils ...@@ -215,14 +214,14 @@ namespace Utils
/* Inline static methods. */ /* Inline static methods. */
inline inline
unsigned int Crypto::GetRandomUInt(unsigned int min, unsigned int max) uint32_t Crypto::GetRandomUInt(uint32_t min, uint32_t max)
{ {
// NOTE: This is the original, but produces very small values. // NOTE: This is the original, but produces very small values.
// Crypto::seed = (214013 * Crypto::seed) + 2531011; // Crypto::seed = (214013 * Crypto::seed) + 2531011;
// return (((Crypto::seed>>16)&0x7FFF) % (max - min + 1)) + min; // return (((Crypto::seed>>16)&0x7FFF) % (max - min + 1)) + min;
// This seems to produce better results. // This seems to produce better results.
Crypto::seed = (unsigned int)((214013 * Crypto::seed) + 2531011); Crypto::seed = (uint32_t)((214013 * Crypto::seed) + 2531011);
return (((Crypto::seed>>4)&0x7FFF7FFF) % (max - min + 1)) + min; return (((Crypto::seed>>4)&0x7FFF7FFF) % (max - min + 1)) + min;
} }
...@@ -247,23 +246,6 @@ namespace Utils ...@@ -247,23 +246,6 @@ namespace Utils
return std::string(buffer, len); return std::string(buffer, len);
} }
// inline
// const char* Crypto::GetRandomHexString(char* buffer, size_t len)
// {
// static const char hex_chars[] =
// {
// '0','1','2','3','4','5','6','7','8','9',
// 'A','B','C','D','E','F','G','H','I','J',
// 'K','L','M','N','O','P','Q','R','S','T',
// 'U','V','W','X','Y','Z'
// };
// for (size_t i = 0; i < len; ++i)
// buffer[i] = hex_chars[GetRandomUInt(0, sizeof(hex_chars) - 1)];
// return buffer;
// }
inline inline
MS_4BYTES Crypto::GetCRC32(const MS_BYTE* data, size_t size) MS_4BYTES Crypto::GetCRC32(const MS_BYTE* data, size_t size)
{ {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdint> // uint8_t, etc #include <cstdint> // uint8_t, etc
#include <cinttypes> // PRIu64, etc
#include <sys/socket.h> // struct sockaddr, struct sockaddr_storage, AF_INET, AF_INET6 #include <sys/socket.h> // struct sockaddr, struct sockaddr_storage, AF_INET, AF_INET6
#include <netinet/in.h> // sockaddr_in, sockaddr_in6 #include <netinet/in.h> // sockaddr_in, sockaddr_in6
#include <arpa/inet.h> // htonl(), htons(), ntohl(), ntohs() #include <arpa/inet.h> // htonl(), htons(), ntohl(), ntohs()
......
...@@ -87,7 +87,7 @@ void Loop::Close() ...@@ -87,7 +87,7 @@ void Loop::Close()
this->channel->Close(); this->channel->Close();
} }
RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roomId) RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* roomId)
{ {
MS_TRACE(); MS_TRACE();
...@@ -177,7 +177,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -177,7 +177,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
case Channel::Request::MethodId::worker_createRoom: case Channel::Request::MethodId::worker_createRoom:
{ {
RTC::Room* room; RTC::Room* room;
unsigned int roomId; uint32_t roomId;
try try
{ {
...@@ -209,7 +209,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -209,7 +209,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
this->rooms[roomId] = room; this->rooms[roomId] = room;
MS_DEBUG("Room created [roomId:%u]", roomId); MS_DEBUG("Room created [roomId:%" PRIu32 "]", roomId);
request->Accept(); request->Accept();
break; break;
......
...@@ -206,8 +206,8 @@ namespace RTC ...@@ -206,8 +206,8 @@ namespace RTC
LOG_OPENSSL_ERROR("X509_get_subject_name() failed"); LOG_OPENSSL_ERROR("X509_get_subject_name() failed");
goto error; goto error;
} }
X509_NAME_add_entry_by_txt(cert_name, "O", MBSTRING_ASC, (unsigned char *)MS_APP_NAME, -1, -1, 0); X509_NAME_add_entry_by_txt(cert_name, "O", MBSTRING_ASC, (uint8_t*)MS_APP_NAME, -1, -1, 0);
X509_NAME_add_entry_by_txt(cert_name, "CN", MBSTRING_ASC, (unsigned char *)MS_APP_NAME, -1, -1, 0); X509_NAME_add_entry_by_txt(cert_name, "CN", MBSTRING_ASC, (uint8_t*)MS_APP_NAME, -1, -1, 0);
// It is self-signed so set the issuer name to be the same as the subject. // It is self-signed so set the issuer name to be the same as the subject.
ret = X509_set_issuer_name(DTLSTransport::certificate, cert_name); ret = X509_set_issuer_name(DTLSTransport::certificate, cert_name);
...@@ -418,7 +418,7 @@ namespace RTC ...@@ -418,7 +418,7 @@ namespace RTC
{ {
std::string algorithm_str = it->first; std::string algorithm_str = it->first;
FingerprintAlgorithm algorithm = it->second; FingerprintAlgorithm algorithm = it->second;
unsigned char binary_fingerprint[EVP_MAX_MD_SIZE]; uint8_t binary_fingerprint[EVP_MAX_MD_SIZE];
unsigned int size = 0; unsigned int size = 0;
char hex_fingerprint[(EVP_MAX_MD_SIZE * 2) + 1]; char hex_fingerprint[(EVP_MAX_MD_SIZE * 2) + 1];
const EVP_MD* hash_function; const EVP_MD* hash_function;
...@@ -960,7 +960,7 @@ namespace RTC ...@@ -960,7 +960,7 @@ namespace RTC
MS_ASSERT(this->remoteFingerprint.algorithm != FingerprintAlgorithm::NONE, "remote fingerprint not set"); MS_ASSERT(this->remoteFingerprint.algorithm != FingerprintAlgorithm::NONE, "remote fingerprint not set");
X509* certificate; X509* certificate;
unsigned char binary_fingerprint[EVP_MAX_MD_SIZE]; uint8_t binary_fingerprint[EVP_MAX_MD_SIZE];
unsigned int size = 0; unsigned int size = 0;
char hex_fingerprint[(EVP_MAX_MD_SIZE * 2) + 1]; char hex_fingerprint[(EVP_MAX_MD_SIZE * 2) + 1];
const EVP_MD* hash_function; const EVP_MD* hash_function;
......
...@@ -136,7 +136,7 @@ namespace RTC ...@@ -136,7 +136,7 @@ namespace RTC
return; return;
} }
MS_DEBUG("processing STUN Binding Request [Priority:%u, UseCandidate:%s]", (unsigned int)msg->GetPriority(), msg->HasUseCandidate() ? "true" : "false"); MS_DEBUG("processing STUN Binding Request [Priority:%" PRIu32 ", UseCandidate:%s]", (uint32_t)msg->GetPriority(), msg->HasUseCandidate() ? "true" : "false");
// Create a success response. // Create a success response.
RTC::STUNMessage* response = msg->CreateSuccessResponse(); RTC::STUNMessage* response = msg->CreateSuccessResponse();
...@@ -191,13 +191,63 @@ namespace RTC ...@@ -191,13 +191,63 @@ namespace RTC
return false; return false;
} }
void ICEServer::RemoveTuple(RTC::TransportTuple* tuple)
{
auto it = this->tuples.begin();
RTC::TransportTuple* removed_tuple = nullptr;
// Find the removed tuple.
for (; it != this->tuples.end(); ++it)
{
RTC::TransportTuple* stored_tuple = &(*it);
if (stored_tuple->Compare(tuple))
{
removed_tuple = stored_tuple;
break;
}
}
// If not found, ignore.
if (!removed_tuple)
return;
// If this is not the selected tuple just remove it.
if (removed_tuple != this->selectedTuple)
{
this->tuples.erase(it);
return;
}
// Otherwise this was the selected tuple.
else
{
this->tuples.erase(it);
this->selectedTuple = nullptr;
// Mark the first tuple as selected tuple (if any).
if (this->tuples.begin() != this->tuples.end())
{
SetSelectedTuple(&(*this->tuples.begin()));
}
// Or just emit 'disconnected' and the null selected tuple..
else
{
// Update state.
this->state = IceState::DISCONNECTED;
// Notify the listener.
this->listener->onICESelectedTuple(this, nullptr);
this->listener->onICEDisconnected(this);
}
}
}
void ICEServer::ForceSelectedTuple(RTC::TransportTuple* tuple) void ICEServer::ForceSelectedTuple(RTC::TransportTuple* tuple)
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(this->state != IceState::NEW, "cannot force the selected tuple if not in 'connecting' or 'connected' state");
// TODO: Remove as this assert is reduntant.
MS_ASSERT(this->selectedTuple != nullptr, "cannot force the selected tuple if there was not a selected tuple"); MS_ASSERT(this->selectedTuple != nullptr, "cannot force the selected tuple if there was not a selected tuple");
auto stored_tuple = HasTuple(tuple); auto stored_tuple = HasTuple(tuple);
...@@ -258,6 +308,50 @@ namespace RTC ...@@ -258,6 +308,50 @@ namespace RTC
break; break;
} }
case IceState::DISCONNECTED:
{
// There should be no tuples.
MS_ASSERT(this->tuples.size() == 0, "state is 'disconnected' but there are %zu tuples", this->tuples.size());
// There shouldn't be a selected tuple.
MS_ASSERT(this->selectedTuple == nullptr, "state is 'disconnected' but there is selected tuple");
if (!has_use_candidate)
{
MS_DEBUG("transition from state 'disconnected' 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->onICEConnected(this);
}
else
{
MS_DEBUG("transition from state 'disconnected' 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->onICECompleted(this);
}
break;
}
case IceState::CONNECTED: case IceState::CONNECTED:
{ {
// There should be some tuples. // There should be some tuples.
...@@ -385,42 +479,4 @@ namespace RTC ...@@ -385,42 +479,4 @@ namespace RTC
// Notify the listener. // Notify the listener.
this->listener->onICESelectedTuple(this, this->selectedTuple); this->listener->onICESelectedTuple(this, this->selectedTuple);
} }
// 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;
// }
} }
...@@ -6,7 +6,7 @@ namespace RTC ...@@ -6,7 +6,7 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
IceCandidate::IceCandidate(RTC::UDPSocket* udpSocket, uint64_t priority) : IceCandidate::IceCandidate(RTC::UDPSocket* udpSocket, uint32_t priority) :
foundation("udpcandidate"), foundation("udpcandidate"),
priority(priority), priority(priority),
ip(udpSocket->GetLocalIP()), ip(udpSocket->GetLocalIP()),
...@@ -15,7 +15,7 @@ namespace RTC ...@@ -15,7 +15,7 @@ namespace RTC
type(CandidateType::HOST) type(CandidateType::HOST)
{} {}
IceCandidate::IceCandidate(RTC::TCPServer* tcpServer, uint64_t priority) : IceCandidate::IceCandidate(RTC::TCPServer* tcpServer, uint32_t priority) :
foundation("tcpcandidate"), foundation("tcpcandidate"),
priority(priority), priority(priority),
ip(tcpServer->GetLocalIP()), ip(tcpServer->GetLocalIP()),
...@@ -42,7 +42,7 @@ namespace RTC ...@@ -42,7 +42,7 @@ namespace RTC
Json::Value data; Json::Value data;
data[k_foundation] = this->foundation; data[k_foundation] = this->foundation;
data[k_priority] = (unsigned int)this->priority; data[k_priority] = this->priority;
data[k_ip] = this->ip; data[k_ip] = this->ip;
data[k_port] = this->port; data[k_port] = this->port;
......
...@@ -8,7 +8,7 @@ namespace RTC ...@@ -8,7 +8,7 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
Peer::Peer(Listener* listener, Channel::Notifier* notifier, unsigned int peerId, std::string& peerName) : Peer::Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName) :
peerId(peerId), peerId(peerId),
peerName(peerName), peerName(peerName),
listener(listener), listener(listener),
...@@ -75,11 +75,11 @@ namespace RTC ...@@ -75,11 +75,11 @@ namespace RTC
{ {
case Channel::Request::MethodId::peer_close: case Channel::Request::MethodId::peer_close:
{ {
unsigned int peerId = this->peerId; uint32_t peerId = this->peerId;
Close(); Close();
MS_DEBUG("Peer closed [peerId:%u]", peerId); MS_DEBUG("Peer closed [peerId:%" PRIu32 "]", peerId);
request->Accept(); request->Accept();
break; break;
...@@ -97,7 +97,7 @@ namespace RTC ...@@ -97,7 +97,7 @@ namespace RTC
case Channel::Request::MethodId::peer_createTransport: case Channel::Request::MethodId::peer_createTransport:
{ {
RTC::Transport* transport; RTC::Transport* transport;
unsigned int transportId; uint32_t transportId;
try try
{ {
...@@ -129,7 +129,7 @@ namespace RTC ...@@ -129,7 +129,7 @@ namespace RTC
this->transports[transportId] = transport; this->transports[transportId] = transport;
MS_DEBUG("Transport created [transportId:%u]", transportId); MS_DEBUG("Transport created [transportId:%" PRIu32 "]", transportId);
auto data = transport->toJson(); auto data = transport->toJson();
...@@ -141,7 +141,7 @@ namespace RTC ...@@ -141,7 +141,7 @@ namespace RTC
case Channel::Request::MethodId::peer_createAssociatedTransport: case Channel::Request::MethodId::peer_createAssociatedTransport:
{ {
RTC::Transport* transport; RTC::Transport* transport;
unsigned int transportId; uint32_t transportId;
try try
{ {
...@@ -197,7 +197,7 @@ namespace RTC ...@@ -197,7 +197,7 @@ namespace RTC
this->transports[transportId] = transport; this->transports[transportId] = transport;
MS_DEBUG("Associated Transport created [transportId:%u]", transportId); MS_DEBUG("Associated Transport created [transportId:%" PRIu32 "]", transportId);
auto data = transport->toJson(); auto data = transport->toJson();
...@@ -242,7 +242,7 @@ namespace RTC ...@@ -242,7 +242,7 @@ namespace RTC
} }
} }
RTC::Transport* Peer::GetTransportFromRequest(Channel::Request* request, unsigned int* transportId) RTC::Transport* Peer::GetTransportFromRequest(Channel::Request* request, uint32_t* transportId)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
/* Static helpers. */ /* Static helpers. */
static inline static inline
uint64_t generateIceCandidatePriority(uint16_t local_preference, RTC::ICEServer::IceComponent component) uint32_t generateIceCandidatePriority(uint16_t local_preference, RTC::ICEServer::IceComponent component)
{ {
MS_TRACE(); MS_TRACE();
...@@ -29,13 +29,9 @@ uint64_t generateIceCandidatePriority(uint16_t local_preference, RTC::ICEServer: ...@@ -29,13 +29,9 @@ uint64_t generateIceCandidatePriority(uint16_t local_preference, RTC::ICEServer:
namespace RTC namespace RTC
{ {
/* Class variables. */
size_t Transport::maxTuples = 8;
/* Instance methods. */ /* Instance methods. */
Transport::Transport(Listener* listener, Channel::Notifier* notifier, unsigned int transportId, Json::Value& data, Transport* rtpTransport) : Transport::Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data, Transport* rtpTransport) :
transportId(transportId), transportId(transportId),
listener(listener), listener(listener),
notifier(notifier) notifier(notifier)
...@@ -108,7 +104,7 @@ namespace RTC ...@@ -108,7 +104,7 @@ namespace RTC
if (preferUdp) if (preferUdp)
local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT; local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT;
uint64_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent()); uint32_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent());
try try
{ {
...@@ -135,7 +131,7 @@ namespace RTC ...@@ -135,7 +131,7 @@ namespace RTC
if (preferUdp) if (preferUdp)
local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT; local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT;
uint64_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent()); uint32_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent());
try try
{ {
...@@ -162,7 +158,7 @@ namespace RTC ...@@ -162,7 +158,7 @@ namespace RTC
if (preferTcp) if (preferTcp)
local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT; local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT;
uint64_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent()); uint32_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent());
try try
{ {
...@@ -189,7 +185,7 @@ namespace RTC ...@@ -189,7 +185,7 @@ namespace RTC
if (preferTcp) if (preferTcp)
local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT; local_preference += ICE_CANDIDATE_LOCAL_PRIORITY_PREFER_PROTOCOL_INCREMENT;
uint64_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent()); uint32_t priority = generateIceCandidatePriority(local_preference, this->iceServer->GetComponent());
try try
{ {
...@@ -283,6 +279,7 @@ namespace RTC ...@@ -283,6 +279,7 @@ namespace RTC
static const Json::StaticString v_new("new"); static const Json::StaticString v_new("new");
static const Json::StaticString v_connected("connected"); static const Json::StaticString v_connected("connected");
static const Json::StaticString v_completed("completed"); static const Json::StaticString v_completed("completed");
static const Json::StaticString v_disconnected("disconnected");
static const Json::StaticString k_dtlsLocalParameters("dtlsLocalParameters"); static const Json::StaticString k_dtlsLocalParameters("dtlsLocalParameters");
static const Json::StaticString k_fingerprints("fingerprints"); static const Json::StaticString k_fingerprints("fingerprints");
static const Json::StaticString k_role("role"); static const Json::StaticString k_role("role");
...@@ -329,6 +326,9 @@ namespace RTC ...@@ -329,6 +326,9 @@ namespace RTC
case RTC::ICEServer::IceState::COMPLETED: case RTC::ICEServer::IceState::COMPLETED:
data[k_iceState] = v_completed; data[k_iceState] = v_completed;
break; break;
case RTC::ICEServer::IceState::DISCONNECTED:
data[k_iceState] = v_disconnected;
break;
} }
// Add `dtlsLocalParameters.fingerprints`. // Add `dtlsLocalParameters.fingerprints`.
...@@ -381,11 +381,11 @@ namespace RTC ...@@ -381,11 +381,11 @@ namespace RTC
{ {
case Channel::Request::MethodId::transport_close: case Channel::Request::MethodId::transport_close:
{ {
unsigned int transportId = this->transportId; uint32_t transportId = this->transportId;
Close(); Close();
MS_DEBUG("Transport closed [transportId:%u]", transportId); MS_DEBUG("Transport closed [transportId:%" PRIu32 "]", transportId);
request->Accept(); request->Accept();
break; break;
...@@ -522,6 +522,8 @@ namespace RTC ...@@ -522,6 +522,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
this->selectedTuple = nullptr;
for (auto socket : this->udpSockets) for (auto socket : this->udpSockets)
socket->Close(); socket->Close();
this->udpSockets.clear(); this->udpSockets.clear();
...@@ -531,7 +533,7 @@ namespace RTC ...@@ -531,7 +533,7 @@ namespace RTC
this->tcpServers.clear(); this->tcpServers.clear();
} }
Transport* Transport::CreateAssociatedTransport(unsigned int transportId) Transport* Transport::CreateAssociatedTransport(uint32_t transportId)
{ {
MS_TRACE(); MS_TRACE();
...@@ -738,14 +740,9 @@ namespace RTC ...@@ -738,14 +740,9 @@ namespace RTC
if (!IsAlive()) if (!IsAlive())
return; return;
// TODO RTC::TransportTuple tuple(connection);
// RTC::TransportTuple tuple(connection);
// if (RemoveTuple(&tuple)) this->iceServer->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) void Transport::onSTUNDataRecv(RTC::TCPConnection *connection, const MS_BYTE* data, size_t len)
...@@ -820,11 +817,12 @@ namespace RTC ...@@ -820,11 +817,12 @@ namespace RTC
this->selectedTuple = tuple; this->selectedTuple = tuple;
// Notify. // Notify if tuple is given.
eventData[k_iceSelectedTuple] = tuple->toJson(); if (tuple)
{
// Notify. eventData[k_iceSelectedTuple] = tuple->toJson();
this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData); this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData);
}
} }
void Transport::onICEConnected(RTC::ICEServer* iceServer) void Transport::onICEConnected(RTC::ICEServer* iceServer)
...@@ -865,6 +863,24 @@ namespace RTC ...@@ -865,6 +863,24 @@ namespace RTC
MayRunDTLSTransport(); MayRunDTLSTransport();
} }
void Transport::onICEDisconnected(RTC::ICEServer* iceServer)
{
MS_TRACE();
static const Json::StaticString k_iceState("iceState");
static const Json::StaticString v_disconnected("disconnected");
Json::Value eventData;
MS_DEBUG("ICE disconnected");
// Unset the
// Notify.
eventData[k_iceState] = v_disconnected;
this->notifier->Emit(this->transportId, "icestatechange", eventData);
}
void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
...@@ -901,39 +917,39 @@ namespace RTC ...@@ -901,39 +917,39 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState"); static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_closed("closed"); static const Json::StaticString v_failed("failed");
Json::Value eventData; Json::Value eventData;
MS_DEBUG("DTLS remotely disconnected"); MS_DEBUG("DTLS failed");
ClosePorts(); ClosePorts();
// Notify. // Notify.
eventData[k_dtlsState] = v_closed; eventData[k_dtlsState] = v_failed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
void Transport::onDTLSFailed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_dtlsState("dtlsState"); static const Json::StaticString k_dtlsState("dtlsState");
static const Json::StaticString v_failed("failed"); static const Json::StaticString v_closed("closed");
Json::Value eventData; Json::Value eventData;
MS_DEBUG("DTLS failed"); MS_DEBUG("DTLS remotely disconnected");
ClosePorts(); ClosePorts();
// Notify. // Notify.
eventData[k_dtlsState] = v_failed; eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
} }
...@@ -943,7 +959,7 @@ namespace RTC ...@@ -943,7 +959,7 @@ namespace RTC
if (!this->selectedTuple) if (!this->selectedTuple)
{ {
MS_DEBUG("no media address set, cannot send DTLS packet"); MS_DEBUG("no selected tuple set, cannot send DTLS packet");
return; return;
} }
......
...@@ -8,7 +8,7 @@ namespace Utils ...@@ -8,7 +8,7 @@ namespace Utils
{ {
/* Static variables. */ /* Static variables. */
unsigned int Crypto::seed; uint32_t Crypto::seed;
HMAC_CTX Crypto::hmacSha1Ctx; HMAC_CTX Crypto::hmacSha1Ctx;
MS_BYTE Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long. MS_BYTE Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long.
const MS_4BYTES Crypto::crc32Table[] = const MS_4BYTES Crypto::crc32Table[] =
...@@ -66,7 +66,7 @@ namespace Utils ...@@ -66,7 +66,7 @@ namespace Utils
// Init the vrypto seed with a random number taken from the address // Init the vrypto seed with a random number taken from the address
// of the seed variable itself (which is random). // of the seed variable itself (which is random).
Crypto::seed = (unsigned int)(unsigned long)&Crypto::seed; Crypto::seed = (uint32_t)(unsigned long)&Crypto::seed;
// Create an OpenSSL HMAC_CTX context for HMAC SHA1 calculation. // Create an OpenSSL HMAC_CTX context for HMAC SHA1 calculation.
HMAC_CTX_init(&Crypto::hmacSha1Ctx); HMAC_CTX_init(&Crypto::hmacSha1Ctx);
...@@ -88,11 +88,11 @@ namespace Utils ...@@ -88,11 +88,11 @@ namespace Utils
ret = HMAC_Init_ex(&Crypto::hmacSha1Ctx, key.c_str(), key.length(), EVP_sha1(), nullptr); ret = HMAC_Init_ex(&Crypto::hmacSha1Ctx, key.c_str(), key.length(), EVP_sha1(), nullptr);
MS_ASSERT(ret == 1, "OpenSSL HMAC_Init_ex() failed with key '%s'", key.c_str()); MS_ASSERT(ret == 1, "OpenSSL HMAC_Init_ex() failed with key '%s'", key.c_str());
ret = HMAC_Update(&Crypto::hmacSha1Ctx, (const unsigned char*)data, (int)len); ret = HMAC_Update(&Crypto::hmacSha1Ctx, (const uint8_t*)data, (int)len);
MS_ASSERT(ret == 1, "OpenSSL HMAC_Update() failed with key '%s' and data length %zu bytes", key.c_str(), len); MS_ASSERT(ret == 1, "OpenSSL HMAC_Update() failed with key '%s' and data length %zu bytes", key.c_str(), len);
unsigned int result_len; uint32_t result_len;
ret = HMAC_Final(&Crypto::hmacSha1Ctx, (unsigned char *)Crypto::hmacSha1Buffer, &result_len); ret = HMAC_Final(&Crypto::hmacSha1Ctx, (uint8_t *)Crypto::hmacSha1Buffer, &result_len);
MS_ASSERT(ret == 1, "OpenSSL HMAC_Final() failed with key '%s' and data length %zu bytes", key.c_str(), len); MS_ASSERT(ret == 1, "OpenSSL HMAC_Final() failed with key '%s' and data length %zu bytes", key.c_str(), len);
MS_ASSERT(result_len == 20, "OpenSSL HMAC_Final() result_len is %u instead of 20", result_len); MS_ASSERT(result_len == 20, "OpenSSL HMAC_Final() result_len is %u instead of 20", result_len);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论