'Called by network thread, but safe for any'

def network_stop(self, removestate, removecontent):

    with self.dllock:
        self._logger.debug('LibtorrentDownloadImpl:    network_stop    %s', self.tdef.get_name())
        self.cancel_all_pending_tasks()
        pstate = self.get_persistent_download_config()
        if (self.handle is not None):
            self._logger.debug('LibtorrentDownloadImpl:    network_stop:    engineresumedata    from    torrent    handle')
            self.pstate_for_restart = pstate
            if removestate:
                self.ltmgr.remove_torrent(self, removecontent)
                self.handle = None
            else:
                self.set_vod_mode(False)
                self.handle.pause()
                self.save_resume_data()
        else:
            self.cancel_pending_task('check_create_wrapper')
            if (self.dlstate == DLSTATUS_CIRCUITS):
                self.dlstate = DLSTATUS_STOPPED
            if (self.pstate_for_restart is not None):
                self._logger.debug('LibtorrentDownloadImpl:    network_stop:    Reusing    previously    saved    engineresume    data    for    checkpoint')
                pstate.set('state', 'engineresumedata', self.pstate_for_restart.get('state', 'engineresumedata'))
            else:
                self._logger.debug('LibtorrentDownloadImpl:    network_stop:    Could    not    reuse    engineresumedata    as    pstart_for_restart    is    None')
        if removestate:
            self.session.lm.remove_pstate(self.tdef.get_infohash())
