Exemple #1
0
        protected virtual void Connect(string host, int port, UpgradeConnectionStreamCallback createAuthenticatedStreamCallback)
        {
            if (host == null)
            throw new ArgumentNullException("host");
              if (port < IPEndPoint.MinPort || IPEndPoint.MaxPort < port)
            throw new ArgumentOutOfRangeException("port", port, "out of range");

              if (client != null)
            throw new InvalidOperationException("already connected");

              TcpClient c = null;

              try {

            c = new TcpClient();

            c.ReceiveTimeout = DefaultTimeoutMilliseconds;
            c.SendTimeout    = DefaultTimeoutMilliseconds;

            ConnectionTrace.Log(this, "connecting to {0}:{1}", host, port);

            c.Connect(host, port);

            this.host = host;
            this.client = c;
            this.remoteEndPoint = c.Client.RemoteEndPoint as IPEndPoint;
            this.localEndPoint  = c.Client.LocalEndPoint as IPEndPoint;
            this.stream = CreateBufferedStream(ConnectionTrace.CreateTracingStream(this, client.GetStream()));

            ConnectionTrace.Log(this, "connected: {0} ({1})", remoteEndPoint, host);
              }
              catch (Exception ex) {
            ConnectionTrace.Log(this, ex);

            if (c != null)
              c.Close();

            this.client = null;

            throw new ConnectionException("connect failed", ex);
              }

              try {
            if (createAuthenticatedStreamCallback != null) {
              UpgradeStream(createAuthenticatedStreamCallback);

              isSecurePortConnection = true;
            }
              }
              catch {
            this.client.Close();
            this.client = null;

            throw;
              }
        }
Exemple #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing) {
            try {
              if (client != null) {
            client.Close();

            ConnectionTrace.Log(this, "disconnected: {0} ({1})", remoteEndPoint, host);
              }
            }
            finally {
              client = null;
              stream = null;
            }
              }
        }
Exemple #3
0
        public virtual void UpgradeStream(UpgradeConnectionStreamCallback upgradeStreamCallback)
        {
            if (upgradeStreamCallback == null)
            throw new ArgumentNullException("upgradeStreamCallback");

              try {
            var upgradedStream = upgradeStreamCallback(this, RawStream);
            var sslStream = upgradedStream as SslStream;

            if (sslStream != null)
              ConnectionTrace.Log(this, "TLS started: {0}, {1}, {2}, {3}{4}",
                              sslStream.SslProtocol,
                              sslStream.CipherAlgorithm,
                              sslStream.HashAlgorithm,
                              sslStream.KeyExchangeAlgorithm,
                              sslStream.IsMutuallyAuthenticated ? ", mutually authenticated" : string.Empty);

            stream = CreateBufferedStream(ConnectionTrace.CreateTracingStream(this, upgradedStream));
              }
              catch (AuthenticationException ex) {
            ConnectionTrace.Log(this, ex);

            throw new ConnectionException(string.Format("upgrading stream failed (callback: {0})", upgradeStreamCallback.Method), ex);
              }
              catch (IOException ex) {
            ConnectionTrace.Log(this, ex);

            throw new ConnectionException(string.Format("upgrading stream failed (callback: {0})", upgradeStreamCallback.Method), ex);
              }
        }