Skip to content

Commit

Permalink
array deque .
Browse files Browse the repository at this point in the history
  • Loading branch information
beykery committed Apr 26, 2020
1 parent 6a8264a commit 1293b9f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ kcp for java . base on netty .

kcp是一种独立于底层通信协议的重传算法,jkcp直接构建于udp之上
并提供方便的编程接口,只需要继承相关的类即可;用户不用关心udp
和kcp的使用细节就能轻松驾驭moba类等需要高速传输环境的应用开发
和kcp的使用细节就能轻松驾驭视频、moba类等需要高速传输环境的应用开发

##坐标

```xml
<dependency>
<groupId>org.beykery</groupId>
<artifactId>jkcp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.beykery</groupId>
<artifactId>jkcp</artifactId>
<version>1.2.9</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.45.Final</version>
<version>4.1.49.Final</version>
</dependency>

<!--
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/org/beykery/jkcp/Kcp.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

import java.util.LinkedList;
import java.util.ArrayDeque;
import java.util.ArrayList;

/**
*
* @author beykery
*/
public class Kcp {
Expand Down Expand Up @@ -70,11 +70,11 @@ public class Kcp {
private int probe_wait;
private final int dead_link;
private int incr;
private final LinkedList<Segment> snd_queue = new LinkedList<>();
private final LinkedList<Segment> rcv_queue = new LinkedList<>();
private final LinkedList<Segment> snd_buf = new LinkedList<>();
private final LinkedList<Segment> rcv_buf = new LinkedList<>();
private final LinkedList<Integer> acklist = new LinkedList<>();
private final ArrayDeque<Segment> snd_queue = new ArrayDeque<>();
private final ArrayDeque<Segment> rcv_queue = new ArrayDeque<>();
private final ArrayList<Segment> snd_buf = new ArrayList<>();
private final ArrayList<Segment> rcv_buf = new ArrayList<>();
private final ArrayList<Integer> acklist = new ArrayList<>();
private ByteBuf buffer;
private int fastresend;
private int nocwnd;
Expand Down Expand Up @@ -244,7 +244,7 @@ public int receive(ByteBuf buffer) {
}
if (c > 0) {
for (int i = 0; i < c; i++) {
rcv_buf.removeFirst();
rcv_buf.remove(0);
}
}
// fast recover
Expand All @@ -271,7 +271,7 @@ public int send(ByteBuf buffer) {
Segment seg = snd_queue.getLast();
if (seg.data != null && seg.data.readableBytes() < mss) {
int capacity = mss - seg.data.readableBytes();
int extend = (buffer.readableBytes() < capacity) ? buffer.readableBytes() : capacity;
int extend = Math.min(buffer.readableBytes(), capacity);
seg.data.writeBytes(buffer, extend);
if (buffer.readableBytes() == 0) {
return 0;
Expand All @@ -292,7 +292,7 @@ public int send(ByteBuf buffer) {
}
//fragment
for (int i = 0; i < count; i++) {
int size = buffer.readableBytes() > mss ? mss : buffer.readableBytes();
int size = Math.min(buffer.readableBytes(), mss);
Segment seg = new Segment(size);
seg.data.writeBytes(buffer, size);
seg.frg = this.stream ? 0 : count - i - 1;
Expand Down Expand Up @@ -328,7 +328,7 @@ private void update_ack(int rtt) {

private void shrink_buf() {
if (snd_buf.size() > 0) {
snd_una = snd_buf.getFirst().sn;
snd_una = snd_buf.get(0).sn;
} else {
snd_una = snd_nxt;
}
Expand Down Expand Up @@ -362,7 +362,7 @@ private void parse_una(int una) {
}
if (c > 0) {
for (int i = 0; i < c; i++) {
Segment seg = snd_buf.removeFirst();
Segment seg = snd_buf.remove(0);
seg.data.release(seg.data.refCnt());
}
}
Expand Down Expand Up @@ -413,11 +413,7 @@ private void parse_data(Segment newseg) {
}
}
if (!repeat) {
if (temp == -1) {
rcv_buf.addFirst(newseg);
} else {
rcv_buf.add(temp + 1, newseg);
}
rcv_buf.add(temp + 1, newseg);
} else {
newseg.release();
}
Expand All @@ -434,13 +430,12 @@ private void parse_data(Segment newseg) {
}
if (0 < c) {
for (int i = 0; i < c; i++) {
rcv_buf.removeFirst();
rcv_buf.remove(0);
}
}
}

/**
*
* when you received a low level packet (eg. UDP packet), call it
*
* @param data
Expand Down

0 comments on commit 1293b9f

Please sign in to comment.