kcp 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/kcp/ikcp.c +1 -1
- data/ext/kcp/kcp_native.c +20 -39
- data/lib/kcp/version.rb +1 -1
- data/lib/kcp.rb +0 -6
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 42d14476648e8ceeab7e1719c5f60ef345c1756985653d680250f7cff245f556
|
|
4
|
+
data.tar.gz: 7e9598575a4293778ea9def78f24dfcdcdb5d8a280cf6dd7457ab613c40ea5d9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 75c1449845bf2f6c3df5afa9cb0cd158430101c090b1069174b56a210933576865b502cc76b735aa59253b8f87d79e590a19c2c7dc2c63c8a6a0770dd61646ab
|
|
7
|
+
data.tar.gz: 3e74c12241e69202ccb56e4f5fe600a0b3f69bc6b918c4e17ea8abedca016bb15464d7b81cf700f1454056e592f4ad13ab9d48c9a014de6ce07e1d8504db475e
|
data/ext/kcp/ikcp.c
CHANGED
|
@@ -34,7 +34,7 @@ const IUINT32 IKCP_ASK_SEND = 1; // need to send IKCP_CMD_WASK
|
|
|
34
34
|
const IUINT32 IKCP_ASK_TELL = 2; // need to send IKCP_CMD_WINS
|
|
35
35
|
const IUINT32 IKCP_WND_SND = 32;
|
|
36
36
|
const IUINT32 IKCP_WND_RCV = 128; // must >= max fragment size
|
|
37
|
-
const IUINT32 IKCP_MTU_DEF =
|
|
37
|
+
const IUINT32 IKCP_MTU_DEF = 1300;
|
|
38
38
|
const IUINT32 IKCP_ACK_FAST = 3;
|
|
39
39
|
const IUINT32 IKCP_INTERVAL = 100;
|
|
40
40
|
const IUINT32 IKCP_OVERHEAD = 24;
|
data/ext/kcp/kcp_native.c
CHANGED
|
@@ -8,36 +8,25 @@
|
|
|
8
8
|
#include <stdlib.h>
|
|
9
9
|
#include <string.h>
|
|
10
10
|
|
|
11
|
-
typedef struct {
|
|
12
|
-
char *data;
|
|
13
|
-
int len;
|
|
14
|
-
} kcp_pkt;
|
|
15
|
-
|
|
16
11
|
typedef struct {
|
|
17
12
|
ikcpcb *kcp;
|
|
18
|
-
|
|
19
|
-
int
|
|
20
|
-
int
|
|
13
|
+
char *out;
|
|
14
|
+
int outlen;
|
|
15
|
+
int outcap;
|
|
21
16
|
} kcp_ctx;
|
|
22
17
|
|
|
23
|
-
/*
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
/* Max bytes returned per output() call -- safely under the 64KB UDP datagram
|
|
19
|
+
limit, so a burst of KCP packets never overflows a single sendto(). */
|
|
20
|
+
#define KCP_MAX_BLOB 60000
|
|
21
|
+
|
|
26
22
|
static int shim_output(const char *buf, int len, ikcpcb *kcp, void *user) {
|
|
27
23
|
kcp_ctx *ctx = (kcp_ctx *)user;
|
|
28
|
-
if (ctx->
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (!npkts) return -1;
|
|
32
|
-
ctx->pkts = npkts;
|
|
33
|
-
ctx->cap = newcap;
|
|
24
|
+
if (ctx->outlen + len > ctx->outcap) {
|
|
25
|
+
ctx->outcap = (ctx->outlen + len) * 2 + 256;
|
|
26
|
+
ctx->out = (char *)realloc(ctx->out, ctx->outcap);
|
|
34
27
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!p->data) return -1;
|
|
38
|
-
memcpy(p->data, buf, len);
|
|
39
|
-
p->len = len;
|
|
40
|
-
ctx->count++;
|
|
28
|
+
memcpy(ctx->out + ctx->outlen, buf, len);
|
|
29
|
+
ctx->outlen += len;
|
|
41
30
|
return 0;
|
|
42
31
|
}
|
|
43
32
|
|
|
@@ -45,8 +34,7 @@ static void ctx_free(void *ptr) {
|
|
|
45
34
|
kcp_ctx *ctx = (kcp_ctx *)ptr;
|
|
46
35
|
if (ctx) {
|
|
47
36
|
if (ctx->kcp) ikcp_release(ctx->kcp);
|
|
48
|
-
|
|
49
|
-
free(ctx->pkts);
|
|
37
|
+
free(ctx->out);
|
|
50
38
|
free(ctx);
|
|
51
39
|
}
|
|
52
40
|
}
|
|
@@ -120,23 +108,17 @@ static VALUE m_kcp_flush(VALUE mod, VALUE self) {
|
|
|
120
108
|
|
|
121
109
|
static VALUE m_kcp_output(VALUE mod, VALUE self) {
|
|
122
110
|
kcp_ctx *ctx = get_ctx(self);
|
|
123
|
-
if (ctx->
|
|
124
|
-
/*
|
|
125
|
-
|
|
126
|
-
VALUE buf = rb_str_new(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (ctx->count > 0) {
|
|
130
|
-
memmove(&ctx->pkts[0], &ctx->pkts[1], ctx->count * sizeof(kcp_pkt));
|
|
111
|
+
if (ctx->outlen <= 0) return Qnil;
|
|
112
|
+
/* 一次最多返回 60KB 拼接好的包,避免单个 UDP 数据报超过 64KB(EMSGSIZE) */
|
|
113
|
+
int n = ctx->outlen < KCP_MAX_BLOB ? ctx->outlen : KCP_MAX_BLOB;
|
|
114
|
+
VALUE buf = rb_str_new(ctx->out, n);
|
|
115
|
+
if (n < ctx->outlen) {
|
|
116
|
+
memmove(ctx->out, ctx->out + n, ctx->outlen - n);
|
|
131
117
|
}
|
|
118
|
+
ctx->outlen -= n;
|
|
132
119
|
return buf;
|
|
133
120
|
}
|
|
134
121
|
|
|
135
|
-
static VALUE m_kcp_setmtu(VALUE mod, VALUE self, VALUE mtu) {
|
|
136
|
-
kcp_ctx *ctx = get_ctx(self);
|
|
137
|
-
return INT2NUM(ikcp_setmtu(ctx->kcp, NUM2INT(mtu)));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
122
|
static VALUE m_kcp_waitsnd(VALUE mod, VALUE self) {
|
|
141
123
|
kcp_ctx *ctx = get_ctx(self);
|
|
142
124
|
return INT2NUM(ikcp_waitsnd(ctx->kcp));
|
|
@@ -151,6 +133,5 @@ void Init_kcp_native(void) {
|
|
|
151
133
|
rb_define_singleton_method(m, "kcp_update", m_kcp_update, 2);
|
|
152
134
|
rb_define_singleton_method(m, "kcp_flush", m_kcp_flush, 1);
|
|
153
135
|
rb_define_singleton_method(m, "kcp_output", m_kcp_output, 1);
|
|
154
|
-
rb_define_singleton_method(m, "kcp_setmtu", m_kcp_setmtu, 2);
|
|
155
136
|
rb_define_singleton_method(m, "kcp_waitsnd", m_kcp_waitsnd, 1);
|
|
156
137
|
}
|
data/lib/kcp/version.rb
CHANGED
data/lib/kcp.rb
CHANGED
|
@@ -57,12 +57,6 @@ module Kcp
|
|
|
57
57
|
KcpNative.kcp_output(@ctx)
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
# 设置 KCP 内部 MTU(默认 1400)。上层如 mnet 会额外加自己的 header + GCM tag,
|
|
61
|
-
# 需要把 MTU 调小,保证「KCP 包 + 外层封装」仍小于路径 MTU,避免 IP 分片。
|
|
62
|
-
def mtu=(mtu)
|
|
63
|
-
KcpNative.kcp_setmtu(@ctx, mtu)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
60
|
# 发送队列里待发送的包数(>0 表示窗口已满、有积压)。
|
|
67
61
|
def waitsnd
|
|
68
62
|
KcpNative.kcp_waitsnd(@ctx)
|