本文整理汇总了Java中io.netty.channel.embedded.EmbeddedChannel.writeInbound方法的典型用法代码示例。如果您正苦于以下问题:Java EmbeddedChannel.writeInbound方法的具体用法?Java EmbeddedChannel.writeInbound怎么用?Java EmbeddedChannel.writeInbound使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.embedded.EmbeddedChannel
的用法示例。
在下文中一共展示了EmbeddedChannel.writeInbound方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testThatPipeliningWorksWithFastSerializedRequests
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
public void testThatPipeliningWorksWithFastSerializedRequests() throws InterruptedException {
final int numberOfRequests = randomIntBetween(2, 128);
final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new HttpPipeliningHandler(numberOfRequests), new WorkEmulatorHandler());
for (int i = 0; i < numberOfRequests; i++) {
embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i)));
}
final List<CountDownLatch> latches = new ArrayList<>();
for (final String url : waitingRequests.keySet()) {
latches.add(finishRequest(url));
}
for (final CountDownLatch latch : latches) {
latch.await();
}
embeddedChannel.flush();
for (int i = 0; i < numberOfRequests; i++) {
assertReadHttpMessageHasContent(embeddedChannel, String.valueOf(i));
}
assertTrue(embeddedChannel.isOpen());
}
示例2: testThatPipeliningClosesConnectionWithTooManyEvents
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
public void testThatPipeliningClosesConnectionWithTooManyEvents() throws InterruptedException {
final int numberOfRequests = randomIntBetween(2, 128);
final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new HttpPipeliningHandler(numberOfRequests), new WorkEmulatorHandler());
for (int i = 0; i < 1 + numberOfRequests + 1; i++) {
embeddedChannel.writeInbound(createHttpRequest("/" + Integer.toString(i)));
}
final List<CountDownLatch> latches = new ArrayList<>();
final List<Integer> requests = IntStream.range(1, numberOfRequests + 1).mapToObj(r -> r).collect(Collectors.toList());
Randomness.shuffle(requests);
for (final Integer request : requests) {
latches.add(finishRequest(request.toString()));
}
for (final CountDownLatch latch : latches) {
latch.await();
}
finishRequest(Integer.toString(numberOfRequests + 1)).await();
embeddedChannel.flush();
assertFalse(embeddedChannel.isOpen());
}
示例3: decode
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
/**
* Happy path test.
*/
@Test
@Tag("fast")
public void decode() {
EmbeddedChannel channel = new EmbeddedChannel(new IsoOnTcpProtocol());
channel.writeInbound(Unpooled.wrappedBuffer(new byte[]{IsoOnTcpProtocol.ISO_ON_TCP_MAGIC_NUMBER,
(byte)0x00,(byte)0x00,(byte)0x0D,
(byte)0x01,(byte)0x02,(byte)0x03,(byte)0x04,(byte)0x05,(byte)0x06,(byte)0x07,(byte)0x08,(byte)0x09}));
channel.checkException();
Object obj = channel.readInbound();
assertThat(obj).isInstanceOf(IsoOnTcpMessage.class);
IsoOnTcpMessage isoOnTcpMessage = (IsoOnTcpMessage) obj;
assertNotNull(isoOnTcpMessage.getUserData());
assertEquals(9, isoOnTcpMessage.getUserData().readableBytes());
}
示例4: processPacket
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
/**
* Passes this Packet on to the NetHandler for processing.
*/
@Override
public void processPacket(INetHandler inethandler)
{
this.netHandler = inethandler;
EmbeddedChannel internalChannel = NetworkRegistry.INSTANCE.getChannel(this.channel, this.target);
if (internalChannel != null)
{
internalChannel.attr(NetworkRegistry.NET_HANDLER).set(this.netHandler);
try
{
if (internalChannel.writeInbound(this))
{
badPackets.add(this.channel);
if (badPackets.size() % packetCountWarning == 0)
{
FMLLog.severe("Detected ongoing potential memory leak. %d packets have leaked. Top offenders", badPackets.size());
int i = 0;
for (Entry<String> s : Multisets.copyHighestCountFirst(badPackets).entrySet())
{
if (i++ > 10) break;
FMLLog.severe("\t %s : %d", s.getElement(), s.getCount());
}
}
}
internalChannel.inboundMessages().clear();
}
catch (FMLNetworkException ne)
{
FMLLog.log(Level.ERROR, ne, "There was a network exception handling a packet on channel %s", channel);
dispatcher.rejectHandshake(ne.getMessage());
}
catch (Throwable t)
{
FMLLog.log(Level.ERROR, t, "There was a critical exception handling a packet on channel %s", channel);
dispatcher.rejectHandshake("A fatal error has occurred, this connection is terminated");
}
}
}
示例5: executeNormalChannelRead0
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
private MqttConnAckMessage executeNormalChannelRead0(String clientId, boolean cleanSession, ChannelId channelId)
throws Exception {
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false,
10);
MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, true, true, true, 0, true,
cleanSession, 60);
MqttConnectPayload payload = new MqttConnectPayload(clientId, "willtopic", "willmessage", "username",
"password");
MqttConnectMessage msg = new MqttConnectMessage(fixedHeader, variableHeader, payload);
ChannelId cid = channelId == null ? TestUtil.newChannelId(clientId, false) : channelId;
EmbeddedChannel channel = new EmbeddedChannel(cid, new ConnectReceiver());
channel.writeInbound(msg);
return channel.readOutbound();
}
示例6: addDecoderReplaysLastHttp
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void addDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel,
(response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
示例7: addNamedDecoderReplaysLastHttp
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void addNamedDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel,
(response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
示例8: addEncoderReplaysLastHttp
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void addEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel,
(response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
示例9: addNamedEncoderReplaysLastHttp
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void addNamedEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel,
(response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
示例10: sendRequest
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
static Reply sendRequest(EmbeddedChannel channel, Request request) throws Exception {
channel.writeInbound(request);
Object encodedReply = channel.readOutbound();
for (int i = 0; encodedReply == null && i < 50; i++) {
channel.runPendingTasks();
Thread.sleep(10);
encodedReply = channel.readOutbound();
}
if (encodedReply == null) {
throw new IllegalStateException("No reply to request: " + request);
}
WireCommand decoded = CommandDecoder.parseCommand((ByteBuf) encodedReply);
((ByteBuf) encodedReply).release();
assertNotNull(decoded);
return (Reply) decoded;
}
示例11: testPrematureClosureWithChunkedEncoding1
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void testPrematureClosureWithChunkedEncoding1() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
ch.writeInbound(
Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n", CharsetUtil.US_ASCII));
// Read the response headers.
HttpResponse res = (HttpResponse) ch.readInbound();
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked"));
assertThat(ch.readInbound(), is(nullValue()));
// Close the connection without sending anything.
ch.finish();
// The decoder should not generate the last chunk because it's closed prematurely.
assertThat(ch.readInbound(), is(nullValue()));
}
示例12: transferAllDataWithMerge
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
/**
* Transfers all pending data from the source channel into the destination channel.<br>
* Merges all data into a single buffer before transmission into the destination.
* @param srcChannel The source channel
* @param dstChannel The destination channel
*/
private static void transferAllDataWithMerge(EmbeddedChannel srcChannel, EmbeddedChannel dstChannel) {
ByteBuf mergedBuffer = null;
for (;;) {
Object srcData = srcChannel.readOutbound();
if (srcData != null) {
assertTrue(srcData instanceof ByteBuf);
ByteBuf srcBuf = (ByteBuf) srcData;
try {
if (mergedBuffer == null) {
mergedBuffer = Unpooled.buffer();
}
mergedBuffer.writeBytes(srcBuf);
} finally {
srcBuf.release();
}
} else {
break;
}
}
if (mergedBuffer != null) {
dstChannel.writeInbound(mergedBuffer);
}
}
示例13: testHttpUpgradeRequestMissingWSKeyHeader
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void testHttpUpgradeRequestMissingWSKeyHeader() {
EmbeddedChannel ch = createChannel();
HttpRequest httpRequest = new WebSocketRequestBuilder().httpVersion(HTTP_1_1)
.method(HttpMethod.GET)
.uri("/test")
.key(null)
.connection("Upgrade")
.upgrade(WEBSOCKET.toLowerCase())
.version13()
.build();
ch.writeInbound(httpRequest);
FullHttpResponse response = ReferenceCountUtil.releaseLater(responses.remove());
assertEquals(BAD_REQUEST, response.getStatus());
assertEquals("not a WebSocket request: missing key", getResponseMessage(response));
}
示例14: unitTestForRedirectHandler
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
/**
* Check whether, redirect request is written to the backend when a redirect response is received.
*
* @throws URISyntaxException
* @throws IOException
*/
@Test
public void unitTestForRedirectHandler() throws URISyntaxException, IOException {
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
embeddedChannel.pipeline().addLast(new HttpResponseDecoder());
embeddedChannel.pipeline().addLast(new HttpRequestEncoder());
embeddedChannel.pipeline().addLast(new RedirectHandler(null, false, 5, false));
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT,
Unpooled.EMPTY_BUFFER);
response.headers().set(HttpHeaders.Names.LOCATION, FINAL_DESTINATION);
embeddedChannel.attr(Constants.ORIGINAL_REQUEST)
.set(createHttpRequest(Constants.HTTP_GET_METHOD, FINAL_DESTINATION));
embeddedChannel.writeInbound(response);
embeddedChannel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
assertNotNull(embeddedChannel.readOutbound());
}
示例15: testFailSlowTooLongFrameRecovery
import io.netty.channel.embedded.EmbeddedChannel; //导入方法依赖的package包/类
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(
new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4, false));
for (int i = 0; i < 2; i ++) {
assertFalse(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 })));
try {
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 })));
fail(DecoderException.class.getSimpleName() + " must be raised.");
} catch (TooLongFrameException e) {
// Expected
}
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' }));
ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release();
}
}