コード例 #1
0
ファイル: MyUDPSender.cs プロジェクト: GiMoody/PDSProject
        public void Sender(string multicastAddr)
        {
            UdpClient sender = new UdpClient();

            // It serialize the file content and send it
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Host));

            try
            {
                MemoryStream ms = new MemoryStream();
                ser.WriteObject(ms, _referenceData.GetInfoLocalUser().ConvertToHost());
                ms.Position = 0;
                byte[] buffer = ms.ToArray();
                sender.Send(buffer, buffer.Length, multicastAddr, SharedInfo.UDPReceivedPort);
                sender.Close();
            }
            catch (Exception e) {
                Console.WriteLine($"{DateTime.Now.ToString()}\t - UDPSender Exception - {e.GetType()} {e.Message}");
            }
            finally {
                sender.Close();
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
0
ファイル: MyTCPSender.cs プロジェクト: GiMoody/PDSProject
        /// <summary>
        /// Send to all the connected user the profile image of local user
        /// </summary>
        public async Task SendProfilePicture()
        {
            // Get the current list of online users
            List <Host> currentHosts = _referenceData.GetOnlineUsers();

            string currentImagePath = _referenceData.GetInfoLocalUser().ProfileImagePath;

            if (!Utility.PathToFileName(currentImagePath).Equals(_referenceData.defaultImage))
            {
                IPAddress serverAddr;
                foreach (Host host in currentHosts)
                {
                    serverAddr = IPAddress.Parse(host.Ip);
                    int attempts = 0;

                    // In case of exception resend the packet three times
                    do
                    {
                        TcpClient     client = null;
                        NetworkStream stream = null;

                        try {
                            attempts++;
                            client = new TcpClient();
                            await client.ConnectAsync(serverAddr.ToString(), SharedInfo.TCPPort).ConfigureAwait(false);

                            // It sends also the Hash of the current image
                            byte[] bytes = new byte[1 + 256 + 8 + 32];

                            // 1^ byte: packet type
                            bytes[0] = (byte)PacketType.CIMAGE;

                            // Following 256 bytes : file name
                            UTF8Encoding encorder = new UTF8Encoding();
                            encorder.GetBytes(Utility.PathToFileName(currentImagePath)).CopyTo(bytes, 1);

                            // Save the hash of the file
                            byte[] hash;
                            using (SHA256 sha = SHA256.Create()) {
                                FileStream file = File.OpenRead(currentImagePath);
                                hash = sha.ComputeHash(file);
                                file.Close();
                            }

                            // Open file image
                            using (var file = new FileStream(currentImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize * 1024, FileOptions.Asynchronous | FileOptions.SequentialScan)) {
                                // Following 8 bytes : file legth
                                BitConverter.GetBytes(file.Length).CopyTo(bytes, 257);

                                // Following 32 bytes : hash of the image
                                hash.CopyTo(bytes, 265);

                                // Get Network stream
                                stream = client.GetStream();

                                // First 297 : header
                                await stream.WriteAsync(bytes, 0, 297).ConfigureAwait(false);

                                // Following 64K of payload (profile image)
                                bytes = new byte[bufferSize * 64];
                                await file.CopyToAsync(stream, bufferSize).ConfigureAwait(false);
                            }
                            break;
                        }
                        catch (SocketException e) {
                            Console.WriteLine($"{DateTime.Now.ToString()}\t - SocketException on SendProfilePicture - {e.Message}");

                            // If the remote host was offline, try to resend it for three times
                            string UserStatus = _referenceData.GetUserStatus(host.Ip);
                            if (!UserStatus.Equals("") && UserStatus.Equals("offline"))
                            {
                                break;
                            }
                            else if (attempts == 3)
                            {
                                break;
                            }
                            else
                            {
                                await Task.Delay(10).ConfigureAwait(false);
                            }
                        }
                        catch (Exception e) {
                            Console.WriteLine($"{DateTime.Now.ToString()}\t - Exception on SendProfilePicture - {e.Message}");
                            if (attempts == 3)
                            {
                                break;
                            }
                            else
                            {
                                await Task.Delay(10).ConfigureAwait(false);
                            }
                        }
                        finally {
                            client.Close();
                            stream.Close();
                        }
                    } while (true);
                }
            }
        }