56;yXB}I*#`5UC$xcIhwv*
z8_&77*7bBX9PR2j*5&%RI+lzbir<4idgp+>gF~()RddMIPd?mRH}=Cx(r~g!I{6zo
ziMh^oZakOj7`hJurA`)fxsrz8Dg<^6f%U{1jvR8GqRHsq+HeY<`np_4+_B#vkUQX`
zGnpE(y?aY{bm%^+^yJVHueeT+c%|ELR0>BJz>dDxMRBosgOuXC&zV}_&h`+3
z>h`(2T!;5lfM}i@m5)Z{?wBhnvDfpkA-qAGPW)L^7jyM@ZtPEWTrT@A@hn+wb@lA0
zN?ykSABW=8{wI}cI^^of7hSHs=oCPk2#UGhLtW^VL-E_eqmDQkySFYYRQN$vQ9cQ2
z0=W96W7X}afEwLCDgF&a(Iu`;F14G7W32?(mYBs~a_<
z6I{tdu099FjllNPiP4HyxI%aLp^MPv=rkJpv8F!Y`BQ^=LYp+r69iGcPfowsyZfor
zUMC6c{vOWWj8DLX8L0wwW)IXlK3ExhcpTv94cdI;qtOw14)ZuTQB6h|=dy=UhFXn2
zM3F*hHiBqI
zs2PBtOc=&^1_%h%3^=!+9I4JH(kH)v$*!xqe`8#Lt?q8Jbw}=ZvZ0
zFs(zZKe&7nidn_6x;Vz;c$X^WGuoyZd>d_hQtu8Y|xmFW_6bdX*f>
zxr0iY?7ZnvOaah^08FNvO{&m1z0YEk=|T9%mS6#(vk%AKba!=NlcgONcK?pZ^sE{k
ze*+w?u0rgR@GEGSO!rwu7Ps-oU~-6bF~-N-z8=gFtYBTV6zhs}>jO9jP^k3F`$}d#UY6xjn>w3g;#q3$frq0!JkE*On&{Gy<1j()`<^VYZ>Jhs_`uHVy^@_STHdll^D=KQ>>8GqH46zhGO~S5*I0jbQyh@`%+if>|!ouj-Qa$P8ACSZ}V`Cdn#X91#hU552>T>mB
zHPduQW%LW!5bx^1^ruDQgq`8e0V}|2qf+xw&YE}`0#cJotao4rZ9Y;QdkdPc(;gW+
zq?FFU)kEDIhVG-8f3iq|@|RV1q2g6lQe^y=IdmiNTZSfM(7UiLZKn4f+zE2;cE|oT
z)$`7QZG7cmW7T~TRFVg6c^3h)+fn1jBvpqP=suuRUH0dVFZgyJT1OXyWBOAm
zde?Co`er-mV$i*yJ3)_vo&@a&9fqkq28($X=nT+%Kk=|jr;Zu)5|9Tl{yPRN1;CXoTqwt>BIRoR}5)~BbNI?
zr(8h31@d{2&jA(MK9aWI1-R|YRI1u6e;_S?8~6_)UtyN#-ys#K4aadDgnUtke1Rb^
zM0-Dne4<%?PuhPuv3*={DZztac
z`3cC+n*HaWmJXn~`_BL%WA#Q5&OJeBv^T7Gvr
zzH-R_4)VEX|LfE81(5H(fW05`mmrt#q_qDvY5OgZzXkbpv;SaPz6hV|8)A@>>b1KwgM-^kc|ZXV^Cy_T`X&0{NpE@`nuh0?6yIuIVP!n7{Q>
zEc@3Fc`f9#%<}uv@-2|x1NpsXd47}Oe;4Hc2)TUDrTec|*^|koa;XO{^}wYbxYPre
zdf-wITAIB)-6K)?@_Z^pRDZY4VTvRxa2TQ&*RoXT86%Y-
z3-mkCG6mz!H5Y!5#%AgRX^8ZFAH7(jGFB0hq-Vq^8lS_wjW6JV?-{JN~6+U(Ptm}nvMmDrXy^WyJ
zu%gZSEEtLgoU4N!&c=?GR{zu%zmQzBH_|Me{tZD`DjE$d$=X0TLLX2`HD)}Jg#)c#
zGT^iJ)~ImGj~1QLzDxo=WU=rC_)n{@@D)c(e#EXsfo{1043m|y`b{z-L+f4luBVQ&H$FAuxBkH_BTw&ys
zn56{*ubn5F5v@PM*jLTc0(?oW_tQ*#q&PO!VTR;+0lw7MzXxVvff$W9soC_$2>pFd
zral%A@2i>k!0)HmU3&j_yG@{PvcP0;6D%SYv_az_S8|%CzZRccs6|Y;3
zS2J$?J)~zSc_JMr#X+SBM_!ix+zOoP?X~!STIvrRmwmvqwd(-;IZkiZQRK4@(2sGD
z{}&jaMM4z2{)tQ3aM4{Z)r>!1Nqk_xkwUh3@=-ya7&wk)j9b6YFPHeh`BN$Jf%E4U
z;O6gxbf8OZ@PG}zUg9Of`n~#28~xX8@ZSP==pan=gbn_+^fNGjfnHo?YgetrON2FF
zASm&H`#_ZSt>5SGwDC{R&PQRMSih@3!MOGN`LVdL$QI|#Hu!wt+4|?NZ1lr6`j1Kd
zfq5A(+xU40_$1SK=+jTxk2N1;M4{>jYhJ)S#;y4aEsW3S3$@8y!H*fY=3l%oaUDgP
zp0vU9aiN;6pU2tY(`|5_|60V|G?O2brg0}Y7Ks4ZY&YC`RCOl_RlF2&iIu>7KN5dP0
zy7KjRw6$%3iHVchJsGMhvBl|YZ^w7x_aehZCAS2T64ZzshI^5hBch=WU$aN1g1}wv
z^6EO5$F+0;Wedrc^7+Cgx6iL$;<>%H_6}FQr@ngr5*KM8ZOG8}M?#)vZ_rN(L<_#R
zw7PCl4X0OBRp}4O={s>FuObtcxfGeu;NRWr#0vflUgjvIeb5)`895zk5d~t+j>?kE
zGnt+cDol;mlqz@=4M#uOc!=CR`e)aWbWMPxD8^k
z>Sro5UQ-CU=Dw=ju(TVbWy!3sG(wfh6fl+Qt1qbjOiItW
zacPHpcvbXsFHOrC*95l_8HJu-;M0|mw%5ndR6TifgB?V9T6>rjyex5XL74UV35YkU1%PSXcjTgR{UG;M&Ko}Fu6
zKflv-8QW9*RJV?wVd2M#ZX03xIiIH81fuBrwY_eC5Hhl#Pf93Be%`OCHY9s`=U}y8
z4~*jCZZ_VJ==UW0y$4iep}Nc^0>*5wpD$`EzdbVJ_WJplroYiIDp+6J
zF)OxM?1h6ROfBZJuUJD8`X-3t*W)+LAz|bBX1`*Mzt;BefW&NXJpY`iOy1T5#~yV5
zYlk~=f~Tf>n2hJ7`v1?d>Rb62U`wxLbT#^UYR^=FC{}$dPhXQ#{MueWw>*}kJ>^`O
z?e+87-W=_Zz@Fxwu3taL)&D<|-bZx5>-hCNc;8aLe%^cRW(ZKGC}RB8_L_bKi8+4b
zxp0Ltq34O_qU|*O1VTF3@$38JvPxy8^|TL*I}wL~Fs@Fk;XYfPayJ^dw%2qdB)V=x
z2`4VL|H2I7T-#}wcs=(uMs&_J#TE05O4
#include
#include
+#include
#include
#include
#include
@@ -12,6 +13,7 @@
#include
#include
#include
+#include
#include
@@ -294,6 +296,51 @@ static inline int make_socket_non_blocking(int fd){
return 0;
}
+JNIEXPORT jint JNICALL Java_com_firenio_component_Native_open0
+ (JNIEnv * e, jclass c,jstring path,jint op, jint pem){
+ const char *file_path = e->GetStringUTFChars(path, NULL);
+ return open(file_path, op, pem);
+ }
+
+JNIEXPORT jlong JNICALL Java_com_firenio_component_Native_posix_1memalign_1allocate0
+ (JNIEnv * e, jclass c, jint len, jint align){
+ unsigned char *buf;
+ int ret = posix_memalign((void **) &buf, align, len);
+ if (ret < 0) {
+ return ret;
+ }
+ return (jlong) buf;
+ }
+
+JNIEXPORT jlong JNICALL Java_com_firenio_component_Native_file_1length0
+ (JNIEnv * e, jclass c, jint fd){
+ struct stat stbuf;
+ int ret = fstat(fd, &stbuf);
+ if (( ret < 0) ) {
+ return ret;
+ }
+ if((!S_ISREG(stbuf.st_mode))){
+ return 0;
+ }
+ return (jlong)(stbuf.st_size);
+ }
+
+JNIEXPORT jlong JNICALL Java_com_firenio_component_Native_lseek0
+ (JNIEnv * e, jclass c, jint fd, jlong off, jint seek_mode){
+ return (jlong)(lseek(fd, off, seek_mode));
+ }
+
+JNIEXPORT jint JNICALL Java_com_firenio_component_Native_pread0
+ (JNIEnv * e, jclass c, jint fd, jlong address, jint len, jlong off){
+ return pread(fd, (void *)address, len, off);
+ }
+
+JNIEXPORT jint JNICALL Java_com_firenio_component_Native_pwrite0
+ (JNIEnv * e, jclass c, jint fd, jlong address, jint len, jlong off){
+ return pwrite(fd, (void *)address, len, off);
+ }
+
+
int main(){ return 0;}
#ifdef __cplusplus
diff --git a/firenio-homepage/http-startup.sh b/firenio-homepage/http-startup.sh
index 1e585f0e3..f64b9493c 100644
--- a/firenio-homepage/http-startup.sh
+++ b/firenio-homepage/http-startup.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+
cd ../firenio
mvn clean install -DskipTests
@@ -27,5 +29,6 @@ java -XX:+PrintGCDetails -Xloggc:gc.log -Xdebug -Xrunjdwp:server=y,transport=dt_
-cp $CLASSPATH \
-Dboot.mode=prod \
-Dboot.libPath=/app/lib \
+ -Dorg.wildfly.openssl.path=$1 \
-Dboot.class=sample.http11.startup.TestHttpBootstrapEngine \
com.firenio.container.Bootstrap
diff --git a/firenio-sample/sample-http/pom.xml b/firenio-sample/sample-http/pom.xml
index 6ba85644b..366a66aca 100644
--- a/firenio-sample/sample-http/pom.xml
+++ b/firenio-sample/sample-http/pom.xml
@@ -123,19 +123,19 @@
org.wildfly.openssl
wildfly-openssl-windows-x86_64
- 1.0.6.Final
+ 1.0.8.Final
org.wildfly.openssl
wildfly-openssl-linux-x86_64
- 1.0.6.Final
+ 1.0.8.Final
org.wildfly.openssl
wildfly-openssl-java
- 1.0.6.Final
+ 1.0.8.Final
javax.annotation
diff --git a/firenio-sample/sample-http/src/main/java/sample/http11/startup/TestHttpBootstrapEngine.java b/firenio-sample/sample-http/src/main/java/sample/http11/startup/TestHttpBootstrapEngine.java
index 73b622bc7..3fcfb2ebd 100644
--- a/firenio-sample/sample-http/src/main/java/sample/http11/startup/TestHttpBootstrapEngine.java
+++ b/firenio-sample/sample-http/src/main/java/sample/http11/startup/TestHttpBootstrapEngine.java
@@ -65,8 +65,8 @@ public void bootstrap(final String rootPath, final String mode) throws Exception
ConfigurationParser.parseConfiguration("server.", context, properties);
ConfigurationParser.parseConfiguration("server.", group, properties);
context.setIoEventHandle(handle);
- context.addChannelEventListener(new WebSocketChannelListener());
context.addChannelIdleEventListener(new ChannelAliveListener());
+ context.addChannelEventListener(new WebSocketChannelListener());
context.addChannelEventListener(new LoggerChannelOpenListener());
context.addChannelEventListener(new CountChannelListener());
context.setExecutorGroup(new ThreadEventLoopGroup());
diff --git a/firenio-sample/sample-http/src/main/resources/server.properties b/firenio-sample/sample-http/src/main/resources/server.properties
index 86e8e1f1e..d8e706338 100644
--- a/firenio-sample/sample-http/src/main/resources/server.properties
+++ b/firenio-sample/sample-http/src/main/resources/server.properties
@@ -24,6 +24,6 @@ server.writeBuffers=8
###################################### app basic properties ######################################
app.enableHttp2=false
-#app.webRoot=C:\\ֿ\\yxaAdmin\\docs
+#app.webRoot=C:\\工作仓库\\yxaAdmin\\docs
#app.welcome=/index.html
app.proxy=false
diff --git a/firenio-test/src/main/java/test/io/TestDio.java b/firenio-test/src/main/java/test/io/TestDio.java
new file mode 100644
index 000000000..497319f89
--- /dev/null
+++ b/firenio-test/src/main/java/test/io/TestDio.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2015 The FireNio Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package test.io;
+
+import com.firenio.DevelopConfig;
+import com.firenio.common.Unsafe;
+import com.firenio.component.Native;
+import com.firenio.log.LoggerFactory;
+
+/**
+ * @program: firenio
+ * @description:
+ * @author: wangkai
+ * @create: 2019-09-11 16:18
+ **/
+public class TestDio {
+
+ static {
+ LoggerFactory.setEnableSLF4JLogger(false);
+ DevelopConfig.EPOLL_DEBUG = true;
+ DevelopConfig.NATIVE_DEBUG = true;
+ }
+
+ static final int PS = 1024 * 4;
+
+ static final String path = "/home/test/temp/test_direct2.txt";
+
+ public static void main(String[] args) throws Exception {
+
+ test_direct_write();
+ test_direct_read();
+ }
+
+ static void test_direct_write() {
+ long address = Native.posix_memalign_allocate(PS, PS);
+ int fd = Native.open(path, Native.O_WRONLY | Native.O_CREAT | Native.O_DIRECT | Native.O_TRUNC, 0755);
+ System.out.println("file len: " + Native.file_length(fd));
+ byte[] data = "hello world!0".getBytes();
+ do_write_data(fd, address, data, 0);
+ do_write_data(fd, address, data, 1);
+ do_write_data(fd, address, data, 2);
+ do_write_data(fd, address, data, 3);
+ System.out.println("file len: " + Native.file_length(fd));
+ Unsafe.free(address);
+ Native.close(fd);
+ }
+
+ static void do_write_data(int fd, long address, byte[] data, int index) {
+ data[data.length - 1] = (byte) (index);
+ Unsafe.copyFromArray(data, 0, address, data.length);
+ if (index > 0) {
+ Native.pwrite(fd, address, PS, index * PS);
+ } else {
+ Native.write(fd, address, PS);
+ }
+
+ }
+
+ static void test_direct_read() {
+ long address = Native.posix_memalign_allocate(PS, PS);
+ int fd = Native.open(path, Native.O_RDONLY | Native.O_CREAT | Native.O_DIRECT, 0755);
+ long file_len = Native.file_length(fd);
+ byte[] data = new byte[16];
+ System.out.println("file len: " + file_len);
+ Native.lseek(fd, file_len, Native.SEEK_SET);
+ do_read_data(fd, address, data, 0);
+ do_read_data(fd, address, data, PS * 2);
+ Unsafe.free(address);
+ Native.close(fd);
+ }
+
+ static void do_read_data(int fd, long address, byte[] data, long pos) {
+ int read;
+ if (pos == 0) {
+ Native.lseek(fd, pos, Native.SEEK_SET);
+ read = Native.read(fd, address, PS);
+ } else {
+ read = Native.pread(fd, address, PS, pos);
+ }
+ System.out.println("read len: " + read);
+ Unsafe.copyToArray(address, data, 0, 16);
+ String content = new String(data);
+ System.out.println("content: " + content);
+
+ }
+
+}
diff --git a/firenio-test/src/main/java/test/io/buffer/TestByteBufCopy.java b/firenio-test/src/main/java/test/io/buffer/TestByteBufCopy.java
index e8c1ca825..c98a3d471 100644
--- a/firenio-test/src/main/java/test/io/buffer/TestByteBufCopy.java
+++ b/firenio-test/src/main/java/test/io/buffer/TestByteBufCopy.java
@@ -17,6 +17,7 @@
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
+import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
@@ -26,12 +27,17 @@
import com.firenio.common.Assert;
import com.firenio.common.Unsafe;
import com.firenio.common.Util;
+import com.firenio.log.LoggerFactory;
/**
* @author wangkai
*/
public class TestByteBufCopy {
+ static {
+ LoggerFactory.setEnableSLF4JLogger(false);
+ }
+
static final byte[] _data = "abc123abc123".getBytes();
static final ByteBuf arrayData;
static final ByteBuf directData;
@@ -82,10 +88,6 @@ static ByteBuffer jDirectData() {
}
void _invoke(Method m) throws Exception {
- String name = m.getName();
- if (name == null) {
- System.out.println();
- }
Object res = m.invoke(this, (Object[]) null);
byte[] bytes;
if (res instanceof ByteBuf) {
@@ -107,7 +109,7 @@ void _invoke(Method m) throws Exception {
public void _testAll() throws Exception {
Method[] ms = TestByteBufCopy.class.getDeclaredMethods();
- List list = Util.array2List(ms);
+ List list = Arrays.asList(ms);
list.sort(Comparator.comparing(Method::getName));
for (int i = 0; i < list.size(); i++) {
diff --git a/firenio-test/src/main/java/test/io/buffer/TestBytebufAllocator.java b/firenio-test/src/main/java/test/io/buffer/TestBytebufAllocator.java
index d7cdf7dd7..7d58fd71b 100644
--- a/firenio-test/src/main/java/test/io/buffer/TestBytebufAllocator.java
+++ b/firenio-test/src/main/java/test/io/buffer/TestBytebufAllocator.java
@@ -19,10 +19,15 @@
import com.firenio.buffer.ByteBuf;
import com.firenio.common.Assert;
+import com.firenio.log.LoggerFactory;
public class TestBytebufAllocator {
+ static {
+ LoggerFactory.setEnableSLF4JLogger(false);
+ }
+
static final String data = "hello;hello;";
static final String data1 = "hello;";
diff --git a/firenio-test/src/main/java/test/others/ssl/SSLSocketClient.java b/firenio-test/src/main/java/test/others/ssl/SSLSocketClient.java
index 3878bfa20..eb1491ce0 100644
--- a/firenio-test/src/main/java/test/others/ssl/SSLSocketClient.java
+++ b/firenio-test/src/main/java/test/others/ssl/SSLSocketClient.java
@@ -60,7 +60,7 @@ public X509Certificate[] getAcceptedIssuers() {
context.init(null, new TrustManager[]{x509m}, new SecureRandom());
SSLSocketFactory factory = context.getSocketFactory();
- SSLSocket s = (SSLSocket) factory.createSocket("192.168.133.134", 1443);
+ SSLSocket s = (SSLSocket) factory.createSocket("localhost", 443);
System.out.println("ok");
OutputStream output = s.getOutputStream();
From 9939a95998a9c319443140b6803021fbe7066f85 Mon Sep 17 00:00:00 2001
From: Kai Wang <8738115@qq.com>
Date: Mon, 16 Sep 2019 11:57:34 +0800
Subject: [PATCH 13/16] [Version] Update to 1.3.1 (#46)
* bug fix, PooledByteBufAllocator stop
* bug fix, ReConnector
* add direct io support
* add wrap unsafe ByteBuf
* add define openssl path
* update wildfly to 1.0.8
* update allow user set channel read buffer
* rm tls1.0 support
---
README-en.md | 4 ++--
README.md | 4 ++--
firenio-all/pom.xml | 2 +-
firenio-codec/pom.xml | 2 +-
firenio-core/pom.xml | 2 +-
firenio/pom.xml | 2 +-
6 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/README-en.md b/README-en.md
index fa52ac044..143161714 100644
--- a/README-en.md
+++ b/README-en.md
@@ -2,7 +2,7 @@
# FireNio Project
[](https://www.firenio.com)
-[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
+[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
[](https://github.com/firenio/firenio/blob/master/LICENSE.txt)
FireNio is an io framework which can build network project fast, it based on java nio, it is popular with Developers because of simple and easy of use APIs and high-performance.
@@ -28,7 +28,7 @@ FireNio is an io framework which can build network project fast, it based on jav
com.firenio
firenio-all
- 1.3.0
+ 1.3.1
```
diff --git a/README.md b/README.md
index 6bcf5d30c..ffb65244b 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
# FireNio Project
[](https://www.firenio.com)
-[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
+[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
[](https://github.com/firenio/firenio/blob/master/LICENSE.txt)
FireNio是基于java nio开发的一款可快速构建网络通讯项目的异步IO框架,其以简单易用的API和优良的性能深受开发者喜爱。
@@ -29,7 +29,7 @@ FireNio是基于java nio开发的一款可快速构建网络通讯项目的异
com.firenio
firenio-all
- 1.3.0
+ 1.3.1
```
diff --git a/firenio-all/pom.xml b/firenio-all/pom.xml
index d08e53484..353a42382 100644
--- a/firenio-all/pom.xml
+++ b/firenio-all/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1-SNAPSHOT
+ 1.3.1
../firenio/pom.xml
diff --git a/firenio-codec/pom.xml b/firenio-codec/pom.xml
index da9faaae0..669debc58 100644
--- a/firenio-codec/pom.xml
+++ b/firenio-codec/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1-SNAPSHOT
+ 1.3.1
../firenio/pom.xml
diff --git a/firenio-core/pom.xml b/firenio-core/pom.xml
index 4fce3cd5b..df0f12ac0 100644
--- a/firenio-core/pom.xml
+++ b/firenio-core/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1-SNAPSHOT
+ 1.3.1
../firenio/pom.xml
diff --git a/firenio/pom.xml b/firenio/pom.xml
index bc37277b9..73efd59cd 100644
--- a/firenio/pom.xml
+++ b/firenio/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.firenio
firenio
- 1.3.1-SNAPSHOT
+ 1.3.1
${project.artifactId}
An io framework project based on java nio
https://github.com/firenio/firenio
From 50a90d034157da928cf3db4b8591f9c2641d0f3d Mon Sep 17 00:00:00 2001
From: Kai Wang <8738115@qq.com>
Date: Mon, 16 Sep 2019 12:04:03 +0800
Subject: [PATCH 14/16] [Version] Update to 1.3.2-SNAPSHOT (#47)
---
firenio-all/pom.xml | 2 +-
firenio-codec/pom.xml | 2 +-
firenio-core/pom.xml | 2 +-
firenio-sample/sample-http/pom.xml | 2 +-
firenio/pom.xml | 2 +-
5 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/firenio-all/pom.xml b/firenio-all/pom.xml
index 353a42382..54f767fe1 100644
--- a/firenio-all/pom.xml
+++ b/firenio-all/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1
+ 1.3.2-SNAPSHOT
../firenio/pom.xml
diff --git a/firenio-codec/pom.xml b/firenio-codec/pom.xml
index 669debc58..cf8dc5041 100644
--- a/firenio-codec/pom.xml
+++ b/firenio-codec/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1
+ 1.3.2-SNAPSHOT
../firenio/pom.xml
diff --git a/firenio-core/pom.xml b/firenio-core/pom.xml
index df0f12ac0..4c1411ea0 100644
--- a/firenio-core/pom.xml
+++ b/firenio-core/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.1
+ 1.3.2-SNAPSHOT
../firenio/pom.xml
diff --git a/firenio-sample/sample-http/pom.xml b/firenio-sample/sample-http/pom.xml
index 366a66aca..e746adc91 100644
--- a/firenio-sample/sample-http/pom.xml
+++ b/firenio-sample/sample-http/pom.xml
@@ -117,7 +117,7 @@
com.firenio
firenio-codec
- 1.3.1-SNAPSHOT
+ 1.3.2-SNAPSHOT
diff --git a/firenio/pom.xml b/firenio/pom.xml
index 73efd59cd..32e62bf29 100644
--- a/firenio/pom.xml
+++ b/firenio/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.firenio
firenio
- 1.3.1
+ 1.3.2-SNAPSHOT
${project.artifactId}
An io framework project based on java nio
https://github.com/firenio/firenio
From 4c4e0799bd4a6de7cf5052a3520f417eaf085173 Mon Sep 17 00:00:00 2001
From: Kai Wang <8738115@qq.com>
Date: Fri, 22 Nov 2019 12:13:03 +0800
Subject: [PATCH 15/16] [Version] Update to 1.3.2 (#51)
* bug fix IntMap
* bug fix handle NOT_HANDSHAKING
* add AttributeMap
* add ByteBuf reset
* add ByteBuf.getMarkedReadIndex/getMarkedWriteIndex
* update HttpCodec
* update channel port(int) >> short
* update catch(exception) >> catch(throwable)
* update ChannelConnector.channelEstablish
* update PooledByteBufAllocator.BitSet >> InternalBitSet
* update make idle time absolutely
* update recycle ByteBuf(do not use buf recycle)
---
README-en.md | 4 +-
README.md | 4 +-
firenio-all/pom.xml | 2 +-
firenio-codec/pom.xml | 2 +-
.../firenio/codec/http11/ClientHttpCodec.java | 3 +-
.../com/firenio/codec/http11/HttpCodec.java | 84 +++---
.../com/firenio/codec/http11/HttpHeader.java | 14 +-
.../com/firenio/codec/http11/KMPUtil.java | 139 ++++-----
.../firenio/codec/http11/WebSocketCodec.java | 43 +--
firenio-core/pom.xml | 2 +-
.../src/main/java/com/firenio/Develop.java | 12 +-
.../main/java/com/firenio/DevelopConfig.java | 10 +-
.../src/main/java/com/firenio/LifeCycle.java | 16 +-
.../java/com/firenio/LifeCycleListener.java | 2 +-
.../src/main/java/com/firenio/Options.java | 7 +-
.../main/java/com/firenio/buffer/ByteBuf.java | 35 ++-
.../firenio/buffer/ByteBufAllocatorGroup.java | 2 +-
.../java/com/firenio/buffer/HeapByteBuf.java | 2 +-
.../buffer/PooledByteBufAllocator.java | 137 +++++----
.../firenio/buffer/PooledDirectByteBuf.java | 21 +-
.../com/firenio/buffer/PooledHeapByteBuf.java | 21 +-
.../firenio/buffer/PooledUnsafeByteBuf.java | 21 +-
.../firenio/buffer/UnpooledDirectByteBuf.java | 11 +
.../firenio/buffer/UnpooledHeapByteBuf.java | 20 ++
.../firenio/buffer/UnpooledUnsafeByteBuf.java | 11 +
.../com/firenio/buffer/UnsafeByteBuf.java | 2 +-
.../com/firenio/collection/AttributeKey.java | 54 ++++
.../com/firenio/collection/AttributeMap.java | 150 ++++++++++
.../firenio/collection/AttributesImpl.java | 56 ----
.../java/com/firenio/collection/IntArray.java | 8 +
.../java/com/firenio/collection/IntMap.java | 246 ++++++++-------
.../com/firenio/collection/ObjectPool.java | 96 ++++++
.../java/com/firenio/common/ByteUtil.java | 22 +-
.../main/java/com/firenio/common/Unsafe.java | 4 +-
.../main/java/com/firenio/common/Util.java | 79 ++---
.../java/com/firenio/component/Channel.java | 92 ++++--
.../component/ChannelActiveListener.java | 2 +-
.../component/ChannelAliveListener.java | 2 +-
.../firenio/component/ChannelConnector.java | 81 ++---
.../component/ChannelIdleListener.java | 2 +-
.../component/ExceptionCaughtHandle.java | 2 +-
.../firenio/component/FastThreadLocal.java | 74 ++---
.../com/firenio/component/IoEventHandle.java | 2 +-
.../LoggerExceptionCaughtHandle.java | 2 +-
.../java/com/firenio/component/Native.java | 40 ++-
.../com/firenio/component/NioEventLoop.java | 185 +++++++-----
.../firenio/component/NioEventLoopGroup.java | 15 +-
.../com/firenio/component/ProtocolCodec.java | 5 -
.../SilentExceptionCaughtHandle.java | 2 +-
.../com/firenio/component/SslContext.java | 7 +-
.../firenio/concurrent/ExecutorEventLoop.java | 2 +-
.../firenio/concurrent/ThreadEventLoop.java | 4 +-
firenio-homepage/http-startup.sh | 7 +-
firenio-sample/sample-http/pom.xml | 2 +-
.../java/sample/http11/HttpFrameHandle.java | 2 +-
.../sample/http11/proxy/HttpProxyServer.java | 12 +-
.../proxy4cloud/HttpProxy4CloudServer.java | 2 +
.../startup/TestHttpBootstrapEngine.java | 23 +-
.../src/main/resources/app/html/favicon.ico | Bin 7638 -> 4286 bytes
.../src/main/resources/server.properties | 2 +-
firenio-test/pom.xml | 12 +-
firenio-test/src/main/java/test/io/Test1.java | 52 ----
firenio-test/src/main/java/test/io/Test7.java | 58 ++++
firenio-test/src/main/java/test/io/Test8.java | 79 +++++
.../main/java/test/io/TestByteBufSkip.java | 71 +++++
.../src/main/java/test/io/TestFloat.java | 59 ++++
.../src/main/java/test/io/TestJDKBug1.java | 38 +++
.../src/main/java/test/io/TestLoad.java | 164 ++++++++++
.../src/main/java/test/io/TestLoad1.java | 47 +++
.../src/main/java/test/io/TestMMap.java | 55 ++++
.../java/test/io/TestMulticastSocket.java | 39 +++
.../src/main/java/test/io/jni/TestJNA.java | 18 +-
.../test/io/lenthvalue/TestByteBufPool.java | 45 +++
.../io/load/http11/TestHttpLoadServerTFB.java | 46 +--
.../test/io/load/http11/TestLoadClient.java | 99 +++---
.../main/java/test/others/FibonacciSeq.java | 2 +-
.../main/java/test/others/GraphicsTest.java | 16 +-
.../src/main/java/test/others/TestBitSet.java | 94 ++++++
.../src/main/java/test/others/TestIntMap.java | 30 +-
.../main/java/test/others/TestIntMap1.java | 81 +++++
.../java/test/others/TestNettyByteBuf.java | 49 +++
.../main/java/test/others/TestShutdown.java | 61 ++++
.../main/java/test/others/TestVolatile.java | 37 +++
.../main/java/test/others/TestVolatile2.java | 39 +++
.../test/others/algorithm/BubbleSort.java | 12 +-
.../java/test/others/algorithm/ByteArray.java | 138 ---------
.../test/others/algorithm/InsertSort.java | 111 +++++++
.../java/test/others/algorithm/KMPUtil.java | 152 ----------
.../java/test/others/algorithm/QuickSort.java | 18 +-
.../test/others/algorithm/QuickSort1.java | 70 +++++
.../test/others/algorithm/QuickSort2.java | 58 ++--
.../java/test/others/algorithm/RSAUtil.java | 281 ------------------
.../java/test/others/algorithm/TestSort.java | 94 ++++++
.../java/test/others/netty/EchoServer.java | 28 +-
.../java/test/others/netty/HelloClient.java | 1 -
.../java/test/others/netty/MyNettyServer.java | 6 +-
.../java/test/others/netty/NettyClient.java | 5 +-
.../test/others/netty/NettyClientThread.java | 4 +-
.../java/test/others/netty/NettyUtil.java | 75 +++++
.../test/others/netty/TcpServerHandler.java | 14 +-
.../others/netty/TestLoadEchoClient1.java | 4 +-
firenio/pom.xml | 2 +-
102 files changed, 2655 insertions(+), 1620 deletions(-)
create mode 100644 firenio-core/src/main/java/com/firenio/collection/AttributeKey.java
create mode 100644 firenio-core/src/main/java/com/firenio/collection/AttributeMap.java
delete mode 100644 firenio-core/src/main/java/com/firenio/collection/AttributesImpl.java
create mode 100644 firenio-core/src/main/java/com/firenio/collection/ObjectPool.java
create mode 100644 firenio-test/src/main/java/test/io/Test7.java
create mode 100644 firenio-test/src/main/java/test/io/Test8.java
create mode 100644 firenio-test/src/main/java/test/io/TestByteBufSkip.java
create mode 100644 firenio-test/src/main/java/test/io/TestFloat.java
create mode 100644 firenio-test/src/main/java/test/io/TestJDKBug1.java
create mode 100644 firenio-test/src/main/java/test/io/TestLoad.java
create mode 100644 firenio-test/src/main/java/test/io/TestLoad1.java
create mode 100644 firenio-test/src/main/java/test/io/TestMMap.java
create mode 100644 firenio-test/src/main/java/test/io/TestMulticastSocket.java
rename firenio-core/src/main/java/com/firenio/collection/Attributes.java => firenio-test/src/main/java/test/io/jni/TestJNA.java (64%)
create mode 100644 firenio-test/src/main/java/test/io/lenthvalue/TestByteBufPool.java
create mode 100644 firenio-test/src/main/java/test/others/TestBitSet.java
create mode 100644 firenio-test/src/main/java/test/others/TestIntMap1.java
create mode 100644 firenio-test/src/main/java/test/others/TestNettyByteBuf.java
create mode 100644 firenio-test/src/main/java/test/others/TestShutdown.java
create mode 100644 firenio-test/src/main/java/test/others/TestVolatile.java
create mode 100644 firenio-test/src/main/java/test/others/TestVolatile2.java
delete mode 100644 firenio-test/src/main/java/test/others/algorithm/ByteArray.java
create mode 100644 firenio-test/src/main/java/test/others/algorithm/InsertSort.java
delete mode 100644 firenio-test/src/main/java/test/others/algorithm/KMPUtil.java
create mode 100644 firenio-test/src/main/java/test/others/algorithm/QuickSort1.java
delete mode 100644 firenio-test/src/main/java/test/others/algorithm/RSAUtil.java
create mode 100644 firenio-test/src/main/java/test/others/algorithm/TestSort.java
create mode 100644 firenio-test/src/main/java/test/others/netty/NettyUtil.java
diff --git a/README-en.md b/README-en.md
index 143161714..d9ebcc92f 100644
--- a/README-en.md
+++ b/README-en.md
@@ -2,7 +2,7 @@
# FireNio Project
[](https://www.firenio.com)
-[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
+[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
[](https://github.com/firenio/firenio/blob/master/LICENSE.txt)
FireNio is an io framework which can build network project fast, it based on java nio, it is popular with Developers because of simple and easy of use APIs and high-performance.
@@ -28,7 +28,7 @@ FireNio is an io framework which can build network project fast, it based on jav
com.firenio
firenio-all
- 1.3.1
+ 1.3.2
```
diff --git a/README.md b/README.md
index ffb65244b..419191a0e 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
# FireNio Project
[](https://www.firenio.com)
-[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
+[](http://mvnrepository.com/artifact/com.firenio/firenio-all)
[](https://github.com/firenio/firenio/blob/master/LICENSE.txt)
FireNio是基于java nio开发的一款可快速构建网络通讯项目的异步IO框架,其以简单易用的API和优良的性能深受开发者喜爱。
@@ -29,7 +29,7 @@ FireNio是基于java nio开发的一款可快速构建网络通讯项目的异
com.firenio
firenio-all
- 1.3.1
+ 1.3.2
```
diff --git a/firenio-all/pom.xml b/firenio-all/pom.xml
index 54f767fe1..c8530b9c8 100644
--- a/firenio-all/pom.xml
+++ b/firenio-all/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.2-SNAPSHOT
+ 1.3.2
../firenio/pom.xml
diff --git a/firenio-codec/pom.xml b/firenio-codec/pom.xml
index cf8dc5041..6d3c13c39 100644
--- a/firenio-codec/pom.xml
+++ b/firenio-codec/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.2-SNAPSHOT
+ 1.3.2
../firenio/pom.xml
diff --git a/firenio-codec/src/main/java/com/firenio/codec/http11/ClientHttpCodec.java b/firenio-codec/src/main/java/com/firenio/codec/http11/ClientHttpCodec.java
index 5ed7beba1..828d50da5 100644
--- a/firenio-codec/src/main/java/com/firenio/codec/http11/ClientHttpCodec.java
+++ b/firenio-codec/src/main/java/com/firenio/codec/http11/ClientHttpCodec.java
@@ -77,7 +77,7 @@ public ByteBuf encode(Channel ch, Frame frame) {
int len_len = 32 - len_idx;
int len = mtd_bytes.length + 1 + url_bytes.length + PROTOCOL.length + len_len + 2;
int header_size = 0;
- List bytes_array = getEncodeBytesArray(FastThreadLocal.get());
+ List bytes_array = (List) FastThreadLocal.get().getList();
IntMap headers = f.getRequestHeaders();
if (headers != null) {
headers.remove(HttpHeader.Content_Length.getId());
@@ -94,7 +94,6 @@ public ByteBuf encode(Channel ch, Frame frame) {
len += k.length;
len += v.length;
}
- headers.finishScan();
}
len += 2;
if (isArray) {
diff --git a/firenio-codec/src/main/java/com/firenio/codec/http11/HttpCodec.java b/firenio-codec/src/main/java/com/firenio/codec/http11/HttpCodec.java
index b46a19add..f02410a15 100644
--- a/firenio-codec/src/main/java/com/firenio/codec/http11/HttpCodec.java
+++ b/firenio-codec/src/main/java/com/firenio/codec/http11/HttpCodec.java
@@ -22,8 +22,10 @@
import com.firenio.Develop;
import com.firenio.buffer.ByteBuf;
+import com.firenio.collection.AttributeKey;
import com.firenio.collection.ByteTree;
import com.firenio.collection.IntMap;
+import com.firenio.collection.Stack;
import com.firenio.common.ByteUtil;
import com.firenio.common.Util;
import com.firenio.component.Channel;
@@ -37,24 +39,23 @@
*/
public class HttpCodec extends ProtocolCodec {
- static final byte[] CONTENT_LENGTH_MATCH = ByteUtil.b("Content-Length:");
- static final int decode_state_body = 2;
- static final int decode_state_complete = 3;
- static final int decode_state_header = 1;
- static final int decode_state_line_one = 0;
- static final int encode_bytes_arrays_index = nextIndexedVariablesIndex();
- static final int content_len_index = nextIndexedVariablesIndex();
- static final String FRAME_CACHE_KEY = "_HTTP_FRAME_CACHE_KEY";
- static final byte N = '\n';
- static final IOException OVER_LIMIT = EXCEPTION("over writeIndex");
- static final IOException ILLEGAL_METHOD = EXCEPTION("illegal http method");
- static final byte R = '\r';
- static final byte SPACE = ' ';
- static final int NUM_GET = ByteUtil.getInt("GET ".getBytes(), 0);
- static final int NUM_POST = ByteUtil.getInt("POST".getBytes(), 0);
+ static final byte[] CONTENT_LENGTH_MATCH = ByteUtil.b("Content-Length:");
+ static final int decode_state_body = 2;
+ static final int decode_state_complete = 3;
+ static final int decode_state_header = 1;
+ static final int decode_state_line_one = 0;
+ static final AttributeKey> FRAME_CACHE_KEY = NioEventLoop.valueOfKey("http_frame_cache_key");
+ static final byte N = '\n';
+ static final IOException OVER_LIMIT = EXCEPTION("over writeIndex");
+ static final IOException ILLEGAL_METHOD = EXCEPTION("illegal http method");
+ static final byte R = '\r';
+ static final byte SPACE = ' ';
+ static final int NUM_GET = ByteUtil.getIntLE("GET ".getBytes(), 0);
+ static final int NUM_POST = ByteUtil.getIntLE("POST".getBytes(), 0);
private final int blimit;
private final byte[][] cl_bytes = new byte[1024][];
+ private final int com_threshold;
private final int hlimit;
private final int fcache;
private final boolean lite;
@@ -79,20 +80,21 @@ public HttpCodec(String server, int frameCache) {
}
public HttpCodec(String server, int frameCache, boolean lite, boolean inline) {
- this(server, frameCache, 1024 * 8, 1024 * 256, lite, inline, null);
+ this(server, frameCache, 1024 * 8, 1024 * 256, 1024 * 4, lite, inline, null);
}
public HttpCodec(String server, int frameCache, boolean lite, boolean inline, ByteTree cachedUrls) {
- this(server, frameCache, 1024 * 8, 1024 * 256, lite, inline, cachedUrls);
+ this(server, frameCache, 1024 * 8, 1024 * 256, 1024 * 4, lite, inline, cachedUrls);
}
- public HttpCodec(String server, int fcache, int hlimit, int blimit, boolean lite, boolean inline, ByteTree cachedUrls) {
+ public HttpCodec(String server, int fcache, int hlimit, int blimit, int com_threshold, boolean lite, boolean inline, ByteTree cachedUrls) {
this.lite = lite;
this.inline = inline;
this.hlimit = hlimit;
this.blimit = blimit;
this.fcache = fcache;
this.cached_urls = cachedUrls;
+ this.com_threshold = com_threshold;
ByteBuffer temp = ByteBuffer.allocate(128);
if (server == null) {
temp.put(ByteUtil.b("\r\nContent-Length: "));
@@ -161,11 +163,6 @@ protected static void parse_url(HttpFrame f, int skip, CharSequence line) {
}
}
- @SuppressWarnings("unchecked")
- static List getEncodeBytesArray(FastThreadLocal l) {
- return (List) l.getList(encode_bytes_arrays_index);
- }
-
private static int read_line(StringBuilder line, ByteBuf src, int abs_pos, int length, int limit) throws IOException {
int maybeRead = limit - length;
int s_limit = src.absWriteIndex();
@@ -249,7 +246,7 @@ private int decode_lite(ByteBuf src, HttpFrame f) throws IOException {
h_len += (l_end - abs_pos);
decode_state = decode_state_header;
int url_start = abs_pos;
- int num = src.getInt(abs_pos);
+ int num = src.getIntLE(abs_pos);
if (num == NUM_GET) {
f.setMethod(HttpMethod.GET);
url_start += 4;
@@ -424,25 +421,13 @@ int decode_remain_body(Channel ch, ByteBuf src, HttpFrame f) {
}
}
- private byte[] get_c_len_buf(FastThreadLocal l) {
- byte[] bb = (byte[]) l.getIndexedVariable(content_len_index);
- if (bb == null) {
- int limit = cl_buf.limit();
- bb = new byte[cl_buf.limit() + 16];
- cl_buf.get(bb, 0, limit);
- cl_buf.clear().limit(limit);
- l.setIndexedVariable(content_len_index, bb);
- }
- return bb;
- }
-
@Override
public ByteBuf encode(final Channel ch, Frame frame) {
boolean inline = this.inline;
HttpFrame f = (HttpFrame) frame;
FastThreadLocal l = FastThreadLocal.get();
HttpAttachment att = (HttpAttachment) ch.getAttachment();
- List bytes_array = getEncodeBytesArray(l);
+ List bytes_array = (List) l.getList();
Object content = f.getContent();
ByteBuf content_buf = null;
byte[] content_array = null;
@@ -454,7 +439,7 @@ public ByteBuf encode(final Channel ch, Frame frame) {
int write_size = 0;
if (content instanceof ByteBuf) {
content_buf = ((ByteBuf) content);
- write_size = content_buf.writeIndex();
+ write_size = content_buf.readableBytes();
} else if (content instanceof byte[]) {
is_array = true;
content_array = (byte[]) content;
@@ -466,7 +451,7 @@ public ByteBuf encode(final Channel ch, Frame frame) {
cl_len_bytes = cl_bytes[write_size];
cl_len = cl_len_bytes.length;
} else {
- cl_len_bytes = get_c_len_buf(l);
+ cl_len_bytes = cl_buf.array();
int tmp_len = cl_buf.limit();
int len_idx = Util.valueOf(write_size, cl_len_bytes);
int num_len = cl_len_bytes.length - len_idx;
@@ -532,15 +517,23 @@ public ByteBuf encode(final Channel ch, Frame frame) {
buf.writeByte(R);
buf.writeByte(N);
if (write_size > 0) {
- if (is_array) {
- buf.writeBytes(content_array);
- } else {
+ if (write_size > com_threshold) {
+ if (is_array) {
+ content_buf = ByteBuf.wrap(content_array);
+ }
if (inline) {
att.setLastWriteBuf(ByteBuf.empty());
}
ch.write(buf);
- ch.write(content_buf);
+ ch.writeAndFlush(content_buf);
return null;
+ } else {
+ if (is_array) {
+ buf.writeBytes(content_array);
+ } else {
+ buf.writeBytes(content_buf);
+ content_buf.release();
+ }
}
}
return offer ? buf : null;
@@ -600,10 +593,11 @@ int header_complete(HttpFrame f) throws IOException {
}
protected void parse_line_one(HttpFrame f, CharSequence line) throws IOException {
- if (line.charAt(0) == 'G' && line.charAt(1) == 'E' && line.charAt(2) == 'T') {
+ int v = (line.charAt(0) << 0) | (line.charAt(1) << 8) | (line.charAt(2) << 16) | (line.charAt(3) << 24);
+ if (v == NUM_GET) {
f.setMethod(HttpMethod.GET);
parse_url(f, 4, line);
- } else if (line.charAt(0) == 'P' && line.charAt(1) == 'O' && line.charAt(2) == 'S' && line.charAt(3) == 'T') {
+ } else if (v == NUM_POST) {
f.setMethod(HttpMethod.POST);
parse_url(f, 5, line);
} else {
diff --git a/firenio-codec/src/main/java/com/firenio/codec/http11/HttpHeader.java b/firenio-codec/src/main/java/com/firenio/codec/http11/HttpHeader.java
index 13dceaedb..3c06716cf 100644
--- a/firenio-codec/src/main/java/com/firenio/codec/http11/HttpHeader.java
+++ b/firenio-codec/src/main/java/com/firenio/codec/http11/HttpHeader.java
@@ -70,15 +70,11 @@ public enum HttpHeader {
private static final HttpHeader[] enums;
static {
- try {
- enums = new HttpHeader[values().length];
- for (HttpHeader value : values()) {
- enums[value.id] = value;
- ALL.put(value.getKey(), value);
- ALL.put(value.getLowercase(), value);
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
+ enums = new HttpHeader[values().length];
+ for (HttpHeader value : values()) {
+ enums[value.id] = value;
+ ALL.put(value.getKey(), value);
+ ALL.put(value.getLowercase(), value);
}
}
diff --git a/firenio-codec/src/main/java/com/firenio/codec/http11/KMPUtil.java b/firenio-codec/src/main/java/com/firenio/codec/http11/KMPUtil.java
index c199f957a..761b28547 100644
--- a/firenio-codec/src/main/java/com/firenio/codec/http11/KMPUtil.java
+++ b/firenio-codec/src/main/java/com/firenio/codec/http11/KMPUtil.java
@@ -15,7 +15,6 @@
*/
package com.firenio.codec.http11;
-import com.firenio.collection.IntArray;
import com.firenio.common.Util;
//关键字:前缀,后缀,部分匹配表
@@ -28,53 +27,50 @@ public class KMPUtil {
private String match_value;
public KMPUtil(String value) {
- this.initialize(value);
- }
-
- public static void main(String[] args) {
-
- String s1 = "1111111111111111111211111111112111121111211111111";
-
- String match = "1112";
-
- KMPUtil kmp = new KMPUtil(match);
-
- System.out.println(kmp.match_all(s1));
-
- }
-
- private void initialize(String value) {
if (Util.isNullOrBlank(value)) {
throw new IllegalArgumentException("null value");
}
this.match_value = value;
this.match_array = match_value.toCharArray();
- this.match_table = new int[match_value.length()];
- this.initialize_part_match_table();
+ this.match_table = new int[match_value.length() - 1];
+ int length = this.match_value.length() - 1;
+ // 只需要计算长度减一位的部分匹配表,因为最后一位如果也匹配整个字符串就匹配了
+ // 直接从第二位开始比较
+ for (int i = 1; i < length; i++) {
+ match_table[i] = init_table(value, i + 1);
+ }
}
- private void initialize_part_match_table() {
- int length = this.match_value.length();
- // 直接从两位开始比较
- for (int i = 2; i < length; i++) {
- match_table[i] = initialize_part_match_table0(match_array, i);
+ public static void main(String[] args) {
+
+ String src = "ABC ABCA ABCABA";
+ String match = "ABCAB";
+ src = "AABAABAABC";
+ match = "AABAABC";
+ KMPUtil kmp = new KMPUtil(match);
+ int find = 0;
+ for (; ; ) {
+ find = kmp.match(src, find);
+ if (find == -1) {
+ break;
+ }
+ System.out.println(find);
+ find += match.length();
}
+
}
- private int initialize_part_match_table0(char[] array, int length) {
+ private int init_table(String value, int length) {
int e = 0;
WORD:
for (int i = 1; i < length; i++) {
- int t = 0;
- int p = 0;
- int s = length - i;
for (int j = 0; j < i; j++) {
- if (array[p++] != array[s++]) {
+ int fix = length - i;
+ if (value.charAt(j) != value.charAt(fix + j)) {
continue WORD;
}
- t++;
}
- e = t;
+ e = i;
}
return e;
}
@@ -83,70 +79,35 @@ public int match(String value) {
return match(value, 0);
}
- public int match(String value, int begin) {
- if (Util.isNullOrBlank(value) || begin < 0) {
- return -1;
- }
- if (value.length() - begin < this.match_array.length) {
+ public int match(String value, int offset) {
+ if (value.length() - offset < match_array.length) {
return -1;
}
- int source_length = value.length();
- int index = begin;
- int match_length = this.match_array.length;
- char[] match_array = this.match_array;
- int[] match_table = this.match_table;
- LOOP:
- for (; index < source_length; ) {
- for (int i = 0; i < match_length; i++) {
- if (value.charAt(index + i) != match_array[i]) {
- if (i == 0) {
- index++;
- } else {
- index += (i - match_table[i]);
- }
- continue LOOP;
+ int src_length = value.length();
+ int mat_index = 0;
+ int src_index = offset;
+ int match_length = this.match_array.length;
+ char[] match_array = this.match_array;
+ int[] match_table = this.match_table;
+ for (; src_index < src_length; ) {
+ char s_c = value.charAt(src_index);
+ char m_c = match_array[mat_index];
+ if (s_c == m_c) {
+ mat_index++;
+ src_index++;
+ if (mat_index == match_length) {
+ return src_index - match_length;
+ }
+ } else {
+ if (mat_index == 0) {
+ src_index++;
+ } else {
+ // 第mat_index位不匹配,查找这以前的匹配情况(-1)
+ mat_index = match_table[mat_index - 1];
}
}
- return index;
}
return -1;
}
- public IntArray match_all(String value) {
- if (Util.isNullOrBlank(value)) {
- return null;
- }
- if (value.length() < match_value.length()) {
- return null;
- }
- IntArray matchs = new IntArray();
- if (value.equals(match_value)) {
- matchs.add(0);
- return matchs;
- }
- int source_length = value.length();
- int index = 0;
- int match_length = this.match_array.length;
- char[] match_array = this.match_array;
- int[] match_table = this.match_table;
- LOOP:
- for (; index < source_length; ) {
- if (source_length - index < match_length) {
- break;
- }
- for (int i = 0; i < match_length; i++) {
- if (value.charAt(index + i) != match_array[i]) {
- if (i == 0) {
- index++;
- } else {
- index += (i - match_table[i]);
- }
- continue LOOP;
- }
- }
- matchs.add(index);
- index += match_length;
- }
- return matchs;
- }
}
diff --git a/firenio-codec/src/main/java/com/firenio/codec/http11/WebSocketCodec.java b/firenio-codec/src/main/java/com/firenio/codec/http11/WebSocketCodec.java
index 116aba3ce..3245ae605 100644
--- a/firenio-codec/src/main/java/com/firenio/codec/http11/WebSocketCodec.java
+++ b/firenio-codec/src/main/java/com/firenio/codec/http11/WebSocketCodec.java
@@ -16,12 +16,10 @@
package com.firenio.codec.http11;
import java.io.IOException;
-import java.util.List;
import com.firenio.buffer.ByteBuf;
import com.firenio.component.Channel;
import com.firenio.component.Frame;
-import com.firenio.component.NioEventLoop;
import com.firenio.component.ProtocolCodec;
//FIXME 心跳貌似由服务端发起
@@ -53,7 +51,6 @@
*/
public final class WebSocketCodec extends ProtocolCodec {
- public static final String FRAME_STACK_KEY = "FRAME_STACK_KEY_WS";
public static final int HEADER_LENGTH = 2;
public static final int MAX_HEADER_LENGTH = 10;
public static final int MAX_UNSIGNED_SHORT = 0xffff;
@@ -83,20 +80,15 @@ public final class WebSocketCodec extends ProtocolCodec {
PONG.writeByte((byte) (o_h1 | 0));
}
- private final int frameStackSize;
private final int limit;
public WebSocketCodec() {
this(1024 * 64);
}
- public WebSocketCodec(int limit) {
- this(limit, 0);
- }
- public WebSocketCodec(int limit, int frameStackSize) {
+ public WebSocketCodec(int limit) {
this.limit = limit;
- this.frameStackSize = frameStackSize;
}
@Override
@@ -181,7 +173,7 @@ public Frame decode(Channel ch, ByteBuf src) throws IOException {
} else {
src.readBytes(array);
}
- WebSocketFrame f = newWebSocketFrame(ch, opcode);
+ WebSocketFrame f = new WebSocketFrame(opcode);
if (opcode == TYPE_TEXT) {
f.setContent(new String(array, ch.getCharset()));
} else if (opcode == TYPE_BINARY) {
@@ -216,10 +208,6 @@ public ByteBuf encode(Channel ch, Frame frame) throws IOException {
return buf;
}
- public int getFrameStackSize() {
- return frameStackSize;
- }
-
@Override
public String getProtocolId() {
return PROTOCOL_ID;
@@ -230,36 +218,9 @@ public int getHeaderLength() {
return MAX_HEADER_LENGTH;
}
- private WebSocketFrame newWebSocketFrame(Channel ch, byte opcode) {
- if (frameStackSize > 0) {
- // NioEventLoop eventLoop = ch.getEventLoop();
- // FixedThreadStack stack = (FixedThreadStack) eventLoop
- // .getAttribute(FRAME_STACK_KEY);
- // if (stack == null) {
- // stack = new FixedThreadStack<>(frameStackSize);
- // eventLoop.setAttribute(FRAME_STACK_KEY, stack);
- // }
- // WebSocketFrame frame = stack.pop();
- // if (frame == null) {
- // return new WebSocketFrame(ch, writeIndex);
- // }
- // return frame.reset(ch, writeIndex);
- }
- return new WebSocketFrame(opcode);
- }
-
@Override
protected ByteBuf getPingBuf() {
return PING.duplicate();
}
- @SuppressWarnings("unchecked")
- public void release(NioEventLoop eventLoop, Frame frame) {
- //FIXME ..final stack is null or not null
- List stack = (List) eventLoop.getAttribute(FRAME_STACK_KEY);
- if (stack != null && stack.size() < frameStackSize) {
- stack.add((WebSocketFrame) frame);
- }
- }
-
}
diff --git a/firenio-core/pom.xml b/firenio-core/pom.xml
index 4c1411ea0..c8b4921ea 100644
--- a/firenio-core/pom.xml
+++ b/firenio-core/pom.xml
@@ -10,7 +10,7 @@
com.firenio
firenio
- 1.3.2-SNAPSHOT
+ 1.3.2
../firenio/pom.xml
diff --git a/firenio-core/src/main/java/com/firenio/Develop.java b/firenio-core/src/main/java/com/firenio/Develop.java
index 9ccc6f734..425b3d239 100644
--- a/firenio-core/src/main/java/com/firenio/Develop.java
+++ b/firenio-core/src/main/java/com/firenio/Develop.java
@@ -22,11 +22,13 @@
*/
public class Develop {
- public static final boolean BUF_DEBUG = DevelopConfig.BUF_DEBUG;
- public static final boolean EPOLL_DEBUG = DevelopConfig.EPOLL_DEBUG;
- public static final String EPOLL_PATH = DevelopConfig.EPOLL_PATH;
- public static final boolean NATIVE_DEBUG = DevelopConfig.NATIVE_DEBUG;
- public static final boolean DEBUG_ERROR = Options.isDebugError();
+ public static final boolean BUF_DEBUG = DevelopConfig.BUF_DEBUG;
+ public static final boolean SSL_DEBUG = DevelopConfig.SSL_DEBUG;
+ public static final boolean EPOLL_DEBUG = DevelopConfig.EPOLL_DEBUG;
+ public static final String EPOLL_PATH = DevelopConfig.EPOLL_PATH;
+ public static final boolean NATIVE_DEBUG = DevelopConfig.NATIVE_DEBUG;
+ public static final boolean CHANNEL_DEBUG = DevelopConfig.CHANNEL_DEBUG;
+ public static final boolean DEBUG_ERROR = Options.isDebugError();
public static void debugException(Logger logger, Throwable e) {
if (DEBUG_ERROR) {
diff --git a/firenio-core/src/main/java/com/firenio/DevelopConfig.java b/firenio-core/src/main/java/com/firenio/DevelopConfig.java
index b1015b7b5..3dd960146 100644
--- a/firenio-core/src/main/java/com/firenio/DevelopConfig.java
+++ b/firenio-core/src/main/java/com/firenio/DevelopConfig.java
@@ -20,10 +20,12 @@
*/
public class DevelopConfig {
- public static boolean BUF_DEBUG = false;
- public static boolean EPOLL_DEBUG = false;
- public static String EPOLL_PATH = "/home/test/git-rep/firenio/firenio-epoll/obj/native.o";
- public static boolean NATIVE_DEBUG = false;
+ public static boolean BUF_DEBUG = false;
+ public static boolean SSL_DEBUG = false;
+ public static boolean EPOLL_DEBUG = false;
+ public static String EPOLL_PATH = "/home/test/git-rep/firenio/firenio-epoll/obj/native.o";
+ public static boolean NATIVE_DEBUG = false;
+ public static boolean CHANNEL_DEBUG = false;
}
diff --git a/firenio-core/src/main/java/com/firenio/LifeCycle.java b/firenio-core/src/main/java/com/firenio/LifeCycle.java
index c64b102e4..3603ad931 100644
--- a/firenio-core/src/main/java/com/firenio/LifeCycle.java
+++ b/firenio-core/src/main/java/com/firenio/LifeCycle.java
@@ -49,7 +49,7 @@ protected void checkNotRunning() {
protected abstract void doStop();
- private void fireEvent(int event, Exception exception) {
+ private void fireEvent(int event, Throwable exception) {
if (lifeCycleListeners.size() == 0) {
return;
}
@@ -57,7 +57,7 @@ private void fireEvent(int event, Exception exception) {
for (LifeCycleListener listener : lifeCycleListeners) {
try {
listener.lifeCycleStarting(this);
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
@@ -65,7 +65,7 @@ private void fireEvent(int event, Exception exception) {
for (LifeCycleListener listener : lifeCycleListeners) {
try {
listener.lifeCycleStarted(this);
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
@@ -73,7 +73,7 @@ private void fireEvent(int event, Exception exception) {
for (LifeCycleListener listener : lifeCycleListeners) {
try {
listener.lifeCycleStopping(this);
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
@@ -81,7 +81,7 @@ private void fireEvent(int event, Exception exception) {
for (LifeCycleListener listener : lifeCycleListeners) {
try {
listener.lifeCycleStopped(this);
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
@@ -89,7 +89,7 @@ private void fireEvent(int event, Exception exception) {
for (LifeCycleListener listener : lifeCycleListeners) {
try {
listener.lifeCycleFailure(this, exception);
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
@@ -130,7 +130,7 @@ public synchronized void start() throws Exception {
this.doStart();
this.state = RUNNING;
this.onStarted();
- } catch (Exception e) {
+ } catch (Throwable e) {
Util.stop(this);
this.state = FAILED;
this.fireEvent(state, e);
@@ -153,7 +153,7 @@ public synchronized void stop() {
}
try {
this.doStop();
- } catch (Exception e) {
+ } catch (Throwable e) {
logger.error(e.getMessage(), e);
}
this.state = STOPPED;
diff --git a/firenio-core/src/main/java/com/firenio/LifeCycleListener.java b/firenio-core/src/main/java/com/firenio/LifeCycleListener.java
index a8cd40dc0..9ea527361 100644
--- a/firenio-core/src/main/java/com/firenio/LifeCycleListener.java
+++ b/firenio-core/src/main/java/com/firenio/LifeCycleListener.java
@@ -22,7 +22,7 @@ public int compareTo(LifeCycleListener o) {
return sortIndex() - o.sortIndex();
}
- public void lifeCycleFailure(LifeCycle lifeCycle, Exception exception) {}
+ public void lifeCycleFailure(LifeCycle lifeCycle, Throwable exception) {}
public void lifeCycleStarted(LifeCycle lifeCycle) {}
diff --git a/firenio-core/src/main/java/com/firenio/Options.java b/firenio-core/src/main/java/com/firenio/Options.java
index 85750f4bf..73aa80653 100644
--- a/firenio-core/src/main/java/com/firenio/Options.java
+++ b/firenio-core/src/main/java/com/firenio/Options.java
@@ -16,7 +16,6 @@
package com.firenio;
import com.firenio.common.Util;
-import com.firenio.component.Native;
/**
* @author wangkai
@@ -81,7 +80,7 @@ public static boolean isEnableOpenssl() {
}
public static boolean isEnableUnsafeBuf() {
- return Native.EPOLL_AVAILABLE && isTrue(ENABLE_UNSAFE_BUF);
+ return isTrue(ENABLE_UNSAFE_BUF);
}
public static void setBufAutoExpansion(boolean auto) {
@@ -92,7 +91,11 @@ public static void setBufThreadYield(boolean yield) {
setBool(BUF_THREAD_YIELD, yield);
}
+ // NOTICE: DO NOT USE
public static void setBufRecycle(boolean recycle) {
+ if (recycle){
+ throw new RuntimeException("do not use BufRecycle");
+ }
setBool(BUF_RECYCLE, recycle);
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/ByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/ByteBuf.java
index 456d333ca..5c9ac1d6e 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/ByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/ByteBuf.java
@@ -162,7 +162,7 @@ public boolean retain() {
public abstract int capacity();
- public abstract void collation();
+ public abstract void compact();
protected void capacity(int cap) {}
@@ -437,7 +437,7 @@ public ByteBuf readIndex(int index) {
return this;
}
- protected ByteBuf produce(int unitOffset, int unitEnd) {
+ protected ByteBuf produce(int unitOffset, int unitSize) {
return this;
}
@@ -701,6 +701,22 @@ public int readableBytes() {
return absWriteIndex() - absReadIndex();
}
+ public void reset(byte[] memory) {
+ reset(memory, 0, memory.length);
+ }
+
+ public void reset(byte[] memory, int off, int len) {
+ throw unsupportedOperationException();
+ }
+
+ public void reset(long memory, int capacity, int off, int len) {
+ throw unsupportedOperationException();
+ }
+
+ public void reset(ByteBuffer memory) {
+ throw unsupportedOperationException();
+ }
+
public int writableBytes() {
return capacity() - writeIndex();
}
@@ -715,6 +731,14 @@ public ByteBuf resetReadIndex() {
return this;
}
+ public int getMarkedReadIndex() {
+ return marked_abs_read_index - offset();
+ }
+
+ public int getMarkedWriteIndex() {
+ return marked_abs_write_index - offset();
+ }
+
public int getMarkedAbsReadIndex() {
return marked_abs_read_index;
}
@@ -757,6 +781,8 @@ protected int unitOffset() {
protected void unitOffset(int unitOffset) {}
+ protected void recycleObject(){}
+
UnsupportedOperationException unsupportedOperationException() {
return new UnsupportedOperationException();
}
@@ -790,10 +816,7 @@ static void copy(long src, long dst, int len) {
}
static long toUnsignedInt(int value) {
- if (value < 0) {
- return value & 0xffffffffffffffffL;
- }
- return value;
+ return value & 0xffffffffL;
}
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/ByteBufAllocatorGroup.java b/firenio-core/src/main/java/com/firenio/buffer/ByteBufAllocatorGroup.java
index c120d5ed5..33b23b3b9 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/ByteBufAllocatorGroup.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/ByteBufAllocatorGroup.java
@@ -93,7 +93,7 @@ public boolean isDirect() {
public String[] toDebugString() {
String[] res = new String[groupSize];
for (int i = 0; i < res.length; i++) {
- res[i] = allocators[i].toString();
+ res[i] = allocators[i].toDebugString();
}
return res;
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/HeapByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/HeapByteBuf.java
index 4d7d63f85..beb3d3be3 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/HeapByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/HeapByteBuf.java
@@ -45,7 +45,7 @@ public long address() {
}
@Override
- public void collation() {
+ public void compact() {
if (!hasReadableBytes()) {
clear();
return;
diff --git a/firenio-core/src/main/java/com/firenio/buffer/PooledByteBufAllocator.java b/firenio-core/src/main/java/com/firenio/buffer/PooledByteBufAllocator.java
index 2161c7a8d..ae7eb691c 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/PooledByteBufAllocator.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/PooledByteBufAllocator.java
@@ -17,15 +17,13 @@
import java.nio.ByteBuffer;
import java.util.Arrays;
-import java.util.BitSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import com.firenio.Develop;
import com.firenio.Options;
-import com.firenio.collection.LinkedBQStack;
-import com.firenio.collection.Stack;
+import com.firenio.collection.ObjectPool;
import com.firenio.common.DateUtil;
import com.firenio.common.Unsafe;
import com.firenio.common.Util;
@@ -35,27 +33,26 @@
*/
public final class PooledByteBufAllocator extends ByteBufAllocator {
- public static final Map BUF_DEBUGS;
- public static final ByteBufException EXPANSION_FAILED = EXPANSION_FAILED();
- static final int BYTEBUF_BUFFER = 1024 * 8;
- static final boolean BYTEBUF_RECYCLE = Options.isBufRecycle();
+ public static final Map BUF_DEBUGS = NEW_BUF_DEBUGS();
+ public static final ByteBufException EXPANSION_FAILED = EXPANSION_FAILED();
+ static final int ADDRESS_BITS_PER_WORD = 6;
+ static final int BUF_POOL_SIZE = 1024 * 8;
+ static final boolean BUF_POOL_ENABLE = Options.isBufRecycle();
+ static final ThreadLocal> BUF_POOL = new ThreadLocal>() {
- static {
- if (Develop.BUF_DEBUG) {
- BUF_DEBUGS = new ConcurrentHashMap<>();
- } else {
- BUF_DEBUGS = null;
+ @Override
+ protected ObjectPool initialValue() {
+ return new ObjectPool<>(Thread.currentThread(), BUF_POOL_SIZE);
}
- }
+ };
private final int[] blockEnds;
- private final Stack bufBuffer;
private final int capacity;
- private final BitSet frees;
+ private final long[] frees;
private final boolean isDirect;
- private final ReentrantLock lock = new ReentrantLock();
private final int unit;
private final ByteBufAllocator unpooled;
+ private final ReentrantLock lock = new ReentrantLock();
private long address = -1;
private ByteBuffer directMemory;
private byte[] heapMemory;
@@ -65,14 +62,9 @@ public PooledByteBufAllocator(ByteBufAllocatorGroup group) {
this.unit = group.getUnit();
this.isDirect = group.isDirect();
this.capacity = group.getCapacity();
- this.frees = new BitSet(getCapacity());
+ this.frees = new long[getCapacity() / 8];
this.blockEnds = new int[getCapacity()];
this.unpooled = UnpooledByteBufAllocator.get();
- if (BYTEBUF_RECYCLE) {
- bufBuffer = new LinkedBQStack<>(BYTEBUF_BUFFER);
- } else {
- bufBuffer = null;
- }
}
static ByteBufException EXPANSION_FAILED() {
@@ -110,7 +102,7 @@ public ByteBuf allocate(int limit) {
// FIXME 是否申请java内存
return unpooled.allocate(limit);
} else {
- buf = newByteBuf().produce(blockStart, blockEnds[blockStart]);
+ buf = newByteBuf().produce(blockStart, size);
if (Develop.BUF_DEBUG) {
BufDebug d = new BufDebug();
d.buf = buf;
@@ -123,33 +115,36 @@ public ByteBuf allocate(int limit) {
// FIXME 判断余下的是否足够,否则退出循环
private int allocate(int start, int end, int size) {
- int freeSize = 0;
- int[] blockEnds = this.blockEnds;
- BitSet frees = this.frees;
- for (; start < end; ) {
- int pos = start;
- if (!frees.get(pos)) {
- start = blockEnds[pos];
- freeSize = 0;
+ int[] blockEnds = this.blockEnds;
+ int pos = start;
+ int limit = pos + size;
+ if (limit >= end) {
+ return -1;
+ }
+ for (; ; ) {
+ if (!isFree(pos)) {
+ pos = blockEnds[pos];
+ limit = pos + size;
+ if (limit >= end) {
+ return -1;
+ }
continue;
}
- if (++freeSize == size) {
+ if (++pos == limit) {
int blockEnd = pos + 1;
int blockStart = blockEnd - size;
- frees.set(blockStart, false);
+ clearFree(blockStart);
blockEnds[blockStart] = blockEnd;
this.mark = blockEnd;
return blockStart;
}
- start++;
}
- return -1;
}
@Override
protected void doStart() {
Arrays.fill(blockEnds, 0);
- this.frees.set(0, getCapacity(), true);
+ Arrays.fill(frees, -1);
int cap = capacity * unit;
if (Unsafe.UNSAFE_BUF_AVAILABLE) {
this.address = Unsafe.allocate(cap);
@@ -168,6 +163,25 @@ protected void doStart() {
}
}
+ private static int wordIndex(int bitIndex) {
+ return bitIndex >> ADDRESS_BITS_PER_WORD;
+ }
+
+ private void setFree(int index) {
+ int wordIndex = wordIndex(index);
+ frees[wordIndex] |= (1L << index);
+ }
+
+ private void clearFree(int index) {
+ int wordIndex = wordIndex(index);
+ frees[wordIndex] &= ~(1L << index);
+ }
+
+ private boolean isFree(int index) {
+ int wordIndex = wordIndex(index);
+ return ((frees[wordIndex] & (1L << index)) != 0);
+ }
+
@Override
protected void doStop() {
for (; ; ) {
@@ -199,7 +213,7 @@ protected void expansion(ByteBuf buf, int cap) {
int end = blockStart + size;
int i = blockEnd;
for (; i < end; ) {
- if (!frees.get(i)) {
+ if (!isFree(i)) {
break;
}
i++;
@@ -212,7 +226,7 @@ protected void expansion(ByteBuf buf, int cap) {
blockEnds[blockStart] = end;
buf.capacity((end - buf.unitOffset()) * unit);
} else {
- frees.set(blockStart);
+ setFree(blockStart);
int pos = allocate(mark, capacity, size);
if (pos == -1) {
pos = allocate(0, mark, size);
@@ -223,7 +237,7 @@ protected void expansion(ByteBuf buf, int cap) {
int old_read_index = buf.readIndex();
int old_write_index = buf.writeIndex();
int old_offset = buf.offset();
- buf.produce(pos, blockEnds[pos]);
+ buf.produce(pos, size);
if (Unsafe.UNSAFE_BUF_AVAILABLE) {
Unsafe.copyMemory(address + old_offset, address + buf.offset(), old_write_index);
} else {
@@ -289,10 +303,10 @@ public final int getUnit() {
public ByteBuf getUsedBuf(int skip) {
int skiped = 0;
for (int i = 0; i < getCapacity(); i++) {
- if (!frees.get(i)) {
+ if (!isFree(i)) {
skiped++;
if (skiped > skip) {
- return newByteBuf().produce(i, blockEnds[i]);
+ return newByteBuf().produce(i, blockEnds[i] - i);
}
}
}
@@ -307,7 +321,7 @@ private int maxFree() {
int free = 0;
int maxFree = 0;
for (int i = 0; i < getCapacity(); i++) {
- if (frees.get(i)) {
+ if (isFree(i)) {
free++;
} else {
maxFree = Math.max(maxFree, free);
@@ -319,22 +333,23 @@ private int maxFree() {
}
private ByteBuf newByteBuf() {
- if (BYTEBUF_RECYCLE) {
- ByteBuf buf = bufBuffer.pop();
+ if (BUF_POOL_ENABLE) {
+ ObjectPool pool = BUF_POOL.get();
+ ByteBuf buf = pool.pop();
if (buf == null) {
- return newByteBuf0();
+ return newByteBuf0(pool);
}
return buf;
} else {
- return newByteBuf0();
+ return newByteBuf0(ObjectPool.BLANK);
}
}
- private ByteBuf newByteBuf0() {
+ private ByteBuf newByteBuf0(ObjectPool pool) {
if (Unsafe.UNSAFE_BUF_AVAILABLE) {
- return new PooledUnsafeByteBuf(this, address);
+ return new PooledUnsafeByteBuf(this, pool, address);
} else {
- return isDirect() ? new PooledDirectByteBuf(this, directMemory.duplicate()) : new PooledHeapByteBuf(this, heapMemory);
+ return isDirect() ? new PooledDirectByteBuf(this, pool, directMemory.duplicate()) : new PooledHeapByteBuf(this, pool, heapMemory);
}
}
@@ -343,12 +358,12 @@ public void release(ByteBuf buf) {
ReentrantLock lock = this.lock;
lock.lock();
try {
- frees.set(buf.unitOffset());
+ setFree(buf.unitOffset());
} finally {
lock.unlock();
}
- if (BYTEBUF_RECYCLE) {
- bufBuffer.push(buf);
+ if (BUF_POOL_ENABLE) {
+ buf.recycleObject();
}
if (Develop.BUF_DEBUG && buf.isPooled()) {
BufDebug d = BUF_DEBUGS.remove(buf);
@@ -360,8 +375,7 @@ public void release(ByteBuf buf) {
}
}
- @Override
- public String toString() {
+ public String toDebugString() {
ReentrantLock lock = this.lock;
lock.lock();
try {
@@ -390,7 +404,7 @@ public String toString() {
private int usedBuf() {
int used = 0;
for (int i = 0; i < getCapacity(); i++) {
- if (!frees.get(i)) {
+ if (!isFree(i)) {
used++;
}
}
@@ -400,7 +414,7 @@ private int usedBuf() {
private int usedMem() {
int used = 0;
for (int i = 0; i < getCapacity(); i++) {
- if (!frees.get(i)) {
+ if (!isFree(i)) {
int next = blockEnds[i];
used += (next - i);
i = next - 1;
@@ -409,7 +423,14 @@ private int usedMem() {
return used;
}
- public class BufDebug {
+ static Map NEW_BUF_DEBUGS() {
+ return Develop.BUF_DEBUG ? new ConcurrentHashMap() : null;
+ }
+
+ /**
+ * for debug
+ */
+ public static class BufDebug {
public volatile ByteBuf buf;
public volatile Exception e;
@@ -419,7 +440,7 @@ public class BufDebug {
/**
* for debug
*/
- public class PoolState {
+ public static class PoolState {
public int buf;
public int free;
public int memory;
diff --git a/firenio-core/src/main/java/com/firenio/buffer/PooledDirectByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/PooledDirectByteBuf.java
index 261887f1c..536751897 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/PooledDirectByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/PooledDirectByteBuf.java
@@ -17,14 +17,18 @@
import java.nio.ByteBuffer;
+import com.firenio.collection.ObjectPool;
+
final class PooledDirectByteBuf extends DirectByteBuf {
- private PooledByteBufAllocator allocator;
- private int capacity;
- private int unitOffset;
+ private final PooledByteBufAllocator allocator;
+ private final ObjectPool pool;
+ private int capacity;
+ private int unitOffset;
- PooledDirectByteBuf(PooledByteBufAllocator allocator, ByteBuffer memory) {
+ PooledDirectByteBuf(PooledByteBufAllocator allocator, ObjectPool pool, ByteBuffer memory) {
super(memory);
+ this.pool = pool;
this.allocator = allocator;
}
@@ -54,10 +58,10 @@ public boolean isPooled() {
}
@Override
- protected ByteBuf produce(int unitOffset, int unitEnd) {
+ protected ByteBuf produce(int unitOffset, int unitSize) {
int unit = allocator.getUnit();
this.offset = unitOffset * unit;
- this.capacity = (unitEnd - unitOffset) * unit;
+ this.capacity = unitSize * unit;
this.abs_read_index = offset;
this.abs_write_index = offset;
this.unitOffset = unitOffset;
@@ -80,4 +84,9 @@ protected void unitOffset(int unitOffset) {
this.unitOffset = unitOffset;
}
+ @Override
+ protected void recycleObject() {
+ pool.push(this);
+ }
+
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/PooledHeapByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/PooledHeapByteBuf.java
index 2e8a708aa..c800c371e 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/PooledHeapByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/PooledHeapByteBuf.java
@@ -15,14 +15,18 @@
*/
package com.firenio.buffer;
+import com.firenio.collection.ObjectPool;
+
final class PooledHeapByteBuf extends HeapByteBuf {
- private PooledByteBufAllocator allocator;
- private int capacity;
- private int unitOffset;
+ private final PooledByteBufAllocator allocator;
+ private final ObjectPool pool;
+ private int capacity;
+ private int unitOffset;
- PooledHeapByteBuf(PooledByteBufAllocator allocator, byte[] memory) {
+ PooledHeapByteBuf(PooledByteBufAllocator allocator, ObjectPool pool, byte[] memory) {
super(memory);
+ this.pool = pool;
this.allocator = allocator;
}
@@ -52,10 +56,10 @@ public boolean isPooled() {
}
@Override
- protected PooledHeapByteBuf produce(int unitOffset, int unitEnd) {
+ protected PooledHeapByteBuf produce(int unitOffset, int unitSize) {
int unit = allocator.getUnit();
this.offset = unitOffset * unit;
- this.capacity = (unitEnd - unitOffset) * unit;
+ this.capacity = unitSize * unit;
this.abs_read_index = offset;
this.abs_write_index = offset;
this.unitOffset = unitOffset;
@@ -78,4 +82,9 @@ protected void unitOffset(int unitOffset) {
this.unitOffset = unitOffset;
}
+ @Override
+ protected void recycleObject() {
+ pool.push(this);
+ }
+
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/PooledUnsafeByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/PooledUnsafeByteBuf.java
index 6982f1612..903b57e01 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/PooledUnsafeByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/PooledUnsafeByteBuf.java
@@ -15,14 +15,18 @@
*/
package com.firenio.buffer;
+import com.firenio.collection.ObjectPool;
+
final class PooledUnsafeByteBuf extends UnsafeByteBuf {
- private PooledByteBufAllocator allocator;
- private int capacity;
- private int unitOffset;
+ private final PooledByteBufAllocator allocator;
+ private final ObjectPool pool;
+ private int capacity;
+ private int unitOffset;
- PooledUnsafeByteBuf(PooledByteBufAllocator allocator, long memory) {
+ PooledUnsafeByteBuf(PooledByteBufAllocator allocator, ObjectPool pool, long memory) {
super(memory);
+ this.pool = pool;
this.allocator = allocator;
}
@@ -52,10 +56,10 @@ public boolean isPooled() {
}
@Override
- protected ByteBuf produce(int unitOffset, int unitEnd) {
+ protected ByteBuf produce(int unitOffset, int unitSize) {
int unit = allocator.getUnit();
this.offset = unitOffset * unit;
- this.capacity = (unitEnd - unitOffset) * unit;
+ this.capacity = unitSize * unit;
this.abs_read_index = offset;
this.abs_write_index = offset;
this.unitOffset = unitOffset;
@@ -78,4 +82,9 @@ protected void unitOffset(int unitOffset) {
this.unitOffset = unitOffset;
}
+ @Override
+ protected void recycleObject() {
+ pool.push(this);
+ }
+
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/UnpooledDirectByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/UnpooledDirectByteBuf.java
index 0f1d8f795..3fbf845cf 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/UnpooledDirectByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/UnpooledDirectByteBuf.java
@@ -59,4 +59,15 @@ protected void release0() {
Unsafe.freeByteBuffer(memory);
}
+ @Override
+ public void reset(ByteBuffer memory) {
+ if (!memory.isDirect()){
+ throw unsupportedOperationException();
+ }
+ this.referenceCount = 1;
+ this.setMemory(memory);
+ this.writeIndex(memory.limit());
+ this.reverseRead();
+ }
+
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/UnpooledHeapByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/UnpooledHeapByteBuf.java
index 33adf5c07..414784bba 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/UnpooledHeapByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/UnpooledHeapByteBuf.java
@@ -62,6 +62,26 @@ public boolean isPooled() {
@Override
protected final void release0() {}
+ @Override
+ public void reset(ByteBuffer memory) {
+ if (memory.isDirect()) {
+ throw unsupportedOperationException();
+ }
+ this.referenceCount = 1;
+ this.memory = memory.array();
+ this.nioBuffer = memory;
+ this.writeIndex(memory.limit());
+ this.reverseRead();
+ }
+
+ @Override
+ public void reset(byte[] memory, int off, int len) {
+ this.referenceCount = 1;
+ this.memory = memory;
+ this.readIndex(off);
+ this.writeIndex(off + len);
+ }
+
static final class EmptyByteBuf extends UnpooledHeapByteBuf {
static final EmptyByteBuf EMPTY = new UnpooledHeapByteBuf.EmptyByteBuf();
diff --git a/firenio-core/src/main/java/com/firenio/buffer/UnpooledUnsafeByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/UnpooledUnsafeByteBuf.java
index 78869b7f1..54724de73 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/UnpooledUnsafeByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/UnpooledUnsafeByteBuf.java
@@ -15,6 +15,8 @@
*/
package com.firenio.buffer;
+import java.nio.ByteBuffer;
+
import com.firenio.common.Unsafe;
/**
@@ -59,4 +61,13 @@ protected void release0() {
Unsafe.free(address());
}
+ @Override
+ public void reset(long memory, int capacity, int off, int len) {
+ this.referenceCount = 1;
+ this.setMemory(memory);
+ this.capacity = capacity;
+ this.readIndex(off);
+ this.writeIndex(off + len);
+ }
+
}
diff --git a/firenio-core/src/main/java/com/firenio/buffer/UnsafeByteBuf.java b/firenio-core/src/main/java/com/firenio/buffer/UnsafeByteBuf.java
index fe4c1e5f1..917a86fe0 100644
--- a/firenio-core/src/main/java/com/firenio/buffer/UnsafeByteBuf.java
+++ b/firenio-core/src/main/java/com/firenio/buffer/UnsafeByteBuf.java
@@ -39,7 +39,7 @@ public byte[] array() {
}
@Override
- public void collation() {
+ public void compact() {
if (!hasReadableBytes()) {
clear();
return;
diff --git a/firenio-core/src/main/java/com/firenio/collection/AttributeKey.java b/firenio-core/src/main/java/com/firenio/collection/AttributeKey.java
new file mode 100644
index 000000000..c62062a09
--- /dev/null
+++ b/firenio-core/src/main/java/com/firenio/collection/AttributeKey.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 The FireNio Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.firenio.collection;
+
+import com.firenio.collection.AttributeMap.AttributeInitFunction;
+
+/**
+ * @author: wangkai
+ **/
+public final class AttributeKey {
+
+ private final int index;
+
+ private final String name;
+
+ private final AttributeInitFunction function;
+
+ AttributeKey(int index, String name, AttributeInitFunction function) {
+ this.name = name;
+ this.index = index;
+ this.function = function;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public AttributeInitFunction getFunction() {
+ return function;
+ }
+
+ @Override
+ public String toString() {
+ return "name: " + name + ", index: " + index;
+ }
+
+}
diff --git a/firenio-core/src/main/java/com/firenio/collection/AttributeMap.java b/firenio-core/src/main/java/com/firenio/collection/AttributeMap.java
new file mode 100644
index 000000000..d60515a57
--- /dev/null
+++ b/firenio-core/src/main/java/com/firenio/collection/AttributeMap.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2015 The FireNio Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.firenio.collection;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * use for static variable, eg:
+ *
+ * static final AttributeKey KEY_NAME = AttributeMap.valueOfKey(Channel.class, "KEY_NAME");
+ *
+ */
+//TODO 考虑多个ChannelContext时,每个Context里Channel存储的数据key不一样,会造成内存浪费
+public abstract class AttributeMap {
+
+ private static final Map, AttributeKeys> INDEX_MAPPING = new ConcurrentHashMap<>();
+
+ private final Object[] attributes;
+
+ private volatile int memoryBarrier = 0;
+
+ public AttributeMap() {
+ AttributeKeys keys = getKeys();
+ if (keys == null) {
+ attributes = null;
+ return;
+ }
+ keys.initialized = true;
+ this.attributes = new Object[keys.getCounter()];
+ for (AttributeKey key : keys.keys.values()) {
+ AttributeInitFunction function = key.getFunction();
+ function.setValue(this, key.getIndex());
+ }
+ }
+
+ public T getAttribute(AttributeKey key) {
+ return (T) getAttribute(key.getIndex());
+ }
+
+ public T getAttributeUnsafe(AttributeKey key) {
+ return (T) getAttributeUnsafe(key.getIndex());
+ }
+
+ public Object getAttribute(int key) {
+ memoryBarrier = 1;
+ return attributes[key];
+ }
+
+ public Object getAttributeUnsafe(int key) {
+ return attributes[key];
+ }
+
+ public void setAttribute(AttributeKey key, Object value) {
+ attributes[key.getIndex()] = value;
+ }
+
+ public void setAttribute(int key, Object value) {
+ memoryBarrier = 1;
+ attributes[key] = value;
+ }
+
+ public void setAttributeUnsafe(int key, Object value) {
+ attributes[key] = value;
+ }
+
+ public static AttributeKeys getKeys(Class clazz) {
+ return INDEX_MAPPING.get(clazz);
+ }
+
+ public static AttributeKey valueOfKey(Class clazz, String name) {
+ return valueOfKey(clazz, name, null);
+ }
+
+ public static AttributeKey valueOfKey(Class clazz, String name, AttributeInitFunction function) {
+ AttributeKeys attributeKeys = INDEX_MAPPING.get(clazz);
+ if (attributeKeys == null) {
+ synchronized (INDEX_MAPPING) {
+ attributeKeys = INDEX_MAPPING.get(clazz);
+ if (attributeKeys == null) {
+ attributeKeys = new AttributeKeys();
+ INDEX_MAPPING.put(clazz, attributeKeys);
+ }
+ }
+ }
+ return attributeKeys.valueOf(name, function);
+ }
+
+ protected int getMemoryBarrier() {
+ return memoryBarrier;
+ }
+
+ protected abstract AttributeKeys getKeys();
+
+ public static class AttributeKeys {
+
+ final Map keys = new HashMap<>();
+ final Map ro_keys = Collections.unmodifiableMap(keys);
+
+ int counter = 0;
+ volatile boolean initialized = false;
+
+ synchronized AttributeKey valueOf(String name, AttributeInitFunction function) {
+ if (initialized) {
+ throw new RuntimeException("incorrect usage, AttributeKey can only be static constant");
+ }
+ if (function == null) {
+ function = NULL_VALUE_ATTRIBUTE_INIT_FUNCTION;
+ }
+ keys.put(name, new AttributeKey(counter++, name, function));
+ return keys.get(name);
+ }
+
+ public Map getKeys() {
+ return ro_keys;
+ }
+
+ public int getCounter() {
+ return counter;
+ }
+
+ }
+
+ public interface AttributeInitFunction {
+
+ void setValue(AttributeMap map, int key);
+
+ }
+
+ static final AttributeInitFunction NULL_VALUE_ATTRIBUTE_INIT_FUNCTION = new AttributeInitFunction() {
+ @Override
+ public void setValue(AttributeMap map, int key) { }
+ };
+
+}
diff --git a/firenio-core/src/main/java/com/firenio/collection/AttributesImpl.java b/firenio-core/src/main/java/com/firenio/collection/AttributesImpl.java
deleted file mode 100644
index d5d6f5f04..000000000
--- a/firenio-core/src/main/java/com/firenio/collection/AttributesImpl.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright 2015 The FireNio Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.firenio.collection;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-public class AttributesImpl implements Attributes {
-
- private final Map