def handle_accept(self): current = time.time() if self.state != gvars.NET_STATE_ESTABLISHED: return 0 sock = None try: sock, remote = self.accept() except: pass # 最大支持65536个客户端连接 if self.count >= 0x10000: try: sock.close() except: pass if sock: pos = -1 #找到空位 for i in xrange(len(self.clients)): if self.clients[i] == None: pos = i break #没找到空位则新增 if pos < 0: pos = len(self.clients) self.clients.append(None) hid = (pos & 0xffff) | (self.index << 16) self.index += 1 if self.index >= 0x7fff: self.index = 1 client = netstream.netstream() client.assign(sock) client.hid = hid client.tag = -1 client.active = current client.peername = sock.getpeername() client.on_recv = self.on_client_recv client.on_close = self.on_client_close self.clients[pos] = client self.count += 1 #self.queue.append((gvars.NET_NEW, hid, 0, repr(client.peername))) log.info('client connected, peer: %s, pos: %d, index: %d'%(client.peername, pos, self.index-1))
#coding: gbk import netstream2 as netstream import time import asyncore client = netstream.netstream() client.connect_ex('58.210.93.254', 32005) report = { 'cid': 0x01, 'ctype': 0x11, 'scnt': 198, 'iostat': 0xAA, #0xAA, 0x55 'stot': 200, 'stat': 0x00, 'counter': 0x01, } client.send_report(report) asyncore.loop() while 1: time.sleep(1) #client.close()