Example #1
0
 public void SendFile(Client client, string path, Packet packet, FileUploadCallback sent = null, FileUploadCallback success = null, FileUploadCallback fail = null)
 {
     client.SendingPackets.Add(DownloadPacketCreator.AskDownload((int)new FileInfo(path).Length, "test", packet));
     Tasks.Add(new DownloadTask()
     {
         MainClient = client,
         FileStream = File.OpenRead(path),
         Hash       = "test",
         Sent       = sent,
         Success    = success,
         Fail       = fail
     });
 }
Example #2
0
        public static List <Packet> AskDownload(Core core, Client client, Packet packet)
        {
            PacketArg args = packet.GetArguments(new[]
            {
                typeof(int),
                typeof(string),
                typeof(string)
            });

            DownloadModule module = core.GetModule <DownloadModule>();
            DownloadTask   t      = module.CreateDownload(args.Get <string>(1));

            /*if (core.GetModule<Plexer>().PacketMayBeTreated(args.Get<string>(2)) == false)
             *  throw new Exception("Impossible to treat the packet " + args.Get<string>(2));*/

            t.Size       = args.Get <int>(0);
            t.Callback   = new Packet(packet.ExtractAfter(' ', 2));
            t.MainClient = client;
            return(DownloadPacketCreator.DownloadAccept(t.Token, args.Get <string>(1)).ToList());
        }
Example #3
0
        public void Update(Core core)
        {
            foreach (DownloadTask task in Tasks)
            {
                if (task.Started)
                {
                    try
                    {
                        byte[] b = new byte[DownloadModuleConfig.MaxPacketSize];

                        if (task.Mode == false && !task.Ended)
                        {
                            int i = task.Client.GetSocket().Receive(b, b.Length, SocketFlags.None);

                            task.Downloaded += i;

                            if (i != 0)
                            {
                                task.Write(b, i);
                            }

                            if (task.Downloaded == task.Size || i == 0)
                            {
                                task.Ended = true;
                            }

                            if (task.Ended)
                            {
                                task.ToRemove = true;
                                task.FileStream.Close();
                                try
                                {
                                    if (task.Downloaded == task.Size)
                                    {
                                        Core.GetModule <Plexer>().HandlePackets(Core, task.MainClient, task.Callback);
                                    }
                                    else
                                    {
                                        throw new Exception();
                                    }
                                }
                                catch (Exception)
                                {
                                    task.MainClient.SendingPackets.Add(DownloadPacketCreator.UnvalidateDL(task.Token));
                                }
                            }
                        }
                        else if (!task.Ended)
                        {
                            task.ReadFile();
                            task.Send();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            Tasks.RemoveAll(i => i.ToRemove && i.Client.SocketDisconnect());
        }