예제 #1
0
        private void ReceiveCallback(IAsyncResult AR)
        {
            var socket = (Socket)AR.AsyncState;

            if (!socket.Connected)
            {
                return;
            }
            var received    = socket.EndReceive(AR);
            var dataBuf     = new byte[received];
            var idClient    = socket.RemoteEndPoint.ToString();
            var cryptoClass = new CryptographyObject(idClient);

            Array.Copy(_buffer, dataBuf, received);

            var encryptText = Encoding.ASCII.GetString(dataBuf);

            JsonRequest = cryptoClass.Desencriptar(encryptText);

            UpdateTramaJsonEntrante(JsonRequest, encryptText);

            var jsonSimpleRequest = JsonConvert.DeserializeObject <JsonRequest>(JsonRequest);
            var serverHash        = cryptoClass.Md5Gen();

            UpdateHash(jsonSimpleRequest.Credentials.Hash, serverHash);

            var jsonResponse = new JsonResponse();

            #region ValidateHash
            if (jsonSimpleRequest.Credentials.Hash != serverHash)
            {
                MessageBox.Show("El cliente ha creado una petición con una llave inválida.");
                return;
            }
            if (jsonSimpleRequest == null)
            {
                MessageBox.Show("Ha ocurrido un error con la petición del cliente");
                return;
            }
            if (jsonSimpleRequest.Action == "Desconectar del sistema")
            {
                MessageBox.Show("Usuario se ha desconectado.");
                users.Remove(jsonSimpleRequest.Credentials.CustomerNumber);
                UpdateUsersConnected();
                return;
            }
            #endregion

            switch (jsonSimpleRequest.Service)
            {
            case "CustomerService":
                _controller.SetJsonRequest(jsonSimpleRequest);
                switch (jsonSimpleRequest.Action)
                {
                case "Iniciar Sesion":
                    _controller.Login();
                    jsonResponse = _controller.JsonResponse;
                    if (_controller.JsonResponse.MessageResult == "Autorizado")
                    {
                        users.Add(jsonSimpleRequest.Credentials.CustomerNumber);
                        UpdateUsersConnected();
                    }
                    break;

                case "Cambiar Pin":
                    _controller.ChangePin();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Actualizar Telefono":
                    _controller.UpdateTelefono();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Consultar Usuario":
                    _controller.GetCustomer();
                    jsonResponse = _controller.JsonResponse;
                    break;
                }
                break;

            case "AccountService":
                _controller.SetJsonRequest(jsonSimpleRequest);
                switch (jsonSimpleRequest.Action)
                {
                case "Consultar Saldo":
                    _controller.GetAccount();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Retirar Efectivo":
                    _controller.Withdrawal();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Depositar Efectivo":
                    _controller.Deposit();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Transferir":
                    _controller.Transfer();
                    jsonResponse = _controller.JsonResponse;
                    break;
                }
                break;

            case "ProductService":
                _controller.SetJsonRequest(jsonSimpleRequest);
                switch (jsonSimpleRequest.Action)
                {
                case "Consultar Productos":
                    _controller.GetAllProductsByCustomer();
                    jsonResponse = _controller.JsonResponse;
                    break;

                case "Pagar Producto":
                    _controller.PayProduct();
                    jsonResponse = _controller.JsonResponse;
                    break;
                }
                break;

            case "LogService":
                _controller.SetJsonRequest(jsonSimpleRequest);
                switch (jsonSimpleRequest.Action)
                {
                case "Consultar Bitacora":
                    _controller.GetAllLogByCustomer();
                    jsonResponse = _controller.JsonResponse;
                    break;
                }
                break;

            default:
                jsonResponse.MessageResult = "Invalid Request";
                break;
            }
            UpdateBitacora();

            var json            = JsonConvert.SerializeObject(jsonResponse, Formatting.Indented);
            var encryptSendText = cryptoClass.Encriptar(json);
            UpdateTramaJsonSaliente(json, encryptSendText);
            var data = Encoding.ASCII.GetBytes(encryptSendText);

            socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
            socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
        }