[原创] python udt4 for windows udt4py.py
#coding: utf-8 import ctypes as _ctypes import struct from ctypes import wintypes as _wtypes class CPerfMonitor(_ctypes.Structure): _fields_ = [("mbpsSendRate", _wtypes.DOUBLE), ("msRTT", _wtypes.DOUBLE), ("usPktSndPeriod", _wtypes.DOUBLE), ("pktRecvACK", _wtypes.INT), ("pktRecvNAK", _wtypes.INT), ("mbpsBandwidth", _wtypes.DOUBLE), ("pktCongestionWindow", _wtypes.INT), ] udt4py = _ctypes.CDLL("udt4py.dll") print(udt4py) # UDT4PY_API INT socket_create() udt4py.socket_create.argtypes= () udt4py.socket_create.restype = _ctypes.c_int # UDT4PY_API INT socket_connect(INT fd,LPWSTR servip,WORD port) udt4py.socket_connect.argtypes = (_wtypes.INT,_wtypes.LPWSTR,_wtypes.WORD) udt4py.socket_connect.restype = _ctypes.c_int # UDT4PY_API INT socket_send(INT fd, LPSTR p,INT len ) udt4py.socket_send.argtypes = (_wtypes.INT,_wtypes.LPSTR,_wtypes.INT) udt4py.socket_send.restype = _ctypes.c_int # UDT4PY_API INT socket_bind(INT fd, WORD port ) udt4py.socket_bind.argtypes = (_wtypes.INT,_wtypes.WORD) udt4py.socket_bind.restype = _ctypes.c_int # UDT4PY_API INT socket_listen(INT fd ) udt4py.socket_listen.argtypes = (_wtypes.INT,) udt4py.socket_listen.restype = _ctypes.c_int # UDT4PY_API INT socket_close(INT fd ) udt4py.socket_close.argtypes = (_wtypes.INT,) udt4py.socket_close.restype = _ctypes.c_int # UDT4PY_API INT socket_accept(INT fd,LPSTR clienthost,LPSTR clientservice ) udt4py.socket_accept.argtypes = (_wtypes.INT,_wtypes.LPCSTR,_wtypes.LPCSTR) udt4py.socket_accept.restype = _ctypes.c_int # UDT4PY_API INT socket_setsockopt(INT fd,INT optname,LPSTR optval,INT optlen ) udt4py.socket_setsockopt.argtypes = (_wtypes.INT,_wtypes.INT,_wtypes.LPCSTR,_wtypes.INT) udt4py.socket_setsockopt.restype = _ctypes.c_int # UDT4PY_API INT socket_recv(INT fd,LPSTR buf,INT len,INT flags ) udt4py.socket_recv.argtypes = (_wtypes.INT,_wtypes.LPCSTR,_wtypes.INT,_wtypes.INT) udt4py.socket_recv.restype = _ctypes.c_int # UDT4PY_API INT socket_sendfile(INT fd ) udt4py.socket_sendfile.argtypes = (_wtypes.INT,) udt4py.socket_sendfile.restype = _ctypes.c_int # UDT4PY_API INT socket_recvfile(INT fd,LPSTR file,INT size ) udt4py.socket_recvfile.argtypes = (_wtypes.INT,_wtypes.LPCSTR,_wtypes.INT) udt4py.socket_recvfile.restype = _ctypes.c_int # UDT4PY_API struct PerfMonitor socket_monitor(INT fd ) udt4py.socket_monitor.argtypes = (_wtypes.INT,) udt4py.socket_monitor.restype = CPerfMonitor def decode_buf(buf): bufout = None buflen = len(buf) i = 0 while i < buflen: v = struct.unpack_from("B", buf,i)[0] if ( v == 0x7E ) and ( i+1 < buflen ): next = struct.unpack_from("B", buf,i+1)[0] if next == 0x7C: if bufout is None: bufout = struct.pack("B", 0x00) else: bufout += struct.pack("B", 0x00) i += 2 elif next == 0x7D: if bufout is None: bufout = struct.pack("B", 0x7E) else: bufout +=struct.pack("B", 0x7E) i += 2 else: if bufout is None: bufout = struct.pack("B", v) else: bufout +=struct.pack("B", v) i += 1 else: if bufout is None: bufout = struct.pack("B", v) else: bufout +=struct.pack("B", v) i += 1 return bufout def udt_recv(fd): buflen = 1500 buf = _ctypes.create_string_buffer(buflen) rxlen = udt4py.socket_recv(fd,buf,buflen,0) if rxlen > 0 : return decode_buf(buf.value) else: return None def udt_socket(): return udt4py.socket_create() def udt_connect(fd,ip,port): return udt4py.socket_connect(fd,ip,port) def udt_bind(fd,port): return udt4py.socket_bind(fd,port) def udt_listen(fd): return udt4py.socket_listen(fd) def udt_accept(fd): clienthost = _ctypes.create_string_buffer(1025) clientservice = _ctypes.create_string_buffer(32) clientfd = udt4py.socket_accept(fd,clienthost,clientservice) return (clientfd,clienthost.value,clientservice.value) def udt_send(fd,buf,buflen): return udt4py.socket_send(fd,buf,buflen) def udt_monitor(fd): return udt4py.socket_monitor(fd) def udt_sendfile(fd): return udt4py.socket_sendfile(fd) def udt_recvfile(fd,filename,filelen): return udt4py.socket_recvfile(fd,filename,filelen)
联系方式:heshengjun@tinywsn.com