提交 1f7cc488 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Use unsigned int for peerId rather than a string.

上级 749ce475
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const logger = require('./logger')('Room'); const logger = require('./logger')('Room');
const utils = require('./utils');
const errors = require('./errors'); const errors = require('./errors');
const Peer = require('./Peer'); const Peer = require('./Peer');
...@@ -106,34 +107,35 @@ class Room extends EventEmitter ...@@ -106,34 +107,35 @@ class Room extends EventEmitter
/** /**
* Create a Peer instance * Create a Peer instance
* @param {String} [mandatory] peerId Peer identificator * @param {String} [mandatory] peerName Peer identificator
* @return {Peer} * @return {Peer}
*/ */
Peer(peerId) Peer(peerName)
{ {
logger.debug('Peer() [peerId:%s]', peerId); logger.debug('Peer() [peerName:%s]', peerName);
if (this._closed) if (this._closed)
throw errors.Closed('room closed'); throw errors.Closed('room closed');
if (!peerId || typeof peerId !== 'string') if (!peerName || typeof peerName !== 'string')
throw errors.TypeError('wrong `peerId` parameter'); throw errors.TypeError('wrong `peerName` parameter');
if (this._peers.has(peerId)) if (this._peers.has(peerName))
throw new Error(`peer already exists [peerId:${peerId}]`); throw new Error(`peer already exists [peerName:${peerName}]`);
let internal = let internal =
{ {
roomId : this._internal.roomId, roomId : this._internal.roomId,
peerId : peerId peerId : utils.randomNumber(),
peerName : peerName
}; };
// Create a Peer instance // Create a Peer instance
let peer = new Peer(internal, this._channel); let peer = new Peer(internal, this._channel);
// Store the Peer instance and remove it when closed // Store the Peer instance and remove it when closed
this._peers.set(peerId, peer); this._peers.set(peerName, peer);
peer.on('close', () => this._peers.delete(peerId)); peer.on('close', () => this._peers.delete(peerName));
this._channel.request('room.createPeer', internal) this._channel.request('room.createPeer', internal)
.then(() => .then(() =>
...@@ -152,12 +154,12 @@ class Room extends EventEmitter ...@@ -152,12 +154,12 @@ class Room extends EventEmitter
/** /**
* Get Peer * Get Peer
* @param {String} [mandatory] peerId * @param {String} [mandatory] peerName
* @return {Peer} * @return {Peer}
*/ */
getPeer(peerId) getPeer(peerName)
{ {
return this._peers.get(peerId); return this._peers.get(peerName);
} }
/** /**
......
...@@ -4,7 +4,7 @@ const tap = require('tap'); ...@@ -4,7 +4,7 @@ const tap = require('tap');
const mediasoup = require('../'); const mediasoup = require('../');
tap.test('room.Peer() with `peerId` must succeed', { timeout: 1000 }, (t) => tap.test('room.Peer() with `peerName` must succeed', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
...@@ -23,7 +23,7 @@ tap.test('room.Peer() with `peerId` must succeed', { timeout: 1000 }, (t) => ...@@ -23,7 +23,7 @@ tap.test('room.Peer() with `peerId` must succeed', { timeout: 1000 }, (t) =>
setTimeout(() => peer.close(), 50); setTimeout(() => peer.close(), 50);
}); });
tap.test('room.Peer() without `peerId` must fail', { timeout: 1000 }, (t) => tap.test('room.Peer() without `peerName` must fail', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
...@@ -39,7 +39,7 @@ tap.test('room.Peer() without `peerId` must fail', { timeout: 1000 }, (t) => ...@@ -39,7 +39,7 @@ tap.test('room.Peer() without `peerId` must fail', { timeout: 1000 }, (t) =>
t.end(); t.end();
}); });
tap.test('room.Peer() with same `peerId` must fail', { timeout: 1000 }, (t) => tap.test('room.Peer() with same `peerName` must fail', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
...@@ -57,7 +57,7 @@ tap.test('room.Peer() with same `peerId` must fail', { timeout: 1000 }, (t) => ...@@ -57,7 +57,7 @@ tap.test('room.Peer() with same `peerId` must fail', { timeout: 1000 }, (t) =>
t.end(); t.end();
}); });
tap.test('room.Peer() with same `peerId` must succeed if previous peer was closed before', { timeout: 1000 }, (t) => tap.test('room.Peer() with same `peerName` must succeed if previous peer was closed before', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
...@@ -104,7 +104,7 @@ tap.test('room.dump() must succeed', { timeout: 1000 }, (t) => ...@@ -104,7 +104,7 @@ tap.test('room.dump() must succeed', { timeout: 1000 }, (t) =>
.then((data) => .then((data) =>
{ {
t.pass('room.dump() succeeded'); t.pass('room.dump() succeeded');
t.strictSame(Object.keys(data.peers), ['alice', 'bob'], 'room.dump() should retrieve two peers'); t.equal(Object.keys(data.peers).length, 2, 'room.dump() should retrieve two peers');
t.end(); t.end();
}) })
.catch((error) => t.fail(`room.dump() failed: ${error}`)); .catch((error) => t.fail(`room.dump() failed: ${error}`));
......
...@@ -43,8 +43,7 @@ private: ...@@ -43,8 +43,7 @@ private:
// Others. // Others.
bool closed = false; bool closed = false;
Channel::Notifier* notifier = nullptr; Channel::Notifier* notifier = nullptr;
typedef std::unordered_map<unsigned int, RTC::Room*> Rooms; std::unordered_map<unsigned int, RTC::Room*> rooms;
Rooms rooms;
}; };
#endif #endif
...@@ -20,7 +20,7 @@ namespace RTC ...@@ -20,7 +20,7 @@ namespace RTC
}; };
public: public:
Peer(Listener* listener, std::string& peerId); Peer(Listener* listener, unsigned int peerId, std::string& peerName);
virtual ~Peer(); virtual ~Peer();
void Close(); void Close();
...@@ -36,14 +36,14 @@ namespace RTC ...@@ -36,14 +36,14 @@ namespace RTC
public: public:
// Passed by argument. // Passed by argument.
std::string peerId; unsigned int peerId;
std::string peerName;
private: private:
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
// Others. // Others.
typedef std::unordered_map<unsigned int, RTC::Transport*> Transports; std::unordered_map<unsigned int, RTC::Transport*> transports;
Transports transports;
}; };
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "Channel/Request.h" #include "Channel/Request.h"
#include <string>
#include <unordered_map> #include <unordered_map>
#include <json/json.h> #include <json/json.h>
...@@ -28,7 +27,7 @@ namespace RTC ...@@ -28,7 +27,7 @@ namespace RTC
void HandleRequest(Channel::Request* request); void HandleRequest(Channel::Request* request);
private: private:
RTC::Peer* GetPeerFromRequest(Channel::Request* request, std::string* peerId = nullptr); RTC::Peer* GetPeerFromRequest(Channel::Request* request, unsigned int* peerId = nullptr);
/* Pure virtual methods inherited from RTC::Peer::Listener. */ /* Pure virtual methods inherited from RTC::Peer::Listener. */
public: public:
...@@ -42,8 +41,7 @@ namespace RTC ...@@ -42,8 +41,7 @@ namespace RTC
// Passed by argument. // Passed by argument.
Listener* listener = nullptr; Listener* listener = nullptr;
// Others. // Others.
typedef std::unordered_map<std::string, RTC::Peer*> Peers; std::unordered_map<unsigned int, RTC::Peer*> peers;
Peers peers;
}; };
} }
......
...@@ -96,7 +96,7 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roo ...@@ -96,7 +96,7 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roo
auto jsonRoomId = request->internal[k_roomId]; auto jsonRoomId = request->internal[k_roomId];
if (!jsonRoomId.isUInt()) if (!jsonRoomId.isUInt())
MS_THROW_ERROR("Request has not numeric .roomId field"); MS_THROW_ERROR("Request has not numeric `internal.roomId`");
// If given, fill roomId. // If given, fill roomId.
if (roomId) if (roomId)
......
...@@ -8,8 +8,9 @@ namespace RTC ...@@ -8,8 +8,9 @@ namespace RTC
{ {
/* Instance methods. */ /* Instance methods. */
Peer::Peer(Listener* listener, std::string& peerId) : Peer::Peer(Listener* listener, unsigned int peerId, std::string& peerName) :
peerId(peerId), peerId(peerId),
peerName(peerName),
listener(listener) listener(listener)
{ {
MS_TRACE(); MS_TRACE();
...@@ -46,11 +47,14 @@ namespace RTC ...@@ -46,11 +47,14 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
static const Json::StaticString k_peerName("peerName");
static const Json::StaticString k_transports("transports"); static const Json::StaticString k_transports("transports");
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
Json::Value json_transports(Json::objectValue); Json::Value json_transports(Json::objectValue);
json[k_peerName] = this->peerName;
for (auto& kv : this->transports) for (auto& kv : this->transports)
{ {
RTC::Transport* transport = kv.second; RTC::Transport* transport = kv.second;
...@@ -70,11 +74,11 @@ namespace RTC ...@@ -70,11 +74,11 @@ namespace RTC
{ {
case Channel::Request::MethodId::peer_close: case Channel::Request::MethodId::peer_close:
{ {
std::string peerId = this->peerId; unsigned int peerId = this->peerId;
Close(); Close();
MS_DEBUG("Peer closed [peerId:%s]", peerId.c_str()); MS_DEBUG("Peer closed [peerId:%u]", peerId);
request->Accept(); request->Accept();
break; break;
...@@ -162,9 +166,9 @@ namespace RTC ...@@ -162,9 +166,9 @@ namespace RTC
if (!jsonRtpTransportId.isUInt()) if (!jsonRtpTransportId.isUInt())
{ {
MS_ERROR("Request has not numeric .rtpTransportId field"); MS_ERROR("Request has not numeric `internal.rtpTransportId`");
request->Reject(500, "Request has not numeric .rtpTransportId field"); request->Reject(500, "Request has not numeric `internal.rtpTransportId`");
return; return;
} }
...@@ -246,7 +250,7 @@ namespace RTC ...@@ -246,7 +250,7 @@ namespace RTC
auto jsonTransportId = request->internal[k_transportId]; auto jsonTransportId = request->internal[k_transportId];
if (!jsonTransportId.isUInt()) if (!jsonTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric .transportId field"); MS_THROW_ERROR("Request has not numeric `internal.transportId`");
// If given, fill roomId. // If given, fill roomId.
if (transportId) if (transportId)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "RTC/Room.h" #include "RTC/Room.h"
#include "MediaSoupError.h" #include "MediaSoupError.h"
#include "Logger.h" #include "Logger.h"
#include <string>
namespace RTC namespace RTC
{ {
...@@ -55,7 +56,7 @@ namespace RTC ...@@ -55,7 +56,7 @@ namespace RTC
{ {
RTC::Peer* peer = kv.second; RTC::Peer* peer = kv.second;
json_peers[peer->peerId] = peer->toJson(); json_peers[std::to_string(peer->peerId)] = peer->toJson();
} }
json[k_peers] = json_peers; json[k_peers] = json_peers;
...@@ -91,8 +92,11 @@ namespace RTC ...@@ -91,8 +92,11 @@ namespace RTC
case Channel::Request::MethodId::room_createPeer: case Channel::Request::MethodId::room_createPeer:
{ {
static const Json::StaticString k_peerName("peerName");
RTC::Peer* peer; RTC::Peer* peer;
std::string peerId; unsigned int peerId;
std::string peerName;
try try
{ {
...@@ -112,9 +116,19 @@ namespace RTC ...@@ -112,9 +116,19 @@ namespace RTC
return; return;
} }
if (!request->internal[k_peerName].isString())
{
MS_ERROR("Request has not string `internal.peerName`");
request->Reject(500, "Request has not string `internal.peerName`");
return;
}
peerName = request->internal[k_peerName].asString();
try try
{ {
peer = new RTC::Peer(this, peerId); peer = new RTC::Peer(this, peerId, peerName);
} }
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
...@@ -124,7 +138,7 @@ namespace RTC ...@@ -124,7 +138,7 @@ namespace RTC
this->peers[peerId] = peer; this->peers[peerId] = peer;
MS_DEBUG("Peer created [peerId:%s]", peerId.c_str()); MS_DEBUG("Peer created [peerId:%u, peerName:'%s']", peerId, peerName.c_str());
request->Accept(); request->Accept();
break; break;
...@@ -170,7 +184,7 @@ namespace RTC ...@@ -170,7 +184,7 @@ namespace RTC
} }
} }
RTC::Peer* Room::GetPeerFromRequest(Channel::Request* request, std::string* peerId) RTC::Peer* Room::GetPeerFromRequest(Channel::Request* request, unsigned int* peerId)
{ {
MS_TRACE(); MS_TRACE();
...@@ -178,14 +192,14 @@ namespace RTC ...@@ -178,14 +192,14 @@ namespace RTC
auto jsonPeerId = request->internal[k_peerId]; auto jsonPeerId = request->internal[k_peerId];
if (!jsonPeerId.isString()) if (!jsonPeerId.isUInt())
MS_THROW_ERROR("Request has not string .peerId field"); MS_THROW_ERROR("Request has not numeric .peerId field");
// If given, fill peerId. // If given, fill peerId.
if (peerId) if (peerId)
*peerId = jsonPeerId.asString(); *peerId = jsonPeerId.asUInt();
auto it = this->peers.find(jsonPeerId.asString()); auto it = this->peers.find(jsonPeerId.asUInt());
if (it != this->peers.end()) if (it != this->peers.end())
{ {
......
...@@ -370,18 +370,18 @@ namespace RTC ...@@ -370,18 +370,18 @@ namespace RTC
if (!request->data[k_fingerprint].isObject()) if (!request->data[k_fingerprint].isObject())
{ {
MS_ERROR("missing .fingerprint"); MS_ERROR("missing `data.fingerprint`");
request->Reject(500, "missing .fingerprint"); request->Reject(500, "missing `data.fingerprint`");
return; return;
} }
if (!request->data[k_fingerprint][k_algorithm].isString() || if (!request->data[k_fingerprint][k_algorithm].isString() ||
!request->data[k_fingerprint][k_value].isString()) !request->data[k_fingerprint][k_value].isString())
{ {
MS_ERROR("missing .fingerprint.algorithm and/or .fingerprint.value"); MS_ERROR("missing `data.fingerprint.algorithm` and/or `data.fingerprint.value`");
request->Reject(500, "missing .fingerprint.algorithm and/or .fingerprint.value"); request->Reject(500, "missing `data.fingerprint.algorithm` and/or `data.fingerprint.value`");
return; return;
} }
...@@ -389,9 +389,9 @@ namespace RTC ...@@ -389,9 +389,9 @@ namespace RTC
if (remoteFingerprint.algorithm == RTC::DTLSTransport::FingerprintAlgorithm::NONE) if (remoteFingerprint.algorithm == RTC::DTLSTransport::FingerprintAlgorithm::NONE)
{ {
MS_ERROR("unsupported .fingerprint.algorithm"); MS_ERROR("unsupported `data.fingerprint.algorithm`");
request->Reject(500, "unsupported .fingerprint.algorithm"); request->Reject(500, "unsupported `data.fingerprint.algorithm`");
return; return;
} }
...@@ -416,7 +416,7 @@ namespace RTC ...@@ -416,7 +416,7 @@ namespace RTC
case RTC::DTLSTransport::Role::NONE: case RTC::DTLSTransport::Role::NONE:
MS_ERROR("invalid .role"); MS_ERROR("invalid .role");
request->Reject(500, "invalid .role"); request->Reject(500, "invalid `data.role`");
return; return;
} }
...@@ -560,6 +560,7 @@ namespace RTC ...@@ -560,6 +560,7 @@ namespace RTC
if (!msg) if (!msg)
{ {
MS_DEBUG("ignoring wrong STUN message received"); MS_DEBUG("ignoring wrong STUN message received");
return; return;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论