Module: KcpNative
- Defined in:
- ext/kcp/kcp_native.c
Class Method Summary collapse
- .kcp_flush(self) ⇒ Object
- .kcp_input(self, data) ⇒ Object
- .kcp_mss(self) ⇒ Object
- .kcp_new(conv) ⇒ Object
- .kcp_output(self) ⇒ Object
- .kcp_recv(self, maxlen) ⇒ Object
- .kcp_send(self, data) ⇒ Object
- .kcp_setmtu(self, mtu) ⇒ Object
- .kcp_update(self, now) ⇒ Object
- .kcp_waitsnd(self) ⇒ Object
Class Method Details
.kcp_flush(self) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'ext/kcp/kcp_native.c', line 110
static VALUE m_kcp_flush(VALUE mod, VALUE self) {
kcp_ctx *ctx = get_ctx(self);
/* ikcp_flush returns early if ikcp_update hasn't run yet; initialize the
flag so a bare send+flush works without a prior update call. */
if (ctx->kcp->updated == 0) {
ctx->kcp->updated = 1;
ctx->kcp->ts_flush = ctx->kcp->current;
}
ikcp_flush(ctx->kcp);
return Qnil;
}
|
.kcp_input(self, data) ⇒ Object
98 99 100 101 102 |
# File 'ext/kcp/kcp_native.c', line 98
static VALUE m_kcp_input(VALUE mod, VALUE self, VALUE data) {
kcp_ctx *ctx = get_ctx(self);
StringValue(data);
return INT2NUM(ikcp_input(ctx->kcp, RSTRING_PTR(data), (long)RSTRING_LEN(data)));
}
|
.kcp_mss(self) ⇒ Object
141 142 143 144 |
# File 'ext/kcp/kcp_native.c', line 141
static VALUE m_kcp_mss(VALUE mod, VALUE self) {
kcp_ctx *ctx = get_ctx(self);
return INT2NUM(ctx->kcp->mss);
}
|
.kcp_new(conv) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'ext/kcp/kcp_native.c', line 67
static VALUE m_kcp_new(VALUE mod, VALUE conv) {
kcp_ctx *ctx = (kcp_ctx *)calloc(1, sizeof(kcp_ctx));
ctx->kcp = ikcp_create(NUM2UINT(conv), ctx);
ikcp_setoutput(ctx->kcp, shim_output);
ikcp_nodelay(ctx->kcp, 1, 10, 2, 1);
ikcp_wndsize(ctx->kcp, 128, 128);
ctx->kcp->stream = 1; /* byte-stream mode */
return TypedData_Wrap_Struct(rb_cObject, &kcp_ctx_type, ctx);
}
|
.kcp_output(self) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/kcp/kcp_native.c', line 122
static VALUE m_kcp_output(VALUE mod, VALUE self) {
kcp_ctx *ctx = get_ctx(self);
kcp_pkt *pkt;
VALUE buf;
if (!ctx->head) return Qnil;
/* 一次只返回一个 KCP 包,交由上层各自封装成不超过 MTU 的 UDP 数据报。 */
pkt = ctx->head;
ctx->head = pkt->next;
if (!ctx->head) ctx->tail = NULL;
buf = rb_str_new(pkt->data, pkt->len);
free(pkt);
return buf;
}
|
.kcp_recv(self, maxlen) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'ext/kcp/kcp_native.c', line 83
static VALUE m_kcp_recv(VALUE mod, VALUE self, VALUE maxlen) {
kcp_ctx *ctx = get_ctx(self);
int n = NUM2INT(maxlen);
char *tmp = (char *)malloc((size_t)n);
if (!tmp) rb_raise(rb_eNoMemError, "kcp recv buffer");
int got = ikcp_recv(ctx->kcp, tmp, n);
if (got <= 0) {
free(tmp);
return Qnil;
}
VALUE buf = rb_str_new(tmp, got);
free(tmp);
return buf;
}
|
.kcp_send(self, data) ⇒ Object
77 78 79 80 81 |
# File 'ext/kcp/kcp_native.c', line 77
static VALUE m_kcp_send(VALUE mod, VALUE self, VALUE data) {
kcp_ctx *ctx = get_ctx(self);
StringValue(data);
return INT2NUM(ikcp_send(ctx->kcp, RSTRING_PTR(data), (int)RSTRING_LEN(data)));
}
|
.kcp_setmtu(self, mtu) ⇒ Object
136 137 138 139 |
# File 'ext/kcp/kcp_native.c', line 136
static VALUE m_kcp_setmtu(VALUE mod, VALUE self, VALUE mtu) {
kcp_ctx *ctx = get_ctx(self);
return INT2NUM(ikcp_setmtu(ctx->kcp, NUM2INT(mtu)));
}
|
.kcp_update(self, now) ⇒ Object
104 105 106 107 108 |
# File 'ext/kcp/kcp_native.c', line 104
static VALUE m_kcp_update(VALUE mod, VALUE self, VALUE now) {
kcp_ctx *ctx = get_ctx(self);
ikcp_update(ctx->kcp, NUM2UINT(now));
return Qnil;
}
|
.kcp_waitsnd(self) ⇒ Object
146 147 148 149 |
# File 'ext/kcp/kcp_native.c', line 146
static VALUE m_kcp_waitsnd(VALUE mod, VALUE self) {
kcp_ctx *ctx = get_ctx(self);
return INT2NUM(ikcp_waitsnd(ctx->kcp));
}
|