def gameProcess(): server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.bind(('0.0.0.0', 6666)) server_socket.setblocking(0) epoll = select.epoll() epoll.register(server_socket.fileno(), select.EPOLLIN) rooms = dict() rooms[11] = dict() rooms[11]['person'] = dict() unackseq = [] personsroom = dict() addrperson = dict() userMgr = user.UserManager() try: while True: events = epoll.poll(1) for fileno, event in events: if fileno == server_socket.fileno(): data, addr = server_socket.recvfrom(1024) response = handleData(data, addr, userMgr, rooms, server_socket, unackseq, personsroom, addrperson) server_socket.sendto(data, addr) finally: print('error') epoll.unregister(server_socket.fileno()) epoll.close() server_socket.close()
def gameProcess(rooms, recentaddrs, unackaddrs, alladdrs, unackseq, roomslock): # 读写锁,写者优先 personsroom = dict() addrperson = dict() BUFFER_SIZE = 1024 epoll = select.epoll() epoll.register(server_socket.fileno(), select.EPOLLIN) userMgr = user.UserManager() def handleData(data, addr=None): room = room_pb2.Room() room.ParseFromString(data) resRoom = room_pb2.Room() if room.type == Types.JOINROOM.value: person = room.persons[0] roomslock.acquire() if not rooms[room.id]['person'].get(person.name): roomi = rooms[room.id] roomi['person'][person.name] = dict() roomi['person'][person.name]['addr'] = addr roomi['person'][person.name]['action'] = [(0.0, 0)] roomi['person'][person.name]['position'] = (0.0, 0.0) roomi['person'][person.name]['rotation'] = 0.0 rooms[room.id] = roomi personsroom[person.name] = room.id addrperson[addr] = person.name print('room ' + str(room.id) + ' add person ' + person.name) roomslock.release() elif room.type == Types.GAMEDATA.value: print('recv seq ' + str(room.seq) + ' len ' + str(len(data))) personname = addrperson[addr] roomid = personsroom[personname] roomslock.acquire() if len(rooms[roomid]['person'][personname]['action']) == room.seq: roomi = rooms[roomid] roomi['person'][personname]['action'].append((room.rotation, room.direction)) roomi['person'][personname]['rotation'] = (room.rotation + roomi['person'][personname]['rotation']) % (math.pi * 2) roomi['person'][personname]['rotation'] = roomi['person'][personname]['rotation'] * room.direction rooms[roomid] = roomi else: # rooms[roomid]['person'][personname]['action'][room.seq] = (room.rotation, room.direction) pass roomslock.release() roomres = room_pb2.Room() roomres.type = Types.ACK.value roomres.seq = room.seq server_socket.sendto(roomres.SerializeToString(), addr) print('send seq ' + str(room.seq) + ' ack to ' + str(addr) ) elif room.type == Types.ACK.value: seqs = unackseq[addr] for s in seqs: if s <= room.seq: seqs.remove(s) unackseq[addr] = seqs print('recv seq ' + str(room.seq) + ' ack') return None elif room.type == Types.PING.value: if addr in unackaddrs: unackaddrs.pop(addr) return resRoom.SerializeToString() try: while True: events = epoll.poll(1) for fileno, event in events: if fileno == server_socket.fileno(): data, addr = server_socket.recvfrom(1024) if not alladdrs.get(addr): alladdrs[addr] = addr print(alladdrs) if not recentaddrs.get(addr): recentaddrs[addr] = 100 else: for ad in recentaddrs.keys(): if ad == addr: recentaddrs[ad] += 1 else: recentaddrs[ad] -= 1 if recentaddrs[ad] <= 0: recentaddrs.pop(ad) response = handleData(data, addr) # server_socket.sendto(response, addr) finally: print('error') epoll.unregister(serversocket.fileno()) epoll.close() serversocket.close()
def loginProcess(rooms): BUFFER_SIZE = 1024 server_socket_tcp = socket.socket() server_socket_tcp.bind(('0.0.0.0', 5555)) server_socket_tcp.listen(3) server_socket_tcp.setblocking(0) # 注册事件 epoll = select.epoll() epoll.register(server_socket_tcp.fileno(), select.EPOLLIN) # 用户管理模块 userMgr = user.UserManager() # 读写锁,写者优先 rwl = rwlWriter() connections = {} # 储存所有TCP连接 requests = {} # 储存所有request的bytes responses = {} # 储存所有要response的bytes def handleData(data): room = room_pb2.Room() room.ParseFromString(data) resRoom = room_pb2.Room() if room.type == Types.LOGIN.value: person = room.persons[0] username = person.name password = person.password if not userMgr.checkUser({'name':username, 'password':password}): resRoom.type = Types.SUCC.value print('user: '******' login!') else: resRoom.type = Types.FAILED.value print('user: '******' login failed') elif room.type == Types.REGIST.value: person = room.persons[0] username = person.name password = person.password if not userMgr.addUser({'name':username, 'password':password}): resRoom.type = Types.SUCC.value else: resRoom.type = Types.FAILED.value elif room.type == Types.NEWROOM.value: pass return resRoom.SerializeToString() try: while True: events = epoll.poll(1) for fileno, event in events: if fileno == server_socket_tcp.fileno(): connection, address = server_socket_tcp.accept() print("Client addr: ", address) connection.setblocking(0) epoll.register(connection.fileno(), select.EPOLLIN) connections[connection.fileno()] = connection requests[connection.fileno()] = b'' elif event & select.EPOLLIN: requests[fileno] += connections[fileno].recv(1024) responses[fileno] = handleData(requests[fileno]) requests[fileno] = b'' epoll.modify(fileno, select.EPOLLOUT) elif event & select.EPOLLOUT: byteswritten = connections[fileno].send(responses[fileno]) responses[fileno] = responses[fileno][byteswritten:] if len(responses[fileno]) == 0: epoll.modify(fileno, select.EPOLLIN) # connections[fileno].shutdown(socket.SHUT_RDWR) elif event & select.EPOLLHUP: epoll.unregister(fileno) connections[fileno].close() del connections[fileno] finally: epoll.unregister(server_socket_tcp.fileno()) epoll.close() server_socket_tcp.close()
BUFFER_SIZE = 50 server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) host = '0.0.0.0' port = 4444 server_socket.bind((host, port)) server_socket.setblocking(0) epoll = select.epoll() epoll.register(server_socket.fileno(), select.EPOLLIN) TYPE_LOGIN = 0 TYPE_REGIST = 1 users = dict() userMgr = user.UserManager() rwl = rwlWriter() EOL1 = b'\n\n' EOL2 = b'\n\r\n' response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n' response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n' response += b'Hello, world!' connections = {} requests = {} responses = {} def handleData(data): room = room_pb2.Room()
def gameProcess(): def brocast(): seq = 1 k = 10 time.sleep(2) while True: if len(list(rooms[11]['person'])) > 0: room = room_pb2.Room() room.type = Types.GAMEDATA.value room.seq = seq if len(list(rooms[11]['person']['heim']['action'])) < seq + 1: continue p1 = room.persons.add() p2 = room.persons.add() unackseq.append(seq) seq += 1 data = room.SerializeToString() server_socket.sendto(data, rooms[11]['person']['heim']['addr']) print('send seq ' + str(room.seq) + ' len ' + str(len(data))) # resend unack packet # for s in unackseq: # if seq - s > k: # room = room_pb2.Room() # room.type = Types.GAMEDATA.value # room.seq = s # server_socket.sendto(room.SerializeToString(), rooms[11]['person']['heim']['addr']) # print('resend seq ' + str(s)) # time.sleep(0.2) rooms = dict() rooms[11] = dict() rooms[11]['person'] = dict() unackseq = [] personsroom = dict() addrperson = dict() BUFFER_SIZE = 1024 server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 使用UDP server_socket.bind(('0.0.0.0', 5555)) server_socket.setblocking(0) epoll = select.epoll() epoll.register(server_socket.fileno(), select.EPOLLIN) userMgr = user.UserManager() rwl = rwlWriter() connections = {} requests = {} responses = {} # t = threading.Thread(target=brocast) # t.start() # change to process to improve the speed # pp = multiprocessing.Pipe() # pp2 = multiprocessing.Pipe() # p = Process(target=brocast, args=(server_socket, pp[1], pp2[1])) # p.start() def handleData(data, addr=None): room = room_pb2.Room() room.ParseFromString(data) resRoom = room_pb2.Room() if room.type == Types.LOGIN.value: person = room.persons[0] username = person.name password = person.password # print('user: '******' try to login!') if not userMgr.checkUser({'name': username, 'password': password}): resRoom.type = Types.SUCC.value print('login succ') else: resRoom.type = Types.FAILED.value print('login failed') elif room.type == Types.REGIST.value: person = room.persons[0] username = person.name password = person.password if not userMgr.addUser({'name': username, 'password': password}): resRoom.type = Types.SUCC.value else: resRoom.type = Types.FAILED.value elif room.type == Types.JOINROOM.value: person = room.persons[0] if not rooms[room.id]['person'].get(person.name): rooms[room.id]['person'][person.name] = dict() rooms[room.id]['person'][person.name]['addr'] = addr rooms[room.id]['person'][person.name]['action'] = [(0.0, 0)] personsroom[person.name] = room.id addrperson[addr] = person.name # print('room ' + str(room.id) + ' add person ' + person.name) elif room.type == Types.GAMEDATA.value: print('recv seq ' + str(room.seq) + ' len ' + str(len(data))) personname = addrperson[addr] roomid = personsroom[personname] if len(rooms[roomid]['person'][personname]['action']) == room.seq: rooms[roomid]['person'][personname]['action'].append( (room.rotation, room.direction)) else: # rooms[roomid]['person'][personname]['action'][room.seq] = (room.rotation, room.direction) pass # print(rooms[room.id]['person'][person.name]['action']) roomres = room_pb2.Room() roomres.type = Types.ACK.value roomres.seq = room.seq server_socket.sendto(roomres.SerializeToString(), addr) # print('send seq ' + str(room.seq) + ' ack to ' + str(addr) ) # to delete roomres = room_pb2.Room() roomres.type = Types.GAMEDATA.value roomres.seq = room.seq p1 = roomres.persons.add() data = roomres.SerializeToString() server_socket.sendto(data, addr) # print('send seq ' + str(roomres.seq) + ' len ' + str(len(data))) elif room.type == Types.ACK.value: for s in unackseq: if s <= room.seq: unackseq.remove(s) # print('recv seq ' + str(room.seq) + ' ack') return None return resRoom.SerializeToString() try: while True: events = epoll.poll(1) for fileno, event in events: if fileno == server_socket.fileno(): data, addr = server_socket.recvfrom(1024) response = handleData(data, addr) # server_socket.sendto(response, addr) finally: print('error') epoll.unregister(serversocket.fileno()) epoll.close() serversocket.close()