def __init__(self): self.__ip = '127.0.0.1' self.__port = 8765 self.__exchanges = [Exchange() for Exchange in all_exchanges] self.__exchanges_buffer = Buffer('exchanges') self.__exchanges_buffer.add_view(PriceFrame()) self.__exchanges_buffer_socket = self.__exchanges_buffer.connect() self.__exchanges_buffer_socket.set_owner(self)
class Stream(): name = 'exchanges' __ip = None __port = None __exchanges = None __exchanges_buffer = None __exchanges_buffer_socket = None def __init__(self): self.__ip = '127.0.0.1' self.__port = 8765 self.__exchanges = [Exchange() for Exchange in all_exchanges] self.__exchanges_buffer = Buffer('exchanges') self.__exchanges_buffer.add_view(PriceFrame()) self.__exchanges_buffer_socket = self.__exchanges_buffer.connect() self.__exchanges_buffer_socket.set_owner(self) async def __client_connector(self, pure_websocket, path): try: websocket = Websocket(websocket=pure_websocket) self.__exchanges_buffer.connect(socket=websocket) await websocket.listen() except Exception as e: Logger.log_error(e) ########################### API ############################################ def run(self): tasks = [ websockets.serve(self.__client_connector, self.__ip, self.__port) ] for exchange in self.__exchanges: task = exchange.run() if isinstance(task, list): tasks += task else: tasks.append(task) return asyncio.gather(*tasks) def get_price_frame(self): return utils.pandas_to_dict( self.__exchanges_buffer.views['price_frame'].state) def get_exchanges(self): return [exchange.name for exchange in self.__exchanges]
def _write_func(inst, out, externals, gc_name, signature = None): buf = Buffer() mode = CMode(buf, externals, gc_name) out.no_line() buf.no_line() inst.top_level(mode) mode.stack_flush() buf.close() out << '\n' if 'private' in inst.qualifiers: out << 'static ' if signature is None: signature = externals[inst.name] out << '%s {' % signature mode.declarations(out); out << buf out << '}\n'
async def get(self): pure_socket = web.WebSocketResponse() await pure_socket.prepare(self.request) websocket = Websocket(websocket=pure_socket) Buffer('clients').connect(websocket) await websocket.listen() return pure_socket
class StreamListener(): __ws_path = None __exchanges_buffer = None def __init__(self): self.__ws_path = 'ws://127.0.0.1:8765' self.__exchanges_buffer = Buffer('exchanges', is_mirror=True) async def run(self): while True: try: async with websockets.connect(self.__ws_path) as websocket: socket = Websocket(websocket=websocket) self.__exchanges_buffer.connect(socket=socket) await socket.listen() except Exception as e: Logger.log_error(e) finally: await asyncio.sleep(1)
async def get(self): ctx = decimal.Context() ctx.prec = 10 exchanges_socket = Buffer('exchanges').connect() price_frame_view = await exchanges_socket.execute( 'exchanges.get_price_frame') Logger.log_info('price_frame recieved') Logger.log_info(price_frame_view) await exchanges_socket.close() price_frame_view = price_frame_view.dropna() price_frame_view['gap'] = ( price_frame_view.loc[:, 'gateio'].astype('float64') - price_frame_view.loc[:, 'hitbtc'].astype('float64')).abs() price_frame_view = price_frame_view.sort_values(by='gap', ascending=False) price_frame_view['gap'] = price_frame_view['gap'].apply( lambda x: format(ctx.create_decimal(repr(x)), 'f')) return {'grid': utils.pandas_to_dict(price_frame_view)}
def __init__(self): self.__custom_tasks = [] self.__rest_socket = RESTSocket(url=self._rest_path, request_limit=self._rest_limit) self.__request_counter = 0 self.__exchanges_buffer = Buffer('exchanges').connect()
def __init__(self): self.__ws_path = 'ws://127.0.0.1:8765' self.__exchanges_buffer = Buffer('exchanges', is_mirror=True)
def proxy_loop(socket_src, socket_dst): """ Wait for network activity """ buff_status = 1 buff = None needlen = 0 while not EXIT.get_status(): try: print("[ ] select ...") reader, _, _ = select.select([socket_src, socket_dst], [], [], 1) except select.error as err: error("Select failed", err) return if not reader: #print("[ ] socket events is not read evnets") continue try: for sock in reader: if sock is socket_src: print("[ ] Debug: sock is src") buffer = Buffer(sock.recv(BUFSIZE)) while not buffer.is_empty(): #### 从 client 来的数据,要解密发给 Internet print("[ ] info: process buffer") data = None if buff_status == 1: print("[ ] 1") data = buffer.pop(4) print("[ ] 1 after") if data: print("[ ] 1 : datalen=" + str(len(data))) buff = data[0:] if len(buff) == 4: needlen = (struct.unpack("i", buff))[0] print("[ ] 1 : get data len") buff = None buff_status = 3 elif len(buff) < 4: buff_status = 2 else: break elif buff_status == 2: print("[ ] 2") data = buffer.pop(4-len(buff)) if data: print("[ ] 2 : datalen=" + str(len(data))) buff += data[0:] if len(buff) == 4: needlen = (struct.unpack("i", buff))[0] print("[ ] 2 : get data len") buff = None buff_status = 3 else: break elif buff_status == 3: print("[ ] 3") data = buffer.pop(needlen) if data: print("[ ] 3 : datalen=" + str(len(data))) buff = data[0:] if len(buff) == needlen: buff_status = 4 socket_dst.send(encrypter.decrypt(buff)) print("[ ] 3 : get data") buff = None needlen = 0 buff_status = 1 elif len(buff) < needlen: buff_status = 4 else: break elif buff_status == 4: print("[ ] 4") data = buffer.pop(needlen-len(buff)) if data: print("[ ] 4 : datalen=" + str(len(data))) buff += data[0:] if len(buff) == needlen: socket_dst.send(encrypter.decrypt(buff)) print("[ ] 4 : get data") buff = None needlen = 0 buff_status = 1 else: break else: raise Exception("buff_status error") else: #### 从 Internet 来的数据,要加密发给 client print("[ ] Debug: sock is dst") buff = sock.recv(BUFSIZE) if buff: socket_src.send(encrypter.encrypt(buff)) buff = None except socket.error as err: error("Loop failed", err) return
def write_interpreter(bytecodes, out, gc_name): write_header(bytecodes, out); out << ''' extern int sprintf(void*, ...); uint32_t execution_count[256]; ''' preamble = Buffer() switch = Buffer() postamble = Buffer() l = len(bytecodes.locals) inserts = 0 mode = ExternalMode() for i in bytecodes.instructions: i.process(mode) mode.declarations(out) externals = mode.externals max_refs = 0 return_type = mode.return_type post_check = False have_preamble = False popped = 1 enter_inst = None exit_inst = None for i in bytecodes.instructions: if i.name == '__preamble': have_preamble = True elif i.name == '__postamble': popped += 1 post_check = True elif i.name == '__enter': enter_inst = i elif i.name == '__exit': exit_inst = i for i in bytecodes.instructions: if i.name == '__preamble': temp = Buffer() # Use do { } while (0) to allow far_jump in preamble preamble << ' do { /* Start __preamble */\n' mode = IMode(temp, externals, gc_name, bytecodes.func_name) for x in range(popped): mode.stack_pop(gtypes.p) i.top_level(mode) mode.close() temp.close() mode.declarations(preamble) if max_refs < mode.ref_temps_max: max_refs = mode.ref_temps_max preamble << temp preamble << ' } while (0); /* End __preamble */\n' if post_check: preamble << 'if (_gvmt_ip >= gvmt_ip_end) goto gvmt_postamble;\n' flushed = True elif i.name == '__postamble': temp = Buffer() # far_jump is not allowed here. postamble << ' gvmt_postamble: {\n' mode = IMode(temp, externals, gc_name, bytecodes.func_name) i.top_level(mode) mode.close() temp.close() mode.declarations(postamble); if max_refs < mode.ref_temps_max: max_refs = mode.ref_temps_max postamble << temp postamble << ' }/* */\n' if not have_preamble: preamble << ' {\n' temp = Buffer() mode = IMode(temp, externals, gc_name, bytecodes.func_name) for x in range(popped): mode.stack_pop(gtypes.p) mode.close() mode.declarations(preamble) preamble << temp preamble << ' }\n' if common.token_threading: switch << ' goto *gvmt_operand_table[*_gvmt_ip];\n' else: switch << ' do {\n' switch << ' switch(*_gvmt_ip) {\n' for i in bytecodes.instructions: if 'private' in i.qualifiers or 'componly' in i.qualifiers: continue if common.token_threading: switch << ' _gvmt_label_%s_%s: ((void)0); ' % (bytecodes.func_name, i.name) else: switch << ' case _gvmt_opcode_%s_%s: ' % (bytecodes.func_name, i.name) switch << ' /* Deltas %s %s %s */ ' % i.flow_graph.deltas switch << '{\n' switch << '#define GVMT_CURRENT_OPCODE _gvmt_opcode_%s_%s\n' % (bytecodes.func_name, i.name) # if enter_inst: # switch << '{\n' # temp = Buffer() # mode = IMode(temp, externals, gc_name, bytecodes.func_name) # enter_inst.top_level(mode) # if max_refs < mode.ref_temps_max: # max_refs = mode.ref_temps_max # try: # mode.close() # except UnlocatedException, ex: # raise UnlocatedException("%s in compound instruction '%s'" % (ex.msg, i.name)) # temp.close() # mode.declarations(switch); # switch << temp # switch << ' } \n' temp = Buffer() mode = IMode(temp, externals, gc_name, bytecodes.func_name) mode.stream_fetch() # Opcode if enter_inst: enter_inst.top_level(mode) i.top_level(mode) if exit_inst: exit_inst.top_level(mode) if max_refs < mode.ref_temps_max: max_refs = mode.ref_temps_max try: mode.close() except UnlocatedException, ex: raise UnlocatedException("%s in compound instruction '%s'" % (ex.msg, i.name)) temp.close() mode.declarations(switch); switch << temp # if exit_inst: # switch << '{\n' # temp = Buffer() # mode = IMode(temp, externals, gc_name, bytecodes.func_name) # exit_inst.top_level(mode) # if max_refs < mode.ref_temps_max: # max_refs = mode.ref_temps_max # try: # mode.close() # except UnlocatedException, ex: # raise UnlocatedException("%s in compound instruction '%s'" % (ex.msg, i.name)) # temp.close() # mode.declarations(switch); # switch << temp # switch << ' } \n' if post_check: switch << 'if (_gvmt_ip >= gvmt_ip_end) goto gvmt_postamble;\n' if common.token_threading: switch << ' } goto *gvmt_operand_table[*_gvmt_ip];\n' else: switch << ' } break;\n' switch << '#undef GVMT_CURRENT_OPCODE\n'
if gc_name != 'none': if optimise != '-O0': gc_optimiser.gc_optimise(src_file) gc_inliner.gc_inline(src_file, gc_name) base_name = os.path.basename(args[0]) m_file = os.path.join(sys_compiler.TEMPDIR, '%s.txt' % base_name) to_object(src_file, base_name, library, optimise, sys_headers, gc_name) # out = open(m_file, 'w') # sep = '' full_base = os.path.join(sys_compiler.TEMPDIR, base_name); o = get_output(opts) if not o: if len(args) == 1: dir, f = os.path.split(args[0]) o = open(os.path.join(dir, f.split('.')[0] + '.gso'), 'wb') else: raise GVMTException("No output file specified"); tmp = Buffer() src_file.write_data(tmp) gso_file = gso.GSO(open('%s%s' % (full_base, sys_compiler.OBJ_EXT), 'rb').read(), tmp.get_contents()) gso_file.write(o) o.close() except GVMTException, ex: # raise print ex sys.exit(1)
def proxy_loop_of_client(socket_src, socket_dst): """ Wait for network activity """ # 1: 未读数据 # 2: 等待data_len读取读取完成 # 3: 读取数据 # 4: 等待数据读取完成 buff_status = 1 buff = None needlen = 0 while not EXIT.get_status(): try: reader, _, _ = select.select([socket_src, socket_dst], [], [], 1) except select.error as err: error("Select failed", err) return if not reader: continue try: for sock in reader: if sock is socket_dst: buffer = Buffer(sock.recv(BUFSIZE)) while not buffer.is_empty(): #### 来源于 Sserver 的密文,接收完整后,解密发给 client data = None if buff_status == 1: data = buffer.pop(4) if data: buff = data[0:] if len(buff) == 4: needlen = (struct.unpack("i", buff))[0] buff = None buff_status = 3 elif len(buff) < 4: buff_status = 2 else: break elif buff_status == 2: data = buffer.pop(4 - len(buff)) if data: buff += data[0:] if len(buff) == 4: needlen = (struct.unpack("i", buff))[0] buff = None buff_status = 3 else: break elif buff_status == 3: data = buffer.pop(needlen) if data: buff = data[0:] if len(buff) == needlen: buff_status = 4 socket_src.send(encrypter.decrypt(buff)) buff = None needlen = 0 buff_status = 1 elif len(buff) < needlen: buff_status = 4 else: break elif buff_status == 4: data = buffer.pop(needlen - len(buff)) if data: buff += data[0:] if len(buff) == needlen: socket_src.send(encrypter.decrypt(buff)) buff = None needlen = 0 buff_status = 1 else: break else: raise Exception("buff_status error") else: #### 来源于 client 的明文,直接加密发给 Sserver buff = sock.recv(BUFSIZE) if buff: socket_dst.send(encrypter.encrypt(buff)) buff = None except socket.error as err: error("Loop failed", err) return except Exception as e: error("Loop failed", err) return