| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Globalization; |
| | 5 | |
|
| | 6 | | namespace Ice.Internal; |
| | 7 | |
|
| | 8 | | internal sealed class TraceUtil |
| | 9 | | { |
| | 10 | | internal static void traceSend( |
| | 11 | | OutputStream str, |
| | 12 | | Instance instance, |
| | 13 | | ConnectionI connection, |
| | 14 | | Logger logger, |
| | 15 | | TraceLevels tl) |
| | 16 | | { |
| 1 | 17 | | if (tl.protocol >= 1) |
| | 18 | | { |
| 1 | 19 | | int p = str.pos(); |
| 1 | 20 | | var iss = new Ice.InputStream(instance, str.getEncoding(), str.getBuffer(), false); |
| 1 | 21 | | iss.pos(0); |
| | 22 | |
|
| 1 | 23 | | using (var s = new System.IO.StringWriter(CultureInfo.CurrentCulture)) |
| | 24 | | { |
| 1 | 25 | | byte type = printMessage(s, iss, connection); |
| | 26 | |
|
| 1 | 27 | | logger.trace(tl.protocolCat, "sending " + getMessageTypeAsString(type) + " " + s.ToString()); |
| 1 | 28 | | } |
| 1 | 29 | | str.pos(p); |
| | 30 | | } |
| 1 | 31 | | } |
| | 32 | |
|
| | 33 | | internal static void traceRecv(Ice.InputStream str, ConnectionI connection, Ice.Logger logger, TraceLevels tl) |
| | 34 | | { |
| 1 | 35 | | if (tl.protocol >= 1) |
| | 36 | | { |
| 1 | 37 | | int p = str.pos(); |
| 1 | 38 | | str.pos(0); |
| | 39 | |
|
| 1 | 40 | | using (var s = new System.IO.StringWriter(CultureInfo.CurrentCulture)) |
| | 41 | | { |
| 1 | 42 | | byte type = printMessage(s, str, connection); |
| | 43 | |
|
| 1 | 44 | | logger.trace(tl.protocolCat, "received " + getMessageTypeAsString(type) + " " + s.ToString()); |
| 1 | 45 | | } |
| 1 | 46 | | str.pos(p); |
| | 47 | | } |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | internal static void trace(string heading, InputStream str, ConnectionI connection, Logger logger, TraceLevels tl) |
| | 51 | | { |
| 1 | 52 | | if (tl.protocol >= 1) |
| | 53 | | { |
| 1 | 54 | | int p = str.pos(); |
| 1 | 55 | | str.pos(0); |
| | 56 | |
|
| 1 | 57 | | using (var s = new StringWriter(CultureInfo.CurrentCulture)) |
| | 58 | | { |
| 1 | 59 | | s.Write(heading); |
| 1 | 60 | | printMessage(s, str, connection); |
| | 61 | |
|
| 1 | 62 | | logger.trace(tl.protocolCat, s.ToString()); |
| 1 | 63 | | } |
| 1 | 64 | | str.pos(p); |
| | 65 | | } |
| 1 | 66 | | } |
| | 67 | |
|
| 0 | 68 | | private static readonly HashSet<string> slicingIds = new(); |
| | 69 | |
|
| | 70 | | internal static void traceSlicing(string kind, string typeId, string slicingCat, Ice.Logger logger) |
| | 71 | | { |
| 0 | 72 | | lock (_globalMutex) |
| | 73 | | { |
| 0 | 74 | | if (slicingIds.Add(typeId)) |
| | 75 | | { |
| 0 | 76 | | using var s = new StringWriter(CultureInfo.CurrentCulture); |
| 0 | 77 | | s.Write("unknown " + kind + " type `" + typeId + "'"); |
| 0 | 78 | | logger.trace(slicingCat, s.ToString()); |
| | 79 | | } |
| 0 | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private static void printIdentityFacetOperation(System.IO.StringWriter s, Ice.InputStream str) |
| | 84 | | { |
| | 85 | | try |
| | 86 | | { |
| 1 | 87 | | Ice.ToStringMode toStringMode = str.instance().toStringMode(); |
| | 88 | |
|
| 1 | 89 | | var identity = new Ice.Identity(str); |
| 1 | 90 | | s.Write("\nidentity = " + Ice.Util.identityToString(identity, toStringMode)); |
| | 91 | |
|
| 1 | 92 | | string[] facet = str.readStringSeq(); |
| 1 | 93 | | s.Write("\nfacet = "); |
| 1 | 94 | | if (facet.Length > 0) |
| | 95 | | { |
| 1 | 96 | | s.Write(Ice.UtilInternal.StringUtil.escapeString(facet[0], "", toStringMode)); |
| | 97 | | } |
| | 98 | |
|
| 1 | 99 | | string operation = str.readString(); |
| 1 | 100 | | s.Write("\noperation = " + operation); |
| 1 | 101 | | } |
| 0 | 102 | | catch (System.IO.IOException) |
| | 103 | | { |
| | 104 | | Debug.Assert(false); |
| 0 | 105 | | } |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | private static void printRequest(System.IO.StringWriter s, Ice.InputStream str) |
| | 109 | | { |
| 1 | 110 | | int requestId = str.readInt(); |
| 1 | 111 | | s.Write("\nrequest id = " + requestId); |
| 1 | 112 | | if (requestId == 0) |
| | 113 | | { |
| 1 | 114 | | s.Write(" (oneway)"); |
| | 115 | | } |
| | 116 | |
|
| 1 | 117 | | printRequestHeader(s, str); |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | private static void printBatchRequest(System.IO.StringWriter s, Ice.InputStream str) |
| | 121 | | { |
| 1 | 122 | | int batchRequestNum = str.readInt(); |
| 1 | 123 | | s.Write("\nnumber of requests = " + batchRequestNum); |
| | 124 | |
|
| 1 | 125 | | for (int i = 0; i < batchRequestNum; ++i) |
| | 126 | | { |
| 1 | 127 | | s.Write("\nrequest #" + i + ':'); |
| 1 | 128 | | printRequestHeader(s, str); |
| | 129 | | } |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | private static void printReply(System.IO.StringWriter s, Ice.InputStream str) |
| | 133 | | { |
| 1 | 134 | | int requestId = str.readInt(); |
| 1 | 135 | | s.Write("\nrequest id = " + requestId); |
| | 136 | |
|
| | 137 | | // We can't (shouldn't) use the generated code to unmarshal a possibly unknown reply status. |
| 1 | 138 | | var replyStatus = (ReplyStatus)str.readByte(); |
| 1 | 139 | | s.Write("\nreply status = "); |
| 1 | 140 | | s.Write(replyStatus); |
| | 141 | |
|
| | 142 | | switch (replyStatus) |
| | 143 | | { |
| | 144 | | case ReplyStatus.Ok: |
| | 145 | | case ReplyStatus.UserException: |
| 1 | 146 | | EncodingVersion v = str.skipEncapsulation(); |
| 1 | 147 | | if (v != Ice.Util.Encoding_1_0) |
| | 148 | | { |
| 1 | 149 | | s.Write("\nencoding = "); |
| 1 | 150 | | s.Write(Ice.Util.encodingVersionToString(v)); |
| | 151 | | } |
| 1 | 152 | | break; |
| | 153 | |
|
| | 154 | | case ReplyStatus.ObjectNotExist: |
| | 155 | | case ReplyStatus.FacetNotExist: |
| | 156 | | case ReplyStatus.OperationNotExist: |
| 1 | 157 | | printIdentityFacetOperation(s, str); |
| 1 | 158 | | break; |
| | 159 | |
|
| | 160 | | default: |
| 1 | 161 | | string message = str.readString(); |
| 1 | 162 | | s.Write("\nmessage = "); |
| 1 | 163 | | s.Write(message); |
| | 164 | | break; |
| | 165 | | } |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | private static void printRequestHeader(System.IO.StringWriter s, Ice.InputStream str) |
| | 169 | | { |
| 1 | 170 | | printIdentityFacetOperation(s, str); |
| | 171 | |
|
| | 172 | | try |
| | 173 | | { |
| 1 | 174 | | byte mode = str.readByte(); |
| 1 | 175 | | s.Write("\nmode = " + ((OperationMode)mode).ToString()); |
| | 176 | |
|
| 1 | 177 | | int sz = str.readSize(); |
| 1 | 178 | | s.Write("\ncontext = "); |
| 1 | 179 | | while (sz-- > 0) |
| | 180 | | { |
| 1 | 181 | | string key = str.readString(); |
| 1 | 182 | | string val = str.readString(); |
| 1 | 183 | | s.Write(key + '/' + val); |
| 1 | 184 | | if (sz > 0) |
| | 185 | | { |
| 1 | 186 | | s.Write(", "); |
| | 187 | | } |
| | 188 | | } |
| | 189 | |
|
| 1 | 190 | | Ice.EncodingVersion v = str.skipEncapsulation(); |
| 1 | 191 | | if (!v.Equals(Ice.Util.Encoding_1_0)) |
| | 192 | | { |
| 1 | 193 | | s.Write("\nencoding = "); |
| 1 | 194 | | s.Write(Ice.Util.encodingVersionToString(v)); |
| | 195 | | } |
| 1 | 196 | | } |
| 0 | 197 | | catch (System.IO.IOException) |
| | 198 | | { |
| | 199 | | Debug.Assert(false); |
| 0 | 200 | | } |
| 1 | 201 | | } |
| | 202 | |
|
| | 203 | | private static byte printHeader(System.IO.StringWriter s, Ice.InputStream str) |
| | 204 | | { |
| | 205 | | try |
| | 206 | | { |
| 1 | 207 | | str.readByte(); // Don't bother printing the magic number |
| 1 | 208 | | str.readByte(); |
| 1 | 209 | | str.readByte(); |
| 1 | 210 | | str.readByte(); |
| | 211 | |
|
| | 212 | | /* byte pMajor = */ |
| 1 | 213 | | str.readByte(); |
| | 214 | | /* byte pMinor = */ |
| 1 | 215 | | str.readByte(); |
| | 216 | | // s.Write("\nprotocol version = " + (int)pMajor + "." + (int)pMinor); |
| | 217 | |
|
| | 218 | | /* byte eMajor = */ |
| 1 | 219 | | str.readByte(); |
| | 220 | | /* byte eMinor = */ |
| 1 | 221 | | str.readByte(); |
| | 222 | | // s.Write("\nencoding version = " + (int)eMajor + "." + (int)eMinor); |
| | 223 | |
|
| 1 | 224 | | byte type = str.readByte(); |
| 1 | 225 | | s.Write("\nmessage type = " + (int)type + " (" + getMessageTypeAsString(type) + ')'); |
| | 226 | |
|
| 1 | 227 | | byte compress = str.readByte(); |
| 1 | 228 | | s.Write("\ncompression status = " + (int)compress + ' '); |
| | 229 | | switch (compress) |
| | 230 | | { |
| | 231 | | case 0: |
| | 232 | | { |
| 1 | 233 | | s.Write("(not compressed; do not compress response, if any)"); |
| 1 | 234 | | break; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | case 1: |
| | 238 | | { |
| 1 | 239 | | s.Write("(not compressed; compress response, if any)"); |
| 1 | 240 | | break; |
| | 241 | | } |
| | 242 | |
|
| | 243 | | case 2: |
| | 244 | | { |
| 1 | 245 | | s.Write("(compressed; compress response, if any)"); |
| 1 | 246 | | break; |
| | 247 | | } |
| | 248 | |
|
| | 249 | | default: |
| | 250 | | { |
| 0 | 251 | | s.Write("(unknown)"); |
| | 252 | | break; |
| | 253 | | } |
| | 254 | | } |
| | 255 | |
|
| 1 | 256 | | int size = str.readInt(); |
| 1 | 257 | | s.Write("\nmessage size = " + size); |
| 1 | 258 | | return type; |
| | 259 | | } |
| 0 | 260 | | catch (System.IO.IOException) |
| | 261 | | { |
| | 262 | | Debug.Assert(false); |
| 0 | 263 | | return 0; |
| | 264 | | } |
| 1 | 265 | | } |
| | 266 | |
|
| | 267 | | private static byte printMessage(System.IO.StringWriter s, Ice.InputStream str, ConnectionI connection) |
| | 268 | | { |
| 1 | 269 | | byte type = printHeader(s, str); |
| | 270 | |
|
| | 271 | | switch (type) |
| | 272 | | { |
| | 273 | | case Protocol.closeConnectionMsg: |
| | 274 | | case Protocol.validateConnectionMsg: |
| | 275 | | { |
| | 276 | | // We're done. |
| | 277 | | break; |
| | 278 | | } |
| | 279 | |
|
| | 280 | | case Protocol.requestMsg: |
| | 281 | | { |
| 1 | 282 | | printRequest(s, str); |
| 1 | 283 | | break; |
| | 284 | | } |
| | 285 | |
|
| | 286 | | case Protocol.requestBatchMsg: |
| | 287 | | { |
| 1 | 288 | | printBatchRequest(s, str); |
| 1 | 289 | | break; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | case Protocol.replyMsg: |
| | 293 | | { |
| 1 | 294 | | printReply(s, str); |
| 1 | 295 | | break; |
| | 296 | | } |
| | 297 | |
|
| | 298 | | default: |
| | 299 | | { |
| 0 | 300 | | s.Write("(unknown)"); |
| | 301 | | break; |
| | 302 | | } |
| | 303 | | } |
| | 304 | |
|
| 1 | 305 | | if (connection is not null) |
| | 306 | | { |
| 1 | 307 | | s.Write("\ntransport = " + connection.type() + "\n"); |
| 1 | 308 | | string connectionId = connection.endpoint().connectionId(); |
| 1 | 309 | | if (connectionId.Length > 0) |
| | 310 | | { |
| 1 | 311 | | s.Write("connection ID = " + connectionId + "\n"); |
| | 312 | | } |
| 1 | 313 | | s.Write(connection.ToString()); |
| | 314 | | } |
| | 315 | | else |
| | 316 | | { |
| 1 | 317 | | s.Write("\ncollocated = true"); |
| | 318 | | } |
| | 319 | |
|
| 1 | 320 | | return type; |
| | 321 | | } |
| | 322 | |
|
| | 323 | | private static string getMessageTypeAsString(byte type) |
| | 324 | | { |
| 1 | 325 | | return type switch |
| 1 | 326 | | { |
| 1 | 327 | | Protocol.requestMsg => "request", |
| 1 | 328 | | Protocol.requestBatchMsg => "batch request", |
| 1 | 329 | | Protocol.replyMsg => "reply", |
| 1 | 330 | | Protocol.closeConnectionMsg => "close connection", |
| 1 | 331 | | Protocol.validateConnectionMsg => "validate connection", |
| 0 | 332 | | _ => "unknown", |
| 1 | 333 | | }; |
| | 334 | | } |
| | 335 | |
|
| 0 | 336 | | private static readonly object _globalMutex = new object(); |
| | 337 | | } |