/// <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> > 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); }
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); }