def server_process(self): try: # G = global_Models() msgDist = self.msg_disc.copy() msgDist['label'] = self.G.get("label") if msgDist['fx_type'] == "MakeUpOrder": # 发送补仓命令 flag_num = yield self.TraversingConnection( msgDist.get('sendid'), msgDist.get('followid')) msgDist['return'] = flag_num msgDist['fx_type'] = "ReturnStatus" msgDist['key'] = DistMD5.encryptDist(msgDist) logger.debug("MakeUpOrder:%s" % flag_num) yield self.send_message(json.dumps(msgDist), msgDist.get('sendid')) elif msgDist['fx_type'] == "MakeOpenOrder": # 发送开仓命令 flag_num = yield self.TraversingConnection( msgDist.get('sendid'), msgDist.get('followid')) msgDist['return'] = flag_num msgDist['fx_type'] = "ReturnStatus" msgDist['key'] = DistMD5.encryptDist(msgDist) # logger.debug("MakeOpenOrder:%s" % flag_num) yield self.send_message(json.dumps(msgDist), msgDist.get('followid')) elif msgDist['fx_type'] == "ChickStatus": msgDist['return'] = 5 msgDist['fx_type'] = "ReturnStatus" msgDist['key'] = DistMD5.encryptDist(msgDist) yield self.send_message(json.dumps(msgDist), msgDist.get('label')) # 保持与其他服务器的连接 # time.sleep(60) # yield self.socket_between_process(0, 0) elif msgDist['fx_type'] == "ReturnStatus": logger.info("ReturnStatus:followid:%s" % msgDist.get('followid')) else: # 无命令 logger.debug("[no-process]%s" % msgDist) # yield self.on_close() # return # logger.debug("send_end:%s" % self.msg_disc) self.G.set_map("ServerTime", int(time.time())) # yield self.close_same_label() if self.S: # print("del S...") self.S = None yield self.read_message() except Exception as e: # iostream.StreamClosedError logger.error("[Connection:server_process]error:%s" % e)
def receive_message(self): """ 接收数据 """ try: msg = yield self.stream.read_until(bytes("\n", encoding="utf8")) if msg != "": msg = msg[:-6] self.logger.info("[chick_user]read:%s" % msg) msg_disc = json.loads(msg) if DistMD5.chickDist(msg_disc): # self.time_vol = int(time.time()) # logger.debug("return:%s" % msg_disc['return']) if msg_disc['return'] >= 0: # if msg_disc['fx_type'] == "MakeUpOrder": self.logger.info("UpOrder,set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) # elif msg_disc['fx_type'] == "MakeOpenOrder": # logger.debug("OpenOrder,set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) # else: # yield self.R.set_ErrTotalConsole(self.client_id) # logger.error("fx_type error:", msg_disc['fx_type']) else: # yield self.R.set_ErrTotalConsole(self.client_id) self.logger.info("return<0,%s" % msg_disc['return']) # logger.debug("receive data:%s" % msg_disc) yield self.on_receive() return else: yield self.receive_message() except Exception as e: self.logger.info("tcp client exception:%s" % e) return
def client_send_message(self, msgDist={}): # 处理指令待发送数据 if msgDist == {}: msgDist["sendid"] = 0 msgDist["followid"] = 0 msgDist["fx_type"] = "ChickStatus" # source_label = msgDist['label'] for conn in SocketClientModel.clients: # if conn.label == source_label: # break msgDist['label'] = conn.client_id msgDist['key2'] = config.ServerMd5Info try: msgDist['key'] = DistMD5.encryptDist(msgDist) # 发送数据 msg_str = bytes(json.dumps(msgDist) + "\n", encoding="utf8") conn.time_vol = int(time.time()) if conn.stream: yield conn.stream.write(msg_str) yield conn.receive_message() else: yield conn.connect(msgDist) logger.debug("[SocketClientModel:client_send_message:send_message]to:%s: %s" % (conn.label, msgDist)) conn.time_vol = int(time.time()) except Exception as e: logger.error("[SocketClientModel:client_send_message:label:%s]error:%s" % (conn.label, e)) conn.flag = False yield conn.on_receive()
def receive_message(self): """ 接收数据 """ try: time_out = time.time() # while(True): msg = yield self.stream.read_until(bytes("\n", encoding="utf8")) if msg != "": msg = msg[:-6] logger.debug("[SocketClientModel:receive_message]read:%s" % msg) msg_disc = json.loads(msg) if DistMD5.chickDist(msg_disc): # self.time_vol = int(time.time()) # logger.debug("return:%s" % msg_disc['return']) if msg_disc['fx_type'] == "MakeUpOrder": logger.debug("UpOrder,set_Ok") yield self.client_send_message(msg_disc) # yield self.R.set_EndTotalConsole(self.client_id) msg_disc['fx_type'] = "ReturnStatus" msg_disc['return'] = 5 yield self.send_message(msg_disc) elif msg_disc['fx_type'] == "MakeOpenOrder": logger.debug("OpenOrder,set_Ok") yield self.client_send_message(msg_disc) msg_disc['fx_type'] = "ReturnStatus" msg_disc['return'] = 5 yield self.send_message(msg_disc) # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "ChickStatus": if msg_disc.get('return') >= 0: logger.debug("[SocketClientModel:receive_message]ChickStatus,set_Ok") else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error("[SocketClientModel:receive_message]ChickStatus,return<0,", msg_disc.get('return')) self.time_vol = int(time.time()) # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "ReturnStatus": logger.info("[SocketClientModel:receive_message:ReturnStatus]%s:followid:%s,sendid:%s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), msg_disc['followid'], msg_disc['sendid'])) # yield self.sock_while() else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error("[[SocketClientModel:receive_message:fx_type] error:", msg_disc['fx_type']) # logger.debug("receive data:%s" % msg_disc) # yield self.on_receive() # break else: if time.time() - time_out > 5: logger.error("[SocketClientModel:receive_message:out_time]:%s S", time.time() - time_out) # break # yield self.sock_while() logger.debug("[SocketClientModel]runtime:%s" % (time.time()-self.time_vol,)) except Exception as e: logger.error("[SocketClientModel:receive_message]tcp client exception:%s" % e) finally: # yield self.on_receive() pass
def receive_message(self, conn): """ 接收数据 """ try: time_out = time.time() msg = yield conn.stream.read_until(bytes("\n", encoding="utf8")) if msg != "": msg = msg[:-6] logger.debug("[SocketClientModel:chick_user]read:%s" % msg) msg_disc = json.loads(msg) if DistMD5.chickDist(msg_disc): # self.time_vol = int(time.time()) # logger.debug("return:%s" % msg_disc['return']) if msg_disc['return'] >= 0: if msg_disc['fx_type'] == "MakeUpOrder": logger.debug("[SocketClientModel:UpOrder],set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "MakeOpenOrder": logger.debug( "[SocketClientModel:OpenOrder],set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "ChickStatus": logger.debug( "[SocketClientModel:ChickStatus],set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error( "[SocketClientModel:receive_message] fx_type-error:%s" % msg_disc['fx_type']) else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error( "[SocketClientModel:receive_message]return:%s" % msg_disc['return']) # logger.debug("receive data:%s" % msg_disc) # yield self.on_receive() # break else: logger.error( "[SocketClientModel:receive_message]out_time:%.2f S" % time.time() - time_out) # break conn.time_vol = time.time() logger.debug("[SocketClientModel,updata-time]%s{%s}" % (conn.stream.socket.getsockname(), conn.time_vol)) return except Exception as e: logger.error( "[SocketClientModel:receive_message]tcp client exception:%s" % e) conn.flag = False # self.on_receive(conn) return
def socket_between_process(self, sendid, followid, fx_type=None): msgDist = {} msgDist["sendid"] = int(sendid) msgDist["followid"] = int(followid) msgDist["fx_type"] = fx_type if msgDist["fx_type"] == None: msgDist["fx_type"] = "MakeOpenOrder" # G = global_Models() msgDist['label'] = self.G.get("label") msgDist['key2'] = config.ServerMd5Info msgDist['key'] = DistMD5.encryptDist(msgDist) yield self.socket_server_process(msgDist)
def chick_redisConsole(label): msgDist = R.pop_Console_list() if not msgDist: if time.time() - int(G.get("time_vol_" + label)) < 1 * 55: return None if msgDist == {}: msgDist["sendid"] = 0 msgDist["followid"] = 0 msgDist["fx_type"] = "ChickStatus" msgDist['label'] = client_id msgDist['key2'] = config.ServerMd5Info msgDist['key'] = DistMD5.encryptDist(msgDist) msg_str = bytes(json.dumps(msgDist) + "\n", encoding="utf8") return msg_str
def chick_server_text(self): # 检查文本格式 if self.msg_disc.get('key').encode(encoding='UTF-8').isalnum(): if str(self.msg_disc.get('sendid')).isdigit(): if self.msg_disc.get('fx_type').encode(encoding='UTF-8').isalnum(): if str(self.msg_disc.get('followid')).isdigit(): from models.public.headers_model import DistMD5 if DistMD5.chickDist(self.msg_disc): # print("True") return True else: # print("False") return False return False
def send_message(self, msgDist): # 处理待发送数据 msgDist["sendid"] = int(msgDist["sendid"]) msgDist["followid"] = int(msgDist["followid"]) msgDist['label'] = str(self.client_id) msgDist['key2'] = config.ServerMd5Info try: msgDist['key'] = DistMD5.encryptDist(msgDist) # 发送数据 yy = bytes(json.dumps(msgDist) + "\n", encoding="utf8") yield self.stream.write(yy) self.logger.info("send_message:%s" % msgDist) yield self.receive_message() except Exception as e: self.logger.error("[SocketClientModel:send_message]error:%s" % e) yield self.on_receive()
def sort_data(self, msg_dist={}): self.time_vol = time.time() if msg_dist == {}: msg_dist = self.msgDist if msg_dist == {}: msg_dist["sendid"] = 0 msg_dist["followid"] = 0 msg_dist["fx_type"] = "ChickStatus" msg_dist['label'] = self.client_id if msg_dist.get('label'): source_label = msg_dist['label'] else: source_label = self.client_id msg_dist['label'] = self.client_id msg_dist['key2'] = config.ServerMd5Info msg_dist['key'] = DistMD5.encryptDist(msg_dist) # 发送数据 msg_str = bytes(json.dumps(msg_dist) + "\n", encoding="utf8") return msg_str, source_label
def receive_message(self): """ 接收数据 """ try: time_out = time.time() while(True): msg = yield self.stream.read_until(bytes("\n", encoding="utf8")) if msg != "": msg = msg[:-6] logger.debug("[chick_user]read:%s" % msg) msg_disc = json.loads(msg) if DistMD5.chickDist(msg_disc): # self.time_vol = int(time.time()) # logger.debug("return:%s" % msg_disc['return']) if msg_disc['return'] >= 0: if msg_disc['fx_type'] == "MakeUpOrder": logger.debug("UpOrder,set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "MakeOpenOrder": logger.debug("OpenOrder,set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) elif msg_disc['fx_type'] == "ChickStatus": logger.debug("ChickStatus,set_Ok") # yield self.R.set_EndTotalConsole(self.client_id) else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error("fx_type error:", msg_disc['fx_type']) else: # yield self.R.set_ErrTotalConsole(self.client_id) logger.error("return<0,", msg_disc['return']) # logger.debug("receive data:%s" % msg_disc) # yield self.on_receive() break else: if time.time() - time_out > 5: logger.error("[receive_message:out_time]:%s S", time.time() - time_out) break print(time.time()-self.time_vol) return except Exception as e: logger.error("tcp client exception:%s" % e) return
def send_message(self, msgDist): # 处理待发送数据 msgDist["sendid"] = 0 msgDist["followid"] = 0 msgDist["fx_type"] = "ChickStatus" msgDist['label'] = self.client_id msgDist['key2'] = config.ServerMd5Info try: msgDist['key'] = DistMD5.encryptDist(msgDist) # 发送数据 yy = bytes(json.dumps(msgDist) + "\n", encoding="utf8") # yy = b"ooooooooooo" self.time_vol = time.time() yield self.stream.write(yy) # yield self.stream.shutdown(1) logger.debug("send_message:%s" % msgDist) yield self.receive_message() except Exception as e: logger.error("[SocketClientModel:send_message]error:%s" % e)
def send_message(self, msgDist, conn): # 处理待发送数据 msgDist["sendid"] = 2 msgDist["followid"] = 1 msgDist["fx_type"] = "ChickStatus" msgDist['id'] = conn.client_id msgDist['key2'] = config.ServerMd5Info try: msgDist['key'] = DistMD5.encryptDist(msgDist) # 发送数据 logger.debug("[SocketClientModel:send_message]strat:%s-->%s" % (conn.stream.socket.getsockname(), conn.stream.socket.getpeername())) yy = bytes(json.dumps(msgDist) + "\n", encoding="utf8") yield conn.stream.write(yy) # yield self.stream.shutdown(1) logger.debug("[SocketClientModel:send_message]write:%s" % msgDist) yield self.receive_message(conn) except Exception as e: logger.error("[SocketClientModel:send_message]error:%s" % e) conn.flag = False
def send_message(self, msgDist={}): # 处理待发送数据 if msgDist == {}: msgDist["sendid"] = 0 msgDist["followid"] = 0 msgDist["fx_type"] = "ChickStatus" msgDist['label'] = self.client_id msgDist['key2'] = config.ServerMd5Info try: msgDist['key'] = DistMD5.encryptDist(msgDist) # 发送数据 msg_str = bytes(json.dumps(msgDist) + "\n", encoding="utf8") # self.time_vol = int(time.time()) if self.stream: yield self.stream.write(msg_str) yield self.receive_message() else: yield self.connect(msgDist) logger.debug("send_message:%s" % msgDist) # self.time_vol = int(time.time()) except Exception as e: logger.error("[SocketClientModel:send_message]error:%s" % e) self.flag = False yield self.on_receive()