private void OnPayloadChannelMessage(string targetId, string @event, NotifyData data, ArraySegment <byte> payload) { if (targetId != DataConsumerId) { return; } switch (@event) { case "message": { // TODO: (alby) _closed 的使用及线程安全。 if (_closed) { break; } var ppid = data.PPID; var message = payload; // Emit 暂不支持超过两个参数 Emit("message", new NotifyMessage { Message = message, PPID = ppid }); break; } default: { _logger.LogError($"OnPayloadChannelMessage() | Ignoring unknown event \"{@event}\" in payload channel listener"); break; } } }
private void OnPayloadChannelMessage(string targetId, string @event, NotifyData notifyData, ArraySegment <byte> payload) { if (targetId != ConsumerId) { return; } switch (@event) { case "rtp": { // TODO: (alby) _closed 的使用及线程安全。 if (_closed) { break; } Emit("rtp", payload); break; } default: { _logger.LogError($"OnPayloadChannelMessage() | Ignoring unknown event \"{@event}\""); break; } } }
private void OnPayloadChannelMessage(string targetId, string @event, NotifyData data, ArraySegment <byte> payload) { if (targetId != DataConsumerId) { return; } switch (@event) { case "message": { if (Closed) { break; } var ppid = data.PPID; var message = payload; // Emit 暂不支持超过两个参数 Emit("message", new NoyifyMessage { Message = message, PPID = ppid }); break; } default: { _logger.LogError($"OnPayloadChannelMessage() | ignoring unknown event{@event}"); break; } } }
public void Notify(string @event, object @internal, NotifyData data, byte[] payload) { _logger.LogDebug($"notify() [event:{@event}]"); if (_closed) { throw new InvalidStateException("PayloadChannel closed"); } var notification = new { @event, @internal, data }; var ns1Bytes = Netstring.Encode(notification.ToCamelCaseJson()); var ns2Bytes = Netstring.Encode(payload); if (ns1Bytes.Length > NsMessageMaxLen) { throw new Exception("PayloadChannel notification too big"); } if (ns2Bytes.Length > NsMessageMaxLen) { throw new Exception("PayloadChannel payload too big"); } Loop.Default.Sync(() => { try { // This may throw if closed or remote side ended. _producerSocket.Write(ns1Bytes, ex => { if (ex != null) { _logger.LogError(ex, "_producerSocket.Write() | error"); } }); } catch (Exception ex) { _logger.LogWarning($"notify() | sending notification failed: {ex}"); return; } try { // This may throw if closed or remote side ended. _producerSocket.Write(ns2Bytes, ex => { if (ex != null) { _logger.LogError(ex, "_producerSocket.Write() | error"); } }); } catch (Exception ex) { _logger.LogWarning($"notify() | sending notification failed: {ex}"); return; } }); }
/// <summary> /// Send data (just valid for DataProducers created on a DirectTransport). /// </summary> /// <param name="message"></param> /// <param name="ppid"></param> /// <returns></returns> public Task SendAsync(string message, int?ppid) { _logger.LogDebug($"SendAsync() | DataConsumer:{DataConsumerId}"); /* * +-------------------------------+----------+ * | Value | SCTP | * | | PPID | * +-------------------------------+----------+ * | WebRTC String | 51 | * | WebRTC Binary Partial | 52 | * | (Deprecated) | | * | WebRTC Binary | 53 | * | WebRTC String Partial | 54 | * | (Deprecated) | | * | WebRTC String Empty | 56 | * | WebRTC Binary Empty | 57 | * +-------------------------------+----------+ */ if (ppid == null) { ppid = !message.IsNullOrEmpty() ? 51 : 56; } // Ensure we honor PPIDs. if (ppid == 56) { message = " "; } var requestData = new NotifyData { PPID = ppid.Value }; _payloadChannel.Notify("dataConsumer.send", _internal, requestData, Encoding.UTF8.GetBytes(message)); return(Task.CompletedTask); }
/// <summary> /// Send data (just valid for DataProducers created on a DirectTransport). /// </summary> /// <param name="message"></param> /// <param name="ppid"></param> /// <returns></returns> public Task SendAsync(byte[] message, int?ppid) { _logger.LogDebug($"SendAsync() | DataConsumer:{DataConsumerId}"); if (ppid == null) { ppid = !message.IsNullOrEmpty() ? 53 : 57; } // Ensure we honor PPIDs. if (ppid == 57) { message = new byte[1]; } var requestData = new NotifyData { PPID = ppid.Value }; _payloadChannel.Notify("dataConsumer.send", _internal, requestData, message); return(Task.CompletedTask); }