コード例 #1
0
        public SendMessageResponse Send(OutgoingPushMessageList list)
        {
            WebSocketRequestMessage requestmessage = new WebSocketRequestMessage()
            {
                Id   = BitConverter.ToUInt64(Util.getSecretBytes(sizeof(long)), 0),
                Verb = "PUT",
                Path = $"/v1/messages/{list.getDestination()}",
                Body = ByteString.CopyFrom(Encoding.UTF8.GetBytes(JsonUtil.toJson(list)))
            };

            requestmessage.Headers.Add("content-type:application/json");
            var t = Websocket.SendRequest(requestmessage);

            t.Wait();
            if (t.IsCompleted)
            {
                var response = t.Result;
                if (response.Item1 < 200 || response.Item1 >= 300)
                {
                    throw new IOException("non-successfull response: " + response.Item1 + " " + response.Item2);
                }
                return(JsonUtil.fromJson <SendMessageResponse>(response.Item2));
            }
            else
            {
                throw new IOException("timeout reached while waiting for confirmation");
            }
        }
コード例 #2
0
        /// <summary>
        /// Sends a message through the pipe. Blocks until delivery is confirmed or throws an IOException if a timeout occurs.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task <SendMessageResponse> Send(OutgoingPushMessageList list)
        {
            Logger.LogTrace("Send()");
            WebSocketRequestMessage requestmessage = new WebSocketRequestMessage()
            {
                Id   = BitConverter.ToUInt64(Util.GetSecretBytes(sizeof(long)), 0),
                Verb = "PUT",
                Path = $"/v1/messages/{list.Destination}",
                Body = ByteString.CopyFrom(Encoding.UTF8.GetBytes(JsonUtil.ToJson(list)))
            };

            requestmessage.Headers.Add("content-type:application/json");
            var sendTask          = (await Websocket.SendRequest(requestmessage)).Task;
            var timerCancelSource = new CancellationTokenSource();

            if (await Task.WhenAny(sendTask, Task.Delay(10 * 1000, timerCancelSource.Token)) == sendTask)
            {
                timerCancelSource.Cancel();
                var(Status, Body) = sendTask.Result;
                if (Status < 200 || Status >= 300)
                {
                    throw new IOException("non-successfull response: " + Status);
                }
                if (Util.IsEmpty(Body))
                {
                    return(new SendMessageResponse(false));
                }
                return(JsonUtil.FromJson <SendMessageResponse>(Body));
            }
            else
            {
                Logger.LogError("Sending message {0} failed: timeout", requestmessage.Id);
                throw new IOException("timeout reached while waiting for confirmation");
            }
        }
コード例 #3
0
        private async Task <SendMessageResult> SendMessage(CancellationToken token,
                                                           SignalServiceAddress recipient,
                                                           UnidentifiedAccess?unidentifiedAccess,
                                                           long timestamp,
                                                           byte[] content)
        {
            for (int i = 0; i < 4; i++)
            {
                try
                {
                    OutgoingPushMessageList messages = await GetEncryptedMessages(token, Socket, recipient, unidentifiedAccess, timestamp, content);

                    var pipe             = Pipe;
                    var unidentifiedPipe = UnidentifiedPipe;
                    if (Pipe != null && unidentifiedAccess == null)
                    {
                        try
                        {
                            Logger.LogTrace("Transmitting over pipe...");
                            var response = await Pipe.Send(messages, null);

                            return(SendMessageResult.NewSuccess(recipient, false, response.NeedsSync));
                        }
                        catch (Exception e)
                        {
                            Logger.LogWarning(e.Message + " - falling back to new connection...");
                        }
                    }
                    else if (unidentifiedPipe != null && unidentifiedAccess != null)
                    {
                        var response = await unidentifiedPipe.Send(messages, unidentifiedAccess);

                        return(SendMessageResult.NewSuccess(recipient, true, response.NeedsSync));
                    }

                    Logger.LogTrace("Not transmitting over pipe...");
                    SendMessageResponse resp = await Socket.SendMessage(token, messages, unidentifiedAccess);

                    return(SendMessageResult.NewSuccess(recipient, unidentifiedAccess != null, resp.NeedsSync));
                }
                catch (MismatchedDevicesException mde)
                {
                    await HandleMismatchedDevices(token, Socket, recipient, mde.MismatchedDevices);
                }
                catch (StaleDevicesException ste)
                {
                    HandleStaleDevices(recipient, ste.StaleDevices);
                }
            }
            Logger.LogError("Failed to resolve conflicts after 3 attempts!");
            throw new Exception("Failed to resolve conflicts after 3 attempts!");
        }
コード例 #4
0
        public SendMessageResponse send(OutgoingPushMessageList list)
        {
            throw new NotImplementedException();
            //try
            //{
            //    WebSocketRequestMessage requestmessage = WebSocketRequestMessage.CreateBuilder()
            //        .SetId((ulong)CryptographicBuffer.GenerateRandomNumber())
            //        .SetVerb("PUT")
            //        .SetPath($"/v1/messages/{list.getDestination()}")
            //        .AddHeaders("content-type:application/json")
            //        .SetBody(ByteString.CopyFrom(Encoding.UTF8.GetBytes(JsonUtil.toJson(list))))
            //        .Build();

            //}
        }
コード例 #5
0
        private SendMessageResponse sendMessage(SignalServiceAddress recipient, long timestamp, byte[] content, bool legacy, bool silent)
        {
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    OutgoingPushMessageList messages = getEncryptedMessages(socket, recipient, timestamp, content, legacy, silent);
                    if (pipe != null)
                    {
                        try
                        {
                            Debug.WriteLine("Transmitting over pipe...");
                            return(pipe.Send(messages));
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.Message + " - falling back to new connection...");
                        }
                    }

                    Debug.WriteLine("Not transmitting over pipe...");
                    return(socket.sendMessage(messages));
                }
                catch (MismatchedDevicesException mde)
                {
                    Debug.WriteLine("MismatchedDevicesException");
                    Debug.WriteLine(mde.Message);
                    Debug.WriteLine(mde.StackTrace);
                    handleMismatchedDevices(socket, recipient, mde.getMismatchedDevices());
                }
                catch (StaleDevicesException ste)
                {
                    Debug.WriteLine("MismatchedDevicesException");
                    Debug.WriteLine(ste.Message);
                    Debug.WriteLine(ste.StackTrace);
                    handleStaleDevices(recipient, ste.getStaleDevices());
                }
            }
            Debug.WriteLine("Failed to resolve conflicts after 3 attempts!");
            throw new Exception("Failed to resolve conflicts after 3 attempts!");
        }
コード例 #6
0
        private async Task <SendMessageResponse> sendMessage(SignalServiceAddress recipient, long timestamp, byte[] content, bool legacy, bool silent)
        {
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    OutgoingPushMessageList messages = await getEncryptedMessages(socket, recipient, timestamp, content, legacy, silent);

                    if (pipe.HasValue)
                    {
                        try
                        {
                            Debug.WriteLine("Transmitting over pipe...");
                            return(pipe.ForceGetValue().send(messages));
                        }
                        catch (IOException e)
                        {
                            Debug.WriteLine("Falling back to new connection...");
                        }
                    }

                    Debug.WriteLine("Not transmitting over pipe...");
                    return(await socket.sendMessage(messages));
                }
                catch (MismatchedDevicesException mde)
                {
                    Debug.WriteLine(mde.Message, TAG);
                    await handleMismatchedDevices(socket, recipient, mde.getMismatchedDevices());
                }
                catch (StaleDevicesException ste)
                {
                    //Log.w(TAG, ste);
                    handleStaleDevices(recipient, ste.getStaleDevices());
                }
            }

            throw new Exception("Failed to resolve conflicts after 3 attempts!");
        }
コード例 #7
0
        private async Task <SendMessageResponse> SendMessage(CancellationToken token, SignalServiceAddress recipient, long timestamp, byte[] content, bool silent)
        {
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    OutgoingPushMessageList messages = await GetEncryptedMessages(token, socket, recipient, timestamp, content, silent);

                    if (pipe != null)
                    {
                        try
                        {
                            Logger.LogTrace("Transmitting over pipe...");
                            return(await pipe.Send(messages));
                        }
                        catch (Exception e)
                        {
                            Logger.LogWarning(e.Message + " - falling back to new connection...");
                        }
                    }

                    Logger.LogTrace("Not transmitting over pipe...");
                    return(await socket.SendMessage(token, messages));
                }
                catch (MismatchedDevicesException mde)
                {
                    await HandleMismatchedDevices(token, socket, recipient, mde.MismatchedDevices);
                }
                catch (StaleDevicesException ste)
                {
                    HandleStaleDevices(recipient, ste.StaleDevices);
                }
            }
            Logger.LogError("Failed to resolve conflicts after 3 attempts!");
            throw new Exception("Failed to resolve conflicts after 3 attempts!");
        }
コード例 #8
0
        private async Task <SendMessageResponse> sendMessage(TextSecureAddress recipient, ulong timestamp, byte[] content, bool legacy)
        {
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    OutgoingPushMessageList messages = await getEncryptedMessages(socket, recipient, timestamp, content, legacy);

                    return(await socket.sendMessage(messages));
                }
                catch (MismatchedDevicesException mde)
                {
                    Debug.WriteLine(mde.Message, TAG);
                    handleMismatchedDevices(socket, recipient, mde.getMismatchedDevices());
                }
                catch (StaleDevicesException ste)
                {
                    //Log.w(TAG, ste);
                    handleStaleDevices(recipient, ste.getStaleDevices());
                }
            }

            throw new Exception("Failed to resolve conflicts after 3 attempts!");
        }