예제 #1
0
        /// <summary>
        /// Another user has offered to send a file. This method should be called
        /// to accept the offer and save the file to the give location. The parameters
        /// needed to call this method are provided by the <c>OnDccFileTransferRequest()</c>
        /// event.
        /// </summary>
        /// <remarks>
        /// This method should be called from within a try/catch block
        /// in case it is unable to connect or there are other socket
        /// errors.
        /// </remarks>
        /// <param name="dccUserInfo">Information on the remote user.</param>
        /// <param name="dccFileInfo">The local file that will hold the data being sent. If the file
        /// is the result of a previous incomplete download the the attempt will be made
        /// to resume where the previous left off.</param>
        /// <param name="turbo">Will the send ahead protocol be used.</param>
        /// <returns>A unique session instance for this file and remote user.</returns>
        /// <exception cref="ArgumentException">If the listen port is already in use.</exception>
        public static DccFileSession Receive(DccUserInfo dccUserInfo, DccFileInfo dccFileInfo, bool turbo)
        {
            Debug.WriteLineIf(DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] DccFileSession::Receive()");
            //Test if we are already using this port
            if (DccFileSessionManager.DefaultInstance.ContainsSession("C" + dccUserInfo.remoteEndPoint.Port))
            {
                throw new ArgumentException("Already listening on port " + dccUserInfo.remoteEndPoint.Port);
            }
            DccFileSession session = null;

            try
            {
                session = new DccFileSession(dccUserInfo, dccFileInfo, (64 * 1024),
                                             dccUserInfo.remoteEndPoint.Port, "C" + dccUserInfo.remoteEndPoint.Port);
                //Has the initiator specified the turbo protocol?
                session.turboMode = turbo;
                //Open file for writing
                dccFileInfo.OpenForWrite();
                DccFileSessionManager.DefaultInstance.AddSession(session);
                //Determine if we can resume a download
                if (session.dccFileInfo.ShouldResume())
                {
                    session.waitingOnAccept = true;
                    session.dccFileInfo.SetResumeToFileSize();
                    session.SendResume();
                }
                else
                {
                    session.thread      = new Thread(new ThreadStart(session.Download));
                    session.thread.Name = session.ToString();
                    session.thread.Start();
                }
                return(session);
            }
            catch (Exception e)
            {
                if (session != null)
                {
                    DccFileSessionManager.DefaultInstance.RemoveSession(session);
                }
                throw e;
            }
        }
		/// <summary>
		/// Another user has offered to send a file. This method should be called
		/// to accept the offer and save the file to the give location. The parameters
		/// needed to call this method are provided by the <c>OnDccFileTransferRequest()</c>
		/// event.
		/// </summary>
		/// <remarks>
		/// This method should be called from within a try/catch block 
		/// in case it is unable to connect or there are other socket
		/// errors.
		/// </remarks>
		/// <param name="dccUserInfo">Information on the remote user.</param>
		/// <param name="dccFileInfo">The local file that will hold the data being sent. If the file 
		/// is the result of a previous incomplete download the the attempt will be made
		/// to resume where the previous left off.</param>
		/// <param name="turbo">Will the send ahead protocol be used.</param>
		/// <returns>A unique session instance for this file and remote user.</returns>
		/// <exception cref="ArgumentException">If the listen port is already in use.</exception>
		public static DccFileSession Receive(DccUserInfo dccUserInfo, DccFileInfo dccFileInfo, bool turbo ) 
		{
			Debug.WriteLineIf( DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name +"] DccFileSession::Receive()");
			//Test if we are already using this port
			if( DccFileSessionManager.DefaultInstance.ContainsSession( "C" + dccUserInfo.remoteEndPoint.Port ) ) 
			{
				throw new ArgumentException("Already listening on port " + dccUserInfo.remoteEndPoint.Port );
			}
			DccFileSession session = null;
			try 
			{
				session = new DccFileSession( dccUserInfo, dccFileInfo, (64 * 1024 ), 
					dccUserInfo.remoteEndPoint.Port, "C" + dccUserInfo.remoteEndPoint.Port );
				//Has the initiator specified the turbo protocol? 
				session.turboMode = turbo;
				//Open file for writing
				dccFileInfo.OpenForWrite();
				DccFileSessionManager.DefaultInstance.AddSession( session );
				//Determine if we can resume a download
				if( session.dccFileInfo.ShouldResume() ) 
				{
					session.waitingOnAccept = true;
					session.dccFileInfo.SetResumeToFileSize();
					session.SendResume();
				}
				else 
				{
					session.thread = new Thread( new ThreadStart( session.Download ) );
					session.thread.Name = session.ToString();
					session.thread.Start();	
				}
				return session;
			}
			catch( Exception e ) 
			{
				if( session != null ) 
				{
					DccFileSessionManager.DefaultInstance.RemoveSession( session );
				}
				throw e;
			}
		}