Ejemplo n.º 1
0
    }    // END Application_Idle

    private void RefreshView()
    {
        // Wait until it is safe to enter.
        //mut.WaitOne();

        lock (mThreadLock)
        {
            RefreshListView();

            mStatusStripDhtLabel.Text = String.Format("Dht Nodes: {0}", DllInterface.NumberOfDhtNodes());

            if (mListView.SelectedItems.Count == 1)
            {
                Torrent torrent = torrentManager.GetTorrent(mSelectedTorrentId);
                if (torrent != null)
                {
                    if (mTabPageGeneral.Visible)
                    {
                        RefreshGeneralTabPage(mSelectedTorrentId, torrent.MetaData);
                    }

                    if (mTabPageTracker.Visible)
                    {
                        RefreshTrackerTab(torrent.Handle);
                    }

                    if (mTabPagePeers.Visible)
                    {
                        RefreshPeersTab(torrent.Handle);
                    }

                    if (mTabPagePieces.Visible)
                    {
                        RefreshPiecesTab(torrent.Handle);
                    }

                    if (mTabPageFiles.Visible)
                    {
                        RefreshFileTab(torrent.Handle);
                    }


                    //if (mTabPageBandwidth.Visible)
                    //{
                    //	mBandwidthGraph.Refresh(torrent);
                    //}


                    //if (mPeersListView.Visible == true)
                    //{
                    //	this.mPeersListView.Sort();
                    //}

                    ProcessTorrentEventFlags();
                }
            }
        }
        // Release the Mutex.
        //mut.ReleaseMutex();
    }    // END RefreshView
Ejemplo n.º 2
0
    private void TimerEventOneSecond(Object myObject, EventArgs myEventArgs)
    {
        if (State != BtmState.Running)
        {
            return;
        }

        DllInterface.TorrentHandles handles = new DllInterface.TorrentHandles();
        DllInterface.GetAllTorrentHandles(ref handles);


        for (Int32 i = 0; i < handles.mNumHandles; i++)
        {
            TorrentHandle handle = handles.mHandles[i];


            // THREADING ISSUE???!!?!??!?!

            DllInterface.TorrentMetaData meta = new DllInterface.TorrentMetaData();
            DllInterface.GetTorrentMetaData(handle, ref meta);

            Torrent torrent = GetTorrent(handle);
            if (torrent != null)
            {
                torrent.MetaData = meta;
            }
        }
    }
Ejemplo n.º 3
0
    static void CompressStream(Stream from, Stream to)
    {
        byte[] src = new byte[from.Length];

        // read file
        if (from.Read(src, 0, src.Length) == src.Length)
        {
            int dstSize = DllInterface.aP_max_packed_size(src.Length);
            int wrkSize = DllInterface.aP_workmem_size(src.Length);

            // allocate mem
            byte[] dst = new byte[dstSize];
            byte[] wrk = new byte[wrkSize];

            // compress data
            int packedSize = DllInterface.aPsafe_pack(
                src,
                dst,
                src.Length,
                wrk,
                new DllInterface.CompressionCallback(ShowProgress),
                0
                );

            // write compressed data
            to.Write(dst, 0, packedSize);

            Console.WriteLine("compressed to {0} bytes", packedSize);
        }
    }
Ejemplo n.º 4
0
    }    // END AddTorrentToolStripMenuItem_Click

    private void ExitMenu_Click(object sender, EventArgs e)
    {
        DllInterface.SetDebugStringOutputCb(null);
        DllInterface.DeInitTorrentManager();
        mAppNotifyIcon.Dispose();
        Application.Exit();
    }    // END ExitMenu_Click
Ejemplo n.º 5
0
    void PrintDistanceViaUnity()
    {
        var pos1 = cube1.transform.position;
        var pos2 = cube2.transform.position;

        Debug.Log("This is a log from Unity");
        Debug.Log("Distance:" + DllInterface.GetDistance(pos1.x, pos1.y, pos2.x, pos2.y));
    }
Ejemplo n.º 6
0
 // Use this for initialization
 void Start()
 {
     cube1 = GameObject.Find("Cube1");
     cube2 = GameObject.Find("Cube2");
     //pass C#'s delegate to C++
     DllInterface.InitCSharpDelegate(DllInterface.LogMessageFromCpp);
     PrintDistanceViaUnity();
 }
Ejemplo n.º 7
0
 private static void OnApplicationIdle(object sender, EventArgs e)
 {
     while (AppStillIdle)
     {
         // idle time (no messages are waiting)
         DllInterface.UpdateTorrentManager();
         Thread.Sleep(1);
     }
 }
Ejemplo n.º 8
0
    }    // END OptionsMenuItem_Click

    private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        lock (mThreadLock)
        {
            DllInterface.SetDebugStringOutputCb(null);
            DllInterface.DeInitTorrentManager();
            mAppNotifyIcon.Dispose();
            //mTorrentWorkerThread.Abort("MK Shutdown Abort.");
        }
    }    // END MainForm_FormClosed
Ejemplo n.º 9
0
    }    // END RemoveAndDeleteTorrentToolStripMenuItem_Click

    private void ForceRecheckToolStripMenuItem_Click(object sender, EventArgs e)
    {
        lock (mThreadLock)
        {
            if (mSelectedTorrentId != (TorrentHandle)Torrent.TorrentValues.INVALID_TORRENT)
            {
                DllInterface.TorrentForceRecheck(mSelectedTorrentId);
            }
        }
    }    // END ForceRecheckToolStripMenuItem_Click
Ejemplo n.º 10
0
 private void OptionsDialog_Load(object sender, EventArgs e)
 {
     DllInterface.GetTorrentClientOptions(ref mOptions);
     mOptionCheck_ForceEncryption.Checked          = (mOptions.mAllConnectionsMustBeEncrypted != 0);
     mOptionCheck_EnableDht.Checked                = (mOptions.mUseDht != 0);
     mOptionCheck_UseTrackers.Checked              = (mOptions.mUseTrackers != 0);
     mOptionCheck_CheckForBuild.Checked            = (mOptions.mCheckForLatestBuild != 0);
     mOptionCheck_StopOnCompletionTorrents.Checked = (mOptions.mStopOnCompletion != 0);
     mEditbox_MaxUploadRate.Text = (mOptions.mMaxUploadRate / 1024).ToString();
     mEditbox_ListenPort.Text    = DllInterface.GetListenerPort().ToString();
 }
Ejemplo n.º 11
0
    }    // END AllowNonEncryptedConnectionsForThisTorrentToolStripMenuItem_Click

    private void mMainListViewRightClickMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
    {
        lock (mThreadLock)
        {
            if (mSelectedTorrentId != (TorrentHandle)Torrent.TorrentValues.INVALID_TORRENT)
            {
                bool connectionsMustbeSecure = DllInterface.DoesTorrentOnlyUsesEncryptedConnections(mSelectedTorrentId);
                mMenuItemAllowUnencryptedComs.Checked = !connectionsMustbeSecure;
            }
        }
    }
Ejemplo n.º 12
0
    }    // END StopAllDownloadsToolStripMenuItem_Click

    private void AllowNonEncryptedConnectionsForThisTorrentToolStripMenuItem_Click(object sender, EventArgs e)
    {
        lock (mThreadLock)
        {
            if (mSelectedTorrentId != (TorrentHandle)Torrent.TorrentValues.INVALID_TORRENT)
            {
                bool connectionsMustbeSecure = DllInterface.DoesTorrentOnlyUsesEncryptedConnections(mSelectedTorrentId);
                DllInterface.SetTorrentOnlyUsesEncryptedConnections(mSelectedTorrentId, !connectionsMustbeSecure);
            }
        }
    }    // END AllowNonEncryptedConnectionsForThisTorrentToolStripMenuItem_Click
Ejemplo n.º 13
0
 private void mOkButton_Click(object sender, EventArgs e)
 {
     // write all options back to the struct
     mOptions.mAllConnectionsMustBeEncrypted = (byte)(mOptionCheck_ForceEncryption.Checked ? 1 : 0);
     mOptions.mStopOnCompletion    = (byte)(mOptionCheck_StopOnCompletionTorrents.Checked ? 1 : 0);
     mOptions.mUseDht              = (byte)(mOptionCheck_EnableDht.Checked ? 1 : 0);
     mOptions.mUseTrackers         = (byte)(mOptionCheck_UseTrackers.Checked ? 1 : 0);
     mOptions.mCheckForLatestBuild = (byte)(mOptionCheck_CheckForBuild.Checked ? 1 : 0);
     mOptions.mMaxUploadRate       = (Convert.ToUInt32(mEditbox_MaxUploadRate.Text) * 1024);
     DllInterface.SetListenerPort((Convert.ToUInt16(mEditbox_ListenPort.Text)));
 }
Ejemplo n.º 14
0
    }// END MainForm_Resize

    private void OptionsMenuItem_Click(object sender, EventArgs e)
    {
        lock (mThreadLock)
        {
            // show the modal dialog until the OK Cancel is	pressed
            OptionsDialog optionsDialog = new OptionsDialog();
            if (optionsDialog.ShowDialog() == DialogResult.OK)
            {
                DllInterface.SetTorrentClientOptions(optionsDialog.mOptions);
            }
        }
    }    // END OptionsMenuItem_Click
Ejemplo n.º 15
0
 private void mEditbox_ListenPort_Leave(object sender, EventArgs e)
 {
     try
     {
         mEditbox_ListenPort.Text = Convert.ToUInt16(mEditbox_ListenPort.Text).ToString();
     }
     catch
     {
         MessageBox.Show("You can only type a number here (0-65535). You should use a port number higher than 1024.");
         mEditbox_ListenPort.Text = DllInterface.GetListenerPort().ToString();
         mEditbox_ListenPort.Focus();
     }
 }
Ejemplo n.º 16
0
    }    // END ProcessTorrentEventFlags

    public void UpdateStatusStripValues()
    {
        lock (mThreadLock)
        {
            float dlSpeed = ((float)DllInterface.TotalDownloadRate() / 1024.0f);
            float ulSpeed = ((float)DllInterface.TotalUploadRate() / 1024.0f);

            mStatusStripDlLabel.Text = String.Format("D:{0:0.0}kB/s", dlSpeed);
            mStatusStripUlLabel.Text = String.Format("U:{0:0.0}kB/s", ulSpeed);

            // sort the system tray balloon tip while we are here as the BalloonShown msg does not work
            mAppNotifyIcon.Text = String.Format("D:{0:0.0}kB/s\nU:{1:0.0}kB/s", dlSpeed, ulSpeed);
        }
    }    // END UpdateStatusStripValues
Ejemplo n.º 17
0
    }    // END PauseTorrentButton_Click

    private void StopTorrentButton_Click(object sender, EventArgs e)
    {
        if (torrentManager.State != BitTorrentManager.BtmState.Running)
        {
            return;
        }

        lock (mThreadLock)
        {
            if (mSelectedTorrentId != (TorrentHandle)Torrent.TorrentValues.INVALID_TORRENT)
            {
                DllInterface.StopTorrent(mSelectedTorrentId);
            }
        }
    }    // END StopTorrentButton_Click
Ejemplo n.º 18
0
    }    // END MainForm_FormClosed

    private void AddTorrentButton_Click(object sender, EventArgs e)
    {
        if (torrentManager.State != BitTorrentManager.BtmState.Running)
        {
            return;
        }

        OpenFileDialog openFileDialog = new OpenFileDialog();

        //openFileDialog1.InitialDirectory = "c:\\" ;
        openFileDialog.Filter           = "All files (*.*)|*.*|torrent files (*.torrent)|*.torrent";
        openFileDialog.FilterIndex      = 2;
        openFileDialog.RestoreDirectory = true;
        openFileDialog.Multiselect      = true;

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            lock (mThreadLock)
            {
                for (Int32 i = 0; i < openFileDialog.FileNames.Length; i++)
                {
                    FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
                    folderDialog.Description = "Select where to download the torrent files.";
                    // Default to the My Documents folder.
                    //folderDialog.RootFolder = Environment.SpecialFolder.Personal;

                    DialogResult result = folderDialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        String folderName = folderDialog.SelectedPath;

                        Int32 handle = DllInterface.AddTorrent(openFileDialog.FileNames[i], folderName);
                        if (handle < 0)
                        {
                            MessageBox.Show(String.Format("The torrent: {0} is invalid", openFileDialog.SafeFileNames[i]), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
    }    // END AddTorrentButton_Click
Ejemplo n.º 19
0
    static void DecompressStream(Stream from, Stream to)
    {
        byte[] src = new byte[from.Length];

        // read file
        if (from.Read(src, 0, src.Length) == src.Length)
        {
            int dstSize = DllInterface.aPsafe_get_orig_size(src);

            // allocate mem
            byte[] dst = new byte[dstSize];

            // decompress data
            int depackedSize = DllInterface.aPsafe_depack(src, src.Length, dst, dstSize);

            // write compressed data
            to.Write(dst, 0, depackedSize);

            Console.WriteLine("decompressed to {0} bytes", depackedSize);
        }
    }
Ejemplo n.º 20
0
    private void ShowNewVersionDialog(String version)
    {
        // Marshall the call to the ui thread
        if (InvokeRequired)
        {
            object[]       o = new object[] { version };
            InvokeDelegate d = new InvokeDelegate(ShowNewVersionDialog);
            Invoke(d, o);
        }
        else
        {
            DllInterface.sTorrentClientOptions options = new DllInterface.sTorrentClientOptions();
            DllInterface.GetTorrentClientOptions(ref options);

            if (options.mCheckForLatestBuild != 0 &&
                Application.ProductVersion.Equals(version) == false)
            {
                AppFrontend.AboutBox dialog = new AppFrontend.AboutBox();
                dialog.ShowDialog(AppFrontend.AboutBox.AboutMode.NewBuildAvailable);
            }
        }
    }
Ejemplo n.º 21
0
    public MainForm()
    {
        mSelectedTorrentId = (TorrentHandle)Torrent.TorrentValues.INVALID_TORRENT;
        versionChecked     = false;

        mSyncContext = System.ComponentModel.AsyncOperationManager.SynchronizationContext;
        InitializeComponent();

#if DEBUG
        mOutputWnd = new ConsoleOutputWindow();
        mOutputWnd.AddString("App Startup\n");
#endif

        debugStringDelegate = new DllInterface.DebugString_Delegate(DebugStringCallback);
        DllInterface.SetDebugStringOutputCb(debugStringDelegate);

        torrentManager = new BitTorrentManager();
        DllInterface.InitTorrentManager(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

        // Create an instance of a ListView column sorter and assign it to the ListView control.
        lvwColumnSorter = new ListViewColumnSorter();
        this.mPeersListView.ListViewItemSorter = lvwColumnSorter;
    }// END MainForm
Ejemplo n.º 22
0
    private void TimerEventOneMinute(Object myObject, EventArgs myEventArgs)
    {
        if (State != BtmState.Running)
        {
            return;
        }

        foreach (Torrent torrent in torrentList)
        {
            torrent.SubmitGraphData((TimeTorrentManagerRunning() / (60000)), (torrent.MetaData.mDownloadSpeed / 1024), (TimeTorrentManagerRunning() / 60000), (torrent.MetaData.mUploadSpeed / 1024));
        }


        float dlSpeed = ((float)DllInterface.TotalDownloadRate() / 1024.0f);
        float ulSpeed = ((float)DllInterface.TotalUploadRate() / 1024.0f);

        downloadGraphData.Add(TimeTorrentManagerRunning() / 60000, dlSpeed);
        uploadGraphData.Add(TimeTorrentManagerRunning() / 60000, ulSpeed);

        if (MainForm.mBandwidthGraph != null)
        {
            MainForm.mBandwidthGraph.UpdateCurves();
        }
    }
Ejemplo n.º 23
0
    }    // END FindPeerInPeerListView

    public void RefreshPeersTab(TorrentHandle torrentId)
    {
        UInt32 numConnectedPeers = DllInterface.NumberOfConnectedPeers(torrentId);

        if (numConnectedPeers == 0)
        {
            return;
        }

        // this approach allocates space for the data here, UNMANAGED needs freeing
        IntPtr[] ps = new IntPtr[numConnectedPeers];
        for (int n = 0; n < numConnectedPeers; n++)
        {
            ps[n] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DllInterface.sPeerInfo)));
        }

        UInt32 numWritten = DllInterface.GetConnectedPeersInfo(torrentId, ps, numConnectedPeers);

        DllInterface.sPeerInfo[] peersInfo = new DllInterface.sPeerInfo[numConnectedPeers];
        for (int i = 0; i < numConnectedPeers; i++)
        {
            peersInfo[i] = (DllInterface.sPeerInfo)Marshal.PtrToStructure(ps[i], typeof(DllInterface.sPeerInfo));
        }


        /*
         * // this approach relies on the called function allocating space for the data
         * IntPtr ptrArray = IntPtr.Zero;
         * int count = DllInterface.ArrayTest(ref ptrArray);
         * DllInterface.sPeerInfo sAddr = (DllInterface.sPeerInfo)Marshal.PtrToStructure(ptrArray, typeof(DllInterface.sPeerInfo));
         * for (int i = 0; i < count; i++)
         * {
         *      IntPtr nextPtr = new IntPtr(ptrArray.ToInt32() + i * Marshal.SizeOf(sAddr));
         *      sAddr = (DllInterface.sPeerInfo)Marshal.PtrToStructure(nextPtr, typeof(DllInterface.sPeerInfo));
         *
         *      Console.WriteLine("{0} {1}", sAddr.addr, sAddr.port);
         * }
         */


        // remove peers we are no longer connected to
        for (Int32 i = 0; i < mPeersListView.Items.Count; i++)
        {
            SockAddr listId = (SockAddr)mPeersListView.Items[i].Tag;

            bool found = false;
            for (UInt32 j = 0; j < numConnectedPeers; j++)
            {
                SockAddr peerId = new SockAddr(peersInfo[j].mIp, peersInfo[j].mPort);
                if (listId == peerId)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                mPeersListView.Items.RemoveAt(i);
                i = -1;
            }
        }


        for (UInt32 i = 0; i < numConnectedPeers; i++)
        {
            if (peersInfo[i].mHandshakeRecvd == false)
            {
                continue;
            }

            // keep a copy of the ip & port as the id
            SockAddr peersSockAddr = new SockAddr(peersInfo[i].mIp, peersInfo[i].mPort);

            ListViewItem listViewItem = FindPeerInPeerListView(peersSockAddr);
            ListViewItem.ListViewSubItem peerIdItem;
            ListViewItem.ListViewSubItem percentageItem;
            ListViewItem.ListViewSubItem downSpeedItem;
            ListViewItem.ListViewSubItem upSpeedItem;
            ListViewItem.ListViewSubItem reqsItem;
            ListViewItem.ListViewSubItem flagsItem;
            ListViewItem.ListViewSubItem connectionTypeItem;
            ListViewItem.ListViewSubItem totalDownloadedItem;
            ListViewItem.ListViewSubItem totalUploadedItem;
            ListViewItem.ListViewSubItem connectionLengthItem;

            if (listViewItem == null)
            {
                // create the new item and pass the ip string
                listViewItem = new System.Windows.Forms.ListViewItem(String.Format("{0}.{1}.{2}.{3} : {4}", peersInfo[i].mIp[0], peersInfo[i].mIp[1], peersInfo[i].mIp[2], peersInfo[i].mIp[3], peersInfo[i].mPort));

                // peer id
                peerIdItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(peerIdItem);

                // % done
                percentageItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(percentageItem);


                // dl
                downSpeedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(downSpeedItem);

                // ul
                upSpeedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(upSpeedItem);

                // reqs
                reqsItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(reqsItem);

                // flags
                flagsItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(flagsItem);

                // connection type
                connectionTypeItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(connectionTypeItem);

                // total downloaded
                totalDownloadedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(totalDownloadedItem);

                // total uploaded
                totalUploadedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(totalUploadedItem);

                // Connection Length
                connectionLengthItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(connectionLengthItem);


                listViewItem.Tag = peersSockAddr;

                mPeersListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem });
            }
            else
            {
                peerIdItem           = listViewItem.SubItems[1];
                percentageItem       = listViewItem.SubItems[2];
                downSpeedItem        = listViewItem.SubItems[3];
                upSpeedItem          = listViewItem.SubItems[4];
                reqsItem             = listViewItem.SubItems[5];
                flagsItem            = listViewItem.SubItems[6];
                connectionTypeItem   = listViewItem.SubItems[7];
                totalDownloadedItem  = listViewItem.SubItems[8];
                totalUploadedItem    = listViewItem.SubItems[9];
                connectionLengthItem = listViewItem.SubItems[10];
            }

            String strNewValue;

            // peer id
            System.Text.ASCIIEncoding ae = new System.Text.ASCIIEncoding();
            String strPeerId             = ae.GetString(peersInfo[i].mPeerId);
            if (peerIdItem.Text != strPeerId)
            {
                peerIdItem.Text = strPeerId;
            }


            // % done
            strNewValue = String.Format("{0:0.0}%", peersInfo[i].mPercentageDone);
            if (percentageItem.Text != strNewValue)
            {
                percentageItem.Text = strNewValue;
            }

            // dl
            float speed = ((float)peersInfo[i].mDlRate / 1024.0f);
            strNewValue = String.Format("{0:0.0} kB/s", speed);
            if (downSpeedItem.Text != strNewValue)
            {
                downSpeedItem.Text = strNewValue;
            }

            // ul
            speed       = ((float)peersInfo[i].mUlRate / 1024.0f);
            strNewValue = String.Format("{0:0.0} kB/s", speed);
            if (upSpeedItem.Text != strNewValue)
            {
                upSpeedItem.Text = strNewValue;
            }

            // reqs
            strNewValue = String.Format("{0} | {1}", peersInfo[i].mOustandingDownloadRequests, peersInfo[i].mOustandingUploadRequests);
            if (reqsItem.Text != strNewValue)
            {
                reqsItem.Text = strNewValue;
            }

            // Flags
            String strFlags = "";
            if (peersInfo[i].mAmChoking == 1)
            {
                strFlags = strFlags + "C";
            }
            if (peersInfo[i].mIsChokingMe == 1)
            {
                strFlags = strFlags + "c";
            }
            if (peersInfo[i].mAmInterested == 1)
            {
                strFlags = strFlags + "I";
            }
            if (peersInfo[i].mIsInterestedInMe == 1)
            {
                strFlags = strFlags + "i";
            }
            if (flagsItem.Text != strFlags)
            {
                flagsItem.Text = strFlags;
            }


            // Connection type
            if ((peersInfo[i].mConnectionFlags & (byte)DllInterface.sPeerInfo.ConnectionFlag.INCOMING_CONNECTION) != 0)
            {
                strNewValue = "Incoming";
            }
            else
            {
                strNewValue = "Outgoing";
            }
            if ((peersInfo[i].mConnectionFlags & (byte)DllInterface.sPeerInfo.ConnectionFlag.ENCRYPTED_CONNECTION) != 0)
            {
                strNewValue += " Encrypted";
            }
            else
            {
                strNewValue += " Unencrypted";
            }
            if (connectionTypeItem.Text != strNewValue)
            {
                connectionTypeItem.Text = strNewValue;
            }


            // total downloaded
            strNewValue = FormatBytes(peersInfo[i].mTotalBytesDownloaded);
            if (totalDownloadedItem.Text != strNewValue)
            {
                totalDownloadedItem.Text = strNewValue;
            }

            // total uploaded
            strNewValue = FormatBytes(peersInfo[i].mTotalBytesUploaded);
            if (totalUploadedItem.Text != strNewValue)
            {
                totalUploadedItem.Text = strNewValue;
            }


            // Connection Length
            TimeSpan t = TimeSpan.FromSeconds(peersInfo[i].mConnectionLengthInSeconds);
            if (t.Days > 0)
            {
                strNewValue = string.Format("{0}d {1:D2}h {2:D2}m", t.Days, t.Hours, t.Minutes);
            }
            else if (t.Hours > 0)
            {
                strNewValue = string.Format("{0}h {1:D2}m", t.Hours, t.Minutes);
            }
            else if (t.Minutes > 0)
            {
                strNewValue = string.Format("{0} minute", t.Minutes);
                if (t.Minutes > 1)
                {
                    strNewValue = strNewValue + "s";
                }
            }
            else
            {
                strNewValue = string.Format("{0} seconds", t.Seconds);
            }

            if (connectionLengthItem.Text != strNewValue)
            {
                connectionLengthItem.Text = strNewValue;
            }
        }


        // free the unmanaged memory
        for (int n = 0; n < ps.Length; n++)
        {
            Marshal.FreeHGlobal(ps[n]);
        }
    }    // END RefreshPeersTab
Ejemplo n.º 24
0
    }    // END FindTorrentItemInMainListView

    public void RefreshListView()
    {
        // remove old torrents
        bool removed = false;

        do
        {
            removed = false;
            for (Int32 i = 0; i < mListView.Items.Count; i++)
            {
                TorrentHandle handle = (TorrentHandle)mListView.Items[i].Tag;

                if (torrentManager.GetTorrent(handle) == null)
                {
                    mListView.Items.Remove(mListView.Items[i]);
                    removed = true;
                    break;
                }
            }
        }while(removed == true);


        for (Int32 i = 0; i < torrentManager.TorrentCount; i++)
        {
            Torrent torrent = torrentManager.TorrentAtIndex(i);

            DllInterface.TorrentMetaData meta = torrent.MetaData;


            ListViewItem listViewItem = FindTorrentItemInMainListView(torrent.Handle);
            ListViewItem.ListViewSubItem idItem;
            ListViewItem.ListViewSubItem sizeItem;
            ListViewItem.ListViewSubItem doneItem;
            ListViewItem.ListViewSubItem statusItem;
            ListViewItem.ListViewSubItem seedsItem;
            ListViewItem.ListViewSubItem peersItem;
            ListViewItem.ListViewSubItem downSpeedItem;
            ListViewItem.ListViewSubItem upSpeedItem;
            ListViewItem.ListViewSubItem etaItem;


            bool addToControl = false;
            if (listViewItem == null)
            {
                addToControl = true;
            }

            if (addToControl)
            {
                // create the new item and pass the torrent name, all other details are sub items
                listViewItem = new System.Windows.Forms.ListViewItem(meta.mName);

                idItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(idItem);

                sizeItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(sizeItem);

                doneItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(doneItem);

                statusItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(statusItem);

                seedsItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(seedsItem);

                peersItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(peersItem);

                downSpeedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(downSpeedItem);

                upSpeedItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(upSpeedItem);

                etaItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(etaItem);

                mListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem });

                // custom data
                listViewItem.Tag = torrent.Handle;
            }
            else
            {
                idItem        = listViewItem.SubItems[1];
                sizeItem      = listViewItem.SubItems[2];
                doneItem      = listViewItem.SubItems[3];
                statusItem    = listViewItem.SubItems[4];
                seedsItem     = listViewItem.SubItems[5];
                peersItem     = listViewItem.SubItems[6];
                downSpeedItem = listViewItem.SubItems[7];
                upSpeedItem   = listViewItem.SubItems[8];
                etaItem       = listViewItem.SubItems[9];
            }

            // set the image depending on if the connection is encrypted or not
            if (DllInterface.DoesTorrentOnlyUsesEncryptedConnections(torrent.Handle))
            {
                // fully encrypted, padlock icon
                listViewItem.ImageIndex = 6;
            }
            else
            {
                // some connections are plain text
                listViewItem.ImageIndex = 7;
            }


            String strNewValue;

            // Name
            if (listViewItem.Text != meta.mName)
            {
                listViewItem.Text = meta.mName;
            }

            strNewValue = torrent.Handle.ToString();
            if (idItem.Text != strNewValue)
            {
                idItem.Text = strNewValue;
            }

            // size
            Int64 totalSize = meta.mTotalSize;
            strNewValue = FormatBytes(totalSize);             //String.Format("{0:0} MB", (totalSize / (1024.0f * 1024.0f)));
            if (sizeItem.Text != strNewValue)
            {
                sizeItem.Text = strNewValue;
            }


            // done
            float onePercent = (meta.mTotalPieces / 100.0f);
            float done       = meta.mPiecesDownloaded / onePercent;
            if (float.IsNaN(done))
            {
                done = 0.0f;
            }
            strNewValue = String.Format("{0:0.0}%", done);
            if (doneItem.Text != strNewValue)
            {
                doneItem.Text = strNewValue;
            }

            // status
            switch (meta.mState)
            {
            case DllInterface.TorrentMetaData.TorrentState.Stopped:
                strNewValue = String.Format("Stopped");
                break;

            case DllInterface.TorrentMetaData.TorrentState.CreateFiles:
                strNewValue = String.Format("Creating Files");
                break;

            case DllInterface.TorrentMetaData.TorrentState.PeerMode:
                strNewValue = String.Format("Downloading");
                break;

            case DllInterface.TorrentMetaData.TorrentState.SeedMode:
                strNewValue = String.Format("Seeding");
                break;

            case DllInterface.TorrentMetaData.TorrentState.Queued:
                strNewValue = String.Format("Queued");
                break;

            case DllInterface.TorrentMetaData.TorrentState.Rechecking:
                strNewValue = String.Format("Checking");
                break;
            }
            if (statusItem.Text != strNewValue)
            {
                statusItem.Text = strNewValue;
            }

            // Seeds
            UInt32 numConnectedSeeds = meta.mNumSeeds;
            //UInt32 totalSeeds = 0;
            //strNewValue = String.Format("{0} ({1})", numConnectedSeeds, totalSeeds);
            strNewValue = String.Format("{0}", numConnectedSeeds);
            if (seedsItem.Text != strNewValue)
            {
                seedsItem.Text = strNewValue;
            }

            // Peers
            UInt32 numConnectedPeers = meta.mNumPeers;
            //UInt32 totalPeers = 0;
            strNewValue = String.Format("{0}", numConnectedPeers);
            if (peersItem.Text != strNewValue)
            {
                peersItem.Text = strNewValue;
            }


            // down speed
            float speed = ((float)meta.mDownloadSpeed / 1024.0f);
            strNewValue = String.Format("{0:0.0} kB/s", speed);
            if (downSpeedItem.Text != strNewValue)
            {
                downSpeedItem.Text = strNewValue;
            }

            // up speed
            speed       = ((float)meta.mUploadSpeed / 1024.0f);
            strNewValue = String.Format("{0:0.0} kB/s", speed);
            if (upSpeedItem.Text != strNewValue)
            {
                upSpeedItem.Text = strNewValue;
            }


            // TODO : if state is done, leave the following field blank...


            // eta
            if (meta.mDownloadSpeed == 0)
            {
                char chInfinity = (char)0x221E;

                strNewValue = String.Format("{0}", chInfinity);
                if (etaItem.Text != strNewValue)
                {
                    etaItem.Text = strNewValue;
                }
            }
            else
            {
                TimeSpan t = TimeSpan.FromSeconds(meta.mEta);
                if (t.Days > 0)
                {
                    strNewValue = string.Format("{0}d {1:D2}h {2:D2}m", t.Days, t.Hours, t.Minutes);
                }
                else if (t.Hours > 0)
                {
                    strNewValue = string.Format("{0}h {1:D2}m", t.Hours, t.Minutes);
                }
                else if (t.Minutes > 0)
                {
                    strNewValue = string.Format("{0} minute", t.Minutes);
                    if (t.Minutes > 1)
                    {
                        strNewValue = strNewValue + "s";
                    }
                }
                else
                {
                    strNewValue = string.Format("{0} seconds", t.Seconds);
                }

                if (etaItem.Text != strNewValue)
                {
                    etaItem.Text = strNewValue;
                }
            }
        }
    }// END RefreshListView
Ejemplo n.º 25
0
    }    // END FindPieceInListView

    public void RefreshFileTab(TorrentHandle torrentId)
    {
        UInt32 numFiles = DllInterface.NumberOfFilesInTorrent(torrentId);

        if (numFiles == 0)
        {
            //mFilesListView.Items.Clear();
            return;
        }

        // this approach allocates space for the data here, UNMANAGED needs freeing
        IntPtr[] ps = new IntPtr[numFiles];
        for (int n = 0; n < numFiles; n++)
        {
            ps[n] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DllInterface.sFileInfo)));
        }

        UInt32 numWritten = DllInterface.GetTorrentFilesInfo(torrentId, ps, numFiles);

        DllInterface.sFileInfo[] fileInfo = new DllInterface.sFileInfo[numFiles];
        for (int i = 0; i < numFiles; i++)
        {
            fileInfo[i] = (DllInterface.sFileInfo)Marshal.PtrToStructure(ps[i], typeof(DllInterface.sFileInfo));
        }



        // Remove file we are no longer actively downloading
        for (Int32 i = 0; i < mFilesListView.Items.Count; i++)
        {
            String filename = (String)mFilesListView.Items[i].Tag;

            bool found = false;
            for (UInt32 j = 0; j < numFiles; j++)
            {
                String storedFilename = fileInfo[j].mFilename;
                if (storedFilename == filename)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                mFilesListView.Items.RemoveAt(i);
                i = -1;
            }
        }


        for (UInt32 i = 0; i < numFiles; i++)
        {
            // keep a copy of the file name to use as the id
            String storedfilename = fileInfo[i].mFilename;

            ListViewItem listViewItem = FindFileInListView(storedfilename);
            ListViewItem.ListViewSubItem nameItem;
            ListViewItem.ListViewSubItem sizeItem;
            ListViewItem.ListViewSubItem numPiecesItem;
            ListViewItem.ListViewSubItem percentageDoneItem;

            if (listViewItem == null)
            {
                // create the new item and pass the name string
                listViewItem = new System.Windows.Forms.ListViewItem(String.Format("{0}", storedfilename));

                // size
                float size = (float)fileInfo[i].mSize / (1024.0f * 1024.0f);
                sizeItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0:0.00} MB", size));
                listViewItem.SubItems.Add(sizeItem);

                // num pieces
                numPiecesItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0}", fileInfo[i].mNumberOfPieces));
                listViewItem.SubItems.Add(numPiecesItem);

                // % done
                percentageDoneItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0:0.0}%", fileInfo[i].mPercentageComplete));
                listViewItem.SubItems.Add(percentageDoneItem);

                listViewItem.Tag = storedfilename;

                mFilesListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem });
            }
            else
            {
                nameItem           = listViewItem.SubItems[0];
                sizeItem           = listViewItem.SubItems[1];
                numPiecesItem      = listViewItem.SubItems[2];
                percentageDoneItem = listViewItem.SubItems[3];
            }

            String strNewValue;


            // percentageDoneItem
            strNewValue = String.Format("{0:0.0}%", fileInfo[i].mPercentageComplete);
            if (percentageDoneItem.Text != strNewValue)
            {
                percentageDoneItem.Text = strNewValue;
            }
        }


        // free the unmanaged memory
        for (int n = 0; n < ps.Length; n++)
        {
            Marshal.FreeHGlobal(ps[n]);
        }
    }    // END RefreshFileTab
Ejemplo n.º 26
0
    }    // END FindPieceInListView

    public void RefreshPiecesTab(TorrentHandle torrentId)
    {
        UInt32 numPieces = DllInterface.NumberOfActivePieces(torrentId);

        if (numPieces == 0)
        {
            mPiecesListView.Items.Clear();
            return;
        }

        // this approach allocates space for the data here, UNMANAGED needs freeing
        IntPtr[] ps = new IntPtr[numPieces];
        for (int n = 0; n < numPieces; n++)
        {
            ps[n] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DllInterface.sPiecesInfo)));
        }

        UInt32 numWritten = DllInterface.GetActivePiecesInfo(torrentId, ps, numPieces);

        DllInterface.sPiecesInfo[] piecesInfo = new DllInterface.sPiecesInfo[numPieces];
        for (int i = 0; i < numPieces; i++)
        {
            piecesInfo[i] = (DllInterface.sPiecesInfo)Marshal.PtrToStructure(ps[i], typeof(DllInterface.sPiecesInfo));
        }


        // Remove pieces we are no longer actively downloading
        for (Int32 i = 0; i < mPiecesListView.Items.Count; i++)
        {
            UInt32 pieceNumber = (UInt32)mPiecesListView.Items[i].Tag;

            bool found = false;
            for (UInt32 j = 0; j < numPieces; j++)
            {
                UInt32 storedPieceNum = piecesInfo[j].mPieceNumber;
                if (storedPieceNum == pieceNumber)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                mPiecesListView.Items.RemoveAt(i);
                i = -1;
            }
        }


        for (UInt32 i = 0; i < numPieces; i++)
        {
            // keep a copy of the piece number to use as the id
            UInt32 storedPieceNum = piecesInfo[i].mPieceNumber;

            ListViewItem listViewItem = FindPieceInListView(storedPieceNum);
            ListViewItem.ListViewSubItem pieceNumItem;
            ListViewItem.ListViewSubItem sizeItem;
            ListViewItem.ListViewSubItem numBlocksItem;
            ListViewItem.ListViewSubItem requestedBlocksItem;
            ListViewItem.ListViewSubItem completedBlocksItem;

            if (listViewItem == null)
            {
                // create the new item and pass the id string
                listViewItem = new System.Windows.Forms.ListViewItem(String.Format("{0}", storedPieceNum));

                // size
                float size = (float)piecesInfo[i].mPieceSize / (1024.0f * 1024.0f);
                sizeItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0:0.0} MB", size));
                listViewItem.SubItems.Add(sizeItem);

                // num blocks
                numBlocksItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0}", piecesInfo[i].mNumberOfBlocks));
                listViewItem.SubItems.Add(numBlocksItem);

                // requested blocks
                requestedBlocksItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0}", piecesInfo[i].mRequestedBlocks));
                listViewItem.SubItems.Add(requestedBlocksItem);

                // completed blocks
                completedBlocksItem = new ListViewItem.ListViewSubItem(listViewItem, String.Format("{0}", piecesInfo[i].mCompletedBlocks));
                listViewItem.SubItems.Add(completedBlocksItem);

                listViewItem.Tag = storedPieceNum;

                mPiecesListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem });
            }
            else
            {
                pieceNumItem        = listViewItem.SubItems[0];
                sizeItem            = listViewItem.SubItems[1];
                numBlocksItem       = listViewItem.SubItems[2];
                requestedBlocksItem = listViewItem.SubItems[3];
                completedBlocksItem = listViewItem.SubItems[4];
            }

            String strNewValue;


            // requested blocks
            strNewValue = String.Format("{0}", piecesInfo[i].mRequestedBlocks);
            if (requestedBlocksItem.Text != strNewValue)
            {
                requestedBlocksItem.Text = strNewValue;
            }


            // completedBlocksItem
            strNewValue = String.Format("{0}", piecesInfo[i].mCompletedBlocks);
            if (completedBlocksItem.Text != strNewValue)
            {
                completedBlocksItem.Text = strNewValue;
            }
        }



        // free the unmanaged memory
        for (int n = 0; n < ps.Length; n++)
        {
            Marshal.FreeHGlobal(ps[n]);
        }
    }    // END RefreshPiecesTab
Ejemplo n.º 27
0
    }    // END FindAnnounceTargetInTrackerListView

    public void RefreshTrackerTab(TorrentHandle torrentId)
    {
        UInt32 numAnnounceTargets = DllInterface.NumberOfAnnounceTargets(torrentId);

        if (numAnnounceTargets == 0)
        {
            mTrackerListView.Items.Clear();
            return;
        }

        // this approach allocates space for the data here, UNMANAGED needs freeing
        IntPtr[] ps = new IntPtr[numAnnounceTargets];
        for (int n = 0; n < numAnnounceTargets; n++)
        {
            ps[n] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DllInterface.sAnnounceInfo)));
        }

        UInt32 numWritten = DllInterface.GetAnnounceTargetsInfo(torrentId, ps, numAnnounceTargets);

        DllInterface.sAnnounceInfo[] announceTargetsInfo = new DllInterface.sAnnounceInfo[numAnnounceTargets];
        for (int i = 0; i < numAnnounceTargets; i++)
        {
            announceTargetsInfo[i] = (DllInterface.sAnnounceInfo)Marshal.PtrToStructure(ps[i], typeof(DllInterface.sAnnounceInfo));
        }


        // remove peers we are no longer connected to
        for (Int32 i = 0; i < mTrackerListView.Items.Count; i++)
        {
            String urlTag = (string)mTrackerListView.Items[i].Tag;

            bool found = false;
            for (UInt32 j = 0; j < numAnnounceTargets; j++)
            {
                if (urlTag == announceTargetsInfo[j].mUrl)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                mTrackerListView.Items.RemoveAt(i);
                i = -1;
            }
        }


        for (UInt32 i = 0; i < numAnnounceTargets; i++)
        {
            ListViewItem listViewItem = FindAnnounceTargetInTrackerListView(announceTargetsInfo[i].mUrl);

            ListViewItem.ListViewSubItem urlItem;
            ListViewItem.ListViewSubItem updateInItem;
            ListViewItem.ListViewSubItem seedsItem;
            ListViewItem.ListViewSubItem peersItem;
            ListViewItem.ListViewSubItem intervalItem;

            if (listViewItem == null)
            {
                // create the new item and pass the url string
                listViewItem = new System.Windows.Forms.ListViewItem(String.Format("{0}", announceTargetsInfo[i].mUrl));

                // Url
                //urlItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                //listViewItem.SubItems.Add(urlItem);

                // Update In
                updateInItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(updateInItem);

                // Seeds
                seedsItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(seedsItem);

                // Peers
                peersItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(peersItem);

                // Interval
                intervalItem = new ListViewItem.ListViewSubItem(listViewItem, "");
                listViewItem.SubItems.Add(intervalItem);

                listViewItem.Tag = announceTargetsInfo[i].mUrl;

                mTrackerListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem });
            }
            else
            {
                urlItem      = listViewItem.SubItems[0];
                updateInItem = listViewItem.SubItems[1];
                seedsItem    = listViewItem.SubItems[2];
                peersItem    = listViewItem.SubItems[3];
                intervalItem = listViewItem.SubItems[4];
            }



            String str;

            // Update In
            UInt32 minutes = (announceTargetsInfo[i].mNextUpdateMs / 1000 / 60);
            switch (minutes)
            {
            case 0:
                str = String.Format("Updating ...", minutes);
                break;

            default:
                str = String.Format("{0}m", minutes);
                break;
            }
            if (updateInItem.Text != str)
            {
                updateInItem.Text = str;
            }

            // Seeds
            str = String.Format("{0}", 0);
            if (seedsItem.Text != str)
            {
                seedsItem.Text = str;
            }

            // Peers
            str = String.Format("{0}", announceTargetsInfo[i].mNumberOfPeersFound);
            if (peersItem.Text != str)
            {
                peersItem.Text = str;
            }

            // Interval
            minutes = (announceTargetsInfo[i].mAnnounceInterval / 1000 / 60);
            str     = String.Format("{0}m", minutes);
            if (intervalItem.Text != str)
            {
                intervalItem.Text = str;
            }
        }

        // free the unmanaged memory
        for (int n = 0; n < ps.Length; n++)
        {
            Marshal.FreeHGlobal(ps[n]);
        }
    }    // END RefreshTrackerTab