ADD file via upload

master
pcrsmk28o 2 years ago
parent c11c19dc2f
commit 75c733b620

@ -0,0 +1,212 @@
#!/bin/python3
import signal
import socket
import sys
import os
from ctypes import *
import threading
so_libskiplist_path = './libskiplist.so'
lib = cdll.LoadLibrary(so_libskiplist_path)
skiplist_cmp_t = CFUNCTYPE(c_int,c_void_p,c_void_p)
lib.skiplist_create.restype = c_int
lib.skiplist_create.argtypes = [c_void_p,c_double,c_void_p]
lib.skiplist_insert.restype = c_int
lib.skiplist_insert.argtypes = [c_void_p,c_void_p,c_void_p]
lib.skiplist_erase.restype = c_int
lib.skiplist_erase.argtypes = [c_void_p,c_void_p,c_void_p]
lib.skiplist_exist.restype = c_int
lib.skiplist_exist.argtypes = [c_void_p,c_void_p,c_void_p]
lib.skiplist_begin.restype = c_int
lib.skiplist_begin.argtypes = [c_void_p,c_void_p]
lib.skiplist_end.restype = c_int
lib.skiplist_end.argtypes = [c_void_p,c_void_p]
lib.skiplist_iterator_equal.restype = c_int
lib.skiplist_iterator_equal.argtypes = [c_void_p,c_void_p]
lib.skiplist_iterator_value.restype = c_void_p
lib.skiplist_iterator_value.argtypes = [c_void_p]
lib.skiplist_iterator_next.argtypes = [c_void_p]
lib.skiplist_iterator_prev.argtypes = [c_void_p]
lib.skiplist_destroy.restype = c_int
lib.skiplist_destroy.argtypes = [c_void_p]
c_int_p = POINTER(c_int)
def __int_cmp(a,b):
_a = cast(a,c_int_p)
_b = cast(b,c_int_p)
return _a.contents.value - _b.contents.value
def __int_show(v):
_v = cast(v,c_int_p)
print(_v.contents.value,end=" ")
int_cmp = skiplist_cmp_t(__int_cmp)
HOST = '0.0.0.0'
PORT = 8001
class int_set:
class iterator:
def __init__(self,show):
self.it = c_void_p()
self._show = show
return
def __eq__(self,o):
return (lib.skiplist_iterator_equal(addressof(self.it),addressof(o.it)) == 1)
def next(self):
lib.skiplist_iterator_next(addressof(self.it))
return
def prev(self):
lib.skiplist_iterator_prev(addressof(self.it))
return
def value(self):
pvalue = lib.skiplist_iterator_value(addressof(self.it))
_v = cast(pvalue,c_int_p)
v = int(_v.contents.value)
return v
def show(self):
pvalue = lib.skiplist_iterator_value(addressof(self.it))
self._show(pvalue)
return
def __init__(self,int_cmp):
self.sl = c_void_p()
self.array = []
self.errno = lib.skiplist_create(addressof(self.sl),0.25,int_cmp)
return
def __del__(self):
errno = lib.skiplist_destroy(addressof(self.sl))
return
def begin(self,it):
errno = lib.skiplist_begin(self.sl,addressof(it.it))
return
def end(self,it):
errno = lib.skiplist_end(self.sl,addressof(it.it))
return
def insert(self,key):
self.array.append(c_int(key))
errno = lib.skiplist_insert(self.sl,addressof(self.array[len(self.array) - 1]),addressof(self.array[len(self.array) - 1]))
if errno != 0:
self.array.pop()
return
def erase(self,key,ret):
_key = c_int(key)
retvalue = c_int(0)
errno = lib.skiplist_erase(self.sl,address(_key),addressof(retvalue))
if errno == 0 :
ret = int(retvalue)
return True
else :
ret = -1
return False
def exist(self,key,ret):
_key = c_int(key)
retvalue = c_int(0)
errno = lib.skiplist_exist(self.sl,addressof(_key),addressof(retvalue))
if errno == 0 :
return True
else :
return False
s = int_set(int_cmp)
conn_pool = []
number = 65536
class MyThread(threading.Thread):
lock = threading.Lock()
def __init__(self,conn):
super(MyThread,self).__init__()
self.conn = conn
return
def run(self):
while True:
request = self.conn.recv(1024)
string = request.decode("UTF-8")
if len(string) == 0:
break
if string.isdigit() == False:
print(string)
self.conn.sendall(bytes("不是数字","UTF-8"))
else:
val = int(string)
self.lock.acquire()
ret = c_void_p()
if s.exist(val,ret) == False:
s.insert(val)
if val == number :
self.conn.sendall(bytes("猜对了","UTF-8"))
for i in range(len(conn_pool)):
conn_pool[i].sendall(bytes("游戏结束","UTF-8"))
sys.exit()
elif val > number:
self.conn.sendall(bytes("大了","UTF-8"))
else :
self.conn.sendall(bytes("小了","UTF-8"))
else:
self.conn.sendall(bytes("重复了","UTF-8"))
self.lock.release()
return
def main():
global int_cmp
global HOST
global PORT
signal.signal(signal.SIGPIPE,signal.SIG_IGN)#忽略半关闭写数据产生的 管道破裂信号
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
s.bind((HOST,PORT))
s.listen(3)
while True :
conn,addr = s.accept() #阻塞 在这 需要用 select 或者 epoll
print("连接上了:",addr)
conn_pool.append(conn)
t = MyThread(conn)
t.start()
if __name__ == "__main__" :
main()
Loading…
Cancel
Save