public string Read(ChannelBuffer buf, byte separator) { int count = buf.BytesBefore(separator); if (count < 0) { return(null); } byte[] data = new byte[count]; buf.ReadBytes(data); buf.ReadByte(); // get rid of separator return(Encoding.UTF8.GetString(data)); }
public string ReadRaw(ChannelBuffer buf, byte separator) { int count = buf.BytesBefore(separator); if (count < 0) { return(null); } byte[] data = new byte[count]; buf.ReadBytes(data); buf.ReadByte(); // get rid of separator int length = data.Length; for (int i = 0; i < length; i++) { if (data[i] == '\\') { if (i + 1 < length) { byte b = data[i + 1]; if (b == 't') { data[i] = (byte)'\t'; } else if (b == 'r') { data[i] = (byte)'\r'; } else if (b == 'n') { data[i] = (byte)'\n'; } else { data[i] = b; } Array.Copy(data, i + 2, data, i + 1, length - i - 2); length--; } } } return(Encoding.UTF8.GetString(data, 0, length)); }
public string ReadRaw(ChannelBuffer buf, byte separator) { int count = buf.BytesBefore(separator); if (count < 0) { return null; } byte[] data = new byte[count]; buf.ReadBytes(data); buf.ReadByte(); // get rid of separator int length = data.Length; for (int i = 0; i < length; i++) { if (data[i] == '\\') { if (i + 1 < length) { byte b = data[i + 1]; if (b == 't') { data[i] = (byte)'\t'; } else if (b == 'r') { data[i] = (byte)'\r'; } else if (b == 'n') { data[i] = (byte)'\n'; } else { data[i] = b; } Array.Copy(data, i + 2, data, i + 1, length - i - 2); length--; } } } return Encoding.UTF8.GetString(data, 0, length); }
public string Read(ChannelBuffer buf, byte separator) { int count = buf.BytesBefore(separator); if (count < 0) { return null; } byte[] data = new byte[count]; buf.ReadBytes(data); buf.ReadByte(); // get rid of separator return Encoding.UTF8.GetString(data); }
protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent, Stack<ITransaction> stack, IMessageTree tree) { BufferHelper helper = _mBufferHelper; char identifier = (char)buf.ReadByte(); string timestamp = helper.Read(buf, TAB); string type = helper.Read(buf, TAB); string name = helper.Read(buf, TAB); switch (identifier) { case 't': IMessage transaction = new DefaultTransaction(type, name, null); helper.Read(buf, LF); // get rid of line feed transaction.Timestamp = _mDateHelper.Parse(timestamp); if (parent != null) { parent.AddChild(transaction); } stack.Push(parent); return transaction; case 'A': DefaultTransaction tran = new DefaultTransaction(type, name, null); string status = helper.Read(buf, TAB); string duration = helper.Read(buf, TAB); string data = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed tran.Timestamp = _mDateHelper.Parse(timestamp); tran.Status = status; tran.AddData(data); long d = long.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer); tran.DurationInMicros = d; if (parent != null) { parent.AddChild(tran); return parent; } return tran; case 'T': string transactionStatus = helper.Read(buf, TAB); string transactionDuration = helper.Read(buf, TAB); string transactionData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed parent.Status = transactionStatus; parent.AddData(transactionData); long transactionD = long.Parse(transactionDuration.Substring(0, transactionDuration.Length - 2), NumberStyles.Integer); parent.DurationInMicros = transactionD; return stack.Pop(); case 'E': DefaultEvent evt = new DefaultEvent(type, name); string eventStatus = helper.Read(buf, TAB); string eventData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed evt.Timestamp = _mDateHelper.Parse(timestamp); evt.Status = eventStatus; evt.AddData(eventData); if (parent != null) { parent.AddChild(evt); return parent; } return evt; case 'M': DefaultMetric metric = new DefaultMetric(type, name); string metricStatus = helper.Read(buf, TAB); string metricData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); metric.Timestamp = _mDateHelper.Parse(timestamp); metric.Status = metricStatus; metric.AddData(metricData); if (parent != null) { parent.AddChild(metric); return parent; } return metric; case 'L': DefaultTrace trace = new DefaultTrace(type, name); string traceStatus = helper.Read(buf, TAB); string traceData = helper.Read(buf, TAB); helper.Read(buf, LF); // get rid of line feed trace.Timestamp = _mDateHelper.Parse(timestamp); trace.Status = traceStatus; trace.AddData(traceData); if (parent != null) { parent.AddChild(trace); return parent; } return trace; case 'H': DefaultHeartbeat heartbeat = new DefaultHeartbeat(type, name); string heartbeatStatus = helper.Read(buf, TAB); string heartbeatData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed heartbeat.Timestamp = _mDateHelper.Parse(timestamp); heartbeat.Status = heartbeatStatus; heartbeat.AddData(heartbeatData); if (parent != null) { parent.AddChild(heartbeat); return parent; } return heartbeat; } Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf); //throw new Exception("Unknown identifier int name"); //java版的抛出异常 // unknown message, ignore it return parent; }
protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent, Stack <ITransaction> stack, IMessageTree tree) { BufferHelper helper = _mBufferHelper; char identifier = (char)buf.ReadByte(); string timestamp = helper.Read(buf, TAB); string type = helper.Read(buf, TAB); string name = helper.Read(buf, TAB); switch (identifier) { case 't': IMessage transaction = new DefaultTransaction(type, name, null); helper.Read(buf, LF); // get rid of line feed transaction.Timestamp = _mDateHelper.Parse(timestamp); if (parent != null) { parent.AddChild(transaction); } stack.Push(parent); return(transaction); case 'A': DefaultTransaction tran = new DefaultTransaction(type, name, null); string status = helper.Read(buf, TAB); string duration = helper.Read(buf, TAB); string data = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed tran.Timestamp = _mDateHelper.Parse(timestamp); tran.Status = status; tran.AddData(data); long d = long.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer); tran.DurationInMicros = d; if (parent != null) { parent.AddChild(tran); return(parent); } return(tran); case 'T': string transactionStatus = helper.Read(buf, TAB); string transactionDuration = helper.Read(buf, TAB); string transactionData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed parent.Status = transactionStatus; parent.AddData(transactionData); long transactionD = long.Parse(transactionDuration.Substring(0, transactionDuration.Length - 2), NumberStyles.Integer); parent.DurationInMicros = transactionD; return(stack.Pop()); case 'E': DefaultEvent evt = new DefaultEvent(type, name); string eventStatus = helper.Read(buf, TAB); string eventData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed evt.Timestamp = _mDateHelper.Parse(timestamp); evt.Status = eventStatus; evt.AddData(eventData); if (parent != null) { parent.AddChild(evt); return(parent); } return(evt); case 'M': DefaultMetric metric = new DefaultMetric(type, name); string metricStatus = helper.Read(buf, TAB); string metricData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); metric.Timestamp = _mDateHelper.Parse(timestamp); metric.Status = metricStatus; metric.AddData(metricData); if (parent != null) { parent.AddChild(metric); return(parent); } return(metric); case 'L': DefaultTrace trace = new DefaultTrace(type, name); string traceStatus = helper.Read(buf, TAB); string traceData = helper.Read(buf, TAB); helper.Read(buf, LF); // get rid of line feed trace.Timestamp = _mDateHelper.Parse(timestamp); trace.Status = traceStatus; trace.AddData(traceData); if (parent != null) { parent.AddChild(trace); return(parent); } return(trace); case 'H': DefaultHeartbeat heartbeat = new DefaultHeartbeat(type, name); string heartbeatStatus = helper.Read(buf, TAB); string heartbeatData = helper.ReadRaw(buf, TAB); helper.Read(buf, LF); // get rid of line feed heartbeat.Timestamp = _mDateHelper.Parse(timestamp); heartbeat.Status = heartbeatStatus; heartbeat.AddData(heartbeatData); if (parent != null) { parent.AddChild(heartbeat); return(parent); } return(heartbeat); } Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf); //throw new Exception("Unknown identifier int name"); //java版的抛出异常 // unknown message, ignore it return(parent); }