请问ikcp_setoutput回调问题

rxztt3cl  于 2022-12-31  发布在  其他
关注(0)|答案(7)|浏览(120)

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

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

  • “output” | 6684544 | 6689080 | 128
  • “output” | 6684544 | 6689080 | 128
  • “output” | 6684544 | 6689080 | 128
  • “output” | 6684544 | 6689080 | 128
  • “output” | 6684544 | 104 | 6689104 (到这里我程序就崩了,buf 指针不对造成)

请问这是什么问题?

ia2d9nvy

ia2d9nvy1#

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

soat7uwm

soat7uwm2#

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

vjhs03f7

vjhs03f73#

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

  • “output” | 3065320 | 3076272 | 128
  • “output” | 3065320 | 3076272 | 128
  • “output” | 3065320 | 3076272 | 128
  • “output” | 3065320 | 3076272 | 128
  • “output” | 3065320 | 104 | 3076296(这104跟3076296=3076272+24两个位置刚好调换了,104成了buf指针, 3076296成了len)
3zwjbxry

3zwjbxry4#

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

b5buobof

b5buobof5#

用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)

  • “out_puta” | 7249872 | 1400
  • “out_puta” | 7249872 | 1400
  • “out_puta” | 7249872 | 1400
  • “out_puta” | 7249872 | 1400
  • “out_puta” | 1376 | 7249896(固定在第5个包出现问题,len和buf+24错位)

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

a64a0gku

a64a0gku6#

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

pexxcrt2

pexxcrt27#

.版本 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一样的流程

相关问题