private byte[] SendMessageToServer(Command command, byte value) { byte[] reply = null; using (NamedPipeClientStream stream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous)) { stream.Connect(); oneByteParameterMessageBuffer[4] = (byte)command; oneByteParameterMessageBuffer[5] = value; stream.Write(oneByteParameterMessageBuffer, 0, oneByteParameterMessageBuffer.Length); stream.ProperRead(replyLengthBuffer, 0, replyLengthBuffer.Length); int replyLength = BitConverter.ToInt32(replyLengthBuffer, 0); if (replyLength > 0) { reply = replyLength == 1 ? oneByteReplyBuffer : new byte[replyLength]; stream.ProperRead(reply, 0, replyLength); } stream.WaitForPipeDrain(); } return(reply); }
private byte[] SendMessageToServer(Command command, string value) { byte[] reply = null; using (NamedPipeClientStream stream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous)) { stream.Connect(); int dataLength = sizeof(byte) + Encoding.UTF8.GetByteCount(value); byte[] messageBuffer = new byte[sizeof(int) + dataLength]; messageBuffer[0] = (byte)(dataLength & 0xff); messageBuffer[1] = (byte)((dataLength >> 8) & 0xff); messageBuffer[2] = (byte)((dataLength >> 16) & 0xff); messageBuffer[3] = (byte)((dataLength >> 24) & 0xff); messageBuffer[4] = (byte)command; Encoding.UTF8.GetBytes(value, 0, value.Length, messageBuffer, 5); stream.Write(messageBuffer, 0, messageBuffer.Length); stream.ProperRead(replyLengthBuffer, 0, replyLengthBuffer.Length); int replyLength = BitConverter.ToInt32(replyLengthBuffer, 0); if (replyLength > 0) { reply = replyLength == 1 ? oneByteReplyBuffer : new byte[replyLength]; stream.ProperRead(reply, 0, replyLength); } stream.WaitForPipeDrain(); } return(reply); }