Java Code Examples for io.vertx.core.net.NetSocket#handler()

The following examples show how to use io.vertx.core.net.NetSocket#handler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxRLPxService.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
private void receiveMessage(NetSocket netSocket) {
  netSocket.handler(new Handler<>() {

    private RLPxConnection conn;

    private DefaultWireConnection wireConnection;

    @Override
    public void handle(Buffer buffer) {
      if (conn == null) {
        conn = RLPxConnectionFactory
            .respondToHandshake(
                Bytes.wrapBuffer(buffer),
                keyPair,
                bytes -> netSocket.write(Buffer.buffer(bytes.toArrayUnsafe())));
        if (wireConnection == null) {
          this.wireConnection = createConnection(conn, netSocket, AsyncResult.incomplete());
        }
      } else {
        conn.stream(Bytes.wrapBuffer(buffer), wireConnection::messageReceived);
      }
    }
  });
}
 
Example 2
Source File: SecureScuttlebuttVertxClient.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
NetSocketClientHandler(
    NetSocket socket,
    Signature.PublicKey remotePublicKey,
    ClientHandlerFactory<T> handlerFactory,
    CompletableAsyncResult<T> completionHandle) {
  this.socket = socket;
  this.handshakeClient = SecureScuttlebuttHandshakeClient.create(keyPair, networkIdentifier, remotePublicKey);
  this.handlerFactory = handlerFactory;
  this.completionHandle = completionHandle;
  socket.closeHandler(res -> {
    if (handler != null) {
      handler.streamClosed();
    }
  });
  socket.exceptionHandler(e -> logger.error(e.getMessage(), e));
  socket.handler(this::handle);
  socket.write(Buffer.buffer(handshakeClient.createHello().toArrayUnsafe()));
}
 
Example 3
Source File: VertxRLPxService.java    From cava with Apache License 2.0 6 votes vote down vote up
private void receiveMessage(NetSocket netSocket) {
  netSocket.handler(new Handler<Buffer>() {

    private RLPxConnection conn;

    private DefaultWireConnection wireConnection;

    @Override
    public void handle(Buffer buffer) {
      if (conn == null) {
        conn = RLPxConnectionFactory.respondToHandshake(
            Bytes.wrapBuffer(buffer),
            keyPair,
            bytes -> netSocket.write(Buffer.buffer(bytes.toArrayUnsafe())));
        if (wireConnection == null) {
          this.wireConnection = createConnection(conn, netSocket);
          wireConnection.handleConnectionStart();
        }
      } else {
        conn.stream(Bytes.wrapBuffer(buffer), wireConnection::messageReceived);
      }
    }
  });
}
 
Example 4
Source File: SecureScuttlebuttVertxClient.java    From cava with Apache License 2.0 6 votes vote down vote up
NetSocketClientHandler(
    Logger logger,
    NetSocket socket,
    Signature.PublicKey remotePublicKey,
    ClientHandlerFactory<T> handlerFactory,
    CompletableAsyncResult<T> completionHandle) {
  this.logger = logger;
  this.socket = socket;
  this.handshakeClient = SecureScuttlebuttHandshakeClient.create(keyPair, networkIdentifier, remotePublicKey);
  this.handlerFactory = handlerFactory;
  this.completionHandle = completionHandle;
  socket.closeHandler(res -> {
    if (handler != null) {
      handler.streamClosed();
    }
  });
  socket.exceptionHandler(e -> logger.error(e.getMessage(), e));
  socket.handler(this::handle);
  socket.write(Buffer.buffer(handshakeClient.createHello().toArrayUnsafe()));
}
 
Example 5
Source File: TcpServerConnection.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public void init(NetSocket netSocket) {
  // currently, socket always be NetSocketImpl
  this.initNetSocket((NetSocketImpl) netSocket);

  String remoteAddress = netSocket.remoteAddress().toString();
  LOGGER.info("connect from {}, in thread {}",
      remoteAddress,
      Thread.currentThread().getName());
  netSocket.exceptionHandler(e -> {
    LOGGER.error("disconected from {}, in thread {}, cause {}",
        remoteAddress,
        Thread.currentThread().getName(),
        e.getMessage());
  });
  netSocket.closeHandler(Void -> {
    LOGGER.error("disconected from {}, in thread {}",
        remoteAddress,
        Thread.currentThread().getName());
  });

  netSocket.handler(splitter);
}
 
Example 6
Source File: ProtonTransport.java    From vertx-proton with Apache License 2.0 6 votes vote down vote up
ProtonTransport(Connection connection, Vertx vertx, NetClient netClient, NetSocket socket,
                ProtonSaslAuthenticator authenticator, ProtonTransportOptions options) {
  this.connection = connection;
  this.vertx = vertx;
  this.netClient = netClient;
  this.socket = socket;
  int maxFrameSize = options.getMaxFrameSize() == 0 ? DEFAULT_MAX_FRAME_SIZE : options.getMaxFrameSize();
  transport.setMaxFrameSize(maxFrameSize);
  transport.setOutboundFrameSizeLimit(maxFrameSize);
  transport.setEmitFlowEventOnSend(false); // TODO: make configurable
  transport.setIdleTimeout(2 * options.getHeartbeat());
  ((TransportInternal) transport).setUseReadOnlyOutputBuffer(false);
  if (authenticator != null) {
    authenticator.init(this.socket, (ProtonConnection) this.connection.getContext(), transport);
  }
  this.authenticator = authenticator;
  transport.bind(connection);
  connection.collect(collector);
  socket.endHandler(this::handleSocketEnd);
  socket.handler(this::handleSocketBuffer);
}
 
Example 7
Source File: SecureScuttlebuttVertxServer.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
void handle(NetSocket netSocket) {
  this.netSocket = netSocket;
  netSocket.closeHandler(res -> {
    if (handler != null) {
      handler.streamClosed();
    }
  });

  netSocket.handler(this::handleMessage);
}
 
Example 8
Source File: StratumServer.java    From besu with Apache License 2.0 5 votes vote down vote up
private void handle(final NetSocket socket) {
  StratumConnection conn =
      new StratumConnection(
          protocols, socket::close, bytes -> socket.write(Buffer.buffer(bytes)));
  socket.handler(conn::handleBuffer);
  socket.closeHandler(conn::close);
}
 
Example 9
Source File: SecureScuttlebuttVertxServer.java    From cava with Apache License 2.0 5 votes vote down vote up
void handle(NetSocket netSocket) {
  this.netSocket = netSocket;
  netSocket.closeHandler(res -> {
    if (handler != null) {
      handler.streamClosed();
    }
  });

  netSocket.handler(this::handleMessage);
}
 
Example 10
Source File: VertxEcho.java    From vertx-in-action with MIT License 5 votes vote down vote up
private static void handleNewClient(NetSocket socket) {
  numberOfConnections++;
  socket.handler(buffer -> {
    socket.write(buffer);
    if (buffer.toString().endsWith("/quit\n")) {
      socket.close();
    }
  });
  socket.closeHandler(v -> numberOfConnections--);
}
 
Example 11
Source File: TcpClientConnection.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void onConnectSuccess(NetSocket socket) {
  LOGGER.info("connected to address {} success in thread {}.",
      socketAddress.toString(),
      Thread.currentThread().getName());
  // currently, socket always be NetSocketImpl
  this.initNetSocket((NetSocketImpl) socket);
  socket.handler(new TcpParser(this::onReply));

  socket.exceptionHandler(this::onException);
  socket.closeHandler(this::onClosed);

  // 开始登录
  tryLogin();
}
 
Example 12
Source File: TelnetSocketHandler.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(final NetSocket socket) {
  TelnetHandler handler = factory.get();
  final VertxTelnetConnection connection = new VertxTelnetConnection(handler, Vertx.currentContext(), socket);
  socket.handler(event -> connection.receive(event.getBytes()));
  socket.closeHandler(event -> connection.onClose());
  connection.onInit();
}
 
Example 13
Source File: TcpEventBusBridgeImpl.java    From vertx-tcp-eventbus-bridge with Apache License 2.0 4 votes vote down vote up
private void handler(NetSocket socket) {

    final Map<String, MessageConsumer<?>> registry = new ConcurrentHashMap<>();
    final Map<String, Message<JsonObject>> replies = new ConcurrentHashMap<>();

    // create a protocol parser
    final FrameParser parser = new FrameParser(res -> {
      if (res.failed()) {
        // could not parse the message properly
        log.error(res.cause());
        return;
      }

      final JsonObject msg = res.result();

      // short reference

      // default to message
      final String type = msg.getString("type", "message");
      final String address = msg.getString("address");
      BridgeEventType eventType = parseType(type);
      checkCallHook(() -> new BridgeEventImpl(eventType, msg, socket),
        () -> {
          if (eventType != BridgeEventType.SOCKET_PING && address == null) {
            sendErrFrame("missing_address", socket);
            log.error("msg does not have address: " + msg.toString());
            return;
          }
          doSendOrPub(socket, address, msg, registry, replies);
        },
        () -> sendErrFrame("blocked by bridgeEvent handler", socket));
    });

    socket.handler(parser);

    socket.exceptionHandler(t -> {
      log.error(t.getMessage(), t);
      registry.values().forEach(MessageConsumer::unregister);
      registry.clear();
      socket.close();
    });

    socket.endHandler(v -> {
      registry.values().forEach(MessageConsumer::unregister);
      registry.clear();
    });
  }