Search in sources :

Example 1 with TcpOutputStream

use of org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream in project incubator-servicecomb-java-chassis by apache.

the class TcpClientConnection method tryLogin.

protected void tryLogin() {
    if (!localSupportLogin || !remoteSupportLogin) {
        LOGGER.error("local or remote not support login, address={}, localSupportLogin={}, remoteSupportLogin={}.", socketAddress.toString(), localSupportLogin, remoteSupportLogin);
        onLoginSuccess();
        return;
    }
    this.status = Status.TRY_LOGIN;
    LOGGER.info("try login to address {}", socketAddress.toString());
    try (TcpOutputStream os = createLogin()) {
        requestMap.put(os.getMsgId(), new TcpRequest(clientConfig.getRequestTimeoutMillis(), this::onLoginResponse));
        netSocket.write(os.getBuffer());
    }
}
Also used : TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Example 2 with TcpOutputStream

use of org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayCodec method testEncodeRequest.

@Test
public void testEncodeRequest() {
    boolean status = true;
    try {
        commonMock();
        TcpOutputStream os = HighwayCodec.encodeRequest(0, invocation, operationProtobuf, null);
        Assert.assertNotNull(os);
        Assert.assertArrayEquals(TcpParser.TCP_MAGIC, os.getBuffer().getBytes(0, 7));
    } catch (Exception e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with TcpOutputStream

use of org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream in project java-chassis by ServiceComb.

the class TestHighwayCodec method testEncodeRequest.

@Test
public void testEncodeRequest() {
    boolean status = true;
    try {
        commonMock();
        Map<String, Object> args = new HashMap<>(0);
        Mockito.when(invocation.getInvocationArguments()).thenReturn(args);
        Mockito.when(requestSerializer.serialize(args)).thenReturn(new byte[0]);
        TcpOutputStream os = HighwayCodec.encodeRequest(0, invocation, operationProtobuf);
        Assert.assertNotNull(os);
        Assert.assertArrayEquals(TcpParser.TCP_MAGIC, os.getBuffer().getBytes(0, 7));
    } catch (Exception e) {
        e.printStackTrace();
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : HashMap(java.util.HashMap) TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 4 with TcpOutputStream

use of org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream in project incubator-servicecomb-java-chassis by apache.

the class TestTcpParser method test.

@Test
public void test() throws UnsupportedEncodingException {
    TcpBufferHandler output = new TcpBufferHandler() {

        @Override
        public void handle(long _msgId, Buffer _headerBuffer, Buffer _bodyBuffer) {
            msgId = _msgId;
            headerBuffer = _headerBuffer;
            bodyBuffer = _bodyBuffer;
        }
    };
    byte[] header = new byte[] { 1, 2, 3 };
    byte[] body = new byte[] { 1, 2, 3, 4 };
    TcpOutputStream os = new TcpOutputStream(1);
    os.writeInt(header.length + body.length);
    os.writeInt(header.length);
    os.write(header);
    os.write(body);
    TcpParser parser = new TcpParser(output);
    parser.handle(os.getBuffer());
    os.close();
    Assert.assertEquals(1, msgId);
    Assert.assertArrayEquals(header, headerBuffer.getBytes());
    Assert.assertArrayEquals(body, bodyBuffer.getBytes());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 5 with TcpOutputStream

use of org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream in project incubator-servicecomb-java-chassis by apache.

the class TcpClientConnection method writePackageInContext.

private void writePackageInContext() {
    for (; ; ) {
        AbstractTcpClientPackage pkg = packageQueue.poll();
        if (pkg == null) {
            break;
        }
        try (TcpOutputStream os = pkg.createStream()) {
            Buffer buf = os.getBuffer();
            netSocket.write(buf);
        }
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Aggregations

TcpOutputStream (org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)10 Buffer (io.vertx.core.buffer.Buffer)5 Test (org.junit.Test)5 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 LoginRequest (org.apache.servicecomb.transport.highway.message.LoginRequest)1 RequestHeader (org.apache.servicecomb.transport.highway.message.RequestHeader)1