Git Product home page Git Product logo

Comments (3)

miroslavpejic85 avatar miroslavpejic85 commented on July 20, 2024 1

Hello @Reda-Beloued, thank you very much, yes of course, It would be a very nice feature to add.

from p2p.

leoashish avatar leoashish commented on July 20, 2024 1

Hey @miroslavpejic85, I would like to contribute to this issue. I have around 6 months of experience in ASP.NET and C#.

from p2p.

miroslavpejic85 avatar miroslavpejic85 commented on July 20, 2024

We can create a new UdtSocket for a new connection to handle the File Transfer.

I leave a snippet to start/take inspiration from and a simple video on how to enable the drag & drop on the PictureBox item.

Contributions are welcome :)

Receiver.cs

using System.IO;
using UdtSharp;

namespace p2pconn
{
    static class Receiver
    {
        static internal void Run(UdtSocket connection)
        {
            using (var netStream = new UdtNetworkStream(connection))
            using (var writer = new BinaryWriter(netStream))
            using (var reader = new BinaryReader(netStream))
            {
                string fileName = reader.ReadString();
                long size = reader.ReadInt64();

                byte[] buffer = new byte[4 * 1024 * 1024];

                using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
                {
                    long read = 0;

                    while (read < size)
                    {
                        int toRecv = reader.ReadInt32();

                        ReadFragment(reader, toRecv, buffer);

                        fileStream.Write(buffer, 0, toRecv);

                        read += toRecv;

                        writer.Write(true);
                    }
                }
            }
        }

        static int ReadFragment(BinaryReader reader, int size, byte[] buffer)
        {
            int read = 0;

            while (read < size)
            {
                read += reader.Read(buffer, read, size - read);
            }

            return read;
        }
    }
}

Sender.cs

using System;
using System.IO;
using UdtSharp;

namespace p2pconn
{
    static class Sender
    {
        static internal void Run(UdtSocket connection, string file, bool logVerbose)
        {
            using (var netStream = new UdtNetworkStream(connection))
            using (var writer = new BinaryWriter(netStream))
            using (var reader = new BinaryReader(netStream))
            using (var fileReader = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                long fileSize = new FileInfo(file).Length;

                writer.Write(Path.GetFileName(file));
                writer.Write(fileSize);

                byte[] buffer = new byte[512 * 1024];

                long pos = 0;

                while (pos < fileSize)
                {
                    int toSend = buffer.Length < (fileSize - pos)
                        ? buffer.Length
                        : (int)(fileSize - pos);

                    fileReader.Read(buffer, 0, toSend);

                    int iteration = Environment.TickCount;

                    writer.Write(toSend);
                    connection.Send(buffer, 0, toSend);

                    if (!reader.ReadBoolean())
                    {
                        Console.WriteLine("Error in transmission");
                        return;
                    }

                    pos += toSend;

                    if (logVerbose)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Current: {0} / s",
                            SizeConverter.ConvertToSizeString(toSend / (Environment.TickCount - iteration) * 1000));
                    }
                }
            }
        }
    }
}

SizeConverter.cs

namespace p2pconn
{
    internal enum DataSizeUnit
    {
        Bytes = 1,
        KiloBytes = 1024,
        MegaBytes = 1024 * 1024,
        GigaBytes = 1024 * 1024 * 1024
    };

    internal static class SizeConverter
    {
        internal static string ConvertToSizeString(long size)
        {
            DataSizeUnit totalSizeUnit = SizeConverter.GetSuitableUnit(size);
            return string.Format("{0:#0.##} {1}", SizeConverter.ConvertToSize(
                size, totalSizeUnit), SizeConverter.GetUnitString(totalSizeUnit));
        }

        internal static float ConvertToSize(long size, DataSizeUnit unit)
        {
            return (float)size / (float)unit;
        }

        static string GetUnitString(DataSizeUnit unit)
        {
            switch (unit)
            {
                case DataSizeUnit.Bytes: return "bytes";
                case DataSizeUnit.KiloBytes: return "KB";
                case DataSizeUnit.MegaBytes: return "MB";
                case DataSizeUnit.GigaBytes: return "GB";
            }
            return string.Empty;
        }

        static DataSizeUnit GetSuitableUnit(long size)
        {
            if (size >= 0 && size < (long)DataSizeUnit.KiloBytes)
                return DataSizeUnit.Bytes;
            else if (size >= (long)DataSizeUnit.KiloBytes && size <= (long)DataSizeUnit.MegaBytes)
                return DataSizeUnit.KiloBytes;
            else if (size >= (long)DataSizeUnit.MegaBytes && size <= (long)DataSizeUnit.GigaBytes)
                return DataSizeUnit.MegaBytes;
            else
                return DataSizeUnit.GigaBytes;
        }
    }
}

from p2p.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.