void airServer_UserWantToSendFiles(Air.SendFiles sendFiles, AirServer.IPEndPointHolder remotePoint, TcpClient client)
        {
            if (Dispatcher.Thread != Thread.CurrentThread)
            {
                Dispatcher.BeginInvoke(new AirServer.UserSendsFiles(airServer_UserWantToSendFiles), new object[] { sendFiles, remotePoint, client });
                return;
            }

            foreach (UserIcon userIcon in PanelOfUsers.Children)
            {
                if (userIcon.RemotePoint.Equals(remotePoint))
                {
                    if (MessageBox.Show(this, string.Format("\"{0}\" wants to share {1} file(s) (~{2:0.00} MB) with you. Would you like to receive them now?",
                                                            userIcon.TextDisplayName.Text, sendFiles.Count, sendFiles.Size / 1024f), "AirFileExchange - Receiving files",
                                        MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        userIcon.IsCanceled = false;
                        userIcon.LastStatus = UserIcon.OperationStatus.None;

                        airServer.ReceiveFilesFromAsync(remotePoint, client, sendFiles, true, userIcon,
                                                        new AirServer.UserReceiveFilesProgress(airServer_UserReceiveFilesProgress),
                                                        new AirServer.UserReceiveFilesComplete(airServer_UserReceiveFilesComplete));
                    }
                    else
                    {
                        airServer.ReceiveFilesFromAsync(remotePoint, client, sendFiles, false, userIcon, null,
                                                        new AirServer.UserReceiveFilesComplete(airServer_UserReceiveFilesComplete));
                    }
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        public void SendFilesTo(IPAddress ipAddress, string[] files, object state, SendFilesProgress progress)
        {
            SendFiles sendFiles = new SendFiles();
            sendFiles.Count = files.Length;
            sendFiles.UserAddress = new UserAddress() { Address = Helper.LocalIPAddress(), Port = DefaultPort };
            sendFiles.Size = 0;

            long totalSize = 0, sentTotal = 0, sentCurrent = 0;

            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                sendFiles.Files.Add(new SendFile()
                {
                    Name = fileInfo.Name,
                    Size = fileInfo.Length
                });

                sendFiles.Size += fileInfo.Length / 1024;
                totalSize += fileInfo.Length;
            }

            using (TcpClient tcpClient = new TcpClient())
            {
                tcpClient.Connect(ipAddress, DefaultPort);

                byte[] buffer = Encoding.UTF8.GetBytes(Helper.XmlSerialize<SendFiles>(sendFiles));

                NetworkStream networkStream = tcpClient.GetStream();
                networkStream.Write(buffer, 0, buffer.Length);

                try
                {
                    byte[] bytes = new byte[1024];
                    StringBuilder stringBuilder = new StringBuilder();

                    int i;
                    while ((i = networkStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        stringBuilder.Append(Encoding.UTF8.GetString(bytes, 0, i));
                        if (i < bytes.Length)
                            break;
                    }

                    // Sending files?
                    try
                    {
                        SendFilesData sendFilesData = Helper.XmlDeserialize<SendFilesData>(stringBuilder.ToString());

                        if ("allowed".Equals(sendFilesData.Status))
                        {
                            bool cancel = false;

                            foreach (string file in files)
                            {
                                // not open a file from user's desktop directory ???
                                using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                                {
                                    sentCurrent = 0;

                                    while (!cancel && (i = fileStream.Read(bytes, 0, bytes.Length)) != 0)
                                    {
                                        networkStream.Write(bytes, 0, i);

                                        sentCurrent += i;
                                        sentTotal += i;

                                        if (progress != null)
                                        {
                                            progress(file, ipAddress, sentCurrent, fileStream.Length, sentTotal, totalSize, state, out cancel);
                                        }
                                    }
                                }
                                if (cancel)
                                {
                                    throw new OperationCanceledException();
                                }
                            }
                        }
                        else
                        {
                            throw new AirDeniedException();
                        }
                    }
                    finally
                    {
                    }
                }
                finally
                {
                }
            }
        }