Refine search
/** * Echo back the closing frame and close the connection * * @param channel * Channel * @param frame * Web Socket frame that was received */ @Override public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) { ChannelFuture f = channel.write(frame); f.addListener(ChannelFutureListener.CLOSE); return f; }
/** * Echo back the closing frame and close the connection * * @param channel * Channel * @param frame * Web Socket frame that was received */ @Override public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) { ChannelFuture f = channel.write(frame); f.addListener(ChannelFutureListener.CLOSE); return f; }
/** * Echo back the closing frame and close the connection * * @param channel * Channel * @param frame * Web Socket frame that was received */ @Override public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) { ChannelFuture future = channel.write(frame); future.addListener(ChannelFutureListener.CLOSE); return future; } }
protected void closeChannelWithLoginFailure(Channel channel) { // Close the connection as soon as the error message is sent. channel.write(eventToFrame(Events.LOG_IN_FAILURE, null)).addListener( ChannelFutureListener.CLOSE); }
/** * Helper method which will close the channel after writing * {@link Events#LOG_IN_FAILURE} to remote connection. * * @param channel * The tcp connection to remote machine that will be closed. */ private void closeChannelWithLoginFailure(Channel channel) { ChannelFuture future = channel.write(NettyUtils .createBufferForOpcode(Events.LOG_IN_FAILURE)); future.addListener(ChannelFutureListener.CLOSE); }
/** * This method will write an event to the channel and then add a close * listener which will close it after the write has completed. * * @param channel * @param event */ public void closeAfterFlushingPendingWrites(Channel channel, Event event) { if (channel.isConnected()) { channel.write(event).addListener(ChannelFutureListener.CLOSE); } else { System.err.println("Unable to write the Event :" + event + " to socket as channel is ot connected"); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { ChannelFuture f = null; if (null != policyFile) { f = e.getChannel().write(policyFile); } else { f = e.getChannel().write(this.getPolicyFileContents()); } f.addListener(ChannelFutureListener.CLOSE); }
private void protocolViolation(Channel channel, CorruptedFrameException ex) throws CorruptedFrameException { checkpoint(State.CORRUPT); if (channel.isConnected()) { channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } throw ex; }
/** * Writes an the {@link Events#DISCONNECT} to the client, flushes * all the pending writes and closes the channel. * */ @Override public void close() { LOG.debug("Going to close tcp connection in class: {}", this .getClass().getName()); Event event = Events.event(null, Events.DISCONNECT); if (channel.isConnected()) { channel.write(event).addListener(ChannelFutureListener.CLOSE); } else { channel.close(); LOG.trace("Unable to write the Event {} with type {} to socket", event, event.getType()); } }
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
protected void flushRequest(Channel channel, final NettyMessage requests) { if (requests == null || requests.isEmpty()) return; Long batchSize = (long) requests.getEncodedLength(); pendings.incrementAndGet(); if (this.enableNettyMetrics && sendSpeed != null) { sendSpeed.update(batchSize); } NettyMetricInstance.totalSendSpeed.update(batchSize); if (MetricUtils.metricAccurateCal) { NettyMetricInstance.batchSizeWorkerHistogram.update(batchSize); } ChannelFuture future = channel.write(requests); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { pendings.decrementAndGet(); if (!future.isSuccess()) { Channel channel = future.getChannel(); if (!isClosed()) { LOG.info("Failed to send requests to " + name + ": " + channel.toString() + ":", future.getCause()); } if (null != channel) { exceptionChannel(channel); } } } }); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { if (e.getCause() instanceof WebSocketHandshakeException) { DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST); response.setContent(ChannelBuffers.wrappedBuffer(e.getCause().getMessage().getBytes())); ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.getChannel().close(); } }
public void handleGameRoomJoin(Player player, Channel channel, String refKey) { GameRoom gameRoom = lookupService.gameRoomLookup(refKey); if (null != gameRoom) { PlayerSession playerSession = gameRoom.createPlayerSession(player); gameRoom.onLogin(playerSession); String reconnectKey = (String)idGeneratorService .generateFor(playerSession.getClass()); playerSession.setAttribute(JetConfig.RECONNECT_KEY, reconnectKey); playerSession.setAttribute(JetConfig.RECONNECT_REGISTRY, reconnectRegistry); LOG.trace("Sending GAME_ROOM_JOIN_SUCCESS to channel {}", channel.getId()); ChannelFuture future = channel.write(eventToFrame( Events.GAME_ROOM_JOIN_SUCCESS, reconnectKey)); connectToGameRoom(gameRoom, playerSession, future); } else { // Write failure and close channel. ChannelFuture future = channel.write(eventToFrame( Events.GAME_ROOM_JOIN_FAILURE, null)); future.addListener(ChannelFutureListener.CLOSE); LOG.error( "Invalid ref key provided by client: {}. Channel {} will be closed", refKey, channel.getId()); } }
final DataOutputBuffer dob = new DataOutputBuffer(); header.write(dob); ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength())); final File spillfile = new File(mapOutputInfo.mapOutputFileName.toString()); readaheadPool, spillfile.getAbsolutePath(), shuffleBufferSize, shuffleTransferToAllowed, canEvictAfterTransfer); writeFuture = ch.write(partition); writeFuture.addListener(new ChannelFutureListener() { manageOsCache, readaheadLength, readaheadPool, spillfile.getAbsolutePath()); writeFuture = ch.write(chunk);
break; lastWriteFuture = channel.write(buffer); channel.close(); } else { lastWriteFuture.addListener(ChannelFutureListener.CLOSE);
response, keepAliveParam, mapOutputInfoMap); } catch(IOException e) { ch.write(response); LOG.error("Shuffle error in populating headers :", e); String errorMessage = getErrorMessage(e); return; ch.write(response); lastMap.addListener(ChannelFutureListener.CLOSE); } else { lastMap.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception {
public void handleGameRoomJoin(Player player, Channel channel, ChannelBuffer buffer) { String refKey = NettyUtils.readString(buffer); GameRoom gameRoom = lookupService.gameRoomLookup(refKey); if(null != gameRoom) { PlayerSession playerSession = gameRoom.createPlayerSession(player); gameRoom.onLogin(playerSession); String reconnectKey = (String)idGeneratorService .generateFor(playerSession.getClass()); playerSession.setAttribute(JetConfig.RECONNECT_KEY, reconnectKey); playerSession.setAttribute(JetConfig.RECONNECT_REGISTRY, reconnectRegistry); LOG.trace("Sending GAME_ROOM_JOIN_SUCCESS to channel {}", channel.getId()); ChannelBuffer reconnectKeyBuffer = ChannelBuffers.wrappedBuffer(NettyUtils.createBufferForOpcode(Events.GAME_ROOM_JOIN_SUCCESS), NettyUtils.writeString(reconnectKey)); ChannelFuture future = channel.write(reconnectKeyBuffer); connectToGameRoom(gameRoom, playerSession, future); loginUdp(playerSession, buffer); } else { // Write failure and close channel. ChannelFuture future = channel.write(NettyUtils.createBufferForOpcode(Events.GAME_ROOM_JOIN_FAILURE)); future.addListener(ChannelFutureListener.CLOSE); LOG.error("Invalid ref key provided by client: {}. Channel {} will be closed",refKey,channel.getId()); } }
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.headers().add(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.headers().add(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.headers().add(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
private void processRequest(final ChannelHandlerContext ctx, final Request request, long processStartTime) { Object result; try { result = messageHandler.handle(serverChannel, request); } catch (Exception e) { LoggerUtil.error("NettyChannelHandler processRequest fail!request:" + MotanFrameworkUtil.toString(request), e); result = MotanFrameworkUtil.buildErrorResponse(request.getRequestId(), new MotanServiceException("process request fail. errmsg:" + e.getMessage())); } MotanFrameworkUtil.logRequestEvent(request.getRequestId(), "after invoke biz method: " + MotanFrameworkUtil.getFullMethodString(request), System.currentTimeMillis()); DefaultResponse response = null; if (!(result instanceof DefaultResponse)) { response = new DefaultResponse(result); } else { response = (DefaultResponse) result; } response.setRequestId(request.getRequestId()); response.setProcessTime(System.currentTimeMillis() - processStartTime); if (ctx.getChannel().isConnected()) { ChannelFuture channelFuture = ctx.getChannel().write(response); if (channelFuture != null && request instanceof TraceableRequest) { channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { MotanFrameworkUtil.logRequestEvent(request.getRequestId(), "after send rpc response: " + MotanFrameworkUtil.getFullMethodString(request), System.currentTimeMillis()); ((TraceableRequest) request).onFinish(); } }); } } }
/** * Upgrades the connection and send the handshake response. */ protected ChannelFuture writeHandshakeResponse( Channel channel, HttpResponse res, ChannelHandler encoder, ChannelHandler decoder) { final ChannelPipeline p = channel.getPipeline(); if (p.get(HttpChunkAggregator.class) != null) { p.remove(HttpChunkAggregator.class); } final String httpEncoderName = p.getContext(HttpResponseEncoder.class).getName(); p.addAfter(httpEncoderName, "wsencoder", encoder); p.get(HttpRequestDecoder.class).replace("wsdecoder", decoder); final ChannelFuture future = channel.write(res); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { p.remove(httpEncoderName); } }); return future; }