Ejemplo n.º 1
0
        //ORIGINAL LINE: @Test public void testServerSyncUsingAddress() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void testServerSyncUsingAddress()
        {
            Connection clientConn = client.createStandaloneConnection(ip, port, 1000);
            string     remote     = clientConn.Channel.RemoteAddress.ToString();
            string     local      = clientConn.Channel.LocalAddress.ToString();

            logger.LogWarning("Client say local:" + local);
            logger.LogWarning("Client say remote:" + remote);

            for (int i = 0; i < invokeTimes; i++)
            {
                InvokeContext invokeContext = new InvokeContext();
                RequestBody   req1          = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
                string        serverres     = (string)client.invokeSync(clientConn, req1, invokeContext, 1000);
                Assert.Equal(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR);

                invokeContext.clear();
                Assert.NotNull(serverConnectProcessor.Connection);
                // only when client invoked, the remote address can be get by UserProcessor
                // otherwise, please use ConnectionEventProcessor
                string      remoteAddr = serverUserProcessor.RemoteAddr;
                RequestBody req        = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
                string      clientres  = (string)server.RpcServer.invokeSync(remoteAddr, req, invokeContext, 1000);
                Assert.Equal(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR);

                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.SERVER_LOCAL_IP));
                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.SERVER_REMOTE_IP));
                Assert.NotNull(invokeContext.get(InvokeContext.SERVER_LOCAL_PORT));
                Assert.NotNull(invokeContext.get(InvokeContext.SERVER_REMOTE_PORT));
            }

            Assert.True(serverConnectProcessor.Connected);
            Assert.Equal(1, serverConnectProcessor.ConnectTimes);
            Assert.Equal(invokeTimes, serverUserProcessor.InvokeTimes);
        }
Ejemplo n.º 2
0
        //ORIGINAL LINE: @Test public void testServerSyncUsingConnection() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void testServerSyncUsingConnection()
        {
            Connection clientConn = client.createStandaloneConnection(ip, port, 1000);

            for (int i = 0; i < invokeTimes; i++)
            {
                InvokeContext invokeContext = new InvokeContext();
                RequestBody   req1          = new RequestBody(1, RequestBody.DEFAULT_CLIENT_STR);
                string        serverres     = (string)client.invokeSync(clientConn, req1, invokeContext, 1000);
                Assert.Equal(serverres, RequestBody.DEFAULT_SERVER_RETURN_STR);

                Assert.NotNull(serverConnectProcessor.Connection);
                Connection serverConn = serverConnectProcessor.Connection;
                invokeContext.clear();
                RequestBody req       = new RequestBody(1, RequestBody.DEFAULT_SERVER_STR);
                string      clientres = (string)server.RpcServer.invokeSync(serverConn, req, invokeContext, 1000);
                Assert.Equal(clientres, RequestBody.DEFAULT_CLIENT_RETURN_STR);

                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.SERVER_LOCAL_IP));
                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.SERVER_REMOTE_IP));
                Assert.NotNull(invokeContext.get(InvokeContext.SERVER_LOCAL_PORT));
                Assert.NotNull(invokeContext.get(InvokeContext.SERVER_REMOTE_PORT));
            }

            Assert.True(serverConnectProcessor.Connected);
            Assert.Equal(1, serverConnectProcessor.ConnectTimes);
            Assert.Equal(invokeTimes, serverUserProcessor.InvokeTimes);
        }
        //ORIGINAL LINE: @Test public void testOneway() throws ThreadInterruptedException
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void testOneway()
        {
            RequestBody req = new RequestBody(2, "hello world oneway");

            for (int i = 0; i < invokeTimes; i++)
            {
                try
                {
                    InvokeContext invokeContext = new InvokeContext();
                    invokeContext.put(TESTKEY, "TESTVALUE");
                    client.oneway(addr, req, invokeContext);
                    Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_LOCAL_IP));
                    Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_REMOTE_IP));
                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT));
                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT));

                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
                    logger.LogWarning("CLIENT_CONN_CREATETIME:" + invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));

                    Assert.Equal("TESTVALUE", invokeContext.get(TESTKEY));
                    Thread.Sleep(100);
                }
                catch (RemotingException e)
                {
                    string errMsg = "RemotingException caught in oneway!";
                    logger.LogError(errMsg, e);
                    Assert.Null(errMsg);
                }
            }

            Assert.True(serverConnectProcessor.Connected);
            Assert.Equal(1, serverConnectProcessor.ConnectTimes);
            Assert.Equal(invokeTimes, serverUserProcessor.InvokeTimes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// print trace log
        /// </summary>
        /// <param name="traceId"> </param>
        /// <param name="invokeContext"> </param>
        public static void printConnectionTraceLog(ILogger logger, string traceId, InvokeContext invokeContext)
        {
            var           sourceIp   = (IPAddress)invokeContext.get(InvokeContext.CLIENT_LOCAL_IP);
            int?          sourcePort = (int?)invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT);
            var           targetIp   = (IPAddress)invokeContext.get(InvokeContext.CLIENT_REMOTE_IP);
            int?          targetPort = (int?)invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT);
            StringBuilder logMsg     = new StringBuilder();

            logMsg.Append(traceId).Append(",");
            logMsg.Append(sourceIp).Append(",");
            logMsg.Append(sourcePort).Append(",");
            logMsg.Append(targetIp).Append(",");
            logMsg.Append(targetPort);
            if (logger.IsEnabled(LogLevel.Information))
            {
                logger.LogInformation(logMsg.ToString());
            }
        }
Ejemplo n.º 5
0
            internal virtual long getAndPrintCreateConnTime(InvokeContext ctx)
            {
                long time = ctx.get(InvokeContext.CLIENT_CONN_CREATETIME) == null ? -1L : ((long?)ctx.get(InvokeContext.CLIENT_CONN_CREATETIME)).Value;

                if (time > 1500)
                {
                    outerInstance.whetherConnectTimeoutConsumedTooLong.set(true);
                }
                logger.LogWarning("CREATE CONN TIME CONSUMED: " + time);
                return(time);
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Convert application request object to remoting request command.
        /// </summary>
        /// <param name="request"> </param>
        /// <param name="conn"> </param>
        /// <param name="timeoutMillis">
        /// @return </param>
        /// <exception cref="CodecException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: protected RemotingCommand toRemotingCommand(Object request, Connection conn, InvokeContext invokeContext, int timeoutMillis) throws exception.SerializationException
        protected internal virtual RemotingCommand toRemotingCommand(object request, Connection conn, InvokeContext invokeContext, int timeoutMillis)
        {
            RpcRequestCommand command = (RpcRequestCommand)CommandFactory.createRequestCommand(request);

            if (null != invokeContext)
            {
                // set client custom serializer for request command if not null
                object clientCustomSerializer = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
                if (null != clientCustomSerializer)
                {
                    try
                    {
                        command.Serializer = ((byte?)clientCustomSerializer).Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                        throw new System.ArgumentException("Illegal custom serializer [" + clientCustomSerializer + "], the type of value should be [byte], but now is [" + clientCustomSerializer.GetType().FullName + "].");
                    }
                }

                // enable crc by default, user can disable by set invoke context `false` for key `InvokeContext.BOLT_CRC_SWITCH`
                bool?crcSwitch = (bool)invokeContext.get(InvokeContext.BOLT_CRC_SWITCH, ProtocolSwitch.CRC_SWITCH_DEFAULT_VALUE);
                if (null != crcSwitch && crcSwitch.HasValue && crcSwitch.Value)
                {
                    command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
                }
            }
            else
            {
                // enable crc by default, if there is no invoke context.
                command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
            }
            command.Timeout = timeoutMillis;
            //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
            command.RequestClass  = request.GetType();
            command.InvokeContext = invokeContext;
            command.serialize();
            logDebugInfo(command);
            return(command);
        }
Ejemplo n.º 7
0
        //ORIGINAL LINE: @Test public void testCallback() throws ThreadInterruptedException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void testCallback()
        {
            RequestBody req = new RequestBody(1, "hello world callback");
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.List<String> rets = new java.util.ArrayList<String>(1);
            IList <string> rets = new List <string>(1);

            for (int i = 0; i < invokeTimes; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountdownEvent latch = new java.util.concurrent.CountdownEvent(1);
                CountdownEvent latch         = new CountdownEvent(1);
                InvokeContext  invokeContext = new InvokeContext();
                try
                {
                    client.invokeWithCallback(addr, req, invokeContext, new InvokeCallbackAnonymousInnerClass(this, rets, latch), 1000);
                }
                catch (RemotingException e)
                {
                    if (!latch.IsSet)
                    {
                        latch.Signal();
                    }
                    string errMsg = "RemotingException caught in callback!";
                    logger.LogError(errMsg, e);
                    Assert.Null(errMsg);
                }
                try
                {
                    latch.Wait();
                }
                catch (ThreadInterruptedException e)
                {
                    string errMsg = "ThreadInterruptedException caught in callback!";
                    logger.LogError(errMsg, e);
                    Assert.Null(errMsg);
                }
                if (rets.Count == 0)
                {
                    Assert.Null("No result! Maybe exception caught!");
                }
                Assert.Equal(RequestBody.DEFAULT_SERVER_RETURN_STR, rets[0]);
                rets.Clear();

                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_LOCAL_IP));
                Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_REMOTE_IP));
                Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT));
                Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT));

                Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
                logger.LogWarning("CLIENT_CONN_CREATETIME:" + invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));

                TraceLogUtil.printConnectionTraceLog(logger, "0af4232214701387943901253", invokeContext);
            }

            Assert.True(serverConnectProcessor.Connected);
            Assert.Equal(1, serverConnectProcessor.ConnectTimes);
            Assert.Equal(invokeTimes, serverUserProcessor.InvokeTimes);
        }
        //ORIGINAL LINE: @Test public void testFuture() throws ThreadInterruptedException
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void testFuture()
        {
            RequestBody req = new RequestBody(2, "hello world future");

            for (int i = 0; i < invokeTimes; i++)
            {
                try
                {
                    InvokeContext invokeContext = new InvokeContext();
                    invokeContext.put(TESTKEY, "TESTVALUE");
                    RpcResponseFuture future = client.invokeWithFuture(addr, req, invokeContext, 3000);
                    string            res    = (string)future.get();
                    Assert.Equal(RequestBody.DEFAULT_SERVER_RETURN_STR, res);

                    Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_LOCAL_IP));
                    Assert.Equal(IPAddress.Loopback.MapToIPv6(), invokeContext.get(InvokeContext.CLIENT_REMOTE_IP));
                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT));
                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT));

                    Assert.NotNull(invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
                    logger.LogWarning("CLIENT_CONN_CREATETIME:" + invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));

                    Assert.Equal("TESTVALUE", invokeContext.get(TESTKEY));
                    TraceLogUtil.printConnectionTraceLog(logger, "0af4232214701387943901253", invokeContext);
                }
                catch (RemotingException e)
                {
                    string errMsg = "RemotingException caught in future!";
                    logger.LogError(errMsg, e);
                    Assert.Null(errMsg);
                }
                catch (ThreadInterruptedException e)
                {
                    string errMsg = "ThreadInterruptedException caught in future!";
                    logger.LogError(errMsg, e);
                    Assert.Null(errMsg);
                }
            }

            Assert.True(serverConnectProcessor.Connected);
            Assert.Equal(1, serverConnectProcessor.ConnectTimes);
            Assert.Equal(invokeTimes, serverUserProcessor.InvokeTimes);
        }
        /// <seealso cref= CustomSerializer#serializeContent(RequestCommand, InvokeContext) </seealso>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: @Override public <T extends com.alipay.remoting.rpc.RequestCommand> boolean serializeContent(T req, com.alipay.remoting.InvokeContext invokeContext) throws com.alipay.remoting.exception.SerializationException
        public override bool serializeContent(RequestCommand req, InvokeContext invokeContext)
        {
            serialFlag.set(true);
            RpcRequestCommand rpcReq = (RpcRequestCommand)req;

            if (string.Equals(SERIALTYPE1_value, (string)invokeContext.get(SERIALTYPE_KEY)))
            {
                RequestBody bd = (RequestBody)rpcReq.RequestObject;
                int         id = bd.Id;
                byte[]      msg;
                try
                {
                    msg = Encoding.UTF8.GetBytes(bd.Msg);
                    IByteBuffer bb = UnpooledByteBufferAllocator.Default.Buffer(4 + msg.Length);
                    bb.WriteInt(id);
                    bb.WriteBytes(msg);
                    rpcReq.Content = bb.Array;
                }
                catch (UnsupportedEncodingException e)
                {
                    System.Console.WriteLine(e.ToString());
                    System.Console.Write(e.StackTrace);
                }
            }
            else
            {
                try
                {
                    rpcReq.Content = Encoding.UTF8.GetBytes(UNIVERSAL_REQ);
                }
                catch (UnsupportedEncodingException e)
                {
                    System.Console.WriteLine(e.ToString());
                    System.Console.Write(e.StackTrace);
                }
            }

            return(true);
        }
Ejemplo n.º 10
0
        /// <seealso cref= CustomSerializer#deserializeContent(ResponseCommand, InvokeContext) </seealso>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public <T extends com.alipay.remoting.rpc.ResponseCommand> boolean deserializeContent(T response, com.alipay.remoting.InvokeContext invokeContext) throws com.alipay.remoting.exception.DeserializationException
        public override bool deserializeContent(ResponseCommand response, InvokeContext invokeContext)
        {
            deserialFlag.set(true);
            RpcResponseCommand rpcResp = (RpcResponseCommand)response;

            if (string.Equals(SERIALTYPE1_value, (string)invokeContext.get(SERIALTYPE_KEY)))
            {
                try
                {
                    rpcResp.ResponseObject = Encoding.UTF8.GetString(rpcResp.Content) + "RANDOM";
                }
                catch (UnsupportedEncodingException e)
                {
                    System.Console.WriteLine(e.ToString());
                    System.Console.Write(e.StackTrace);
                }
            }
            else
            {
                rpcResp.ResponseObject = UNIVERSAL_RESP;
            }
            return(true);
        }