Skip to content

Commit 775a922

Browse files
committed
version 0.0.1.1 update follow list:
1.you can set keep-alive mode and close mode 2.add pipeline chain and more type s of filter chain 3.support more concurrent
1 parent f48665a commit 775a922

17 files changed

+163
-35
lines changed

res/GunNetty.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#this is a config file
22
#you can write profile file in it
33
core {
4-
maxRunnningNum = 512
4+
maxRunnningNum = 1024
55
port = 8822
66
clientWaitTime = 50
77
fileReadBufferMin = 512
8+
connection = 2
89
profileName = banner.txt
910
}
1011
http {

src/top/gunplan/netty/GunBuffer.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package top.gunplan.netty;
2+
3+
import sun.misc.Unsafe;
4+
5+
6+
/**
7+
* @author dosdrtt
8+
*/
9+
public interface GunBuffer {
10+
class UnsafeInstanse {
11+
private static volatile Unsafe instance = null;
12+
13+
private static Unsafe newUnsafe() throws Exception {
14+
if (instance == null) {
15+
synchronized (UnsafeInstanse.class) {
16+
if (instance == null) {
17+
instance = (Unsafe) Class.forName("sun.misc.Unsafe").getConstructor().newInstance();
18+
}
19+
}
20+
}
21+
return instance;
22+
}
23+
}
24+
25+
26+
default Unsafe newUnsafe() throws Exception {
27+
return UnsafeInstanse.newUnsafe();
28+
}
29+
30+
byte[] malloc(int size, int len) throws Exception;
31+
32+
byte[] malloc(int len) throws Exception;
33+
34+
}

src/top/gunplan/netty/GunNettyFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package top.gunplan.netty;
22

33

4-
import top.gunplan.netty.impl.GunInputFilterChecker;
5-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
4+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
5+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
66

77
/**
88
* filter is a type of handle
@@ -37,5 +37,5 @@ enum DealResult {
3737
* @param filterDto input to the filter's deal Object
3838
* @return filter result true:next false:break
3939
*/
40-
DealResult doOutputFilter(GunOutputFilterDto filterDto) throws Exception;
40+
DealResult doOutputFilter(GunOutputFilterChecker filterDto) throws Exception;
4141
}

src/top/gunplan/netty/GunPilelineInterface.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
*/
1010
public interface GunPilelineInterface {
1111
/**
12-
*
13-
* @param handle
14-
* @return
12+
* register the handle in the chain
13+
* @param handle register handle
14+
* @return chain style is self
1515
*/
1616
GunPilelineInterface register(GunHandle handle);
1717

src/top/gunplan/netty/common/GunNettyPropertyManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
* analyzing config information by reference
16+
*
1617
* @author dosdrtt
1718
*/
1819
public final class GunNettyPropertyManager {
@@ -77,6 +78,9 @@ public static boolean getProperty() {
7778
}
7879

7980
public static boolean isInteger(String str) {
81+
if (str.startsWith("0x")) {
82+
return true;
83+
}
8084
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
8185
return pattern.matcher(str).matches();
8286
}

src/top/gunplan/netty/filters/GunHttpdHostCheck.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import top.gunplan.netty.GunNettyFilter;
44
import top.gunplan.netty.anno.GunNetFilterOrder;
55
import top.gunplan.netty.common.GunNettyPropertyManager;
6-
import top.gunplan.netty.impl.GunInputFilterChecker;
7-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
6+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
7+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
88
import top.gunplan.netty.protocol.GunHttp2InputProtocl;
99

1010
@GunNetFilterOrder(index = 2)
@@ -16,7 +16,7 @@ public DealResult doInputFilter(GunInputFilterChecker filterDto) {
1616
}
1717

1818
@Override
19-
public DealResult doOutputFilter(GunOutputFilterDto filterDto) {
19+
public DealResult doOutputFilter(GunOutputFilterChecker filterDto) {
2020
return DealResult.NEXT;
2121
}
2222
}

src/top/gunplan/netty/filters/GunNettyStdFirstFilter.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import top.gunplan.netty.anno.GunNetFilterOrder;
55
import top.gunplan.netty.common.GunNettyPropertyManager;
66
import top.gunplan.netty.impl.CunCoreDataEventLoop;
7-
import top.gunplan.netty.impl.GunInputFilterChecker;
8-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
7+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
8+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
9+
import top.gunplan.netty.impl.propertys.GunCoreProperty;
910
import top.gunplan.utils.AbstractGunBaseLogUtil;
1011
import top.gunplan.utils.GunBytesUtil;
1112

@@ -16,6 +17,7 @@
1617

1718
/**
1819
* GunNettyStdFirstFilter First Input Filter and Last Output Filter
20+
* this class is high dangerous
1921
*
2022
* @author dosdrtt
2123
*/
@@ -30,9 +32,9 @@ private void dealCloseEvent(SelectionKey key) throws IOException {
3032

3133
@Override
3234
public DealResult doInputFilter(GunInputFilterChecker filterDto) throws Exception {
35+
3336
byte[] readbata;
3437
SelectionKey key = filterDto.getKey();
35-
3638
if (key.isValid()) {
3739
try {
3840
readbata = GunBytesUtil.readFromChannel((SocketChannel) key.channel(), GunNettyPropertyManager.getCore().getFileReadBufferMin());
@@ -45,10 +47,14 @@ public DealResult doInputFilter(GunInputFilterChecker filterDto) throws Exceptio
4547
dealCloseEvent(key);
4648
return DealResult.CLOSE;
4749
} else {
48-
key.interestOps(SelectionKey.OP_READ);
49-
((CunCoreDataEventLoop) key.attachment()).listionSize.incrementAndGet();
50-
((CunCoreDataEventLoop) key.attachment()).continueLoop();
50+
if (GunNettyPropertyManager.getCore().getConnection() == GunCoreProperty.connectionType.CLOSE) {
51+
filterDto.getKey().cancel();
52+
}
53+
else if (GunNettyPropertyManager.getCore().getConnection() == GunCoreProperty.connectionType.KEEP_ALIVE) {
54+
key.interestOps(SelectionKey.OP_READ);
55+
((CunCoreDataEventLoop) key.attachment()).incrAndContinueLoop();
5156

57+
}
5258
return DealResult.NEXT;
5359

5460
}
@@ -59,10 +65,15 @@ public DealResult doInputFilter(GunInputFilterChecker filterDto) throws Exceptio
5965

6066

6167
@Override
62-
public DealResult doOutputFilter(GunOutputFilterDto filterDto) throws IOException {
68+
public DealResult doOutputFilter(GunOutputFilterChecker filterDto) throws IOException {
6369
SocketChannel channel = (SocketChannel) filterDto.getKey().channel();
64-
String s = new String(filterDto.getRespobj().serialize());
6570
channel.write(ByteBuffer.wrap(filterDto.getRespobj().serialize()));
71+
//
72+
73+
if (GunNettyPropertyManager.getCore().getConnection() == GunCoreProperty.connectionType.CLOSE) {
74+
filterDto.getKey().channel().close();
75+
}
76+
//
6677
return DealResult.NEXT;
6778
}
6879
}

src/top/gunplan/netty/filters/GunStdHttp2Filter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package top.gunplan.netty.filters;
22

3-
import top.gunplan.netty.impl.GunInputFilterChecker;
3+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
44
import top.gunplan.netty.GunNettyFilter;
5-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
5+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
66
import top.gunplan.netty.anno.GunNetFilterOrder;
77
import top.gunplan.netty.protocol.GunHttp2InputProtocl;
88
import top.gunplan.netty.protocol.GunNetInputInterface;
@@ -21,7 +21,7 @@ public DealResult doInputFilter(GunInputFilterChecker filterDto) {
2121
}
2222

2323
@Override
24-
public DealResult doOutputFilter(GunOutputFilterDto filterDto) {
24+
public DealResult doOutputFilter(GunOutputFilterChecker filterDto) {
2525
return DealResult.NEXT;
2626
}
2727

src/top/gunplan/netty/filters/GunStdToStringFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package top.gunplan.netty.filters;
22

33
import top.gunplan.netty.GunNettyFilter;
4-
import top.gunplan.netty.impl.GunInputFilterChecker;
5-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
4+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
5+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
66
import top.gunplan.netty.anno.GunNetFilterOrder;
77
import top.gunplan.netty.protocol.GunStdString;
88

@@ -20,7 +20,7 @@ public DealResult doInputFilter(GunInputFilterChecker filterDto) {
2020
}
2121

2222
@Override
23-
public DealResult doOutputFilter(GunOutputFilterDto filterDto) {
23+
public DealResult doOutputFilter(GunOutputFilterChecker filterDto) {
2424
return DealResult.NEXT;
2525
}
2626
}

src/top/gunplan/netty/impl/CunCoreConnetcionThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public synchronized void run() {
5050
public void dealEvent(SelectionKey key) throws Exception {
5151
SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
5252
CunCoreDataEventLoop selectionThread = ((CunCoreDataEventLoop) CoreThreadManage.getDealThread());
53+
socketChannel.socket().setTcpNoDelay(true);
5354
selectionThread.registerReadKey(socketChannel);
5455
selectionThread.incrAndContinueLoop();
5556
this.deal.submit(new GunAcceptWorker(dealHandle, socketChannel));

src/top/gunplan/netty/impl/GunCoreCalculatorWorker.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package top.gunplan.netty.impl;
22
import top.gunplan.netty.GunNettyFilter;
33
import top.gunplan.netty.GunPilelineInterface;
4-
import top.gunplan.netty.impl.example.GunOutputFilterDto;
4+
import top.gunplan.netty.impl.example.GunInputFilterChecker;
5+
import top.gunplan.netty.impl.example.GunOutputFilterChecker;
56
import top.gunplan.netty.protocol.GunNetResponseInterface;
67
import java.nio.channels.SelectionKey;
78
import java.util.ListIterator;
@@ -49,7 +50,7 @@ public synchronized void run() {
4950
} catch (Exception e) {
5051
this.pileline.getHandel().dealExceptionEvent(e);
5152
}
52-
GunOutputFilterDto responseFilterDto = new GunOutputFilterDto(respObject);
53+
GunOutputFilterChecker responseFilterDto = new GunOutputFilterChecker(respObject);
5354
responseFilterDto.setKey(gunFilterObj.getKey());
5455
ListIterator<GunNettyFilter> filters = pileline.getFilters().listIterator(pileline.getFilters().size());
5556

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package top.gunplan.netty.impl.buffer;
2+
3+
4+
import top.gunplan.netty.GunBuffer;
5+
6+
/**
7+
*
8+
*/
9+
public class GunSafedPulledBuffer implements GunBuffer {
10+
@Override
11+
public byte[] malloc(int size, int len) throws Exception {
12+
return new byte[0];
13+
}
14+
15+
@Override
16+
public byte[] malloc(int len) throws Exception {
17+
return new byte[0];
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package top.gunplan.netty.impl.buffer;
2+
3+
import sun.misc.Unsafe;
4+
import top.gunplan.netty.GunBuffer;
5+
6+
public class GunUnsafePulledBuffer implements GunBuffer {
7+
@Override
8+
public byte[] malloc(int size, int len) throws Exception {
9+
final Unsafe unsafe = newUnsafe();
10+
long address = unsafe.allocateMemory(size * len);
11+
// unsafe.putby
12+
return null;
13+
}
14+
15+
@Override
16+
public byte[] malloc(int len) throws Exception {
17+
return malloc(1, len);
18+
}
19+
}

src/top/gunplan/netty/impl/GunInputFilterChecker.java renamed to src/top/gunplan/netty/impl/example/GunInputFilterChecker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package top.gunplan.netty.impl;
1+
package top.gunplan.netty.impl.example;
22

33
import com.sun.istack.internal.NotNull;
44
import top.gunplan.netty.protocol.GunNetInputInterface;
55

66
import java.nio.channels.SelectionKey;
7-
import java.nio.channels.SocketChannel;
87
import java.util.concurrent.atomic.AtomicInteger;
98

109

@@ -16,7 +15,6 @@ public final class GunInputFilterChecker {
1615
private AtomicInteger size;
1716

1817

19-
2018
public GunInputFilterChecker(SelectionKey key) {
2119
this.key = key;
2220
this.size = size;

src/top/gunplan/netty/impl/example/GunOutputFilterDto.java renamed to src/top/gunplan/netty/impl/example/GunOutputFilterChecker.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
import java.nio.channels.SelectionKey;
66

7-
public final class GunOutputFilterDto {
7+
/**
8+
* @author dosdrtt
9+
*
10+
*/
11+
public final class GunOutputFilterChecker {
812
private GunNetResponseInterface respobj;
913
private SelectionKey key;
1014

11-
public GunOutputFilterDto(GunNetResponseInterface respobj, SelectionKey key) {
15+
public GunOutputFilterChecker(GunNetResponseInterface respobj, SelectionKey key) {
1216
this.respobj = respobj;
1317
this.key = key;
1418
}
@@ -29,10 +33,10 @@ public void setRespobj(GunNetResponseInterface respobj) {
2933
this.respobj = respobj;
3034
}
3135

32-
public GunOutputFilterDto(GunNetResponseInterface respobj) {
36+
public GunOutputFilterChecker(GunNetResponseInterface respobj) {
3337
this.respobj = respobj;
3438
}
3539

36-
public GunOutputFilterDto() {
40+
public GunOutputFilterChecker() {
3741
}
3842
}

src/top/gunplan/netty/impl/propertys/GunCoreProperty.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
package top.gunplan.netty.impl.propertys;
22

3+
import sun.jvm.hotspot.debugger.cdbg.EnumType;
4+
35
public class GunCoreProperty implements GunProPerty {
6+
public enum connectionType {
7+
/**
8+
*
9+
*/
10+
KEEP_ALIVE(1, "keep-alive"), CLOSE(2, "close"), NODEF(4, "nodef");
11+
private int index;
12+
private String sval;
13+
14+
public String getSVal() {
15+
return sval;
16+
}
17+
18+
connectionType(int index, String sval) {
19+
this.index = index;
20+
this.sval = sval;
21+
}
22+
23+
public static connectionType getTypeByval(int val) {
24+
for (connectionType type : connectionType.values()) {
25+
if (type.index == val) {
26+
return type;
27+
}
28+
}
29+
return NODEF;
30+
}
31+
}
32+
433
private volatile int port;
534
private volatile int maxRunnningNum;
635
private volatile int clientWaitTime;
736
private volatile int fileReadBufferMin;
37+
private volatile int connection;
838
private volatile String profileName;
939

40+
public connectionType getConnection() {
41+
return connectionType.getTypeByval(connection);
42+
}
43+
1044
public int getFileReadBufferMin() {
1145
return fileReadBufferMin;
1246
}

0 commit comments

Comments
 (0)