List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(ByteBuffer... buffers)
From source file:books.netty.frame.delimiter.EchoServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO// w w w.j a va 2s.c o m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoServerHandler()); } }); // ??? ChannelFuture f = b.bind(port).sync(); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:br.unifei.edu.eco009.steamlansync.cache.HttpChunkContents.java
public FullHttpResponse getFullHttpResponse() { DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(bytes)); HttpHeaders head = response.headers(); for (Entry<String, String> entry : headers.entrySet()) { head.add(entry.getKey(), entry.getValue()); }/*from w w w .java2 s .co m*/ return response; }
From source file:br.unifei.edu.eco009.steamlansync.proxy.HttpResponseContent.java
public FullHttpResponse getFullResponse() { ByteBuf buffer = Unpooled.copiedBuffer(bytes); FullHttpResponse newResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buffer);/* w ww. ja v a 2s.co m*/ if (length != null) newResponse.headers().add("Content-Length", length); if (steam_sid != null) newResponse.headers().add("x-steam-sid", steam_sid); if (crc != null) newResponse.headers().add("x-content-crc", crc); newResponse.headers().add("content-type", "application/x-steam-chunk"); return newResponse.retain(); }
From source file:buildcraft.lib.tile.TileBC_Neptune.java
License:Mozilla Public License
@Override public void handleUpdateTag(NBTTagCompound tag) { super.readFromNBT(tag); byte[] bytes = tag.getByteArray("d"); ByteBuf buf = Unpooled.copiedBuffer(bytes); try {//from ww w .j a v a 2 s . c om int id = buf.readUnsignedShort(); readPayload(id, new PacketBufferBC(buf), world.isRemote ? Side.CLIENT : Side.SERVER, null); // Make sure that we actually read the entire message rather than just discarding it MessageUtil.ensureEmpty(buf, world.isRemote, getClass().getSimpleName()); } catch (IOException e) { throw new RuntimeException(e); } spawnReceiveParticles(); }
From source file:cc.sharper.netty.delimiter.EchoClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO//from www . jav a 2s .c o m EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { //? ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:ccwihr.client.t2.Http2Client.java
License:Apache License
public static void main(String[] args) throws Exception { // // Configure SSL. // final SslContext sslCtx; // if (SSL) { // SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK; // sslCtx = SslContextBuilder.forClient() // .sslProvider(provider) // /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification. // * Please refer to the HTTP/2 specification for cipher requirements. */ // .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE) // .trustManager(InsecureTrustManagerFactory.INSTANCE) // .applicationProtocolConfig(new ApplicationProtocolConfig( // Protocol.ALPN, // // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers. // SelectorFailureBehavior.NO_ADVERTISE, // // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers. // SelectedListenerFailureBehavior.ACCEPT, // ApplicationProtocolNames.HTTP_2, // ApplicationProtocolNames.HTTP_1_1)) // .build(); // } else { // sslCtx = null; // }/*from w w w .j av a 2 s . c om*/ EventLoopGroup workerGroup = new NioEventLoopGroup(); // Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE); Http2ClientInitializer initializer = new Http2ClientInitializer(null, Integer.MAX_VALUE); try { // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(HOST, PORT); b.handler(initializer); // Start the client. Channel channel = b.connect().syncUninterruptibly().channel(); System.out.println("Connected to [" + HOST + ':' + PORT + ']'); // Wait for the HTTP/2 upgrade to occur. Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler(); http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS); HttpResponseHandler responseHandler = initializer.responseHandler(); int streamId = 3; HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP; AsciiString hostName = new AsciiString(HOST + ':' + PORT); System.err.println("Sending request(s)..."); if (URL != null) { // Create a simple GET request. FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL); request.headers().add(HttpHeaderNames.HOST, hostName); request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name()); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE); responseHandler.put(streamId, channel.writeAndFlush(request), channel.newPromise()); streamId += 2; } if (URL2 != null) { // Create a simple POST request with a body. FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2, Unpooled.copiedBuffer(URL2DATA.getBytes(CharsetUtil.UTF_8))); request.headers().add(HttpHeaderNames.HOST, hostName); request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name()); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE); responseHandler.put(streamId, channel.writeAndFlush(request), channel.newPromise()); streamId += 2; } responseHandler.awaitResponses(5, TimeUnit.SECONDS); System.out.println("Finished HTTP/2 request(s)"); // Wait until the connection is closed. channel.close().syncUninterruptibly(); } finally { workerGroup.shutdownGracefully(); } }
From source file:cl.uandes.so.server.LoginServerHandler.java
@Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { //if(packet.content().readableBytes() < 24) { // return; //}//from w w w . j av a 2 s . co m // TEST Cliente: echo -e "\xff\xff\xff\xff\xff\xff\xff\xff\x41\x67\x65\x74\x41\x64\x64\x72\x65\x73\x73\x26\x50\x6f\x72\x74" | nc -4 -w 1 -u 127.0.0.1 9990 String expected_payload = "ffffffffffffffff416765744164647265737326506f7274"; //System.err.println(packet); //System.out.println(packet.toString()); byte[] payload = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x41, (byte) 0x67, (byte) 0x65, (byte) 0x74, (byte) 0x41, (byte) 0x64, (byte) 0x64, (byte) 0x72, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x26, (byte) 0x50, (byte) 0x6f, (byte) 0x72, (byte) 0x74 }; ByteBuf data = packet.content().readBytes(packet.content().readableBytes()); System.out.println(data.array().length); byte[] header = { (byte) 0xff, (byte) 0x52 }; System.out.println(Utils.byteArray2Hex(data.array())); if (Utils.byteArray2Hex(data.array()).equals(Utils.byteArray2Hex(payload))) { System.out.println("Got login of client, sending multicast group address (header+multicastgroup)"); ctx.write(new DatagramPacket( Unpooled.copiedBuffer(ArrayUtils.addAll(header, multicastgroup.getBytes())), packet.sender())); } }
From source file:co.freeside.betamax.proxy.netty.NettyMessageAdapter.java
License:Apache License
@Override protected InputStream getBodyAsStream() throws IOException { //Copy the body into a new ByteBuf so that it can be consumed multiple times. return new ByteBufInputStream(Unpooled.copiedBuffer(body)); }
From source file:co.paralleluniverse.comsat.webactors.netty.HttpRequestWrapper.java
License:Open Source License
public HttpRequestWrapper(ActorRef<? super HttpResponse> actorRef, ChannelHandlerContext ctx, FullHttpRequest req, String sessionId) { this.actorRef = actorRef; this.ctx = ctx; this.req = req; this.sessionId = sessionId; reqContent = Unpooled.copiedBuffer(req.content()); }
From source file:co.paralleluniverse.comsat.webactors.netty.NettyHttpRequest.java
License:Open Source License
public NettyHttpRequest(ActorRef<? super HttpResponse> actorRef, ChannelHandlerContext ctx, FullHttpRequest req, String sessionId) {//from w ww . ja v a2 s . com this.actorRef = actorRef; this.ctx = ctx; this.req = req; this.sessionId = sessionId; reqContent = Unpooled.copiedBuffer(req.content()); }