Click here to Skip to main content
15,885,078 members
Articles / Programming Languages / C#

Inter-Process Communication in .NET Using Named Pipes, Part 2

Rate me:
Please Sign up or sign in to vote.
4.77/5 (41 votes)
14 Jun 20044 min read 273.3K   4.2K   132  
This article explores a way of implementing Named Pipes based Inter-Process Communication between .NET applications
using System;
using System.Collections;
using System.Threading;
using System.Web;
using System.IO;
using System.Configuration;
using System.Diagnostics;

using AppModule.InterProcessComm;
using AppModule.NamedPipes;

namespace Server {

	public sealed class PipeManager : IChannelManager {

		public Hashtable Pipes;

		private uint NumberPipes = 5;
		private uint OutBuffer = 512;
		private uint InBuffer = 512;
		private const int MAX_READ_BYTES = 5000;
		private bool _listen = true;
		public bool Listen {
			get {
				return _listen;
			}
			set {
				_listen=value;
			}
		}
		private int numChannels = 0;
		private Hashtable _pipes = new Hashtable();
		private Thread MainThread;
		private string PipeName = "MyPipe";
		private ManualResetEvent Mre;
		private const int PIPE_MAX_STUFFED_TIME = 5000;
		
		public object SyncRoot = new object();

		public void Initialize() {
			Pipes = Hashtable.Synchronized(_pipes);
			Mre = new ManualResetEvent(false);
			MainThread = new Thread(new ThreadStart(Start));
			MainThread.IsBackground = true;
			MainThread.Name = "Main Pipe Thread";
			MainThread.Start();
			Thread.Sleep(1000);
		}
		public string HandleRequest(string request) {
			string returnVal;

			Form1.ActivityRef.AppendText(request + Environment.NewLine);
			returnVal = "Response to: " + request;

			return returnVal;
		}
		private void Start() {
			try {
				while (_listen) {
					int[] keys = new int[Pipes.Keys.Count];
					Pipes.Keys.CopyTo(keys,0);
					foreach (int key in keys) {
						ServerNamedPipe serverPipe = (ServerNamedPipe)Pipes[key];
						if (serverPipe != null && DateTime.Now.Subtract(serverPipe.LastAction).Milliseconds > PIPE_MAX_STUFFED_TIME && serverPipe.PipeConnection.GetState() != InterProcessConnectionState.WaitingForClient) {
							serverPipe.Listen = false;
							serverPipe.PipeThread.Abort();
							RemoveServerChannel(serverPipe.PipeConnection.NativeHandle);
						}
					}
					if (numChannels <= NumberPipes) {
						ServerNamedPipe pipe = new ServerNamedPipe(PipeName, OutBuffer, InBuffer, MAX_READ_BYTES, false);
						try {
							pipe.Connect();
							pipe.LastAction = DateTime.Now;
							System.Threading.Interlocked.Increment(ref numChannels);
							pipe.Start();
							Pipes.Add(pipe.PipeConnection.NativeHandle, pipe);
						}
						catch (InterProcessIOException ex) {
							RemoveServerChannel(pipe.PipeConnection.NativeHandle);
							pipe.Dispose();
						}
					}
					else {
						Mre.Reset();
						Mre.WaitOne(1000, false);
					}
				}
			}
			catch {
				// Log exception
			}
		}
		public void Stop() {
			_listen = false;
			Mre.Set();
			try {
				int[] keys = new int[Pipes.Keys.Count];
				Pipes.Keys.CopyTo(keys,0);
				foreach (int key in keys) {
					((ServerNamedPipe)Pipes[key]).Listen = false;
				}
				int i = numChannels * 3;
				for (int j = 0; j < i; j++) {
					StopServerPipe();
				}
				Pipes.Clear();
				Mre.Close();
				Mre = null;
			} 
			catch {	
				// Log exception
			}
		}

		public void WakeUp() {
			if (Mre != null) {
				Mre.Set();
			}
		}
		private void StopServerPipe() {
			try {
				ClientPipeConnection pipe = new ClientPipeConnection(PipeName);
				if (pipe.TryConnect()) {
					pipe.Close();
				}
			} catch {	
				// Log exception
			}
		}

		public void RemoveServerChannel(object param) {
			int handle = (int)param;
			System.Threading.Interlocked.Decrement(ref numChannels);
			Pipes.Remove(handle);
			this.WakeUp();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Ivan Latunov is a Software Architect with long term commercial experience in the areas of software architecture and engineering, design and development of web and distributed applications and business and technical analysis. Technical blog. Website: ivanweb.com.

Comments and Discussions