internal short SetWaitHandle(int waitTimeout)
        {
            //this uses GetNextMessageId to generate new ids, but also reuses ids as much as possible. This VASTLY reduces the number of replybuckets needed.
            ResourcePoolItem <short> idItem = idPool.GetItem();
            short       waitId = idItem.Item;
            ReplyBucket bucket = null;

            if (!replies.ContainsKey(waitId)) //we haven't used this waitId yet
            {
                replyLock.Write(() =>
                {
                    if (!replies.ContainsKey(waitId))
                    {
                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat("SetWaitHandle() Creates new ReplyBucket.");
                        }
                        replies.Add(waitId, new ReplyBucket());
                    }
                });
            }

            replyLock.Read(() => { bucket = replies[waitId]; });

            bucket.SetValues(waitTimeout, ReplyEvent, idItem);

            return(waitId);
        }
Example #2
0
 public ConnectionState this[IPEndPoint endPoint]
 {
     get
     {
         ConnectionState state = null;
         connectionsLock.Read(() =>
                              connections.TryGetValue(endPoint, out state)
                              );
         return(state);
     }
 }
        private KeyspaceInfo GetKeySpaceInfo(DataBuffer keySpace)
        {
            KeyspaceInfo ret = null;

            _keyspaceInfoLock.Read(() =>
                                   _keyspaceInfos.TryGetValue(keySpace, out ret));
            return(ret);
        }
		/// <summary>
		/// Returns a typed version of the Serializer.Serialize method.
		/// </summary>
		/// <param name="t">The <see cref="Type"/> to serialize or deserialize.</param>
		/// <param name="methodType">
		///	<para>The type of method to get; <see cref="TypeMethodType.Serialize"/>,
		///	<see cref="TypeMethodType.Deserialize"/>, or <see cref="TypeMethodType.Compare"/>.</para>
		/// </param>
		/// <returns>
		///	<para>A typed version of the <see cref="Serializer.Serialize{T}(Stream, T)"/> method.</para>
		/// </returns>
		internal static MethodInfo GetTypeMethod(Type t, TypeMethodType methodType)
		{
			MethodInfo method = null;
			Dictionary<Type, MethodInfo> methodTypeMethods = null;

			Debug.WriteLine(string.Format("Getting type method {0} for {1}", methodType.ToString(), t.FullName));

			typeMethodsLock.Read(() =>
				{
					if (typeMethods.TryGetValue(methodType, out methodTypeMethods))
					{
						methodTypeMethods.TryGetValue(t, out method);
					}
				});

			if (method == null)
			{
				Type[] typeArgs = { t };

				//  For simple types, find the corresponding IPrimitiveReader/IPrimitveWriter method
				//  For complex types, get a typed version of the appropriate Serializer method
				//  For enumerations, find the corresponding method for the base type
				if (t.IsEnum)
				{
					method = GetTypeMethod(Enum.GetUnderlyingType(t), methodType);
				}
				else if (t.IsValueType || (t == typeof(string)))
				{
					switch (methodType)
					{
						case TypeMethodType.Serialize:
							method = typeof(IPrimitiveWriter).GetMethod("Write", typeArgs);
							if ((method == null) && (t.IsPrimitive == false))
							{
								method = GenerateWriteStructMethod(t);
							}
							break;

						case TypeMethodType.Deserialize:
							if (t.Name.StartsWith("Nullable"))
							{
								Type[] targs = t.GetGenericArguments();
								method = typeof(IPrimitiveReader).GetMethod(
												string.Format("ReadNullable{0}", targs[0].Name),
												Type.EmptyTypes
												);
							}
							else
							{
								method = typeof(IPrimitiveReader).GetMethod(
												string.Format("Read{0}", t.Name),
												Type.EmptyTypes
												);
							}

							if ((method == null) && (t.IsPrimitive == false))
							{
								method = GenerateReadStructMethod(t);
							}
							break;

						case TypeMethodType.Compare:
							{
								for (
									 Type currentType = t;
									 (currentType != null) && (method == null);
									 currentType = currentType.BaseType
									 )
								{
									method = t.GetMethod(
													"Equals",
													BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly,
													null,
													new [] { currentType },
													null
													);
								}

								if (method == null)
								{
									Debug.WriteLine(string.Format("WARNING: The type {0} does contain a supported Equals method", t.FullName));
								}
							}
							break;
					}

				}
				else // complex type
				{
					//  Get a generic version of a method that can
					//  perform the requested operation and then make
					//  a type-specific version of it
					method = genericMethods[methodType];
					method = method.MakeGenericMethod(typeArgs);
				}

				if ((method == null) && (methodType != TypeMethodType.Compare))
				{
					string msg = string.Format("Failed to resolve handler method for type {0}", t.FullName);
					Debug.Fail(msg);
					throw new NotImplementedException(msg);
				}

				//  Update the method cache
				typeMethodsLock.Write(() => typeMethods[methodType][t] = method);

			} // if (method not created yet)

			return method;
		}
Example #5
0
        internal static void ReloadConfig(string configFilePath)
        {
            if (string.IsNullOrEmpty(configFilePath))
            {
                log.Error("Attempted to reload null or empty config file path.");
                return;
            }

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(configFilePath);

                if (doc.DocumentElement == null)
                {
                    log.ErrorFormat("Got a null document element when reloading config file with path {0}", configFilePath);
                    return;
                }

                //refresh the section in case anyone else uses it
                ConfigurationManager.RefreshSection(doc.DocumentElement.Name);

                object newSettings = GetConfigInstance(doc.DocumentElement);
                string fileName    = Path.GetFileName(configFilePath);
                if (fileName == null)
                {
                    log.ErrorFormat("Got null file name for config file path {0}", configFilePath);
                    return;
                }
                object configInstance = configInstances[fileName];

                if (newSettings.GetType() != configInstance.GetType())
                {
                    return;
                }
                Type           newSettingsType = newSettings.GetType();
                PropertyInfo[] props           = newSettingsType.GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    if (prop.CanRead && prop.CanWrite)
                    {
                        prop.SetValue(configInstance, prop.GetValue(newSettings, null), null);
                    }
                }

                reloadDelegatesLock.Read(() =>
                {
                    List <EventHandler> delegateMethods;
                    if (reloadDelegates.TryGetValue(newSettingsType, out delegateMethods) &&
                        delegateMethods != null)
                    {
                        foreach (EventHandler delegateMethod in delegateMethods)
                        {
                            delegateMethod(newSettings, EventArgs.Empty);
                        }
                    }
                });
            }
            catch (Exception e)
            {
                log.ErrorFormat("Exception reloading config with path {0}: {1}", configFilePath, e);
            }
        }