示例#1
0
        public async void SendRequest(string account, string pass, string action)
        {
            try
            {
                UserManager.UserVerification userVerification = new UserManager.UserVerification();
                pass = userVerification.GetSHA256(pass);
                byte[]       tempBuff    = Encoding.UTF8.GetBytes(action + "%" + account + "%" + pass);
                SmallPackage package     = new SmallPackage(1024, 1024, "M", tempBuff, "0");
                byte[]       buffMessage = package.Packing();
                await server.GetStream().WriteAsync(buffMessage, 0, buffMessage.Length);

                byte[] buffReceive = new byte[1024];
                await server.GetStream().ReadAsync(buffReceive, 0, buffReceive.Length);

                SmallPackage packageReceive = new SmallPackage();
                packageReceive.DividePackage(buffReceive);

                string[] data = (Encoding.UTF8.GetString(packageReceive.Data).Trim('\0', '\t', '\n')).Split('%');
                if (data[0] == "LOGINOKE")
                {
                    User    user     = new User(data[1], account, true, @"./images/avatarDefault/avatarDefault.png");
                    FrmMain mainform = new FrmMain(this, user, client, server, data[2]);
                    mainform.Show();
                    this.Hide();
                    label2.Visible = false;
                }
                else if (data[0] == "ERRORLOGINED")
                {
                    MessageBox.Show("Account was logined in other computer", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    label2.Text    = "Tai khoan hoac mat khau khong chinh xac";
                    label2.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.lnConnect.Text = "No connection";
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            List <ItemLine> panier = new List <ItemLine>();

            UserManager.User utilisateur = new UserManager.User();
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "bill_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.BasicQos(0, 1, false);
                    var consumer = new EventingBasicConsumer(channel);
                    channel.BasicConsume(queue: "bill_queue", autoAck: false, consumer: consumer);
                    Console.WriteLine(" [x] Awaiting RPC requests");


                    consumer.Received += (model, ea) =>
                    {
                        string response = null;

                        var body       = ea.Body;
                        var props      = ea.BasicProperties;
                        var replyProps = channel.CreateBasicProperties();
                        replyProps.CorrelationId = props.CorrelationId;

                        try
                        {
                            response = "";
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            if (message == "facture")
                            {
                                Bill b = Bill.CreateBill(utilisateur, panier);
                                Console.WriteLine("Total" + b.sousTotalAvecTaxe);
                                b.afficher();
                                response = "Facture élaborée";
                            }
                            else
                            {
                                string[] messageSplit = message.Split(":");

                                if (messageSplit[0] == "User")
                                {
                                    utilisateur.nom = messageSplit[1];
                                }
                                else
                                {
                                    string nomProduit = messageSplit[0];
                                    int    quantite   = Int32.Parse(messageSplit[1]);


                                    ItemLine selectionne = null;
                                    foreach (ItemLine item in StockManager.StockManager.getStock())
                                    {
                                        if (item.item.nom == nomProduit)
                                        {
                                            selectionne = item;
                                        }
                                    }
                                    if (selectionne != null)
                                    {
                                        panier.Add(new ItemLine(selectionne.item, quantite));
                                        response = "Commande enregistrée";
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(" [.] " + e.Message);
                            response = "";
                        }
                        finally
                        {
                            var responseBytes = Encoding.UTF8.GetBytes(response);
                            channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
                            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        }
                    };

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }