Ejemplo n.º 1
0
        public void DoSendMessaggeWork()
        {
            while (true)
            {
                while (!PayloadStorage.IsEmpty())
                {
                    var payload = PayloadStorage.GetNext();

                    if (payload != null)
                    {
                        var connections = ConnectionsStorage.GetConnectionsByTopic(payload.Topic);

                        foreach (var connection in connections)
                        {
                            var    payloadString = JsonConvert.SerializeObject(payload);
                            byte[] data          = Encoding.UTF8.GetBytes(payloadString);

                            connection.Socket.Send(data);
                        }
                    }
                }

                Thread.Sleep(TIME_TO_SLEEP);
            }
        }
Ejemplo n.º 2
0
          public static void Handle(byte[] payloadBytes, ConnectionInfo connectionInfo)
          {
               var payloadString = Encoding.UTF8.GetString(payloadBytes);
               try
               {
                    payloadString = payloadString.Substring(payloadString.IndexOf('{'));
               }
               catch
               {

               }
               Payload payload = JsonConvert.DeserializeObject<Payload>(payloadString);
               
               if(payload != null)
               {
                    if (payload.type == "subscriber")
                    {
                         connectionInfo.Topic = payload.topic;
                         ConnectionsStorage.Add(connectionInfo);
                    }
                    else
                    {
                         PayloadStorage.Add(payload);
                    }
               }
          }
Ejemplo n.º 3
0
        public void DoSendTopicWork()
        {
            while (true)
            {
                while (!PayloadStorage.IsEmpty() && ConnectionsStorage.ExistConnections() &&
                       (ConnectionsStorage.GetConnectionsByTopic(PayloadStorage.CheckNextTopic()).Count != 0 || PayloadStorage.CheckNextTopic() == "publisher-end"))
                {
                    var payload = PayloadStorage.GetNext();

                    if (payload != null)
                    {
                        var connections = ConnectionsStorage.GetConnectionsByTopic(payload.topic);
                        if (payload.type == "end")
                        {
                            connections = ConnectionsStorage.GetConnections();
                        }

                        foreach (var connection in connections)
                        {
                            var    payloadString = JsonConvert.SerializeObject(payload);
                            byte[] data          = Encoding.UTF8.GetBytes(payloadString);
                            Console.WriteLine(payloadString);
                            connection.Socket.Send(data);
                        }
                    }
                }
                Thread.Sleep(TIME_TO_SLEEP);
            }
        }
Ejemplo n.º 4
0
        public static void Handle(byte[] payloadBytes, ConnectionInfo connectionInfo)
        {
            var payloadString = Encoding.UTF8.GetString(payloadBytes);

            if (payloadString.StartsWith("subscribe"))
            {
                connectionInfo.Topic = payloadString.Split("subscribe#").LastOrDefault();
                ConnectionStorage.Add(connectionInfo);
            }
            else
            {
                Payload payload = JsonConvert.DeserializeObject <Payload>(payloadString);
                PayloadStorage.Add(payload);
            }
        }
Ejemplo n.º 5
0
        private void ReceiveCallBack(IAsyncResult asyncResult)
        {
            ConnectionInfo connection = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                Socket      senderSocket = connection.Socket;
                SocketError response;
                int         buffSize = senderSocket.EndReceive(asyncResult, out response);

                if (response == SocketError.Success)
                {
                    byte[] payload = new byte[buffSize];
                    Array.Copy(connection.Data, payload, payload.Length);

                    PayloadHandler.Handle(payload, connection);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Can't receive data: {e.Message}");
            }
            finally
            {
                try
                {
                    connection.Socket.BeginReceive(connection.Data, 0, connection.Data.Length,
                                                   SocketFlags.None, ReceiveCallBack, connection);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message}");
                    var address = connection.Socket.RemoteEndPoint.ToString();

                    Payload payload = new Payload()
                    {
                        type  = "end",
                        topic = "publisher-end",
                        post  = Settings.LAST_WILL_MESSAGE
                    };

                    PayloadStorage.Add(payload);

                    ConnectionsStorage.Remove(address);
                    connection.Socket.Close();
                }
            }
        }
        public static void Handle(byte[] payloadBytes, ConnectionInfo connectionInfo)
        {
            var payloadString = Encoding.UTF8.GetString(payloadBytes);

            if (payloadString.StartsWith("subscribe#"))
            {
                connectionInfo.Topic = payloadString.Split("subscribe#").LastOrDefault();
                // adaugam conexiunea in storage
                ConnectionStorage.Add(connectionInfo);
            }
            else
            {
                // il facem transient
                Payload payload = JsonConvert.DeserializeObject <Payload>(payloadString);
                // adaugam in storage
                PayloadStorage.Add(payload);
            }

            // Console.WriteLine(payloadString);
        }