/** * Binding completes the connection and makes it available to be used with the ConnectionManager. */ private boolean bind( TcpIpConnection connection, Address remoteEndPoint, Address localEndpoint, boolean reply) { if (logger.isFinestEnabled()) { logger.finest("Binding " + connection + " to " + remoteEndPoint + ", reply is " + reply); } final Address thisAddress = ioService.getThisAddress(); if (ioService.isSocketBindAny() && !connection.isClient() && !thisAddress.equals(localEndpoint)) { logger.warning( "Wrong bind request from " + remoteEndPoint + "! This node is not requested endpoint: " + localEndpoint); connection.close(); return false; } connection.setEndPoint(remoteEndPoint); ioService.onSuccessfulConnection(remoteEndPoint); if (reply) { sendBindRequest(connection, remoteEndPoint, false); } if (checkAlreadyConnected(connection, remoteEndPoint)) { return false; } return registerConnection(remoteEndPoint, connection); }
void sendBindRequest(TcpIpConnection connection, Address remoteEndPoint, boolean replyBack) { connection.setEndPoint(remoteEndPoint); ioService.onSuccessfulConnection(remoteEndPoint); // make sure bind packet is the first packet sent to the end point. if (logger.isFinestEnabled()) { logger.finest("Sending bind packet to " + remoteEndPoint); } BindMessage bind = new BindMessage(ioService.getThisAddress(), remoteEndPoint, replyBack); byte[] bytes = ioService.getSerializationService().toBytes(bind); Packet packet = new Packet(bytes); packet.setHeader(Packet.HEADER_BIND); connection.write(packet); // now you can send anything... }