use of org.jboss.netty.channel.ChannelFuture in project socket.io-netty by ibdknox.
the class PollingIOClient method _write.
private void _write(String message) {
if (!this.open)
return;
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
res.addHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Credentials", "true");
res.addHeader("Connection", "keep-alive");
res.setContent(ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8));
setContentLength(res, res.getContent().readableBytes());
// Send the response and close the connection if necessary.
Channel chan = ctx.getChannel();
if (chan.isOpen()) {
ChannelFuture f = chan.write(res);
if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
this.connected = false;
}
use of org.jboss.netty.channel.ChannelFuture in project socket.io-netty by ibdknox.
the class WebSocketServerHandler method sendHttpResponse.
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
// Generate an error page if response status code is not OK (200).
if (res.getStatus().getCode() != 200) {
res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
setContentLength(res, res.getContent().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.getChannel().write(res);
if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
use of org.jboss.netty.channel.ChannelFuture in project socket.io-netty by ibdknox.
the class FlashPolicyServerHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object msg = e.getMessage();
ChannelFuture f = e.getChannel().write(this.getPolicyFileContents());
f.addListener(ChannelFutureListener.CLOSE);
}
use of org.jboss.netty.channel.ChannelFuture in project motan by weibocom.
the class NettyChannel method open.
@Override
public boolean open() {
if (isAvailable()) {
LoggerUtil.warn("the channel already open, local: " + localAddress + " remote: " + remoteAddress + " url: " + nettyClient.getUrl().getUri());
return true;
}
ChannelFuture channelFuture = null;
try {
synchronized (this) {
channelFuture = nettyClient.getBootstrap().connect(new InetSocketAddress(nettyClient.getUrl().getHost(), nettyClient.getUrl().getPort()));
long start = System.currentTimeMillis();
int timeout = nettyClient.getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
if (timeout <= 0) {
throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
// 不去依赖于connectTimeout
boolean result = channelFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS);
boolean success = channelFuture.isSuccess();
if (result && success) {
channel = channelFuture.getChannel();
if (channel.getLocalAddress() != null && channel.getLocalAddress() instanceof InetSocketAddress) {
localAddress = (InetSocketAddress) channel.getLocalAddress();
}
state = ChannelState.ALIVE;
return true;
}
boolean connected = false;
if (channelFuture.getChannel() != null) {
connected = channelFuture.getChannel().isConnected();
}
if (channelFuture.getCause() != null) {
channelFuture.cancel();
throw new MotanServiceException("NettyChannel failed to connect to server, url: " + nettyClient.getUrl().getUri() + ", result: " + result + ", success: " + success + ", connected: " + connected, channelFuture.getCause());
} else {
channelFuture.cancel();
throw new MotanServiceException("NettyChannel connect to server timeout url: " + nettyClient.getUrl().getUri() + ", cost: " + (System.currentTimeMillis() - start) + ", result: " + result + ", success: " + success + ", connected: " + connected, false);
}
}
} catch (MotanServiceException e) {
throw e;
} catch (Exception e) {
if (channelFuture != null) {
channelFuture.getChannel().close();
}
throw new MotanServiceException("NettyChannel failed to connect to server, url: " + nettyClient.getUrl().getUri(), e);
} finally {
if (!state.isAliveState()) {
// 为避免死锁,client错误计数方法需在同步块外调用。
nettyClient.incrErrorCount();
}
}
}
use of org.jboss.netty.channel.ChannelFuture in project motan by weibocom.
the class NettyChannel method request.
@Override
public Response request(Request request) throws TransportException {
int timeout = nettyClient.getUrl().getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue());
if (timeout <= 0) {
throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
ResponseFuture response = new DefaultResponseFuture(request, timeout, this.nettyClient.getUrl());
this.nettyClient.registerCallback(request.getRequestId(), response);
ChannelFuture writeFuture = this.channel.write(request);
boolean result = writeFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS);
if (result && writeFuture.isSuccess()) {
MotanFrameworkUtil.logEvent(request, MotanConstants.TRACE_CSEND, System.currentTimeMillis());
response.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess() || (future.isDone() && ExceptionUtil.isBizException(future.getException()))) {
// 成功的调用
nettyClient.resetErrorCount();
} else {
// 失败的调用
nettyClient.incrErrorCount();
}
}
});
return response;
}
writeFuture.cancel();
response = this.nettyClient.removeCallback(request.getRequestId());
if (response != null) {
response.cancel();
}
// 失败的调用
nettyClient.incrErrorCount();
if (writeFuture.getCause() != null) {
throw new MotanServiceException("NettyChannel send request to server Error: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request), writeFuture.getCause());
} else {
throw new MotanServiceException("NettyChannel send request to server Timeout: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request), false);
}
}
Aggregations