示例#1
0
        private async void TakeCareOfTCPClient(TcpClient paramClient)
        {
            reader = null;
            try
            {
                MyLogger.Log("Preparing to read and write Data");
                stream = paramClient.GetStream();
                reader = new StreamReader(stream);

                while (running)
                {
                    sendToClient("provide UserName", paramClient);


                    Debug.WriteLine("*** Ready to read ");
                    MyLogger.Log("ready to read data");


                    size = await stream.ReadAsync(buffMessage, 0, buffMessage.Length); //start reading data and get the size

                    username = Encoding.ASCII.GetString(buffMessage, 0, size);         //decode the data in the buffer to a string
                    MyLogger.Log($"username provided is {username}");


                    Array.Clear(buffMessage, 0, buffMessage.Length);
                    sendToClient("Provide Password", paramClient);


                    size = await stream.ReadAsync(buffMessage, 0, buffMessage.Length); //start reading data and get the size

                    password = Encoding.ASCII.GetString(buffMessage, 0, size);         //decode the data in the buffer to a string
                    MyLogger.Log($"password provided is {password}");


                    if (size == 0)
                    {
                        MyLogger.Log("data to read  is null");
                        RemoveClient(paramClient);

                        Debug.WriteLine("Socket disconnected");
                        MyLogger.Log("Socket disconnected");
                        break;
                    }

                    checkAuthorization(username, password);
                    // if the user is authorozied, startlistening to commands.  "user inside the server"
                    if (isAuthorized == true)
                    {
                        Array.Clear(buffMessage, 0, buffMessage.Length);

                        sendToClient("your options are : '1 for list'  ' 2 for delete'  '3 for upload'  '4 for download'", paramClient);

                        MyLogger.Log($"the path to directory is {pathToDirectory} ");
                        Debug.WriteLine($"the path to directory is {pathToDirectory}");

                        size = await stream.ReadAsync(buffMessage, 0, buffMessage.Length);//start reading data and get the size

                        string   getvalue  = Encoding.ASCII.GetString(buffMessage, 0, size);
                        commands comandNum = (commands)int.Parse(getvalue);// cust the comand integer to enum

                        switch (comandNum)
                        {
                        case commands.list:
                            listAllStoredFiles();
                            break;

                        case commands.delete:
                            sendToClient("provide file name", returnedByAccept);
                            size = await stream.ReadAsync(buffMessage, 0, buffMessage.Length); //start reading data and get the size

                            fileName = Encoding.ASCII.GetString(buffMessage, 0, size);         //decode the data in the buffer to a string
                            await delteFileAsync(fileName);

                            break;

                        case commands.download:
                            break;

                        case commands.upload:

                            break;

                        default:
                            break;
                        }
                    }
                    //clear the buffer
                    Array.Clear(buffMessage, 0, buffMessage.Length);
                }
            }
            catch (Exception excp)
            {
                MyLogger.Log($"will remove client- error accured {paramClient}");
                RemoveClient(paramClient);
                MyLogger.Log(excp.ToString());
            }
        }