Skip to content

Commit 72751ab

Browse files
committed
Improved code
1 parent 9e98603 commit 72751ab

11 files changed

+100
-102
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ To add a dependency using Maven:
99
<dependency>
1010
<groupId>io.jpower.kcp</groupId>
1111
<artifactId>kcp-netty</artifactId>
12-
<version>1.4.9</version>
12+
<version>1.4.10</version>
1313
</dependency>
1414
```
1515

kcp-example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.jpower.kcp</groupId>
77
<artifactId>kcp-parent</artifactId>
8-
<version>1.4.9</version>
8+
<version>1.4.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

kcp-netty/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.jpower.kcp</groupId>
77
<artifactId>kcp-parent</artifactId>
8-
<version>1.4.9</version>
8+
<version>1.4.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

kcp-netty/src/main/java/io/jpower/kcp/netty/Kcp.java

+37-49
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import io.jpower.kcp.netty.internal.ReusableListIterator;
1010
import io.netty.buffer.ByteBuf;
1111
import io.netty.buffer.ByteBufAllocator;
12-
import io.netty.util.Recycler;
12+
import io.netty.util.internal.ObjectPool;
13+
import io.netty.util.internal.ObjectPool.Handle;
1314
import io.netty.util.internal.logging.InternalLogger;
1415
import io.netty.util.internal.logging.InternalLoggerFactory;
1516

@@ -68,7 +69,10 @@ public class Kcp {
6869

6970
public static final int IKCP_WND_SND = 32;
7071

71-
public static final int IKCP_WND_RCV = 32;
72+
/**
73+
* must >= max fragment size
74+
*/
75+
public static final int IKCP_WND_RCV = 128;
7276

7377
public static final int IKCP_MTU_DEF = 1400;
7478

@@ -113,9 +117,9 @@ public class Kcp {
113117

114118
private long rcvNxt;
115119

116-
private long tsRecent;
120+
private int tsRecent;
117121

118-
private long tsLastack;
122+
private int tsLastack;
119123

120124
private int ssthresh = IKCP_THRESH_INIT;
121125

@@ -137,11 +141,11 @@ public class Kcp {
137141

138142
private int probe;
139143

140-
private long current;
144+
private int current;
141145

142146
private int interval = IKCP_INTERVAL;
143147

144-
private long tsFlush = IKCP_INTERVAL;
148+
private int tsFlush = IKCP_INTERVAL;
145149

146150
private int xmit;
147151

@@ -151,7 +155,7 @@ public class Kcp {
151155

152156
private boolean updated;
153157

154-
private long tsProbe;
158+
private int tsProbe;
155159

156160
private int probeWait;
157161

@@ -198,10 +202,6 @@ public class Kcp {
198202

199203
private KcpMetric metric = new KcpMetric(this);
200204

201-
private static long long2Uint(long n) {
202-
return n & 0x00000000FFFFFFFFL;
203-
}
204-
205205
private static long int2Uint(int i) {
206206
return i & 0xFFFFFFFFL;
207207
}
@@ -210,10 +210,6 @@ private static int ibound(int lower, int middle, int upper) {
210210
return Math.min(Math.max(lower, middle), upper);
211211
}
212212

213-
private static long ibound(long lower, long middle, long upper) {
214-
return Math.min(Math.max(lower, middle), upper);
215-
}
216-
217213
private static int itimediff(int later, int earlier) {
218214
return later - earlier;
219215
}
@@ -239,7 +235,7 @@ private static int encodeSeg(ByteBuf buf, Segment seg) {
239235
buf.writeByte(seg.cmd);
240236
buf.writeByte(seg.frg);
241237
buf.writeShortLE(seg.wnd);
242-
buf.writeIntLE((int) seg.ts);
238+
buf.writeIntLE(seg.ts);
243239
buf.writeIntLE((int) seg.sn);
244240
buf.writeIntLE((int) seg.una);
245241
buf.writeIntLE(seg.data.readableBytes());
@@ -249,7 +245,7 @@ private static int encodeSeg(ByteBuf buf, Segment seg) {
249245

250246
private static class Segment {
251247

252-
private final Recycler.Handle<Segment> recyclerHandle;
248+
private final Handle<Segment> recyclerHandle;
253249

254250
private int conv;
255251

@@ -259,13 +255,13 @@ private static class Segment {
259255

260256
private int wnd;
261257

262-
private long ts;
258+
private int ts;
263259

264260
private long sn;
265261

266262
private long una;
267263

268-
private long resendts;
264+
private int resendts;
269265

270266
private int rto;
271267

@@ -275,16 +271,9 @@ private static class Segment {
275271

276272
private ByteBuf data;
277273

278-
private static final Recycler<Segment> RECYCLER = new Recycler<Segment>() {
279-
280-
@Override
281-
protected Segment newObject(Handle<Segment> handle) {
282-
return new Segment(handle);
283-
}
284-
285-
};
274+
private static final ObjectPool<Segment> RECYCLER = ObjectPool.newPool(Segment::new);
286275

287-
private Segment(Recycler.Handle<Segment> recyclerHandle) {
276+
private Segment(Handle<Segment> recyclerHandle) {
288277
this.recyclerHandle = recyclerHandle;
289278
}
290279

@@ -344,7 +333,7 @@ private void release(List<Segment> segQueue) {
344333
}
345334
}
346335

347-
private ByteBuf tryCreateAndOutput(ByteBuf buffer, int need) {
336+
private ByteBuf tryCreateOrOutput(ByteBuf buffer, int need) {
348337
if (buffer == null) {
349338
buffer = createFlushByteBuf();
350339
} else if (buffer.readableBytes() + need > mtu) {
@@ -555,7 +544,7 @@ public int send(ByteBuf buf) {
555544
count = (len + mss - 1) / mss;
556545
}
557546

558-
if (count > 255) { // Maybe don't need the conditon in stream mode
547+
if (count >= IKCP_WND_RCV) { // Maybe don't need the conditon in stream mode
559548
return -2;
560549
}
561550

@@ -648,7 +637,7 @@ private void parseFastack(long sn) {
648637
}
649638
}
650639

651-
private void ackPush(long sn, long ts) {
640+
private void ackPush(long sn, int ts) {
652641
int newSize = 2 * (ackcount + 1);
653642

654643
if (newSize > acklist.length) {
@@ -664,7 +653,7 @@ private void ackPush(long sn, long ts) {
664653
}
665654

666655
acklist[2 * ackcount] = (int) sn;
667-
acklist[2 * ackcount + 1] = (int) ts;
656+
acklist[2 * ackcount + 1] = ts;
668657
ackcount++;
669658
}
670659

@@ -736,8 +725,8 @@ public int input(ByteBuf data) {
736725
}
737726

738727
while (true) {
739-
int conv, len, wnd;
740-
long ts, sn, una;
728+
int conv, len, wnd, ts;
729+
long sn, una;
741730
byte cmd;
742731
short frg;
743732
Segment seg;
@@ -754,7 +743,7 @@ public int input(ByteBuf data) {
754743
cmd = data.readByte();
755744
frg = data.readUnsignedByte();
756745
wnd = data.readUnsignedShortLE();
757-
ts = data.readUnsignedIntLE();
746+
ts = data.readIntLE();
758747
sn = data.readUnsignedIntLE();
759748
una = data.readUnsignedIntLE();
760749
len = data.readIntLE();
@@ -776,10 +765,10 @@ public int input(ByteBuf data) {
776765
shrinkBuf();
777766

778767
boolean readed = false;
779-
long uintCurrent = long2Uint(current);
768+
int current = this.current;
780769
switch (cmd) {
781770
case IKCP_CMD_ACK: {
782-
int rtt = itimediff(uintCurrent, ts);
771+
int rtt = itimediff(current, ts);
783772
if (rtt >= 0) {
784773
updateAck(rtt);
785774
}
@@ -889,8 +878,7 @@ private int wndUnused() {
889878
* ikcp_flush
890879
*/
891880
private void flush() {
892-
long current = this.current;
893-
long uintCurrent = long2Uint(current);
881+
int current = this.current;
894882

895883
// 'ikcp_update' haven't been called.
896884
if (!updated) {
@@ -911,9 +899,9 @@ private void flush() {
911899
// flush acknowledges
912900
int count = ackcount;
913901
for (int i = 0; i < count; i++) {
914-
buffer = tryCreateAndOutput(buffer, IKCP_OVERHEAD);
902+
buffer = tryCreateOrOutput(buffer, IKCP_OVERHEAD);
915903
seg.sn = int2Uint(acklist[i * 2]);
916-
seg.ts = int2Uint(acklist[i * 2 + 1]);
904+
seg.ts = acklist[i * 2 + 1];
917905
encodeSeg(buffer, seg);
918906
if (log.isDebugEnabled()) {
919907
log.debug("{} flush ack: sn={}, ts={}", this, seg.sn, seg.ts);
@@ -948,7 +936,7 @@ private void flush() {
948936
// flush window probing commands
949937
if ((probe & IKCP_ASK_SEND) != 0) {
950938
seg.cmd = IKCP_CMD_WASK;
951-
buffer = tryCreateAndOutput(buffer, IKCP_OVERHEAD);
939+
buffer = tryCreateOrOutput(buffer, IKCP_OVERHEAD);
952940
encodeSeg(buffer, seg);
953941
if (log.isDebugEnabled()) {
954942
log.debug("{} flush ask", this);
@@ -958,7 +946,7 @@ private void flush() {
958946
// flush window probing commands
959947
if ((probe & IKCP_ASK_TELL) != 0) {
960948
seg.cmd = IKCP_CMD_WINS;
961-
buffer = tryCreateAndOutput(buffer, IKCP_OVERHEAD);
949+
buffer = tryCreateOrOutput(buffer, IKCP_OVERHEAD);
962950
encodeSeg(buffer, seg);
963951
if (log.isDebugEnabled()) {
964952
log.debug("{} flush tell: wnd={}", this, seg.wnd);
@@ -985,7 +973,7 @@ private void flush() {
985973
newSeg.conv = conv;
986974
newSeg.cmd = IKCP_CMD_PUSH;
987975
newSeg.wnd = seg.wnd;
988-
newSeg.ts = uintCurrent;
976+
newSeg.ts = current;
989977
newSeg.sn = sndNxt++;
990978
newSeg.una = rcvNxt;
991979
newSeg.resendts = current;
@@ -1044,15 +1032,15 @@ private void flush() {
10441032
}
10451033

10461034
if (needsend) {
1047-
segment.ts = uintCurrent;
1035+
segment.ts = current;
10481036
segment.wnd = seg.wnd;
10491037
segment.una = rcvNxt;
10501038

10511039
ByteBuf segData = segment.data;
10521040
int segLen = segData.readableBytes();
10531041
int need = IKCP_OVERHEAD + segLen;
10541042

1055-
buffer = tryCreateAndOutput(buffer, need);
1043+
buffer = tryCreateOrOutput(buffer, need);
10561044
encodeSeg(buffer, segment);
10571045

10581046
if (segLen > 0) {
@@ -1110,7 +1098,7 @@ private void flush() {
11101098
*
11111099
* @param current
11121100
*/
1113-
public void update(long current) {
1101+
public void update(int current) {
11141102
this.current = current;
11151103

11161104
if (!updated) {
@@ -1156,12 +1144,12 @@ public void update(long current) {
11561144
* @param current
11571145
* @return
11581146
*/
1159-
public long check(long current) {
1147+
public int check(int current) {
11601148
if (!updated) {
11611149
return current;
11621150
}
11631151

1164-
long tsFlush = this.tsFlush;
1152+
int tsFlush = this.tsFlush;
11651153
int slap = itimediff(current, tsFlush);
11661154
if (slap >= 10000 || slap < -10000) {
11671155
tsFlush = current;

kcp-netty/src/main/java/io/jpower/kcp/netty/Ukcp.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Ukcp {
2424

2525
private boolean mergeSegmentBuf = true;
2626

27-
private long tsUpdate = -1;
27+
private int tsUpdate = -1;
2828

2929
private volatile boolean active;
3030

@@ -138,9 +138,9 @@ public boolean canSend(boolean curCanSend) {
138138
* @param current current time in milliseconds
139139
* @return the next time to update
140140
*/
141-
public long update(long current) {
141+
public int update(int current) {
142142
kcp.update(current);
143-
long nextTsUp = check(current);
143+
int nextTsUp = check(current);
144144
setTsUpdate(nextTsUp);
145145

146146
return nextTsUp;
@@ -151,9 +151,9 @@ public long update(long current) {
151151
*
152152
* @param current current time in milliseconds
153153
* @return
154-
* @see Kcp#check(long)
154+
* @see Kcp#check(int)
155155
*/
156-
public long check(long current) {
156+
public int check(int current) {
157157
return kcp.check(current);
158158
}
159159

@@ -376,11 +376,11 @@ public Ukcp setMergeSegmentBuf(boolean mergeSegmentBuf) {
376376
return this;
377377
}
378378

379-
public long getTsUpdate() {
379+
public int getTsUpdate() {
380380
return tsUpdate;
381381
}
382382

383-
public Ukcp setTsUpdate(long tsUpdate) {
383+
public Ukcp setTsUpdate(int tsUpdate) {
384384
this.tsUpdate = tsUpdate;
385385
return this;
386386
}

0 commit comments

Comments
 (0)