一、mediasoup之router的创建过程

博客详细描述了Node.js通过Unix Socket与C++子进程间的通信机制。在Node.js中,worker进程通过spawn方法创建C++子进程,并使用channel进行通信。数据以JSON格式传输,包括请求ID、指令名和附加参数。在C++端,接收到的消息通过映射关系转换为相应方法处理,如创建router。整个过程涉及请求、响应和错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        每一个worker代表一个nodejs主进程,通过nodejs的spawn 方法创建一个c++子进程,主进程与子进程之间通过unix socket进行通信,nodejs代码中的channel就是对unix socket的一个封装。

      通过channel的request方法发送 “worker.createRouter”指令,并附加参数routerId,到c++子进程,查看worker.ts代码的createRouter方法:

const internal = { routerId: uuidv4() };

await this.#channel.request('worker.createRouter', internal);

        channel的request方法中把指令包装为{ id, method, internal, data } 的JSON格式的数据,

id:请求id.每次请求递增

method: 即为“worker.createRouter”指令

internal: 此处为 {routerId}

data: 此处为null

然后通过producerSocket发送给c++层面,看如下代码:

/**
	 * @private
	 */
	async request(method: string, internal?: object, data?: any): Promise<any>
	{
		this.#nextId < 4294967295 ? ++this.#nextId : (this.#nextId = 1);

		const id = this.#nextId;

		logger.debug('request() [method:%s, id:%s]', method, id);

		if (this.#closed)
			throw new InvalidStateError('Channel closed');

		const request = { id, method, internal, data };
		const payload = JSON.stringify(request);

		if (Buffer.byteLength(payload) > MESSAGE_MAX_LEN)
			throw new Error('Channel request too big');

		// This may throw if closed or remote side ended.
		this.#producerSocket.write(
			Buffer.from(Uint32Array.of(Buffer.byteLength(payload)).buffer));
		this.#producerSocket.write(payload);
        ........后面省略   
    }

        在c++层面ChannelRequest.cpp代码中,定义了指令到方法id之间的映射关系std::unordered_map:

	std::unordered_map<std::string, ChannelRequest::MethodId> ChannelRequest::string2MethodId =
	{
		{ "worker.close",                                ChannelRequest::MethodId::WORKER_CLOSE                                     },
..................
		{ "worker.createRouter",                         ChannelRequest::MethodId::WORKER_CREATE_ROUTER  
......................
}

从中可以看出createRouter对应的方法MethodId为  ChannelRequest::MethodId::WORKER_CREATE_ROUTER 。我们再看一下MethodId的定义如下,可以看出WORKER_CREATE_ROUTER的值为 5 :

class ChannelRequest
{
	public:
		enum class MethodId
		{
			WORKER_CLOSE = 1,
			WORKER_DUMP,
			WORKER_GET_RESOURCE_USAGE,
			WORKER_UPDATE_SETTINGS,
			WORKER_CREATE_ROUTER,
            ................
        }
};

C++层面在void ConsumerSocket::UserOnUnixStreamRead()中收到消息时,在子类方法ChannelSocket::OnConsumerSocketMessage中对JOSN格式的消息进行解析,并创建ChannelRequest::ChannelRequest类实例,实例中就包含了解析后的{ id, method, internal, data }数据,看下ChannelRequest的成员变量:

	public:
		// Passed by argument.
		Channel::ChannelSocket* channel{ nullptr };
		uint32_t id{ 0u }; 
		std::string method;
		MethodId methodId;
		json internal;
		json data;
		// Others.
		bool replied{ false };

        最后通过c++层面的 Worker::OnChannelRequest对消息进行处理:

inline void Worker::OnChannelRequest(Channel::ChannelSocket* /*channel*/, Channel::ChannelRequest* request)
{
    switch (request->methodId)
	{
        ....................
        case Channel::ChannelRequest::MethodId::WORKER_CREATE_ROUTER:
		{
			std::string routerId;

			try
			{
				SetNewRouterIdFromInternal(request->internal, routerId);
			}
			catch (const MediaSoupError& error)
			{
				MS_THROW_ERROR("%s [method:%s]", error.buffer, request->method.c_str());
			}

			auto* router = new RTC::Router(routerId);

			this->mapRouters[routerId] = router;

			MS_DEBUG_DEV("Router created [routerId:%s]", routerId.c_str());

			request->Accept();

			break;
		}
        ...........................
    }
// Any other request must be delivered to the corresponding Router.
	default:
		{
			try
			{
				RTC::Router* router = GetRouterFromInternal(request->internal);

				router->HandleRequest(request);
			}
			catch (const MediaSoupTypeError& error)
			{
				MS_THROW_TYPE_ERROR("%s [method:%s]", error.buffer, request->method.c_str());
			}
			catch (const MediaSoupError& error)
			{
				MS_THROW_ERROR("%s [method:%s]", error.buffer, request->method.c_str());
			}

			break;
		}
}

其中我们看到了 auto* router = new RTC::Router(routerId); 即是创建了一个router. 

最后通过调用request->Accept(); 向nodejs主进程发送响应消息:

void ChannelRequest::Accept()
{
		MS_TRACE();

		MS_ASSERT(!this->replied, "request already replied");

		this->replied = true;

		json jsonResponse = json::object();

		jsonResponse["id"]       = this->id;
		jsonResponse["accepted"] = true;

		this->channel->Send(jsonResponse);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值