Beispiel #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="serverInboxName">The name of the server shared memory inbox.</param>
        /// <param name="requestCapacity">The maximum byte size of a serialized request (defaults to 1MB).</param>
        /// <param name="responseCapacity">The maximum byte size of a serialized response (defaults to 1MB).</param>
        /// <remarks>
        /// <note>
        /// The <paramref name="requestCapacity"/> and <paramref name="responseCapacity"/> parameters must exactly
        /// match the values configured for the server.
        /// </note>
        /// </remarks>
        public SharedMemClient(string serverInboxName, int requestCapacity = 1024 *1024, int responseCapacity = 1024 *1024)
        {
            if (string.IsNullOrEmpty(serverInboxName))
            {
                throw new ArgumentNullException("name");
            }

            if (requestCapacity <= 0)
            {
                throw new ArgumentException("requestCapacity");
            }

            if (responseCapacity <= 0)
            {
                throw new ArgumentException("responseCapacity");
            }

            this.ServerName       = serverInboxName;
            this.ClientName       = serverInboxName + ":" + Guid.NewGuid().ToString("D");
            this.RequestCapacity  = requestCapacity;
            this.ResponseCapacity = responseCapacity;
            this.outbox           = new SharedMemOutbox(requestCapacity, TimeSpan.FromSeconds(5)); // $todo(jeffli): Make [maxWait] configurable?
            this.inbox            = new SharedMemInbox();
            this.timeoutCts       = new CancellationTokenSource();
            this.timeoutTask      = Task.Run(async() => await TimeoutTask());
            this.Timeout          = TimeSpan.FromSeconds(5);

            this.stopwatch.Start();
            this.inbox.Open(this.ClientName, responseCapacity, new SharedMemInboxReceiveDelegate(OnResponse));
        }
Beispiel #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="serverInboxName">The name of the server shared memory inbox.</param>
        /// <param name="requestHandler">The server's request handler.</param>
        /// <param name="requestCapacity">The maximum byte size of a serialized request (defaults to 1MB).</param>
        /// <param name="responseCapacity">The maximum byte size of a serialized response defaults to 1MB).</param>
        /// <remarks>
        /// <note>
        /// The <paramref name="requestCapacity"/> and <paramref name="responseCapacity"/> parameters must exactly
        /// match the values configured for the server.
        /// </note>
        /// </remarks>
        public SharedMemServer(string serverInboxName, Func <SharedMemMessage, SharedMemMessage> requestHandler, int requestCapacity = 1024 *1024, int responseCapacity = 1024 *1024)
        {
            if (string.IsNullOrEmpty(serverInboxName))
            {
                throw new ArgumentNullException("name");
            }

            if (requestHandler == null)
            {
                throw new ArgumentNullException("requestHandler");
            }

            if (requestCapacity <= 0)
            {
                throw new ArgumentException("requestCapacity");
            }

            if (responseCapacity <= 0)
            {
                throw new ArgumentException("responseCapacity");
            }

            this.ServerInboxName = serverInboxName;
            this.RequestCapacity = requestCapacity;
            this.requestHandler  = requestHandler;
            this.outbox          = new SharedMemOutbox(responseCapacity, TimeSpan.FromSeconds(5)); // $todo(jeffli): Make [maxWait] configurable?
            this.inbox           = new SharedMemInbox();

            this.inbox.Open(this.ServerInboxName, requestCapacity, new SharedMemInboxReceiveDelegate(OnRequest));
        }
Beispiel #3
0
        /// <summary>
        /// Releases any important resources assocated with the instance.
        /// </summary>
        /// <param name="disposing"><c>true</c> if the instance is being disposed as opposed to being finalized.</param>
        protected void Dispose(bool disposing)
        {
            lock (syncLock)
            {
                if (outbox != null)
                {
                    outbox.Close();
                    outbox = null;
                }

                if (inbox != null)
                {
                    inbox.Close();
                    inbox = null;
                }

                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Releases any important resources assocated with the instance.
        /// </summary>
        /// <param name="disposing"><c>true</c> if the instance is being disposed as opposed to being finalized.</param>
        protected void Dispose(bool disposing)
        {
            lock (syncLock)
            {
                if (outbox != null)
                {
                    outbox.Close();
                    outbox = null;
                }

                if (inbox != null)
                {
                    inbox.Close();
                    inbox = null;
                }

                if (timeoutTask != null)
                {
                    timeoutCts.Cancel();

                    try
                    {
                        timeoutTask.Wait(TimeSpan.FromSeconds(1));
                    }
                    catch (TimeoutException)
                    {
                        // Ignore these.
                    }

                    timeoutTask = null;
                }

                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }