コード例 #1
0
        public void SendRequest(TorrentFile inTorrentFile)
        {
            // If the torrent file is not complete, send a request at a more regular interval
            if (!inTorrentFile.completed)
            {
                // Set up and send the request, connect to peers if any are found
                if (inTorrentFile.SetUpRequest())
                {
                    manager.ConnectPeers(inTorrentFile);
                }

                // Create a timer
                System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

                // Set the interval to 30 seconds
                t.Interval = 30000; // specify interval time as you want
                t.Tick    += new EventHandler(timer_Tick);
                t.Start();

                // Send a new request after 30 seconds
                void timer_Tick(object sender, EventArgs e)
                {
                    SendRequest(inTorrentFile);
                }
            }

            // else the file is complete
            else
            {
                // Set up and send the request
                if (inTorrentFile.SetUpRequest())
                {
                    manager.ConnectPeers(inTorrentFile);
                }

                // Create a timer and set it's time to 5 minutes
                System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
                t.Interval = 300000; // specify interval time as you want
                t.Tick    += new EventHandler(timer_Tick);
                t.Start();

                // Send a request after 5 minutes
                void timer_Tick(object sender, EventArgs e)
                {
                    SendRequest(inTorrentFile);
                }
            }
        }
コード例 #2
0
        public void CheckFiles()
        {
            startServerMode();

            VerifyTorrentDialog verifyTorrent = new VerifyTorrentDialog();


            while (!manager.connected)
            {
            }

            // Set the path to find torrent files within the ID directory of this user
            string path = System.IO.Directory.GetCurrentDirectory();

            path = path + "\\" + id + "\\TorrentFiles";

            // If the directory exists, then check for files
            if (Directory.Exists(path))
            {
                // For each file in the directory

                verifyTorrent.StartPosition = FormStartPosition.CenterParent;
                verifyTorrent.Show(this);

                foreach (string file in Directory.EnumerateFiles(path, "*.txt"))
                {
                    verifyTorrent.Refresh();
                    // Create a torrentFile instance
                    TorrentFile torrentFile21 = new TorrentFile(id, manager.ip, manager.port, uIUpdater);
                    verifyTorrent.SetFile(torrentFile21.fileName);
                    // read the file into the instance
                    if (torrentFile21.Read(file))
                    {
                        // Add to the torrentfile list and check what parts of the file have been downloaded
                        manager.torrentFiles.Add(torrentFile21);
                        VerifyFile(torrentFile21);
                    }
                }

                verifyTorrent.Close();
            }
        }
コード例 #3
0
        private void buttonFileSelect_Click(object sender, EventArgs e)
        {
            string path;

            // Open a file dialog to select a torrent file
            OpenFileDialog file = new OpenFileDialog();

            if (file.ShowDialog() == DialogResult.OK)
            {
                path = file.FileName;

                // Create a new instance of a torrentfile
                torrentFile = new TorrentFile(id, manager.ip, manager.port, uIUpdater);

                // If the file is read
                if (torrentFile.Read(path))
                {
                    // Start the listener if it is not already
                    if (!manager.connected)
                    {
                        startServerMode();
                    }

                    else
                    {
                        // Check to make sure this torrent file is not already in use in the program
                        if (!manager.CheckTorrentFileDownloading(torrentFile))
                        {
                            // If not add to the list and verify what parts of the file are downloaded
                            manager.torrentFiles.Add(torrentFile);
                            VerifyFile(torrentFile);
                        }

                        else
                        {
                            MessageBox.Show("File is already downloading");
                        }
                    }
                }
            }
        }
コード例 #4
0
        public Peer(string inIpAdress, int inClient, bool indidConnect, TorrentFile inTorrentFile, Connection conn)
        {
            rand   = new Random();
            paused = false;
            Console.WriteLine("Peer created" + " " + inClient);
            downloading       = false;
            ipAddress         = inIpAdress;
            port              = inClient;
            handshakeSent     = false;
            handshakeRecieved = false;
            torrentFile       = inTorrentFile;
            connection        = conn;


            //if the peer is requesting the connection, the peer must send the handshake first
            didConnect = indidConnect;
            if (didConnect)
            {
                SendHandshake(torrentFile.fileName);
                handshakeSent = true;
            }
        }
コード例 #5
0
ファイル: ConnManager.cs プロジェクト: Swlt91/MyProjectsUni
        public void ConnectPeers(TorrentFile torrent)
        {
            //connects to each peer in the tracker response

            //if the torrent file is not complete, then attempt to connect
            if (!torrent.completed)
            {
                foreach (PeerResponse peer in torrent.peerList.ToList())
                {
                    try
                    {
                        //a check is made to ensure the peer from the tracker response is not already connected
                        bool found = false;

                        foreach (Connection con in connections)
                        {
                            if (con.peer.torrentFile.fileName.Contains(torrent.fileName) && con.ipAddress.Contains(peer.ipAddress) && con.port == peer.port)
                            {
                                found = true;
                                break;
                            }
                        }

                        //if no existing connection is found, then a new connection is made
                        if (!found)
                        {
                            Connection conn = ConnectTo(peer.ipAddress, peer.port, processPeer, torrent);
                        }
                    }

                    catch (Exception e)
                    {
                        // If the connection can not be made, remove this peer entry from the peer list within the torrent file
                        torrent.peerList.Remove(peer);
                        Console.WriteLine("Could not connect to " + peer);
                    }
                }
            }
        }
コード例 #6
0
ファイル: Connection.cs プロジェクト: Swlt91/MyProjectsUni
        public Connection(Socket sock, ConnManager manager, Func <Connection, int> processingFunction, TorrentFile inTorrentFile, int inPort, bool didConnect)
        {
            _state = new ConnectionState();

            // Set a timeout on the socket, as communication should be constant
            sock.ReceiveTimeout = 10000;
            sock.SendTimeout    = 10000;

            _state.sock = sock;

            g = Guid.NewGuid();

            IPEndPoint remoteIpEndPoint = sock.RemoteEndPoint as IPEndPoint;

            ipAddress = remoteIpEndPoint.Address.ToString();

            _manager = manager;

            _reader = new Reader(_state);
            _writer = new Writer(_state);

            // Set the port and torrent file
            port        = inPort;
            torrentFile = inTorrentFile;

            Console.WriteLine("CONNECTION CREATED!");
            peer = new Peer("", port, didConnect, torrentFile, this);

            if (processingFunction != null)
            {
                this.processingFunction = processingFunction;
            }
            else
            {
                this.processingFunction = this.defaultProcessing;
            }
        }
コード例 #7
0
        public void DownloadTorrentFile(Object sender, EventArgs e)
        {
            // Create new torrent file
            torrentFile = new TorrentFile(id, manager.ip, manager.port, uIUpdater);

            // Attempt to download the file
            if (torrentFile.DownloadFile(fileToDownload))
            {
                // If succcessful, add this torrent to the list
                manager.torrentFiles.Add(torrentFile);

                // If the listener is not running, run it
                if (!manager.connected)
                {
                    startServerMode();
                }

                else
                {
                    // Check what parts of the file have been acquired
                    VerifyFile(torrentFile);
                }
            }
        }
コード例 #8
0
 public void VerifyFile(TorrentFile torrent)
 {
     // Check what parts of the file have been downloaded and then send a request
     torrent.Verify();
     SendRequest(torrent);
 }
コード例 #9
0
        public void ReceiveHandshake(string fileName)
        {
            bool found = false;

            // If the peers was created from the listner, the handshake recieved will send which file it is requesting.
            // This peer then checks through the list of open torrents and checks if the requested one is on the list
            if (torrentFile == null)
            {
                foreach (TorrentFile torrent in connection._manager.torrentFiles)
                {
                    if (fileName.Equals(torrent.hashString))
                    {
                        torrentFile = torrent;
                        found       = true;
                        Console.WriteLine("PEER CONNECT");
                    }
                }
            }

            // Else the peer initiated the connection and knows which torrent is in use for this communication
            else
            {
                if (fileName.Equals(torrentFile.hashString))
                {
                    found = true;
                    Console.WriteLine("PEER CONNECT TORRENT");
                }
            }

            // Sends a handshake back and copies the piece need and have list to this peer instance
            // This is so the peer can document which pieces it has sent and recieved within this connection
            if (found && !handshakeSent)
            {
                SendHandshake(torrentFile.fileName);
                handshakeSent              = true;
                handshakeRecieved          = true;
                pieceNeedList              = new List <int>(torrentFile.pieceNeedList);
                pieceNeedListOriginalCount = pieceNeedList.Count;
                pieceHaveList              = new List <int>(torrentFile.pieceHaveList);
                pieceHaveListOriginalCount = pieceHaveList.Count;
                torrentFile.CalculatePercentage();

                // If the peer did not create the connected, send the file status to the connected peer
                if (!didConnect)
                {
                    SendFileStatus();
                }
            }
            else
            {
                pieceNeedList = new List <int>(torrentFile.pieceNeedList);
                pieceNeedListOriginalCount = pieceNeedList.Count;
                pieceHaveList = new List <int>(torrentFile.pieceHaveList);
                pieceHaveListOriginalCount = pieceHaveList.Count;
                torrentFile.CalculatePercentage();


                // If the torrent request was not found,
                if (!handshakeRecieved && !found)
                {
                    connection.write("DISCONNECT");
                    connection._state.kill = true;
                }
            }
        }
コード例 #10
0
ファイル: ConnManager.cs プロジェクト: Swlt91/MyProjectsUni
        public Connection ConnectTo(string inIpAddress, int port, Func <Connection, int> processingFunction, TorrentFile inTorrentFile)
        {
            #region Connect to the specified address
            System.Net.IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress  ipAddress     = IPAddress.Parse(inIpAddress);
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
            Socket     sender        = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            sender.Connect(localEndPoint);
            #endregion

            Connection connection = new Connection(sender, this, processingFunction, inTorrentFile, port, true);
            connections.Add(connection);


            Thread clientThread = new Thread(new ThreadStart(connection.start));
            clientThread.Start();
            return(connection);
        }