-
Notifications
You must be signed in to change notification settings - Fork 324
Closed
Description
Please take a look at this sample:
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
public class ChannelUnpackerTest {
public static void main(String[] args) throws Exception {
try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
serverChannel.socket().bind(new InetSocketAddress("localhost", 7777));
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
pool.execute(() -> {
try {
SocketChannel channel = serverChannel.accept();
channel.write(ByteBuffer.wrap(new byte[200]));
} catch (Exception e) {
//nothing
}
});
}
try (Socket socket = new Socket("localhost", 7777)) {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(socket.getInputStream());
unpacker.readPayload(ByteBuffer.allocate(128));
System.out.println("read from socket");
}
try (SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", 7777))) {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(channel);
unpacker.readPayload(ByteBuffer.allocate(128));
System.out.println("read from channel");
}
}
}
}
This sample accepts incoming connection, writes 200 bytes to it and waits indefinitely. Consuming first 128 (that is from my real use case - after establishing connection I need to read servers hello message with fixed size of 128 bytes) via MessageUnpacker created via socket InputStream handles this case just fine, however via SocketCnahhel it blocks.
Changing the server to write large enough data (e.g. 20000 bytes) leads to filling of MessageUnpackers buffer and it can return first 128 bytes.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels