예제 #1
0
        internal void ReceiveMail(Mail mail)
        {
            switch (mail.Type)
            {
            case Mail.RESPONSE_TYPE:
                WaitCallback callback = null;
                object       context;
                waitingCallbackLock.EnterUpgradeableReadLock();
                try
                {
                    waitingCallback.TryGetValue(mail.Id, out callback);
                    waitingContext.TryGetValue(mail.Id, out context);
                    if (callback != null)
                    {
                        waitingCallbackLock.EnterWriteLock();
                        try
                        {
                            waitingCallback.Remove(mail.Id);
                            waitingContext.Remove(mail.Id);
                        }
                        finally
                        {
                            waitingCallbackLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    waitingCallbackLock.ExitUpgradeableReadLock();
                }
                if (callback != null)
                {
                    ThreadPool.QueueUserWorkItem(callback, new ResponseContext(context, (Response)mail));
                }
                else
                {
                    waitHandlesLock.EnterReadLock();
                    try
                    {
                        SynchResult result;
                        waitHandles.TryGetValue(mail.Id, out result);
                        if (result != null)
                        {
                            result.Response = (Response)mail;
                            result.WaitEvent.Set();
                        }
                    }
                    finally
                    {
                        waitHandlesLock.ExitReadLock();
                    }
                }
                break;

            case Mail.REQUEST_TYPE:
                ThreadPool.QueueUserWorkItem(ProcessRequest, mail);
                break;
            }
        }
예제 #2
0
		public void Send(Mail mail)
		{
			byte[] data;
			data = formatter.Serialize(mail);
			using (var sendStream = new MemoryStream())
			{
				WriteInt(data.Length, sendStream);
				sendStream.Write(data, 0, data.Length);
				var datagram = sendStream.ToArray();
				Write(datagram);
			}
		}