예제 #1
0
        public static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;
            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                content = state.sb.ToString();

                JsonTextReader reader = new JsonTextReader(new System.IO.StringReader(content));
                int            count  = 0;
                reader.Read();
                reader.Read();
                if (reader.Value.ToString().Equals("updateMotoristas"))
                {
                    Console.WriteLine("Adicionando usuário..");
                    user.Clear();
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                user.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString() + " - " + user.Count.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    bool isUpdated = adicionarMotorista();
                    Send(handler, isUpdated.ToString());
                }
                else if (reader.Value.ToString().Equals("updateDistribuidor"))
                {
                    Console.WriteLine("Adicionando usuário..");
                    user.Clear();
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                user.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString() + " - " + user.Count.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    bool isUpdated = adicionarDistribuidor();
                    Send(handler, isUpdated.ToString());
                }
                else if (reader.Value.ToString().Equals("loginInfoMotorista"))
                {
                    List <String> list = new List <String>();
                    Console.WriteLine("Checando motorista..");
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                list.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString() + " - " + list.Count.ToString());

                                //user.Add(reader.Value.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    user isLogged = checkMotorista(list[0], list[1]);
                    if (isLogged != null)
                    {
                        string jsonUserData = JsonConvert.SerializeObject(isLogged);
                        Console.WriteLine(jsonUserData);
                        //if (isLogged) Console.WriteLine("Usuário encontrado !");
                        //else Console.WriteLine("Usuário não encontrado");
                        Send(handler, jsonUserData);
                    }
                    else
                    {
                        Send(handler, "null");
                    }
                }
                else if (reader.Value.ToString().Equals("loginInfoDistribuidor"))
                {
                    List <String> list = new List <String>();
                    Console.WriteLine("Checando distribuidor..");
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                list.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString() + " - " + list.Count.ToString());

                                //user.Add(reader.Value.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    distribuidor isLogged = checkDistribuidor(list[0], list[1]);
                    if (isLogged != null)
                    {
                        string jsonUserData = JsonConvert.SerializeObject(isLogged);
                        Console.WriteLine(jsonUserData);
                        //if (isLogged) Console.WriteLine("Usuário encontrado !");
                        //else Console.WriteLine("Usuário não encontrado");
                        Send(handler, jsonUserData);
                    }
                    else
                    {
                        Send(handler, "null");
                    }
                }
                else if (reader.Value.ToString().Equals("updateAutos"))
                {
                    List <String> list = new List <String>();
                    Console.WriteLine("Adicionando automovel..");
                    autoInfo.Clear();
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                autoInfo.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString());
                                //user.Add(reader.Value.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    bool isUpdated = updateDBAutomoveis();
                    Send(handler, isUpdated.ToString());
                }
                else if (reader.Value.ToString().Equals("autoList"))
                {
                    List <String> list = new List <String>();
                    Console.WriteLine("Buscando lista de automoveis..");
                    autoInfo.Clear();
                    do
                    {
                        if (reader.Value != null)
                        {
                            if (count != 0 && count % 2 == 0)
                            {
                                autoInfo.Add(reader.Value.ToString());
                                Console.WriteLine(reader.Value.ToString());
                                //user.Add(reader.Value.ToString());
                            }
                            count++;
                        }
                    }while (reader.Read());
                    List <automovel> automoveis   = checkAutomoveis(autoInfo[0]);
                    string           jsonUserData = JsonConvert.SerializeObject(automoveis);
                    Console.WriteLine(jsonUserData);
                    //if (isLogged) Console.WriteLine("Usuário encontrado !");
                    //else Console.WriteLine("Usuário não encontrado");
                    Send(handler, jsonUserData);
                }


                // }
                //else
                //{
                //  // Not all data received. Get more.
                //    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                //    new AsyncCallback(ReadCallback), state);
                //}
            }
        }
예제 #2
0
        public static distribuidor checkDistribuidor(String email, String pass)
        {
            bool          userFound        = false;
            bool          passFound        = false;
            distribuidor  dist             = new distribuidor();
            String        connectionString = Properties.Settings.Default.Fretinga;
            SqlConnection sql = new SqlConnection(connectionString);

            sql.Open();
            //String[] info = new String[8];
            using (SqlCommand readCmd = new SqlCommand("SELECT * FROM table_distribuidor WHERE email = @email", sql))
            {
                //readCmd.Parameters.Add("@user", System.Data.SqlDbType.VarChar).Value = user;
                readCmd.Parameters.AddWithValue("@email", email);
                SqlDataReader reader = readCmd.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        dist.razao_social       = reader["razao_social"].ToString();
                        dist.nome_fantasia      = reader["nome_fantasia"].ToString();
                        dist.cnpj               = reader["cnpj"].ToString();
                        dist.inscricao_estadual = reader["inscricao_estadual"].ToString();
                        dist.pais               = reader["pais"].ToString();
                        dist.estado             = reader["estado"].ToString();
                        dist.cidade             = reader["cidade"].ToString();
                        dist.endereco           = reader["endereco"].ToString();
                        dist.bairro             = reader["bairro"].ToString();
                        dist.cep          = reader["cep"].ToString();
                        dist.complemento  = reader["complemento"].ToString();
                        dist.email        = reader["email"].ToString();
                        dist.senha        = reader["senha"].ToString();
                        dist.telefone     = reader["telefone"].ToString();
                        dist.celular      = reader["celular"].ToString();
                        dist.site         = reader["site"].ToString();
                        dist.nome_contato = reader["nome_contato"].ToString();

                        if (dist.email.Equals(email))
                        {
                            userFound = true;
                        }
                        if (dist.senha.Equals(pass))
                        {
                            passFound = true;
                        }
                        Console.WriteLine(String.Format("Formato: {0}, {1}",
                                                        reader["email"], reader["senha"]));// etc
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                //while (reader.Read())
                //{
                //Console.WriteLine("Usuário: {0}", reader.GetInt32(0).ToString());
                //}
                reader.Close();
                //userFound = true;
            }
            //user usuario = new user();

            sql.Close();
            if (passFound && userFound)
            {
                Console.WriteLine("Distribuidor encontrado e autenticado !");
                return(dist);
            }
            else
            {
                return(null);
            }
        }