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