/// <summary> /// Blocks until a message was received, calls the IMessagePipeCallback and confirms the message to the server, unless the pipe's token is cancelled. /// </summary> /// <param name="callback"></param> public async Task ReadBlocking(IMessagePipeCallback callback) { Logger.LogTrace("ReadBlocking()"); WebSocketRequestMessage request = Websocket.ReadRequestBlocking(); if (IsSignalServiceEnvelope(request)) { SignalServiceMessagePipeMessage message = new SignalServiceEnvelope(request.Body.ToByteArray(), CredentialsProvider.SignalingKey); WebSocketResponseMessage response = CreateWebSocketResponse(request); try { Logger.LogDebug("Calling callback with message {0}", request.Id); await callback.OnMessage(message); } finally { if (!Token.IsCancellationRequested) { Logger.LogDebug("Confirming message {0}", request.Id); Websocket.SendResponse(response); } } } else if (IsPipeEmptyMessage(request)) { Logger.LogInformation("Calling callback with SignalServiceMessagePipeEmptyMessage"); await callback.OnMessage(new SignalServiceMessagePipeEmptyMessage()); } else { Logger.LogWarning("Unknown request: {0} {1}", request.Verb, request.Path); } }
public async Task <List <SignalServiceEnvelope> > RetrieveMessagesAsync(IMessagePipeCallback callback, CancellationToken?token = null) { if (token == null) { token = CancellationToken.None; } List <SignalServiceEnvelope> results = new List <SignalServiceEnvelope>(); List <SignalServiceEnvelopeEntity> entities = await socket.GetMessagesAsync(token); foreach (SignalServiceEnvelopeEntity entity in entities) { SignalServiceEnvelope envelope; if (entity.HasSource() && entity.SourceDevice > 0) { SignalServiceAddress address = new SignalServiceAddress(UuidUtil.ParseOrNull(entity.SourceUuid), entity.SourceE164); envelope = new SignalServiceEnvelope((int)entity.Type, address, (int)entity.SourceDevice, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } else { envelope = new SignalServiceEnvelope((int)entity.Type, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } await callback.OnMessageAsync(envelope); results.Add(envelope); if (envelope.HasUuid()) { await socket.AcknowledgeMessageAsync(envelope.GetUuid(), token); } else { await socket.AcknowledgeMessageAsync(entity.SourceE164 !, entity.Timestamp, token); } } return(results); }
/// <summary> /// Blocks until a message was received, calls the IMessagePipeCallback and confirms the message to the server, unless the pipe's token is canceled. /// </summary> /// <param name="callback"></param> public async Task ReadBlockingAsync(IMessagePipeCallback callback) { logger.LogTrace("ReadBlocking()"); if (credentialsProvider == null) { throw new ArgumentException("You can't read messages if you haven't specified credentials"); } WebSocketRequestMessage request = websocket.ReadRequestBlocking(); if (IsSignalServiceEnvelope(request)) { SignalServiceMessagePipeMessage message = new SignalServiceEnvelope(request.Body.ToByteArray()); WebSocketResponseMessage response = CreateWebSocketResponse(request); try { logger.LogDebug("Calling callback with message {0}", request.Id); await callback.OnMessageAsync(message); } finally { if (!token.IsCancellationRequested) { logger.LogDebug("Confirming message {0}", request.Id); websocket.SendResponse(response); } } } else if (IsPipeEmptyMessage(request)) { logger.LogInformation("Calling callback with SignalServiceMessagePipeEmptyMessage"); await callback.OnMessageAsync(new SignalServiceMessagePipeEmptyMessage()); } else { logger.LogWarning("Unknown request: {0} {1}", request.Verb, request.Path); } }
public async Task <List <SignalServiceEnvelope> > RetrieveMessages(CancellationToken token, IMessagePipeCallback callback) { List <SignalServiceEnvelope> results = new List <SignalServiceEnvelope>(); List <SignalServiceEnvelopeEntity> entities = await Socket.GetMessages(token); foreach (SignalServiceEnvelopeEntity entity in entities) { SignalServiceEnvelope envelope; if (entity.Source != null && entity.SourceDevice > 0) { envelope = new SignalServiceEnvelope((int)entity.Type, entity.Source, (int)entity.SourceDevice, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } else { envelope = new SignalServiceEnvelope((int)entity.Type, (int)entity.Timestamp, entity.Message, entity.Content, entity.ServerTimestamp, entity.ServerUuid); } await callback.OnMessage(envelope); results.Add(envelope); if (envelope.Envelope.ServerGUidOneofCase == Envelope.ServerGUidOneofOneofCase.ServerGuid) { await Socket.AcknowledgeMessage(token, envelope.Envelope.ServerGuid); } else { await Socket.AcknowledgeMessage(token, entity.Source, entity.Timestamp); } } return(results); }
public async Task <List <SignalServiceEnvelope> > RetrieveMessages(CancellationToken token, IMessagePipeCallback callback) { List <SignalServiceEnvelope> results = new List <SignalServiceEnvelope>(); List <SignalServiceEnvelopeEntity> entities = await Socket.GetMessages(token); foreach (SignalServiceEnvelopeEntity entity in entities) { SignalServiceEnvelope envelope = new SignalServiceEnvelope((int)entity.Type, entity.Source, (int)entity.SourceDevice, entity.Relay, (int)entity.Timestamp, entity.Message, entity.Content); await callback.OnMessage(envelope); results.Add(envelope); await Socket.AcknowledgeMessage(token, entity.Source, entity.Timestamp); } return(results); }