channel 是用于nodejs 与C++交互的,也就是nodejs 可以通过channel 想c++发送指令,同理C++也可以通过该channel 向nodejs 发响应消息。

    medisoup/lib/Worker.js
    图片.png
    通过socket就可以跟C++那一端通信了。
    new的Channel来自Channel.js。

    C++部分
    mediasoup\worker\src\handles\UnixStreamSocket.cpp
    UnixStreamSocket构造函数

    1. inline static void onRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
    2. {
    3. auto* socket = static_cast<UnixStreamSocket*>(handle->data);
    4. if (socket)
    5. socket->OnUvRead(nread, buf);
    6. }
    7. UnixStreamSocket::UnixStreamSocket(int fd, size_t bufferSize, UnixStreamSocket::Role role)
    8. : bufferSize(bufferSize), role(role)
    9. {
    10. MS_TRACE_STD();
    11. int err;
    12. this->uvHandle = new uv_pipe_t;
    13. this->uvHandle->data = static_cast<void*>(this);
    14. err = uv_pipe_init(DepLibUV::GetLoop(), this->uvHandle, 0);
    15. if (err != 0)
    16. {
    17. delete this->uvHandle;
    18. this->uvHandle = nullptr;
    19. MS_THROW_ERROR_STD("uv_pipe_init() failed: %s", uv_strerror(err));
    20. }
    21. err = uv_pipe_open(this->uvHandle, fd);
    22. if (err != 0)
    23. {
    24. uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
    25. MS_THROW_ERROR_STD("uv_pipe_open() failed: %s", uv_strerror(err));
    26. }
    27. if (this->role == UnixStreamSocket::Role::CONSUMER)
    28. {
    29. // Start reading.
    30. err = uv_read_start(
    31. reinterpret_cast<uv_stream_t*>(this->uvHandle),
    32. static_cast<uv_alloc_cb>(onAlloc),
    33. //数据来时会调用该回调
    34. static_cast<uv_read_cb>(onRead));
    35. if (err != 0)
    36. {
    37. uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
    38. MS_THROW_ERROR_STD("uv_read_start() failed: %s", uv_strerror(err));
    39. }
    40. }
    41. // NOTE: Don't allocate the buffer here. Instead wait for the first uv_alloc_cb().
    42. }

    读取数据后,将数据转为json格式
    inline void UnixStreamSocket::OnUvRead(ssize_t nread, const uv_buf_t /buf*/)

    Worker::OnChannelRequest

    https://www.cnblogs.com/ssyfj/p/14851442.html