本文整理汇总了Java中io.netty.channel.ChannelPromise类的典型用法代码示例。如果您正苦于以下问题:Java ChannelPromise类的具体用法?Java ChannelPromise怎么用?Java ChannelPromise使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelPromise类属于io.netty.channel包,在下文中一共展示了ChannelPromise类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bind
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise)
throws Exception {
if (localAddress == slowAddr) {
// delay binding 1 second.
eventLoop.schedule(
() -> {
try {
super.bind(ctx, localAddress, promise);
} catch (Exception e) {
// shouldn't happen.
}
},
1,
TimeUnit.SECONDS);
} else {
super.bind(ctx, localAddress, promise);
}
}
示例2: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
LOGGER.info("[Client ({})] <= [Server ({})] : {}",
connectionInfo.getClientAddr(), connectionInfo.getServerAddr(),
msg);
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
if (!response.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
if (streams.isEmpty()) {
throw new IllegalStateException("No active streams");
}
response.headers().add(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
streams.poll());
}
}
ctx.write(msg, promise);
}
示例3: createRpcClientRTEDuringConnectionSetup
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
protected AsyncRpcClient createRpcClientRTEDuringConnectionSetup(Configuration conf) {
setConf(conf);
return new AsyncRpcClient(conf, new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
throws Exception {
promise.setFailure(new RuntimeException("Injected fault"));
}
});
}
});
}
示例4: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write ( final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise ) throws Exception
{
if ( msg instanceof OpenResponse )
{
writeOpenResponse ( ctx, (OpenResponse)msg, promise );
}
else if ( msg instanceof SyslogResponse )
{
writeGenericResponse ( ctx, (AbstractGenericResponse)msg, promise );
}
else if ( msg instanceof ServerCloseMessage )
{
ctx.write ( new Frame ( 0, "serverclose", (ByteBuf)null ) );
}
else
{
super.write ( ctx, msg, promise );
}
}
示例5: writeOpenResponse
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
protected void writeOpenResponse ( final ChannelHandlerContext ctx, final OpenResponse msg, final ChannelPromise promise )
{
final StringBuilder sb = new StringBuilder ();
int i = 0;
for ( final Map.Entry<String, String> entry : msg.getOffers ().entrySet () )
{
if ( i > 0 )
{
sb.append ( Constants.LF_STRING );
}
sb.append ( entry.getKey () );
if ( entry.getValue () != null )
{
sb.append ( '=' );
sb.append ( entry.getValue () );
}
i++;
}
ctx.write ( createCommonResponse ( msg.getTransactionId (), msg.getCode (), msg.getMessage (), sb ) );
}
示例6: processFrame
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
protected void processFrame ( final ChannelHandlerContext ctx, final Frame frame, final ChannelPromise promise )
{
final int length = frame.getData () == null ? 0 : frame.getData ().readableBytes ();
final ByteBuf data = ctx.alloc ().buffer ();
data.writeBytes ( String.format ( "%s", frame.getTransactionId () ).getBytes ( TXNR_CHARSET ) );
data.writeByte ( Constants.SP );
data.writeBytes ( frame.getCommand ().getBytes ( COMMAND_CHARSET ) );
data.writeByte ( Constants.SP );
data.writeBytes ( String.format ( "%s", length ).getBytes ( LENGTH_CHARSET ) );
if ( length > 0 )
{
data.writeByte ( Constants.SP );
data.writeBytes ( frame.getData () );
}
if ( frame.getData () != null )
{
frame.getData ().release ();
}
data.writeByte ( Constants.LF );
ctx.write ( data );
}
示例7: handleMessageWrite
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
private void handleMessageWrite ( final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise )
{
final ByteBuf data = encode ( ctx, msg );
if ( data == null )
{
// ignore
return;
}
// if the buffer is full
if ( this.ackBuffer.isFull () )
{
logger.trace ( "Store message for later transmission" );
// ... store now and re-try later
this.messageBuffer.add ( new WriteEvent ( ctx, data, promise ) );
}
else
{
writeMessageToChannel ( ctx, data, promise );
}
}
示例8: writeMessageToChannel
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
private void writeMessageToChannel ( final ChannelHandlerContext ctx, final ByteBuf data, final ChannelPromise promise )
{
final int seq = this.ackBuffer.addMessage ( data );
if ( promise == null )
{
ctx.write ( new InformationTransfer ( seq, this.receiveCounter, data ) );
}
else
{
ctx.write ( new InformationTransfer ( seq, this.receiveCounter, data ), promise );
}
logger.trace ( "Enqueued message as {} : {}", seq, data );
// we can stop timer #2 here ... will be restarted by receive
this.timer2.stop ();
}
示例9: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof DeferredMessage) {
DeferredMessage defMsg = (DeferredMessage)msg;
if (defMsg.isPreEncoded()) {
// Write pre-encoded message.
ctx.write(defMsg.encoded(), promise);
} else {
super.write(ctx, defMsg.encoded(), promise);
}
} else if (msg instanceof ByteBuf) {
ctx.write(msg, promise);
} else {
super.write(ctx, msg, promise);
}
}
示例10: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
IMessage message = (IMessage) msg;
if (message.getMsgId() >= 0x40 && message.getMsgId() <= 0x4F) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(tcpMsgBuilder.buildResponseMsg((BaseTcpResponseMsg) message)));
} else if (message.getMsgId() >= 0x60 && message.getMsgId() <= 0x6F) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(tcpMsgBuilder.buildResponseMsg((BaseTcpResponseMsg) message)));
} else if (message.getMsgId() >= -128 && message.getMsgId() <= -113) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(tcpMsgBuilder.buildResponseMsg((BaseTcpResponseMsg) message)));
} else if (message.getMsgId() == MsgType.INITIALIZE_DEVICE_RESPONSE_CARD) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(tcpMsgBuilder.buildInitConfirmCardNumber((TcpMsgInitResponseCardNumber) message)));
}
if (message.getMsgId() == 0x00) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(tcpMsgBuilder.buildResponseMsg((BaseTcpResponseMsg) message)));
}
if (sendListener != null) {
sendListener.send();
}
}
示例11: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
SendableMsgContainer message = (SendableMsgContainer) msg;
ByteBuf byteBuf = ctx.alloc().buffer();
FrameMajorHeader header = message.getFrameHeader();
byteBuf.writeByte(FrameSetting.MAJOR_FRAME_HEAD_1);
byteBuf.writeByte(FrameSetting.MAJOR_FRAME_HEAD_2);
byteBuf.writeByte(header.getGroupId());
byteBuf.writeByte(header.getMsgId());
byteBuf.writeByte(header.getDeviceId());
byteBuf.writeByte(header.getBackupMsg());
byteBuf.writeShort(header.getDataLength());
List<ByteBuf> dataList = message.getDataList();
dataList.forEach(byteBuf::writeBytes);
ctx.writeAndFlush(byteBuf);
}
示例12: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (!(msg instanceof Packet)) {
throw new RuntimeException("Object is not a *Packet");
}
Packet packet = (Packet) msg;
int packetId = packet.getPacketInfo().id();
int packetSize = packet.getPacketSize() + ProtocolSizer.ofVarInt(packetId);
int bufferSize = packetSize + ProtocolSizer.ofVarInt(packetSize);
ByteBuf unpooledByteBuf = Unpooled.buffer(bufferSize);
PacketSerializer serializer = new PacketSerializer(unpooledByteBuf);
UnknownPandaServer.getLogger().info("Sending packet: " + packet.getClass().getSimpleName() + " & id: 0x" + Integer.toHexString(packetId) + " & size: " + packetSize);
serializer.writeVarInt(packetSize);
serializer.writeVarInt(packetId);
packet.send(serializer);
ctx.writeAndFlush(serializer, promise);
}
示例13: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
public void write(ChannelHandlerContext context, Object packet, ChannelPromise promise) throws Exception
{
BaseComponent[] components = interceptor.getComponents(packet);
if(components != null)
{
boolean allowed = isAllowed(components);
boolean paused = isPaused();
if(!paused || !allowed)
{
while(messageQueue.size() > 20)
messageQueue.remove();
messageQueue.add(components);
}
if(paused && !allowed)
return;
}
super.write(context, packet, promise);
}
示例14: write
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
{
if (msg instanceof FMLProxyPacket)
{
if (side == Side.CLIENT)
{
//Client to server large packets are not supported to prevent client being bad.
ctx.write(((FMLProxyPacket) msg).toC17Packet(), promise);
}
else
{
List<Packet<INetHandlerPlayClient>> parts = ((FMLProxyPacket)msg).toS3FPackets();
for (Packet<INetHandlerPlayClient> pkt : parts)
{
ctx.write(pkt, promise);
}
}
}
else
{
ctx.write(msg, promise);
}
}
示例15: disconnect
import io.netty.channel.ChannelPromise; //导入依赖的package包/类
@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
log.info("NETTY CLIENT PIPELINE: DISCONNECT {}", remoteAddress);
closeChannel(ctx.channel());
super.disconnect(ctx, promise);
if (NettyRemotingClient.this.channelEventListener != null) {
NettyRemotingClient.this.putNettyEvent(new NettyEvent(NettyEventType.CLOSE, remoteAddress.toString(), ctx.channel()));
}
}