skywind3000 / kcp

:zap: KCP - A Fast and Reliable ARQ Protocol
MIT License
15.2k stars 2.49k forks source link

请问ikcp_setoutput回调问题 #375

Open one808 opened 1 year ago

one808 commented 1 year ago

测试我用MTU = 128 发送数据大小 = 1400

用ikcp_send()发送后,ikcp_setoutput()回调输出: 调试输出 (“output”, ikcp, buf, len)

请问这是什么问题?

skywind3000 commented 1 year ago

多线程操作同一个 kcp 对象?

one808 commented 1 year ago

因为我设计接收是死循环,所以我在ikcpsetoutput()后面就启动线程接收,第一次用,不是很明白使用方法,请指教^^!!

one808 commented 1 year ago

我试了用ikcp_send()发送数据后直接调用接收函数(不是线程),ikcp_setoutput()回调函数输出也是提这个错, 调试输出 (“output”, ikcp, buf, len)

skywind3000 commented 1 year ago

纯算法,没有系统调用,你应该放在一个线程里调用,或者自己枷锁,否则出问题。

one808 commented 1 year ago

用python测试没问题 import time from ctypes.util import find_library from l0n0lkcp.ikcp import Ikcp, load_clib

ret = find_library("kcp")

load_clib("build/Release/kcp.dll")

load_clib(ret) a = None b = None

def test_out_puta(data, size): print("test_out_puta",size) b.input(data)

def test_out_putb(data, size): print("test_out_putb",size) a.input(data)

a = Ikcp(123, test_out_puta) b = Ikcp(123, test_out_putb)

a.setmtu(64)

a.setmtu(64)

a.nodelay(1, 20, 1, 1) b.nodelay(1, 20, 1, 1)

while True: a.update(10) b.update(10)

buf = bytes(12800)
a.send(buf)
data, size = b.recv()
if size > 0:
    print(size)
    # print(data[:size], a.current)
time.sleep(0.01)

可是我用的是易语言编程,流程完完全全按python来写(一个线程里跑),问题还是出在回调上(数据大小在MTU内,单包就没问题),回调4个参数都是用整数型(4字节对齐) 调试输出 (“out_puta”, buf, len)

我怀疑是不是回调参数类型引来的。 老大,帮我看看,麻烦了

skywind3000 commented 1 year ago

你先自己改成单线程再说啊。不然看啥啊。

one808 commented 1 year ago

.版本 2 .支持库 spec

.子程序 out_puta, 整数型 .参数 buf, 整数型 .参数 len, 整数型 .参数 ikcp, 整数型 .参数 user, 整数型

调试输出 (“out_puta”, buf, len, ikcp, user) b.input (buf, len) 返回 (0)

.子程序 out_putb, 整数型 .参数 buf, 整数型 .参数 len, 整数型 .参数 ikcp, 整数型 .参数 user, 整数型

调试输出 (“out_putb”, buf, len) a.input (buf, len) 返回 (0)

.子程序 main .局部变量 buf, 字节集 .局部变量 data, 字节集 .局部变量 size, 整数型

a.create (123, 0) b.create (123, 0)

a.setoutput (到整数 (&out_puta)) b.setoutput (到整数 (&out_putb))

a.nodelay (1, 20, 1, 1) b.nodelay (1, 20, 1, 1)

' a.setmtu (64) ' b.setmtu (64)

.判断循环首 (真) a.update (iclock ()) b.update (iclock ()) buf = 取空白字节集 (28000) a.send (buf, 28000) size = b.recv (data, ) .如果真 (size > 0) 调试输出 (size) ' print(data[:size], a.current) .如果真结束 延时 (10) .判断循环尾 ()

我现在就是单线测试,跟python一样的流程