Exemple #1
0
        /// <summary>
        /// Attempt to send a file to a remote user. Start listening
        /// on the given port and address. If the remote user does not accept
        /// the offer within the timeout period the the session
        /// will be closed.
        /// </summary>
        /// <remarks>
        /// This method should be called from within a try/catch block
        /// in case there are socket errors. This methods will also automatically
        /// handle a Resume if the remote client requests it.
        /// </remarks>
        /// <param name="dccUserInfo">The information about the remote user.</param>
        /// <param name="listenIPAddress">The IP address of the local machine in dot
        /// quad format (e.g. 192.168.0.25). This is the address that will be sent to the
        /// remote user. The IP address of the NAT machine must be used if the
        /// client is behind a NAT/Firewall system. </param>
        /// <param name="listenPort">The port that the session will listen on.</param>
        /// <param name="dccFileInfo">The file to be sent. If the file name has spaces in it
        /// they will be replaced with underscores when the name is sent.</param>
        /// <param name="bufferSize">The size of the send buffer. Generally should
        /// be between 4k and 32k.</param>
        /// <param name="turbo">True to use send-ahead mode for transfers.</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 Send(
            DccUserInfo dccUserInfo,
            string listenIPAddress,
            int listenPort,
            DccFileInfo dccFileInfo,
            int bufferSize,
            bool turbo)
        {
            Debug.WriteLineIf(DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] DccFileSession::Send()");
            DccFileSession session = null;

            //Test if we are already using this port
            if (DccFileSessionManager.DefaultInstance.ContainsSession("S" + listenPort))
            {
                throw new ArgumentException("Already listening on port " + listenPort);
            }
            try
            {
                session = new DccFileSession(dccUserInfo, dccFileInfo, bufferSize, listenPort, "S" + listenPort);
                //set turbo mode
                session.turboMode = turbo;
                //Set server IP address
                session.listenIPAddress = listenIPAddress;
                //Add session to active sessions hashtable
                DccFileSessionManager.DefaultInstance.AddSession(session);
                //Create stream to file
                dccFileInfo.OpenForRead();
                //Start session Thread
                session.thread      = new Thread(new ThreadStart(session.Listen));
                session.thread.Name = session.ToString();
                session.thread.Start();
                //Send DCC Send request to remote user
                session.DccSend(IPAddress.Parse(listenIPAddress));
                return(session);
            }
            catch (Exception e)
            {
                if (session != null)
                {
                    DccFileSessionManager.DefaultInstance.RemoveSession(session);
                }
                throw e;
            }
        }
		/// <summary>
		/// Attempt to send a file to a remote user. Start listening
		/// on the given port and address. If the remote user does not accept
		/// the offer within the timeout period the the session
		/// will be closed.
		/// </summary>
		/// <remarks>
		/// This method should be called from within a try/catch block 
		/// in case there are socket errors. This methods will also automatically 
		/// handle a Resume if the remote client requests it.
		/// </remarks>
		/// <param name="dccUserInfo">The information about the remote user.</param>
		/// <param name="listenIPAddress">The IP address of the local machine in dot 
		/// quad format (e.g. 192.168.0.25). This is the address that will be sent to the 
		/// remote user. The IP address of the NAT machine must be used if the
		/// client is behind a NAT/Firewall system. </param>
		/// <param name="listenPort">The port that the session will listen on.</param>
		/// <param name="dccFileInfo">The file to be sent. If the file name has spaces in it
		/// they will be replaced with underscores when the name is sent.</param>
		/// <param name="bufferSize">The size of the send buffer. Generally should
		/// be between 4k and 32k.</param>
		/// <param name="turbo">True to use send-ahead mode for transfers.</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 Send( 
			DccUserInfo dccUserInfo, 
			string listenIPAddress, 
			int listenPort, 
			DccFileInfo dccFileInfo, 
			int bufferSize,
			bool turbo)
		{
			Debug.WriteLineIf( DccUtil.DccTrace.TraceInfo, "[" + Thread.CurrentThread.Name +"] DccFileSession::Send()");
			DccFileSession session = null;
			//Test if we are already using this port
			if( DccFileSessionManager.DefaultInstance.ContainsSession( "S" + listenPort ) ) 
			{
				throw new ArgumentException("Already listening on port " + listenPort );
			}
			try
			{
				session = new DccFileSession( dccUserInfo, dccFileInfo , bufferSize, listenPort, "S" + listenPort );
				//set turbo mode
				session.turboMode = turbo;
				//Set server IP address
				session.listenIPAddress = listenIPAddress; 
				//Add session to active sessions hashtable
				DccFileSessionManager.DefaultInstance.AddSession( session );
				//Create stream to file
				dccFileInfo.OpenForRead();
				//Start session Thread
				session.thread = new Thread(new ThreadStart( session.Listen ) );
				session.thread.Name = session.ToString();
				session.thread.Start();	
				//Send DCC Send request to remote user
				session.DccSend( IPAddress.Parse( listenIPAddress) );
				return session;
			}
			catch( Exception e ) 
			{
				if( session != null ) 
				{
					DccFileSessionManager.DefaultInstance.RemoveSession( session );
				}
				throw e;
			}
		}