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.P ipeName = ReferenceNames. PipeName;
if (!myNPipeServer .Running)
myNPipeServer.S tart();
Above code basically starts a seperate thread to do the listening of
client registration request.
Create Client:
myNPipeClient = new Client();
myNPipeClient.P ipeName = ReferenceNames. PipeName;
myNPipeClient.C onnect();
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.
CreateServerWit hCommonName();
CreateClientWit hCommonName();
Assert.IsNotNul l(myNPipeServer );
Assert.IsNotNul l(myNPipeClient );
bool register = ServerListenedF orClient(listen Count);
if (register)
Assert.IsNotNul l(myNamedPipeSe rver.CurrentCli ent.myHandle);
DisconnectClien t();
DisconnectServe r();
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
ServerListenedF orClient 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_OVERL APPED,
0,
255,
BUFFER_SIZE,
BUFFER_SIZE,
0,
IntPtr.Zero);
//could not create named pipe
if (myClientHandle .IsInvalid)
{
myConnectionSta tus =
ReferenceNames. ConnectionStatu s.ConnectionFai led;
myListenLoopCou nt = -1;
return;
}
int success = ConnectNamedPip e(myClientHandl e,
IntPtr.Zero);
//could not connect client
if (success == 0)
{
myConnectionSta tus =
ReferenceNames. ConnectionStatu s.ConnectionFai led;
myListenLoopCou nt = -2;
return;
}
lock (myClient)
{
if (myClient == null)
myClient = new Client();
myClient.myHand le = myClientHandle;
myClient.myStre am = new FileStream(myCl ientHandle,
FileAccess.Read Write, BUFFER_SIZE, true);
}
myListenLoopCou nt = (myListenLoopCo unt 10000) ? 0 :
myListenLoopCou nt+1;
}
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.P ipeName = ReferenceNames. PipeName;
if (!myNPipeServer .Running)
myNPipeServer.S tart();
Above code basically starts a seperate thread to do the listening of
client registration request.
Create Client:
myNPipeClient = new Client();
myNPipeClient.P ipeName = ReferenceNames. PipeName;
myNPipeClient.C onnect();
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.
CreateServerWit hCommonName();
CreateClientWit hCommonName();
Assert.IsNotNul l(myNPipeServer );
Assert.IsNotNul l(myNPipeClient );
bool register = ServerListenedF orClient(listen Count);
if (register)
Assert.IsNotNul l(myNamedPipeSe rver.CurrentCli ent.myHandle);
DisconnectClien t();
DisconnectServe r();
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
ServerListenedF orClient 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_OVERL APPED,
0,
255,
BUFFER_SIZE,
BUFFER_SIZE,
0,
IntPtr.Zero);
//could not create named pipe
if (myClientHandle .IsInvalid)
{
myConnectionSta tus =
ReferenceNames. ConnectionStatu s.ConnectionFai led;
myListenLoopCou nt = -1;
return;
}
int success = ConnectNamedPip e(myClientHandl e,
IntPtr.Zero);
//could not connect client
if (success == 0)
{
myConnectionSta tus =
ReferenceNames. ConnectionStatu s.ConnectionFai led;
myListenLoopCou nt = -2;
return;
}
lock (myClient)
{
if (myClient == null)
myClient = new Client();
myClient.myHand le = myClientHandle;
myClient.myStre am = new FileStream(myCl ientHandle,
FileAccess.Read Write, BUFFER_SIZE, true);
}
myListenLoopCou nt = (myListenLoopCo unt 10000) ? 0 :
myListenLoopCou nt+1;
}