Search in sources :

Example 6 with TcpOutputStream

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

the class TcpClientConnection method writeToBufferQueue.

private boolean writeToBufferQueue(AbstractTcpClientPackage tcpClientPackage) {
    // just optimize for main scenes
    if (Status.WORKING.equals(status)) {
        // encode in sender thread
        try (TcpOutputStream os = tcpClientPackage.createStream()) {
            write(os.getByteBuf());
            tcpClientPackage.finishWriteToBuffer();
        }
        return true;
    }
    return false;
}
Also used : TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Example 7 with TcpOutputStream

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

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.getMsLoginTimeout(), this::onLoginResponse));
        netSocket.write(os.getBuffer());
    }
}
Also used : TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Example 8 with TcpOutputStream

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

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);
            pkg.finishWriteToBuffer();
        }
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TcpOutputStream(org.apache.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Example 9 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 10 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);
            pkg.finishWriteToBuffer();
        }
    }
}
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)11 Buffer (io.vertx.core.buffer.Buffer)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)2 ByteBuf (io.netty.buffer.ByteBuf)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