|
1 | 1 | package example;
|
2 | 2 |
|
3 |
| -import com.generallycloud.nio.acceptor.SocketChannelAcceptor; |
4 |
| -import com.generallycloud.nio.codec.http11.ServerHTTPProtocolFactory; |
5 |
| -import com.generallycloud.nio.component.IoEventHandleAdaptor; |
6 |
| -import com.generallycloud.nio.component.NioSocketChannelContext; |
7 |
| -import com.generallycloud.nio.component.SocketChannelContext; |
8 |
| -import com.generallycloud.nio.component.SocketSession; |
9 |
| -import com.generallycloud.nio.configuration.ServerConfiguration; |
10 |
| -import com.generallycloud.nio.protocol.ReadFuture; |
11 |
| - |
12 |
| -public class Server { |
13 |
| - |
14 |
| - public static void main(String[] args) throws Exception { |
15 |
| - |
16 |
| - IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() { |
17 |
| - @Override |
18 |
| - public void accept(SocketSession session, ReadFuture future) throws Exception { |
19 |
| - future.write("Hello World!"); |
20 |
| - session.flush(future); |
21 |
| - } |
22 |
| - }; |
23 |
| - |
24 |
| - ServerConfiguration c = new ServerConfiguration(8080); |
25 |
| - |
26 |
| - c.setSERVER_MEMORY_POOL_UNIT(256); |
27 |
| - c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true); |
28 |
| - //c.setSERVER_CORE_SIZE(2); |
29 |
| - c.setSERVER_ENABLE_MEMORY_POOL(true); |
30 |
| - c.setSERVER_MEMORY_POOL_CAPACITY_RATE(4); |
31 |
| - |
32 |
| - SocketChannelContext context = new NioSocketChannelContext(c); |
33 |
| - |
34 |
| - SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context); |
35 |
| - |
36 |
| - context.setIoEventHandleAdaptor(eventHandleAdaptor); |
37 |
| - context.setProtocolFactory(new ServerHTTPProtocolFactory()); |
38 |
| - |
39 |
| - acceptor.bind(); |
40 |
| - |
41 |
| - } |
42 |
| - |
| 3 | +import java.net.StandardSocketOptions; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.generallycloud.baseio.buffer.ByteBuf; |
| 8 | +import com.generallycloud.baseio.codec.http11.ServerHttpCodec; |
| 9 | +import com.generallycloud.baseio.component.ChannelAcceptor; |
| 10 | +import com.generallycloud.baseio.component.ChannelContext; |
| 11 | +import com.generallycloud.baseio.component.ChannelEventListenerAdapter; |
| 12 | +import com.generallycloud.baseio.component.IoEventHandle; |
| 13 | +import com.generallycloud.baseio.component.LoggerChannelOpenListener; |
| 14 | +import com.generallycloud.baseio.component.NioEventLoopGroup; |
| 15 | +import com.generallycloud.baseio.component.NioSocketChannel; |
| 16 | +import com.generallycloud.baseio.protocol.Future; |
| 17 | + |
| 18 | +public class Server { |
| 19 | + |
| 20 | + public static void main(String[] args) throws Exception { |
| 21 | + |
| 22 | + NioEventLoopGroup group = new NioEventLoopGroup(); |
| 23 | + group.setMemoryPoolCapacity(1024 * 64); |
| 24 | + group.setMemoryPoolUnit(512); |
| 25 | + ChannelContext context = new ChannelContext(8087); |
| 26 | + ChannelAcceptor acceptor = new ChannelAcceptor(context, group); |
| 27 | + context.setProtocolCodec(new ServerHttpCodec(1024 * 4)); |
| 28 | + context.setMaxWriteBacklog(Integer.MAX_VALUE); |
| 29 | + context.addChannelEventListener(new LoggerChannelOpenListener()); |
| 30 | + context.addChannelEventListener(new ChannelEventListenerAdapter() { |
| 31 | + |
| 32 | + @Override |
| 33 | + public void channelOpened(NioSocketChannel channel) throws Exception { |
| 34 | + channel.setOption(StandardSocketOptions.TCP_NODELAY, true); |
| 35 | + channel.setIoEventHandle(new IoEventHandle() { |
| 36 | + boolean addTask = true; |
| 37 | + List<ByteBuf> fs = new ArrayList<>(256); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void accept(NioSocketChannel channel, Future future) throws Exception { |
| 41 | + future.write("Hello World", channel); |
| 42 | + fs.add(channel.encode(future)); |
| 43 | + if (addTask) { |
| 44 | + addTask = false; |
| 45 | + channel.getEventLoop().dispatchAfterLoop(() -> { |
| 46 | + channel.flush(fs); |
| 47 | + addTask = true; |
| 48 | + fs.clear(); |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + acceptor.bind(); |
| 57 | + } |
| 58 | + |
43 | 59 | }
|
0 commit comments