Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix python crash #2

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gidra/proxy/kcp_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def bind(self, addr: _Address) -> bool:
)
self.kcp.setmtu(1200)
self.kcp.wndsize(1024, 1024)
self.kcp.nodelay(1, 10, 2, 1)

threading.Thread(target=self._kcp_update).start()
threading.Thread(target=self._kcp_recv).start()
Expand Down
11 changes: 6 additions & 5 deletions python-kcp/lkcp/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ cdef extern from "compat.h":

cdef struct UsrInfo:
int handle
char* recv_buffer

g_KcpAgentCbs = {}

RECV_BUFFER_LEN = 4*1024*1024

cdef char* recv_buffer = <char *>PyMem_Malloc(sizeof(char)*RECV_BUFFER_LEN)

cdef int kcp_output_callback(const char *buf, int len, ikcpcb *kcp, void *arg):
global g_KcpAgentCbs
cdef UsrInfo *c = <UsrInfo *>arg;
Expand All @@ -80,6 +79,7 @@ cdef void del_kcp(PyObject *obj):
if ckcp.user != NULL:
global g_KcpAgentCbs
c = <UsrInfo *>ckcp.user
PyMem_Free(c.recv_buffer)
uid = <object>c.handle
del g_KcpAgentCbs[uid]
PyMem_Free(c)
Expand All @@ -92,18 +92,19 @@ def lkcp_create(conv, token, uid, cb):
g_KcpAgentCbs[uid] = cb
cdef UsrInfo *c = <UsrInfo *>PyMem_Malloc(sizeof(UsrInfo))
c.handle = <int>uid
c.recv_buffer = <char *>PyMem_Malloc(sizeof(char)*RECV_BUFFER_LEN)
cdef ikcpcb* ckcp = ikcp_create(conv, token, c)
ckcp.output = kcp_output_callback
return make_capsule(ckcp, NULL, del_kcp)

def lkcp_recv(kcp):
global recv_buffer
cdef ikcpcb* ckcp = <ikcpcb*>get_pointer(<object>kcp, NULL)
hr = ikcp_recv(ckcp, recv_buffer, RECV_BUFFER_LEN)
c = <UsrInfo *>ckcp.user
hr = ikcp_recv(ckcp, c.recv_buffer, RECV_BUFFER_LEN)
if hr <= 0:
return hr,None
else:
return hr,recv_buffer[:hr]
return hr,c.recv_buffer[:hr]

def lkcp_send(kcp, data):
cdef ikcpcb* ckcp = <ikcpcb*>get_pointer(<object>kcp, NULL)
Expand Down
28 changes: 23 additions & 5 deletions python-kcp/lkcp/kcp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import core
import sys
import sys, threading

__all__ = ["KcpObj"]

Expand All @@ -9,6 +9,7 @@ class KcpObj:
def __init__(self, conv, token, callback):
self.conv = conv
self.token = token
self.mutex = threading.Lock()

global i
self.cobj = core.lkcp_create(conv, token, i, callback)
Expand All @@ -21,24 +22,41 @@ def nodelay(self, nodelay, interval, resend, nc):
return core.lkcp_nodelay(self.cobj, nodelay, interval, resend, nc)

def check(self, current):
return core.lkcp_check(self.cobj, current)
self.mutex.acquire()
ret = core.lkcp_check(self.cobj, current)
self.mutex.release()
return ret

def update(self, current):
self.mutex.acquire()
core.lkcp_update(self.cobj, current)
self.mutex.release()

def send(self, data):
self.mutex.acquire()
if sys.version_info.major == 3 and isinstance(data, str):
data = data.encode("UTF-8")
return core.lkcp_send(self.cobj, data)
ret = core.lkcp_send(self.cobj, data)
self.mutex.release()
return ret

def input(self, data):
return core.lkcp_input(self.cobj, data)
self.mutex.acquire()
ret = core.lkcp_input(self.cobj, data)
self.mutex.release()
return ret

def recv(self):
return core.lkcp_recv(self.cobj)
self.mutex.acquire()
ret = core.lkcp_recv(self.cobj)
self.mutex.release()
return ret

def flush(self):
self.mutex.acquire()
core.lkcp_flush(self.cobj)
self.mutex.release()


def setmtu(self, mtu):
core.lkcp_setmtu(self.cobj, mtu)