Hi All,
I am trying to create a named pipe for two of my process to
communicate. I wrote bunch of unit test to make sure the
funcationality works the way it is supposed to be. In this app I am
trying to broadcast message from server to client (only one client
will be available at any time) and if the client is registered it see
the message. If the client is not registered they need to be bothered.
Before I ask my questions here is the code snippets
Create Server:
myNPipeServer = new Server();
myNPipeServer.PipeName = ReferenceNames.PipeName;
if (!myNPipeServer.Running)
myNPipeServer.Start();
Above code basically starts a seperate thread to do the listening of
client registration request.
Create Client:
myNPipeClient = new Client();
myNPipeClient.PipeName = ReferenceNames.PipeName;
myNPipeClient.Connect();
Above code simply sits and listen to any message coming from server.
I have around 12 test cases (NUnit) I trying to run and I am running
into a problem of inconsistant behaviour. Sometimes server picks up
the client registration and sometime server never sees client
registration. Sometimes all 12 cases runs fine, sometime couple and
some times none completes. This is very limited information but if you
need any additional information, please ask me and I will add them to
the thread. Here is one sample test case that exhibit the protean
behaviour.
CreateServerWithCommonName();
CreateClientWithCommonName();
Assert.IsNotNull(myNPipeServer);
Assert.IsNotNull(myNPipeClient);
bool register = ServerListenedForClient(listenCount);
if (register)
Assert.IsNotNull(myNamedPipeServer.CurrentClient.m yHandle);
DisconnectClient();
DisconnectServer();
if (!register) Assert.IsFalse(true);
Before I register I get a counter value from server, when client
registration runs the same counter is increased by one value. The
ServerListenedForClient method basically verifies the count changed
for a minute with 5 second increment to make sure the test case to
give server enough time to register the client. Sometimes the count
never changes thats how I know the client registration never took
place. If you are interested in the server side listen code here it
is,
Server Listening thread:
while (true)
{
myClientHandle = CreateNamedPipe(
this.myPipeName,
DUPLEX | FILE_FLAG_OVERLAPPED,
0,
255,
BUFFER_SIZE,
BUFFER_SIZE,
0,
IntPtr.Zero);
//could not create named pipe
if (myClientHandle.IsInvalid)
{
myConnectionStatus =
ReferenceNames.ConnectionStatus.ConnectionFailed;
myListenLoopCount = -1;
return;
}
int success = ConnectNamedPipe(myClientHandle,
IntPtr.Zero);
//could not connect client
if (success == 0)
{
myConnectionStatus =
ReferenceNames.ConnectionStatus.ConnectionFailed;
myListenLoopCount = -2;
return;
}
lock (myClient)
{
if (myClient == null)
myClient = new Client();
myClient.myHandle = myClientHandle;
myClient.myStream = new FileStream(myClientHandle,
FileAccess.ReadWrite, BUFFER_SIZE, true);
}
myListenLoopCount = (myListenLoopCount 10000) ? 0 :
myListenLoopCount+1;
}