Exemplo n.º 1
0
        public Subscription ListenToNotifications(Action <string> next)
        {
            if (!IsAuthenticated())
            {
                throw new InvalidAuthException();
            }

            var notifier = SetupConnection();

            var t = new Thread(() =>
            {
                var init = new MessageBuilder()
                           .PutOperationCode("00")
                           .PutAuthInfo(Auth)
                           .PutPayload("NOTIFY")
                           .Build();

                Sender.SendMessage(notifier, init);

                while (true)
                {
                    try
                    {
                        var notification = Receiver.ReceiveMessage(notifier);
                        next(MessageDecoder.DecodePayload(notification.Payload));

                        var ack = new MessageBuilder()
                                  .MarkAsResponse()
                                  .PutOperationCode("00")
                                  .PutPayload("OK")
                                  .Build();
                        Sender.SendMessage(notifier, ack);
                    }
                    catch (DeadConnectionException)
                    {
                        break;
                    }
                }
            });

            t.Start();

            return(new Subscription(() =>
            {
                notifier.Shutdown(SocketShutdown.Both);
                notifier.Close();
            }));
        }
Exemplo n.º 2
0
        private void CallHandler(Type ctrl, MethodInfo handler, Socket sock, DecodedMessage <byte[]> decoded)
        {
            var ctrlInstance = Activator.CreateInstance(ctrl);

            ctrl.GetMethod("SetContext").Invoke(ctrlInstance, new object[] { sock, decoded.Code });
            var castTo = handler.GetParameters().ElementAt(0).ParameterType;

            if (castTo != typeof(string))
            {
                var decodedPayload = MessageDecoder.DecodePayload(decoded.Payload, castTo);
                handler.Invoke(ctrlInstance, new object[] { decodedPayload, decoded.Auth });
            }
            else
            {
                var decodedPayload = MessageDecoder.DecodePayload(decoded.Payload);
                handler.Invoke(ctrlInstance, new object[] { decodedPayload, decoded.Auth });
            }
        }
Exemplo n.º 3
0
        public string Receive()
        {
            DecodedMessage <byte[]> response = Receiver.ReceiveMessage(Socket);

            return(MessageDecoder.DecodePayload(response.Payload));
        }
Exemplo n.º 4
0
        public T Receive <T>() where T : class
        {
            DecodedMessage <byte[]> response = Receiver.ReceiveMessage(Socket);

            return(MessageDecoder.DecodePayload <T>(response.Payload));
        }