c# - async NamedPipeClientStream implementation feedback -
Currently, I have implemented designated pipe using a demo code instead of I instead of synchronous client; However, I want to make it asynchronous. Here's my implementation, where the main program calls StartClientNamedPipeListening:
/// & lt; Summary & gt; /// buffer where received bytes or bytes have been received /// & lt; / Summary & gt; Private byte [] _byteBuffer = null; /// & lt; Summary & gt; /// Callback result for reading data from named pipes /// & lt; / Summary & gt; Private IAsyncResult _pipeResult; /// & lt; Summary & gt; /// Watchdog /// & lt; / Summary & gt; Object is designated to send and receive data from. NamedPipeClientStream _pipeClient; /// & lt; Summary & gt; /// Waiting indicates threads that an event has occurred /// & lt; / Summary & gt; Protected Manual ResetEvent _pipeReadDone = New Manual Retset (incorrect); Private object _pipestate = new object (); Private Zero StartClientNamedPipeListening () {// open and then close the gate as soon as a thread is passed, // that is, the event was a non-signal or closed, status: _pipeReadDone.Reset (); // Coming from the data pipes and calling the // Thread Safe Rep to get the data. _bighbuffre = new byte [50]; _pipe resolt = _pipeClient.BeginRead (_byteBuffer, 0, _byteBuffer.Length, PipeRedcodeBack, _pipestate); // worker thread block here (waiting ... // _pipeReadDone.Set ()), ie, wait to open the door _pipeReadDone.WaitOne (); } Private Zero PipeRedlockback (IAsyncResult AR) {int BitsRed = 0; // if the port serial is open and .. (_pipeClient.IsConnected) can read the // stream then .. if (_pipeClient.CanRead) {// read the asynchronous reads to complete the bytes read = _pip client . Andride (AR); }} If (Bytes Reid> 0) {StreamString ss = New StreamString (_pipeClient); // Validate the signature string of the server (ss.ReadString () == "I am a true server!") {// Client security token is sent with the first write. // Send the name of the file whose content is returned by the // server (@ "C: \ temporary \ namedpipestring.txt") ss.WriteString; // Print the file on the screen. Console.light line (ss.ReadString (), incorrect); } Else {Console.WriteLine ("The server could not be verified."); } // Enter the event in a signal, or open, position: // open the door to the next data _preardon .set (); // Start waiting for next monitoring message StartClientNamedPipeListening (); }} This implementation works according to my tests; However, I was thinking, am I not clear anything, am not? Does anyone have a suggestion that how can it be implemented better? TIA
Here's how I modified the code to work it asynchronously Not sure why I thought I needed a manual reset event:
/// & lt; Summary & gt; /// buffer where received bytes or bytes have been received /// & lt; / Summary & gt; Private byte [] _byteBuffer = null; /// & lt; Summary & gt; /// Callback result for reading data from named pipes /// & lt; / Summary & gt; Private IAsyncResult _pipeResult; /// & lt; Summary & gt; /// Watchdog /// & lt; / Summary & gt; Object is designated to send and receive data from. NamedPipeClientStream _pipeClient; Private object _pipestate = new object (); Private Zero StartClientNamedPipeListening () {_pipeClient = New Named Pipe Client Stream (".", "Testpipe", Pipe Direction. Inopt, Pipe Option. Asynchronous, Token Impression Level. Impersonation); _pipeClient.Connect (); // Coming from the data pipes and calling the // Thread Safe Rep to get the data. _bighbuffre = new byte [50]; _pipe resolt = _pipeClient.BeginRead (_byteBuffer, 0, _byteBuffer.Length, PipeRedcodeBack, _pipestate); } Private Zero PipeRedlockback (IAsyncResult AR) {int BitsRed = 0; // if the port serial is open and .. (_pipeClient.IsConnected) can read the // stream then .. if (_pipeClient.CanRead) {// read the asynchronous reads to complete the bytes read = _pip client . Andride (AR); }} If (Bytes Reid> 0) {StreamString ss = New StreamString (_pipeClient); // Validate the signature string of the server (ss.ReadString () == "I am a true server!") {// Client security token is sent with the first write. // Send the name of the file whose content is returned by the // server (@ "C: \ temporary \ namedpipestring.txt") ss.WriteString; // Print the file on the screen. Console.light line (ss.ReadString (), incorrect); } Else {Console.WriteLine ("The server could not be verified."); } // Start waiting for the next watch's message StartClientNamedPipeListening (); }} Thanks, Swan.
Comments
Post a Comment