Beispiel #1
0
        /// <summary>
        /// Manage the connected client
        /// </summary>
        /// <param name="result">TCPClient object</param>
        /// <returns>Return a task, it is used to allow the Task asynchronous programming model (TAP)</returns>
        public async Task ServeClient(object result)
        {
            NetworkStream stream = null;
            TcpClient     client = null;
            Dictionary <string, FileRecvStatus> updateRecvDictionary = new Dictionary <string, FileRecvStatus>();
            Dictionary <string, FileSendStatus> updateSendDictionary = new Dictionary <string, FileSendStatus>();

            try {
                client = (TcpClient)result;
                Console.WriteLine($"{DateTime.Now.ToString()}\t - TCPListener Client connected! {((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()}");

                byte[] bytes = new byte[1 + 256 + 8];
                stream = client.GetStream();

                int i = 0;
                if ((i = await stream.ReadAsync(bytes, 0, bytes.Length)) != 0)
                {
                    // The first byte define the type of the packet
                    byte[] readPacket = new byte[1];
                    Array.Copy(bytes, 0, readPacket, 0, 1);

                    // If it is not correct, send an Exception
                    if ((int)readPacket[0] > 5)
                    {
                        throw new Exception($"Packet not valid");
                    }

                    // The following 256 bytes are the name of the file (UTF8 coding)
                    PacketType type = (PacketType)readPacket[0];
                    readPacket = new byte[256];
                    Array.Copy(bytes, 1, readPacket, 0, 256);
                    string       filename = "";
                    UTF8Encoding encoder;

                    // Trown an exception in case the sequence of bytes cannot be converted as a stirng
                    try {
                        encoder  = new UTF8Encoding();
                        filename = encoder.GetString(readPacket);
                        filename = filename.Replace("\0", string.Empty);
                    }
                    catch (Exception e) {
                        throw new Exception($"Packet not valid - Reason {e.Message}");
                    }
                    long dimFile = 0;

                    string ipClient = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();

                    switch (type)
                    {
                    case PacketType.RFILE:
                        if (_referenceData.GetInfoLocalUser().AcceptAllFile)
                        {
                            _referenceData.AddOrUpdateRecvStatus(ipClient, filename, FileRecvStatus.YSEND);

                            await MainWindow.main.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
                                MainWindow.main.AddOrUpdateListFile(ipClient, filename, FileRecvStatus.YSEND, "-", 0.0f);
                                MainWindow.main.NotifySistem();
                                MainWindow.main.SendResponse(filename, ipClient, PacketType.YFILE);
                            }));
                        }
                        else
                        {
                            _referenceData.AddOrUpdateRecvStatus(ipClient, filename, FileRecvStatus.TOCONF);

                            await MainWindow.main.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
                                MainWindow.main.AddOrUpdateListFile(ipClient, filename, FileRecvStatus.TOCONF, "-", 0.0f);
                                MainWindow.main.NotifySistem();
                            }));
                        }
                        break;

                    case PacketType.YFILE:
                        if (_referenceData.UpdateSendStatusFileForUser(ipClient, filename, FileSendStatus.CONFERMED))
                        {
                            await MainWindow.main.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
                                MainWindow.main.AddOrUpdateListFile(ipClient, filename, FileSendStatus.CONFERMED, "-", 0);
                                MainWindow.main.SendFile(filename, ipClient);
                            }));
                        }
                        else
                        {
                            throw new Exception("No file with name " + filename + " was announced from this client");
                        }
                        break;

                    case PacketType.NFILE:
                        if (_referenceData.UpdateSendStatusFileForUser(ipClient, filename, FileSendStatus.REJECTED))
                        {
                            await MainWindow.main.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => {
                                MainWindow.main.AddOrUpdateListFile(ipClient, filename, FileSendStatus.REJECTED, "-", 0);
                                MainWindow.main.SendFile(filename, ipClient);
                            }));
                        }
                        else
                        {
                            throw new Exception("No file with name " + filename + " was announced from this client");
                        }
                        break;

                    case PacketType.FSEND:
                    {
                        if (_referenceData.CheckPacketRecvFileStatus(ipClient, filename))
                        {
                            throw new Exception("File with name " + filename + " never confirmed or need to be resend to the user");
                        }
                        string fileNameOriginal = filename;

                        readPacket = new byte[8];
                        Array.Copy(bytes, 257, readPacket, 0, 8);
                        try {
                            dimFile = BitConverter.ToInt64(readPacket, 0);
                        }
                        catch (Exception e) {
                            throw new Exception($"Packet not valid - Reason {e.Message}");
                        }
                        await ServeReceiveFile(client, stream, ipClient, filename, dimFile);
                    }
                    break;

                    case PacketType.CIMAGE:
                        readPacket = new byte[8];
                        Array.Copy(bytes, 257, readPacket, 0, 8);
                        try {
                            dimFile = BitConverter.ToInt64(readPacket, 0);
                        }
                        catch (Exception e) {
                            throw new Exception($"Packet not valid - Reason {e.Message}");
                        }
                        await ServeReceiveProfileImage(client, stream, ipClient, filename, dimFile);

                        break;
                    }
                }
            }
            catch (SocketException e) {
                Console.WriteLine($"{DateTime.Now.ToString()}\t - Serve TCPClient SocketException - {e.Message}");
            }
            catch (Exception e) {
                Console.WriteLine($"{DateTime.Now.ToString()}\t - Serve TCPClient Exception - {e.GetType()} {e.Message}");
            }
            finally {
                stream.Close();
                client.Close();
            }
        }