public override String ToString() { return(LogEventsBuffer.BytesToHexString(data, index, " ")); }
private static void NamedPipeThread() { //logger.debug("create pipe"); SafeFileHandle pipeHandle; String pipeName = "\\\\.\\pipe\\logconsole" + hostProcID.ToString(); pipeHandle = NativeMethods.CreateNamedPipe(pipeName, NativeMethods.PIPE_DUPLEX | NativeMethods.FILE_FLAG_OVERLAPPED, 0, MAX_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); if (pipeHandle.IsInvalid) { //logger.error("create pipe failed"); return; } //logger.debug("wait for client"); while (true) { // wait for client int success = NativeMethods.ConnectNamedPipe(pipeHandle, IntPtr.Zero); //failed to connect client pipe if (success != 1) { // hope that optimizer is not going to optimize this: if (NativeMethods.GetLastError() == NativeMethods.ERROR_PIPE_CONNECTED) { break; } //logger.error("connection failed: " + success); } else { break; } Thread.Sleep(15); } FileStream fStream = new FileStream(pipeHandle, FileAccess.ReadWrite, BUFFER_SIZE, true); //logger.debug("server connected"); Engine.IsReady = true; //logger.debug("wait for console"); while (true) { if (LogConsoleProgram.logConsoleWindow != null && LogConsoleProgram.logConsoleWindow.IsInitialized) { break; } Thread.Sleep(5); } //logger.debug("started"); byte[] buffer = new byte[BUFFER_SIZE]; ASCIIEncoding encoder = new ASCIIEncoding(); int offset = 0; bool readingNumBytes = true; int packetNumBytes = -1; while (!shutdown) { /* * Process hostProcess = null; * try * { * hostProcess = Process.GetProcessById(hostProcID); * if (hostProcess == null) * LogConsoleProgram.Shutdown(); * } * catch * { * LogConsoleProgram.Shutdown(); * } */ int bytesRead = fStream.Read(buffer, offset, BUFFER_SIZE - offset); offset += bytesRead; if (bytesRead == 0) { // disconnected LogConsoleProgram.Shutdown(); } while (true) { if (readingNumBytes) { if (offset < 2) { break; } packetNumBytes = buffer[1] | (buffer[0] << 8); Array.Copy(buffer, 2, buffer, 0, buffer.Length - 2); offset -= 2; readingNumBytes = false; } else { if (offset < packetNumBytes) { break; } LogEventsBuffer byteBuffer = new LogEventsBuffer(buffer, packetNumBytes); Array.Copy(buffer, packetNumBytes, buffer, 0, buffer.Length - packetNumBytes); offset -= packetNumBytes; readingNumBytes = true; logger.LogLevel logLevel = (logger.LogLevel)byteBuffer.GetInt(); DateTime time = byteBuffer.GetDateTime(); String methodName = byteBuffer.GetString(); String threadName = byteBuffer.GetString(); String message = byteBuffer.GetString(); //logger.debug("received " + message); LogConsoleProgram.logConsoleWindow.LogEvent(logLevel, time, methodName, threadName, message); } } //Thread.Sleep(15); } //logger.debug("server finished"); LogConsoleProgram.Shutdown(); }
public void PutByteBuffer(LogEventsBuffer byteBuffer) { //logger.debug("putByteBuffer: " + byteBuffer.toString()); PutInt(byteBuffer.dataLength); PutBytes(byteBuffer.data, byteBuffer.dataLength); }