Beispiel #1
0
        /// <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);
                }
            }
        }