Exemple #1
0
        /// <summary>
        /// Gets information about the remote host.
        /// Automatically creates and initializes a new instance of the HostInformation class when
        /// it is necessary.
        /// </summary>
        public HostInformation this[string uri]
        {
            get
            {
                lock (this.SyncRoot)
                {
                    HostInformation hostInformation = this.Get(uri);

                    if (hostInformation == null)
                    {
                        hostInformation = new HostInformation(uri, this.ITransportContext);
                        this.UpdateHost(uri, hostInformation);

                        // set a reasonable start up lifetime property value
                        int expiration = GenuineUtility.ConvertToMilliseconds(this.ITransportContext.IParameterProvider[GenuineParameter.ConnectTimeout]);
                        hostInformation.Renew(expiration, true);

                        // LOG:
                        BinaryLogWriter binaryLogWriter = GenuineLoggingServices.BinaryLogWriter;
                        if (binaryLogWriter != null && binaryLogWriter[LogCategory.HostInformation] > 0)
                        {
                            binaryLogWriter.WriteEvent(LogCategory.HostInformation, "KnownHosts.this[string]",
                                                       LogMessageType.HostInformationCreated, null, null, hostInformation,
                                                       null,
                                                       GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                       null, null, -1, 0, 0, 0, null, null, null, null,
                                                       "The HostInformation has been created for the remote host: {0}.", uri);
                        }
                    }
                    return(hostInformation);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Releases expired streams.
        /// </summary>
        public void TimerCallback()
        {
            int now          = GenuineUtility.TickCount;
            int releaseAfter = GenuineUtility.ConvertToMilliseconds(this.ITransportContext.IParameterProvider[GenuineParameter.UdpAssembleTimeSpan]);

            lock (this._streams.SyncRoot)
            {
                // gather expired stream
                ArrayList itemsDeleted = new ArrayList();
                foreach (DictionaryEntry entry in this._streams)
                {
                    StreamAssembled streamAssembled = (StreamAssembled)entry.Value;
                    if (GenuineUtility.IsTimeoutExpired(streamAssembled.Started + releaseAfter, now) &&
                        !streamAssembled.IsProcessed)
                    {
                        itemsDeleted.Add(entry.Key);
                        streamAssembled.Close();
                    }
                }

                // and remove them
                foreach (object key in itemsDeleted)
                {
                    this._streams.Remove(key);
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Constructs an instance of the UdpConnectionManager class.
 /// </summary>
 /// <param name="iTransportContext">The transport context.</param>
 public UdpConnectionManager(ITransportContext iTransportContext) : base(iTransportContext)
 {
     this.Local       = new HostInformation("_gudp://" + iTransportContext.HostIdentifier, iTransportContext);
     this._sendBuffer = new byte[(int)iTransportContext.IParameterProvider[GenuineParameter.UdpPacketSize]];
     TimerProvider.Attach(this);
     this._closeInvocationConnectionAfterInactivity = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.CloseInvocationConnectionAfterInactivity]);
 }
        /// <summary>
        /// Constructs an instance of the SharedMemoryConnectionManager class.
        /// </summary>
        /// <param name="iTransportContext">The transport context.</param>
        public SharedMemoryConnectionManager(ITransportContext iTransportContext) : base(iTransportContext)
        {
            this._sendTimeoutSpan = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.SMSendTimeout]);

            this.Local = new HostInformation("_gshmem://" + iTransportContext.HostIdentifier, iTransportContext);
            TimerProvider.Attach(this);
        }
        /// <summary>
        /// Closes expired connections and sends ping via inactive connections.
        /// </summary>
        public void TimerCallback()
        {
            int now            = GenuineUtility.TickCount;
            int forcePingAfter = GenuineUtility.ConvertToMilliseconds(this.ITransportContext.IParameterProvider[GenuineParameter.PersistentConnectionSendPingAfterInactivity]);

            lock (this._persistent.SyncRoot)
            {
                // by all connections
                foreach (DictionaryEntry dictionaryEntry in this._persistent)
                {
                    SharedMemoryConnection sharedMemoryConnection = (SharedMemoryConnection)dictionaryEntry.Value;
                    if (GenuineUtility.IsTimeoutExpired(sharedMemoryConnection.LastTimeAMessageWasSent + forcePingAfter, now))
                    {
                        GenuineThreadPool.QueueUserWorkItem(new WaitCallback(this.SendPing), sharedMemoryConnection, false);
                    }
                }
            }
        }
        /// <summary>
        /// Constructs an instance of the SharedMemoryConnection class.
        /// </summary>
        /// <param name="iTransportContext">The transport context.</param>
        /// <param name="name">The name of the shared chunk.</param>
        /// <param name="isServer">The role.</param>
        /// <param name="setCloseStatusOnExit">Indicates whether it is necessary to set the "closed" status on exit.</param>
        internal SharedMemoryConnection(ITransportContext iTransportContext, string name, bool isServer, bool setCloseStatusOnExit)
        {
            this.ITransportContext     = iTransportContext;
            this.ShareName             = "GenuineChannels_GShMem_" + name;
            this.IsServer              = isServer;
            this._setCloseStatusOnExit = setCloseStatusOnExit;

            this._shareSize            = (int)iTransportContext.IParameterProvider[GenuineParameter.SMShareSize];
            this._pingTimeOut          = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.PersistentConnectionSendPingAfterInactivity]);
            this._closeAfterInactivity = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.ClosePersistentConnectionAfterInactivity]);

            string localSideName  = (isServer ? "Server" : "Client");
            string remoteSideName = (isServer ? "Client" : "Server");

            IParameterProvider parameters = this.ITransportContext.IParameterProvider;

            // construct shared object names for the local side
            string readCompletedEventName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName + localSideName + "ReadCompleted", parameters);
            string writeCompletedEventName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName + localSideName + "WriteCompleted", parameters);

            // construct shared object names for the remote side
            string remoteReadCompletedEventName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName + remoteSideName + "ReadCompleted", parameters);
            string remoteWriteCompletedEventName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName + remoteSideName + "WriteCompleted", parameters);

            if (isServer)
            {
                if (this._shareSize < MIN_SHARE_SIZE || this._shareSize > MAX_SHARE_SIZE)
                {
                    throw GenuineExceptions.Get_Channel_InvalidParameter("SMShareSize");
                }

                this.LowLevel_CreateSharedMemory();
                this._closed           = 0;
                this._writtenShareSize = this._shareSize;

                this._receiveOffset    = 5;
                this._sendOffset       = (this._shareSize - 5) / 2;
                this._receiveSpaceSize = this._sendOffset - 5 - 8;
                this._sendSpaceSize    = this._shareSize - this._sendOffset - 8;

                this._namedEventReadCompleted        = NamedEvent.CreateNamedEvent(readCompletedEventName, false, true);
                this._namedEventWriteCompleted       = NamedEvent.CreateNamedEvent(writeCompletedEventName, false, true);
                this._namedEventRemoteReadCompleted  = NamedEvent.CreateNamedEvent(remoteReadCompletedEventName, false, true);
                this._namedEventRemoteWriteCompleted = NamedEvent.CreateNamedEvent(remoteWriteCompletedEventName, false, true);
            }
            else
            {
                this.OpenSharedMemory();

                if (this._closed != 0)
                {
                    throw GenuineExceptions.Get_Connect_CanNotConnectToRemoteHost(name, "Remote host has already closed the connection.");
                }

                this._shareSize = this._writtenShareSize;
                if (this._shareSize < MIN_SHARE_SIZE || this._shareSize > MAX_SHARE_SIZE)
                {
                    throw GenuineExceptions.Get_Channel_InvalidParameter("SMShareSize");
                }

                this._receiveOffset    = (this._shareSize - 5) / 2;
                this._sendOffset       = 5;
                this._receiveSpaceSize = this._shareSize - this._receiveOffset - 8;
                this._sendSpaceSize    = this._receiveOffset - 5 - 8;

                this._namedEventReadCompleted        = NamedEvent.OpenNamedEvent(readCompletedEventName);
                this._namedEventWriteCompleted       = NamedEvent.OpenNamedEvent(writeCompletedEventName);
                this._namedEventRemoteReadCompleted  = NamedEvent.OpenNamedEvent(remoteReadCompletedEventName);
                this._namedEventRemoteWriteCompleted = NamedEvent.OpenNamedEvent(remoteWriteCompletedEventName);
            }

            this._sendBuffer = new byte[this._sendSpaceSize];
        }
        /// <summary>
        /// Constructs an instance of the HttpInvocationConnection class.
        /// </summary>
        /// <param name="iTransportContext">The transport context.</param>
        /// <param name="remote">The remote host.</param>
        public HttpInvocationConnection(ITransportContext iTransportContext, HostInformation remote)
        {
            this.HostId         = iTransportContext.BinaryHostIdentifier;
            this.HostIdAsString = iTransportContext.HostIdentifier;
            this._asyncCallback_onRequestCompleted = new AsyncCallback(this.OnRequestCompleted);

            this.ITransportContext = iTransportContext;
            this.Remote            = remote;

//			this.OnEndSending = new AsyncCallback(this.Callback_OnEndSending);
//			this.OnEndReceiving = new AsyncCallback(this.Callback_OnEndReceiving);

            // cache all setting's values
            this._userAgent = this.ITransportContext.IParameterProvider[GenuineParameter.HttpWebUserAgent] as string;
            if (this._userAgent == null)
            {
                this._userAgent = @"Mozilla/4.0+ (compatible; MSIE 6.0; Windows " + Environment.OSVersion.Version +
                                  "; Genuine HTTP Client Channel; MS .NET CLR " + Environment.Version.ToString() + ")";
            }
            this._userAgent = Regex.Replace(this._userAgent, "\r|\n", " ", RegexOptions.None);

            this._useUnsafeConnectionSharing = (bool)this.ITransportContext.IParameterProvider[GenuineParameter.HttpUnsafeConnectionSharing];
            this._allowWriteStreamBuffering  = (bool)this.ITransportContext.IParameterProvider[GenuineParameter.HttpAllowWriteStreamBuffering];

            this._keepalive   = (bool)iTransportContext.IParameterProvider[GenuineParameter.HttpKeepAlive];
            this._credentials = this.ITransportContext.IParameterProvider[GenuineParameter.HttpAuthCredential] as ICredentials;
            string userName = this.ITransportContext.IParameterProvider[GenuineParameter.HttpAuthUserName] as string;
            string password = this.ITransportContext.IParameterProvider[GenuineParameter.HttpAuthPassword] as string;
            string domain   = this.ITransportContext.IParameterProvider[GenuineParameter.HttpAuthDomain] as string;

            this._hostRenewingSpan = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.ClosePersistentConnectionAfterInactivity]) + GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.MaxTimeSpanToReconnect]);

            if (this._credentials != null || (userName != null && userName != string.Empty &&
                                              password != null && password != string.Empty))
            {
                this._useWebAuthentication = true;

                // and setup credentials
                if (this._credentials == null)
                {
                    if ((bool)this.ITransportContext.IParameterProvider[GenuineParameter.HttpUseDefaultCredentials])
                    {
                        this._credentials = CredentialCache.DefaultCredentials;
                    }
                    else
                    {
                        if (domain == null)
                        {
                            this._credentials = new NetworkCredential(userName, password);
                        }
                        else
                        {
                            this._credentials = new NetworkCredential(userName, password, domain);
                        }
                    }
                }
            }

            if ((bool)this.ITransportContext.IParameterProvider[GenuineParameter.HttpUseGlobalProxy])
            {
#if (FRM20)
                this._iWebProxy = WebRequest.DefaultWebProxy;
#else
                this._iWebProxy = GlobalProxySelection.Select;
#endif
            }
            else
            {
                if (!(this.ITransportContext.IParameterProvider[GenuineParameter.HttpProxyUri] is string))
                {
#if (FRM20)
                    this._iWebProxy = null;
#else
                    this._iWebProxy = GlobalProxySelection.GetEmptyWebProxy();
#endif
                }
                else
                {
                    this._iWebProxy = new WebProxy((string)this.ITransportContext.IParameterProvider[GenuineParameter.HttpProxyUri], (bool)this.ITransportContext.IParameterProvider[GenuineParameter.HttpBypassOnLocal]);
                }
            }

            this._httpAsynchronousRequestTimeout = GenuineUtility.ConvertToMilliseconds(iTransportContext.IParameterProvider[GenuineParameter.HttpAsynchronousRequestTimeout]);
        }