예제 #1
0
        /// <summary>
        /// Connects to a peer in the peer-to-peer network. If the remote end point resides behind the same firewall as the current application,
        /// a direct connection to the local peer is made, for improved performance.
        /// </summary>
        /// <param name="RemoteEndPoint">Remote End-point.</param>
        /// <returns>Peer connection</returns>
        public PeerConnection ConnectToPeer(IPEndPoint RemoteEndPoint)
        {
            if (this.state != PeerToPeerNetworkState.Ready)
            {
                throw new IOException("Peer-to-peer network not ready.");
            }

            TcpClient  Client = new TcpClient(new IPEndPoint(this.localAddress, 0));
            IPEndPoint RemoteEndPoint2;

            try
            {
                if (IPAddress.Equals(RemoteEndPoint.Address, this.externalAddress))
                {
                    this.serviceWANIPConnectionV1.GetSpecificPortMappingEntry(string.Empty, (ushort)RemoteEndPoint.Port, "TCP",
                                                                              out ushort InternalPort, out string InternalClient, out bool Enabled, out string PortMappingDescription,
                                                                              out uint LeaseDuration);

                    RemoteEndPoint2 = new IPEndPoint(IPAddress.Parse(InternalClient), InternalPort);
                    Client.Connect(RemoteEndPoint2);
                }
                else
                {
                    RemoteEndPoint2 = RemoteEndPoint;
                    Client.Connect(RemoteEndPoint);
                }
            }
            catch (Exception)
            {
                Client.Close();
                throw;
            }

            PeerConnection Result = new PeerConnection(Client, this, RemoteEndPoint2);

            Result.StartIdleTimer();

            return(Result);
        }
예제 #2
0
		/// <summary>
		/// Connects to a peer in the peer-to-peer network. If the remote end point resides behind the same firewall as the current application,
		/// a direct connection to the local peer is made, for improved performance.
		/// </summary>
		/// <param name="RemoteEndPoint">Remote End-point.</param>
		/// <returns>Peer connection</returns>
		public PeerConnection ConnectToPeer(IPEndPoint RemoteEndPoint)
		{
			if (this.state != PeerToPeerNetworkState.Ready)
				throw new IOException("Peer-to-peer network not ready.");

			TcpClient Client = new TcpClient(new IPEndPoint(this.localAddress, 0));
			IPEndPoint RemoteEndPoint2;

			try
			{
				if (IPAddress.Equals(RemoteEndPoint.Address, this.externalAddress))
				{
					ushort InternalPort;
					string InternalClient;
					string PortMappingDescription;
					uint LeaseDuration;
					bool Enabled;

					this.serviceWANIPConnectionV1.GetSpecificPortMappingEntry(string.Empty, (ushort)RemoteEndPoint.Port, "TCP",
						out InternalPort, out InternalClient, out Enabled, out PortMappingDescription, out LeaseDuration);

					RemoteEndPoint2 = new IPEndPoint(IPAddress.Parse(InternalClient), InternalPort);
					Client.Connect(RemoteEndPoint2);
				}
				else
				{
					RemoteEndPoint2 = RemoteEndPoint;
					Client.Connect(RemoteEndPoint);
				}
			}
			catch (Exception)
			{
				Client.Close();
				throw;
			}

			PeerConnection Result = new PeerConnection(Client, this, RemoteEndPoint2);

			Result.StartIdleTimer();

			return Result;
		}