コード例 #1
0
        public void RequestFileTransfer(string sender, string remoteFilename, string localFilename)
        {
            CCBP2PFileWorker fileWorker = GetFileWorker();

            fileWorker.PrepareInFile(sender, m_uid, remoteFilename, localFilename);
            QueueCommand(new CCBNetworkerCommandData(CCBNetworkerCommand.nwc_requestFileTransfer, sender, remoteFilename));
            m_filexferSignal.Set();
        }
コード例 #2
0
        private bool CheckFileWorkerForErrors(CCBP2PFileWorker fileworker)
        {
            string sender    = null;
            string recipient = null;
            string filename  = fileworker.HasErrorFile(ref sender, ref recipient);

            if (null != filename)
            {
                m_clientChannel.OnFileError(sender, recipient, filename);
                return(true);
            }
            return(false);
        }
コード例 #3
0
 public CCBP2PNetworker()
 {
     m_working           = true;
     m_commandList       = new Queue <CCBNetworkerCommandData>();
     m_closeSignal       = new ManualResetEvent(false);
     m_filexferSignal    = new ManualResetEvent(false);
     m_cmdSignal         = new AutoResetEvent(false);
     m_worker            = new Thread(new ThreadStart(Listener));
     m_factory           = null;
     m_clientChannel     = null;
     m_fileWorker        = null;
     m_peer              = new CeebeetlePeerImpl();
     m_peer.PingCallback = new CeebeetlePeerImpl.OnPingedD(PingCallback);
     m_peer.FileTransferResponseCallback = new CeebeetlePeerImpl.OnFileTransferResponseD(OnFileTransferResponse);
     m_onFileDone          = null;
     m_selectStoreCallback = null;
 }
コード例 #4
0
        private void OnFileTransferResponse(string recipient, string filename, bool accept)
        {
            if (accept)
            {
                CCBP2PFileWorker fileWorker = GetFileWorker();

                fileWorker.FileRequested(m_uid, recipient, filename);
                m_filexferSignal.Set();
            }
            else
            {
                //If transfer was canceled and we haven't started the fileworker, no need to do so now.
                if (null != m_fileWorker)
                {
                    m_fileWorker.FileCanceled(recipient, filename);
                }
            }
        }
コード例 #5
0
        private CCBP2PFileWorker GetFileWorker()
        {
            CCBP2PFileWorker fileWorker = m_fileWorker;

            //Accessing fileworker from multiple threads, so use singleton pattern.
            if (null == fileWorker)
            {
                lock (this)
                {
                    if (null == m_fileWorker)
                    {
                        m_fileWorker = new CCBP2PFileWorker(m_closeSignal);
                        m_peer.AddListener(m_fileWorker);
                    }
                    fileWorker = m_fileWorker;
                    fileWorker.OnFileTransferDoneCallback = m_onFileDone;
                }
            }
            return(fileWorker);
        }
コード例 #6
0
 public void Stop()
 {
     m_closeSignal.Set();
     if (null != m_fileWorker)
     {
         if (null != m_peer)
         {
             m_peer.RemoveListener(m_fileWorker);
         }
         m_fileWorker.Stop();
         m_fileWorker = null;
     }
     if (m_worker.IsAlive)
     {
         m_worker.Join();
     }
     if (null != m_host)
     {
         m_host.Close();
         m_host = null;
     }
 }
コード例 #7
0
        public void Listener()
        {
            WaitHandle[] waitors = new WaitHandle[3] {
                m_closeSignal, m_cmdSignal, m_filexferSignal
            };

            if (null == m_host)
            {
                m_host = new ServiceHost(this);
                m_host.Open();
            }
            while (m_working)
            {
                int ixSig = WaitHandle.WaitAny(waitors);

                if (0 == ixSig)
                {
                    break;
                }
                else if (1 == ixSig)
                {
                    ExecuteNextCommand();
                }
                else if ((2 == ixSig) && (null != m_clientChannel))
                {
                    CCBP2PFileWorker fileWorker = GetFileWorker();
                    string           fileToSend = fileWorker.HasUploadWork();

                    if (null != fileToSend)
                    {
                        CCBP2PFileDataEnvelope dataTosend = null;
                        int    cb        = fileWorker.RetrieveDataToSend(fileToSend, ref dataTosend);
                        byte[] hash      = null;
                        string recipient = null;

                        if ((0 != cb) && (null != dataTosend))
                        {
                            try
                            {
                                Debug(string.Format("Sending {0} bytes from {1}", cb, dataTosend.m_localFileName));
                                m_clientChannel.SendFileData(m_uid, dataTosend.m_recipient, dataTosend.m_localFileName, dataTosend.m_start, dataTosend.m_bytes);
                                fileWorker.MarkDataSent(fileToSend, dataTosend);
                                if (fileWorker.IsSent(fileToSend, ref hash, ref recipient))
                                {
                                    m_clientChannel.OnFileComplete(m_uid, recipient, dataTosend.m_localFileName, hash);
                                    fileWorker.FileFinalized(fileToSend);
                                }
                            }
                            catch (CommunicationException commEx)
                            {
                                Error("Exception sending file data: " + commEx.Message);
                                fileWorker.FileOnError(fileToSend);
                            }
                            catch (Exception ex)
                            {
                                Error("Exception sending file data: " + ex.Message);
                                fileWorker.FileOnError(fileToSend);
                            }
                        }
                        else if (fileWorker.IsSent(fileToSend, ref hash, ref recipient))
                        {
                            m_clientChannel.OnFileComplete(m_uid, recipient, fileToSend, hash);
                            fileWorker.FileFinalized(fileToSend);
                        }
                    }
                    if (fileWorker.HasWork())
                    {
                        if (m_closeSignal.WaitOne(73))
                        {
                            break;
                        }
                        while (CheckFileWorkerForErrors(fileWorker))
                        {
                            if (m_closeSignal.WaitOne(0))
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        m_filexferSignal.Reset();
                    }
                }
                else
                {
                    Fatal(string.Format("Error return waiting for handles in networker: {0}", ixSig));
                }
            }
            Close();
            m_peer.OnDisconnected();
            Debug("Chat listener thread exiting.");
        }