Beispiel #1
0
        internal static void TraceSend(Ice.OutputStream str, Ice.ILogger logger, TraceLevels tl)
        {
            if (tl.Protocol >= 1)
            {
                var buffer = str.ToArray(); // TODO avoid copy the data
                var iss    = new InputStream(str.Communicator, str.Encoding, buffer);
                iss.Pos = 0;

                using var s = new System.IO.StringWriter(CultureInfo.CurrentCulture);
                byte type = PrintMessage(s, iss);

                logger.Trace(tl.ProtocolCat, "sending " + GetMessageTypeAsString(type) + " " + s.ToString());
            }
        }
        public Endpoint?CreateEndpoint(string endpointString, bool oaEndpoint)
        {
            string[]? args = IceUtilInternal.StringUtil.SplitString(endpointString, " \t\r\n");
            if (args == null)
            {
                throw new FormatException($"mismatched quote in endpoint `{endpointString}'");
            }

            if (args.Length == 0)
            {
                throw new FormatException("no non-whitespace character in endpoint string");
            }

            string transport = args[0];

            if (transport == "default")
            {
                transport = DefaultTransport;
            }

            var options = new Dictionary <string, string?>();

            // Parse args into options (and skip transport at args[0])
            for (int n = 1; n < args.Length; ++n)
            {
                // Any option with < 2 characters or that does not start with - is illegal
                string option = args[n];
                if (option.Length < 2 || option[0] != '-')
                {
                    throw new FormatException($"invalid option `{option}' in endpoint `{endpointString}'");
                }

                // Extract the argument given to the current option, if any
                string?argument = null;
                if (n + 1 < args.Length && args[n + 1][0] != '-')
                {
                    argument = args[++n];
                }

                try
                {
                    options.Add(option, argument);
                }
                catch (ArgumentException)
                {
                    throw new FormatException($"duplicate option `{option}' in endpoint `{endpointString}'");
                }
            }

            IEndpointFactory?factory = null;

            lock (this)
            {
                for (int i = 0; i < _endpointFactories.Count; i++)
                {
                    IEndpointFactory f = _endpointFactories[i];
                    if (f.Transport() == transport)
                    {
                        factory = f;
                    }
                }
            }

            if (factory != null)
            {
                Endpoint?e = factory.Create(endpointString, options, oaEndpoint);
                if (options.Count > 0)
                {
                    throw new FormatException(
                              $"unrecognized option(s) `{ToString(options)}' in endpoint `{endpointString}'");
                }
                return(e);
            }

            //
            // If the stringified endpoint is opaque, create an unknown endpoint,
            // then see whether the type matches one of the known endpoints.
            //
            if (transport == "opaque")
            {
                Endpoint ue = new OpaqueEndpoint(endpointString, options);
                if (options.Count > 0)
                {
                    throw new FormatException(
                              $"unrecognized option(s) `{ToString(options)}' in endpoint `{endpointString}'");
                }
                factory = GetEndpointFactory(ue.Type);
                if (factory != null)
                {
                    //
                    // Make a temporary stream, write the opaque endpoint data into the stream,
                    // and ask the factory to read the endpoint data from that stream to create
                    // the actual endpoint.
                    //
                    var ostr = new OutputStream(Ice1Definitions.Encoding, new List <ArraySegment <byte> >());
                    ostr.WriteShort((short)ue.Type);
                    ue.IceWrite(ostr);
                    // TODO avoid copy OutputStream buffers
                    var iss = new InputStream(this, ostr.ToArray());
                    iss.Pos = 0;
                    iss.ReadShort(); // type
                    iss.StartEndpointEncapsulation();
                    Endpoint?e = factory.Read(iss);
                    iss.EndEndpointEncapsulation();
                    return(e);
                }
                return(ue); // Endpoint is opaque, but we don't have a factory for its type.
            }

            return(null);
        }
        public Endpoint?CreateEndpoint(string str, bool oaEndpoint)
        {
            string[]? arr = IceUtilInternal.StringUtil.SplitString(str, " \t\r\n");
            if (arr == null)
            {
                throw new System.FormatException("mismatched quote");
            }

            if (arr.Length == 0)
            {
                throw new System.FormatException("value has no non-whitespace characters");
            }

            var    v         = new List <string>(arr);
            string transport = v[0];

            v.RemoveAt(0);

            if (transport.Equals("default"))
            {
                transport = DefaultsAndOverrides.DefaultTransport;
            }

            IEndpointFactory?factory = null;

            lock (this)
            {
                for (int i = 0; i < _endpointFactories.Count; i++)
                {
                    IEndpointFactory f = _endpointFactories[i];
                    if (f.Transport().Equals(transport))
                    {
                        factory = f;
                    }
                }
            }

            if (factory != null)
            {
                Endpoint?e = factory.Create(v, oaEndpoint);
                if (v.Count > 0)
                {
                    throw new System.FormatException($"unrecognized argument `{v[0]}' in endpoint `{str}'");
                }
                return(e);

                // Code below left in place for debugging.

                /*
                 * EndpointI e = f.create(s.Substring(m.Index + m.Length), oaEndpoint);
                 * BasicStream bs = new BasicStream(_instance, true);
                 * e.streamWrite(bs);
                 * Buffer buf = bs.getBuffer();
                 * buf.b.position(0);
                 * short type = bs.readShort();
                 * EndpointI ue = new IceInternal.OpaqueEndpointI(type, bs);
                 * System.Console.Error.WriteLine("Normal: " + e);
                 * System.Console.Error.WriteLine("Opaque: " + ue);
                 * return e;
                 */
            }

            //
            // If the stringified endpoint is opaque, create an unknown endpoint,
            // then see whether the type matches one of the known endpoints.
            //
            if (transport.Equals("opaque"))
            {
                Endpoint ue = new OpaqueEndpointI(v);
                if (v.Count > 0)
                {
                    throw new System.FormatException($"unrecognized argument `{v[0]}' in endpoint `{str}'");
                }
                factory = GetEndpointFactory(ue.Type());
                if (factory != null)
                {
                    //
                    // Make a temporary stream, write the opaque endpoint data into the stream,
                    // and ask the factory to read the endpoint data from that stream to create
                    // the actual endpoint.
                    //
                    var os = new OutputStream(this, Ice1Definitions.Encoding);
                    os.WriteShort(ue.Type());
                    ue.StreamWrite(os);
                    // TODO avoid copy OutputStream buffers
                    var iss = new InputStream(this, Ice1Definitions.Encoding, os.ToArray());
                    iss.Pos = 0;
                    iss.ReadShort(); // type
                    iss.StartEndpointEncapsulation();
                    Endpoint?e = factory.Read(iss);
                    iss.EndEndpointEncapsulation();
                    return(e);
                }
                return(ue); // Endpoint is opaque, but we don't have a factory for its type.
            }

            return(null);
        }
Beispiel #4
0
        private async ValueTask InvokeAllAsync(Ice.OutputStream os, int requestId)
        {
            // The object adapter DirectCount was incremented by the caller and we are responsible to decrement it
            // upon completion.

            Ice.Instrumentation.IDispatchObserver?dispatchObserver = null;
            try
            {
                if (_traceLevels.Protocol >= 1)
                {
                    FillInValue(os, new Ice.OutputStream.Position(0, 10), os.Size);
                    if (requestId > 0)
                    {
                        FillInValue(os, new Ice.OutputStream.Position(0, Ice1Definitions.HeaderSize), requestId);
                    }
                    TraceUtil.TraceSend(os, _logger, _traceLevels);
                }

                // TODO Avoid copy OutputStream buffer
                var requestFrame = new Ice.InputStream(os.Communicator, os.Encoding, os.ToArray());
                requestFrame.Pos = Ice1Definitions.RequestHeader.Length;

                int start   = requestFrame.Pos;
                var current = new Ice.Current(requestId, requestFrame, _adapter);

                // Then notify and set dispatch observer, if any.
                Ice.Instrumentation.ICommunicatorObserver?communicatorObserver = _adapter.Communicator.Observer;
                if (communicatorObserver != null)
                {
                    int encapsSize = requestFrame.GetEncapsulationSize();

                    dispatchObserver = communicatorObserver.GetDispatchObserver(current,
                                                                                requestFrame.Pos - start + encapsSize);
                    dispatchObserver?.Attach();
                }

                bool amd = true;
                try
                {
                    Ice.IObject?servant = current.Adapter.Find(current.Id, current.Facet);

                    if (servant == null)
                    {
                        amd = false;
                        throw new Ice.ObjectNotExistException(current.Id, current.Facet, current.Operation);
                    }

                    ValueTask <Ice.OutputStream> vt = servant.DispatchAsync(requestFrame, current);
                    amd = !vt.IsCompleted;
                    if (requestId != 0)
                    {
                        Ice.OutputStream responseFrame = await vt.ConfigureAwait(false);

                        dispatchObserver?.Reply(responseFrame.Size - Ice1Definitions.HeaderSize - 4);
                        SendResponse(requestId, responseFrame, amd);
                    }
                }
                catch (System.Exception ex)
                {
                    if (requestId != 0)
                    {
                        Ice.RemoteException actualEx;
                        if (ex is Ice.RemoteException remoteEx && !remoteEx.ConvertToUnhandled)
                        {
                            actualEx = remoteEx;
                        }
                        else
                        {
                            actualEx = new UnhandledException(current.Id, current.Facet, current.Operation, ex);
                        }

                        Incoming.ReportException(actualEx, dispatchObserver, current);
                        var responseFrame = new Ice.OutgoingResponseFrame(current, actualEx);
                        dispatchObserver?.Reply(responseFrame.Size - Ice1Definitions.HeaderSize - 4);
                        SendResponse(requestId, responseFrame, amd);
                    }
                }
            }