TMessage IRpcDispatch.CallMethod <TMessage, TBuilder>(string method, IMessageLite request,
                                                       IBuilderLite <TMessage, TBuilder> response)
 {
     byte[] rawResponse = _wire.Execute(method, request.ToByteArray());
     response.MergeFrom(rawResponse);
     return(response.Build());
 }
예제 #2
0
        public void WriteMessage(IMessageLite request)
        {
            var bytes = request.ToByteArray();
            var cos   = CodedOutputStream.CreateInstance(TcpClientWrapper.GetStream());

            cos.WriteRawVarint64((ulong)bytes.Length);
            cos.Flush();
            TcpClientWrapper.GetStream().Write(bytes, 0, bytes.Length);
            TcpClientWrapper.GetStream().Flush();
        }
            byte[] IWireTransfer.Execute(string method, byte[] message)
            {
                //now when we recieve a wire transmission to invoke a method by name with a byte[] or stream payload
                //we just simply call the sub:
                IMessageLite response = _stub.CallMethod(method, CodedInputStream.CreateInstance(message),
                                                         ExtensionRegistry.Empty);

                //now we return the expected response message:
                return(response.ToByteArray());
            }
예제 #4
0
        public virtual Stream Invoke(Stream request)
        {
            ICodedInputStream input  = CodedInputStream.CreateInstance(request);
            string            method = null;

            input.ReadString(ref method);
            IMessageLite response = _createStub.CallMethod(method, input, ExtensionRegistry.Empty);

            return(new MemoryStream(response.ToByteArray()));
        }
예제 #5
0
        public void TestMessageLiteWeakToBuilder()
        {
            IMessageLite msg = TestRequiredLite.CreateBuilder()
                               .SetD(42)
                               .SetEn(ExtraEnum.EXLITE_BAZ)
                               .Build();

            IMessageLite copy = msg.WeakToBuilder().WeakBuild();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #6
0
        public void TestIBuilderLiteWeakClone()
        {
            TestRequiredLite msg = TestRequiredLite.CreateBuilder()
                                   .SetD(1).SetEn(ExtraEnum.EXLITE_BAR).Build();

            Assert.IsTrue(msg.IsInitialized);

            IMessageLite copy = ((IBuilderLite)msg.ToBuilder()).WeakClone().WeakBuild();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
예제 #7
0
        public void TestIBuilderLiteWeakBuildPartial()
        {
            IBuilderLite builder = TestRequiredLite.CreateBuilder();

            Assert.IsFalse(builder.IsInitialized);

            IMessageLite msg = builder.WeakBuildPartial();

            Assert.IsFalse(msg.IsInitialized);

            Assert.AreEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
        }
        public void TestMissingFieldsLite()
        {
            TestAllTypesLite msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalInt32(123)
                                   .SetOptionalString("123")
                                   .Build();

            byte[]       bits = msg.ToByteArray();
            IMessageLite copy = TestAllExtensionsLite.ParseFrom(bits);

            Assert.AreNotEqual(msg, copy);

            //The lite runtime removes all unknown fields and extensions
            byte[] copybits = copy.ToByteArray();
            Assert.AreEqual(0, copybits.Length);
        }
예제 #9
0
        private static KeyValuePair <string, string> MakeWorkItem <T>(string name,
                                                                      IEnumerable <KeyValuePair <string, Action <T> > >
                                                                      builders) where T : IBuilderLite, new()
        {
            T builder = new T();

            foreach (KeyValuePair <string, Action <T> > item in builders)
            {
                item.Value(builder);
            }

            IMessageLite msg   = builder.WeakBuild();
            string       fname = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "unittest_" + name + ".dat");

            File.WriteAllBytes(fname, msg.ToByteArray());
            return
                (new KeyValuePair <string, string>(
                     String.Format("{0},{1}", msg.GetType().FullName, msg.GetType().Assembly.GetName().Name), fname));
        }
예제 #10
0
 public static byte[] Encode(IMessageLite messageLite)
 {
     return(messageLite.ToByteArray());
 }
예제 #11
0
        TMessage IRpcDispatch.CallMethod <TMessage, TBuilder>(string method, IMessageLite request,
                                                              IBuilderLite <TMessage, TBuilder> response)
        {
            int size = request.SerializedSize;

            if (size < multiPartThreshold)
            {
                return(next.CallMethod(method, request, response));
            }
            else
            {
                ByteString transaction  = ByteString.CopyFrom(Guid.NewGuid().ToByteArray());
                byte[]     requestBytes = request.ToByteArray();

                RpcMultiPartRequest.Types.RpcMessageStatus status = RpcMultiPartRequest.Types.RpcMessageStatus.CONTINUE;
                try
                {
                    int total = requestBytes.Length;
                    int amt   = multiPartThreshold - 1024; //reserved for header
                    RpcMultiPartResponse mpr = RpcMultiPartResponse.DefaultInstance;

                    for (int pos = 0; pos < total; pos += amt)
                    {
                        amt    = Math.Min(amt, total - pos);
                        status = (pos + amt) < total ? status : RpcMultiPartRequest.Types.RpcMessageStatus.COMPLETE;
                        mpr    = next.CallMethod(".multi",
                                                 RpcMultiPartRequest.CreateBuilder()
                                                 .SetTransactionId(transaction)
                                                 .SetMethodName(method)
                                                 .SetMessageStatus(status)
                                                 .SetCurrentPosition(pos)
                                                 .SetTotalBytes(total)
                                                 .SetBytesSent(amt)
                                                 .SetPayloadBytes(ByteString.CopyFrom(requestBytes, pos, amt))
                                                 .Build(),
                                                 RpcMultiPartResponse.CreateBuilder()
                                                 );
                        if (!mpr.Continue)
                        {
                            throw new InvalidDataException("The operation was canceled by the server.");
                        }
                    }
                    if (!mpr.HasResponseBytes)
                    {
                        throw new InvalidDataException("The server did not provide a response.");
                    }

                    return(response.MergeFrom(mpr.ResponseBytes.ToByteArray(), extensions).Build());
                }
                catch
                {
                    if (status == RpcMultiPartRequest.Types.RpcMessageStatus.CONTINUE)
                    {
                        try
                        {
                            next.CallMethod(".multi",
                                            RpcMultiPartRequest.CreateBuilder()
                                            .SetTransactionId(transaction)
                                            .SetMessageStatus(RpcMultiPartRequest.Types.RpcMessageStatus.CANCEL)
                                            .Build(),
                                            RpcVoid.CreateBuilder()
                                            );
                        }
                        catch (Exception e)
                        {
                            Trace.TraceWarning("Unable to cancel multi-part message: {0}, error = {1}", transaction, e);
                        }
                    }
                    throw;
                }
            }
        }