コード例 #1
0
        public MainWindow()
        {
            ThreadPool.SetMaxThreads(20, 40);
            InitializeComponent();

            database_connection = DatabaseOrder.DatabaseConnection(database_connection);
            if (database_connection != null)
            {
                users_counter        = DatabaseOrder.CheckUsers(database_connection);
                lbl_AllUsers.Content = users_counter.ToString();
            }
            else
            {
                this.Close();
            }
        }
コード例 #2
0
ファイル: ClientOrder.cs プロジェクト: kpaluch26/Projekt_PRIR
        private void ReceiveCallback(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;
            int    received;
            bool   bool_return = false;
            int    int_return = 0;
            string _login, _password, _ip;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " forcefully disconnected"); });
                MW.updateCounterOfActiveUsers(false);
                current.Close();
                client_sockets.Remove(current);
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(BUFFER, recBuf, received);
            string text = Encoding.ASCII.GetString(recBuf);

            if (text.ToUpper() == "EXIT")
            {
                MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " disconnected"); });
                MW.updateCounterOfActiveUsers(false);
                current.Shutdown(SocketShutdown.Both);
                current.Close();
                client_sockets.Remove(current);
                return;
            }
            else
            {
                string[] roger = null;
                try
                {
                    roger = text.Split('|');
                }
                catch
                {
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " used unknown commend"); });
                }

                if (roger[0].ToUpper() == "REG" && roger.Length == 3)
                {
                    _login    = roger[1];
                    _password = roger[2];
                    _ip       = current.RemoteEndPoint.ToString();

                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { int_return = DatabaseOrder.AddUser(database_connection, _login, _password); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    if (int_return == 0)
                    {
                        MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Registration complied for client: " + _ip + ", username: "******"REG|TRUE");
                        current.Send(data);
                        current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallback, current);
                    }
                    else if (int_return == 1)
                    {
                        MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Duplicated login: "******", client: " + _ip); });
                        byte[] data = Encoding.ASCII.GetBytes("REG|FALSE");
                        current.Send(data);
                        current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallback, current);
                    }
                    else
                    {
                        byte[] data = Encoding.ASCII.GetBytes("REG|FALSE");
                        current.Send(data);
                        current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallback, current);
                    }
                }
                else if (roger[0].ToUpper() == "LOG" && roger.Length == 3)
                {
                    _login    = roger[1];
                    _password = roger[2];
                    _ip       = current.RemoteEndPoint.ToString();

                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { bool_return = DatabaseOrder.LogIn(database_connection, _login, _password); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    if (bool_return)
                    {
                        MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Pomyślnie zalogowano się na użytkownika: " + _login); });
                        byte[] data = Encoding.ASCII.GetBytes("LOG|TRUE");
                        current.Send(data);

                        Monitor.Enter(permits_locker);
                        permits.Add(_login + "|" + current.RemoteEndPoint.ToString());
                        Monitor.Exit(permits_locker);

                        current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                    }
                    else
                    {
                        MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Błędne dane uwierzytelniające z adresu: " + _ip); });
                        byte[] data = Encoding.ASCII.GetBytes("LOG|FALSE");
                        current.Send(data);
                        current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallback, current);
                    }
                }
                else
                {
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallback, current);
                }
            }
        }
コード例 #3
0
ファイル: ClientOrder.cs プロジェクト: kpaluch26/Projekt_PRIR
        private void ReceiveCallbackLoged(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;
            int    received;
            string clientname = "nazwa_uzytkownika";

            string[] connect_data = null;
            bool     bool_return  = false;

            if (current != null && current.Connected)
            {
                current.RemoteEndPoint.ToString();

                Monitor.Enter(permits_locker);
                foreach (string x in permits)
                {
                    connect_data = x.Split('|');
                    if (connect_data[1] == current.RemoteEndPoint.ToString())
                    {
                        clientname = connect_data[0];
                        break;
                    }
                }
                Monitor.Exit(permits_locker);

                try
                {
                    received = current.EndReceive(AR);
                }
                catch (SocketException)
                {
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " forcefully disconnected"); });
                    MW.updateCounterOfActiveUsers(false);
                    client_sockets.Remove(current);
                    foreach (string x in permits)
                    {
                        if (x == clientname + "|" + current.LocalEndPoint.ToString())
                        {
                            permits.Remove(x);
                            break;
                        }
                    }
                    current.Close();
                    return;
                }

                byte[] recBuf = new byte[received];
                Array.Copy(BUFFER, recBuf, received);
                string text = Encoding.ASCII.GetString(recBuf);

                string[] roger = null;
                try
                {
                    roger = text.Split('|');
                }
                catch
                {
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " used unknown commend"); });
                }
                if (roger[0].ToUpper() == "EXIT")
                {
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " disconnected"); });
                    MW.updateCounterOfActiveUsers(false);
                    current.Shutdown(SocketShutdown.Both);
                    current.Close();
                    client_sockets.Remove(current);
                    return;
                }
                else if (roger.Length == 2 && roger[0].ToUpper() == "CONTENT" && roger[1].ToUpper() == "ALLBOOKS")
                {
                    List <string> library = new List <string>();

                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { library = DatabaseOrder.DownloadContent(database_connection); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    foreach (string _book in library)
                    {
                        byte[] data = Encoding.UTF8.GetBytes(_book);
                        current.Send(data);
                        Thread.Sleep(100);
                    }
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " download library content."); });
                    byte[] data_end = Encoding.ASCII.GetBytes("CONTENT|END");
                    current.Send(data_end);
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                }
                else if (roger.Length == 2 && roger[0].ToUpper() == "CONTENT" && roger[1].ToUpper() == "MYBOOKS")
                {
                    List <string> my_library = new List <string>();

                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { my_library = DatabaseOrder.DownloadMyContent(database_connection, clientname); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    foreach (string _book in my_library)
                    {
                        byte[] data = Encoding.UTF8.GetBytes(_book);
                        current.Send(data);
                        Thread.Sleep(100);
                    }
                    MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " download library content."); });
                    byte[] data_end = Encoding.ASCII.GetBytes("CONTENT|END");
                    current.Send(data_end);
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                }
                else if (roger[0].ToUpper() == "ORDER" && roger.Length == 2)
                {
                    int  x = Int32.Parse(roger[1].ToString());
                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { bool_return = DatabaseOrder.OrderBook(database_connection, x, clientname); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    if (bool_return)
                    {
                        byte[] data = Encoding.ASCII.GetBytes("ORDER|TRUE");
                        current.Send(data);
                    }
                    else
                    {
                        byte[] data = Encoding.ASCII.GetBytes("ORDER|FALSE");
                        current.Send(data);
                    }
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                }
                else if (roger[0].ToUpper() == "RETURN" && roger.Length == 2)
                {
                    int  x = Int32.Parse(roger[1].ToString());
                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { bool_return = DatabaseOrder.ReturnBook(database_connection, x, clientname); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    if (bool_return)
                    {
                        byte[] data = Encoding.ASCII.GetBytes("RETURN|TRUE");
                        current.Send(data);
                    }
                    else
                    {
                        byte[] data = Encoding.ASCII.GetBytes("RETURN|FALSE");
                        current.Send(data);
                    }
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                }
                else if (roger[0].ToUpper() == "RESERVE" && roger.Length == 2)
                {
                    int  x = Int32.Parse(roger[1].ToString());
                    Task t = new Task(() => { MW.Dispatcher.Invoke(() => { bool_return = DatabaseOrder.ReserveBook(database_connection, x, clientname); }); });
                    MW.tasklist.Add(SingletonSecured.Instance.AddTask(t));
                    t.Start();
                    t.Wait();

                    if (bool_return)
                    {
                        byte[] data = Encoding.ASCII.GetBytes("RESERVE|TRUE");
                        current.Send(data);
                    }
                    else
                    {
                        byte[] data = Encoding.ASCII.GetBytes("RESERVE|FALSE");
                        current.Send(data);
                    }
                    current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
                }

                current.BeginReceive(BUFFER, 0, config.GetBuffer(), SocketFlags.None, ReceiveCallbackLoged, current);
            }
            else
            {
                MW.Dispatcher.Invoke(() => { MW.lbx_OperationsList.Items.Add("Client: " + current.RemoteEndPoint + " disconnected"); });
                MW.updateCounterOfActiveUsers(false);
                current.Shutdown(SocketShutdown.Both);
                current.Close();
                client_sockets.Remove(current);
                return;
            }
        }