سؤال

In my application I'm using an Anonymous Pipe to send messages containing an int, and enum (technically also an int), and a string. My pipe is setup and modeled exactly after the pipe in this MSDN article. I wan't to keep this as simple, and more importantly fast, as possible. What I wan't to do is make the first 32 bits of the message the int, the next 32 bits the enum, and everything remaining the message. How can I accomplish this?

I had originally though to just format the string to a certain length like in this answer, but the formatting actually produces different length strings if the number is negative vs positive. And this seems to be the wrong approach anyways, I feel like I need the binary representation of these ints, not a string representation.

Currently I send messages like this:

m_RemoteLogger = new Process();
m_RemoteLogger.StartInfo.FileName = @"C:\Work\Library\Utilities.Logging.exe";

m_LoggingStream = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);

// Pass the client process a handle to the server.
m_RemoteLogger.StartInfo.Arguments = m_LoggingStream.GetClientHandleAsString() + " \"" + m_Name + "\" \"" + m_Source + "\" " + m_Size;
m_RemoteLogger.StartInfo.UseShellExecute = false;
m_RemoteLogger.Start();

m_LoggingStream.DisposeLocalCopyOfClientHandle();

m_LoggingStreamWriter = new StreamWriter(m_LoggingStream);
m_LoggingStreamWriter.AutoFlush = true;
// Send a 'sync message' and wait for client to receive it.
m_LoggingStreamWriter.WriteLine("SYNC");

m_LoggingStream.WaitForPipeDrain();

And Receive them like this:

if (args.Length > 0) {
    m_EventLog = CreateEventLog(args[1], args[2], int.Parse(args[3]));

    using (PipeStream pipeClient =
        new AnonymousPipeClientStream(PipeDirection.In, args[0])) {

        using (StreamReader sr = new StreamReader(pipeClient)) {
            string temp;

            // Wait for startup information from our creator. 
            do {
                temp = sr.ReadLine();
            }
            while (!temp.StartsWith("SYNC"));

            // Read messages to log from the stream and write to the event log
            while ((temp = sr.ReadLine()) != null) {
                var loggingInformation = LogMessageFormatter.ConvertFromString(temp);
                m_EventLog.WriteEntry(loggingInformation.Item1, loggingInformation.Item2,
                    loggingInformation.Item3);

            }
        }
    }
}

The LogMessageFormatter static methods are performing some custom serialization with delimiters, that I want to get away from.

هل كانت مفيدة؟

المحلول

You can use BinaryWriter to write binary data directly to the stream.

using (AnonymousPipeServerStream pipeServer =
        new AnonymousPipeServerStream(PipeDirection.Out,
        HandleInheritability.Inheritable))
    {
       // .......
       int id = 123456;
       string msg = "hello";
       using(var binWriter = new BinaryWriter(pipeServer))
       {
          binWriter.Write(id);
          binWriter.Write(msg);
       }
    }
}

On the other side of the pipe, use BinaryReader.

using (var rdr = new BinaryReader(pipeClient))
{
   int id = rdr.ReadInt32();
   string msg = rdr.ReadString();
}

نصائح أخرى

If you want to read/write as binary - use BinaryReader and BinaryWriter instead of text-based StremReader/StreamWriter.

using (BinaryWriter sw = new StreamWriter(pipeServer))
{ 
     sw.Write((int)1234);
     sw.Write((int)someEnumValue);
     sw.Write("my text");
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top