Exemplo n.º 1
0
    def handle_read(self):
        """
            handle read data
        """
        Base.handle_read(self)
        self.recvbuf += self.recv(8192)
        recvbuflen = len(self.recvbuf)
        if recvbuflen >= self.headerlen:
            bodylen, msgid, ownerid, result, encrypt, compress, magicid = struct.unpack("=ihiBBBi",
                                                                                        self.recvbuf[:self.headerlen])
            if recvbuflen >= self.headerlen + bodylen:
                body = self.recvbuf[self.headerlen:self.headerlen + bodylen]
                if result == 0:
                    errid, = struct.unpack("=h", body[:2])
                    self.make_a_tag(self.recvaction, msgid, bodylen, '[errid:{0}]'.format(errid))
                else:
                    strtime = strftime('%Y-%m-%d %H:%M:%S')
                    if msgid == 516:
                        self.make_a_tag(self.recvaction, msgid, bodylen)
                        offset = 1
                        num, = struct.unpack("=B", body[:1])
                        for i in range(0, num):
                            market, timelastsale, code, = struct.unpack("=BI6s", body[offset:offset + 11])
                            offset += 141
                            print "\t[{0}][{1}][{2}]".format(market, code, timelastsale)
                    elif msgid == 609:
                        self.make_a_tag(self.recvaction, msgid, bodylen)
                    elif msgid == 6005:
                        min5_or_day, stocktype, = struct.unpack("=BB", body[:2])
                        self.make_a_tag(self.recvaction, msgid, bodylen,
                                        '[{0}][{1}]'.format({1: 'min5', 0: 'day'}[min5_or_day],
                                                            {2: 'ag',
                                                             7: 'bg',
                                                             9: 'jj',
                                                             5: 'zs',
                                                             8: 'zq'}[stocktype]))
                        offset = 2
                        for i in range(0, 20):
                            for j in range(0, 4):
                                code, name, pre_close, price, volumn, value, = struct.unpack(
                                    "=7s9sffIf",
                                    body[offset+j*20*32:offset+j*20*32+32])
                                print u'\t{0}\t{1:.2f}\t{2:.2f}%'.format(name.decode('cp936'), price,
                                                                         abs(price-pre_close)/pre_close*100),
                                if j == 3:
                                    print ''
                            offset += 32
                    elif msgid == 6008:
                        num, = struct.unpack("=H", body[11:13])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}]'.format(num))
                    elif msgid == 6018:
                        num, = struct.unpack("=B", body[:1])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}]'.format(num))
                    elif msgid == 6020:
                        timelastsale, = struct.unpack("=I", body[9:13])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}]'.format(timelastsale))
                    elif msgid == 6022:
                        offset = 12
                        num, = struct.unpack("=I", body[8:12])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}]'.format(num))
                        for i in range(0, num):
                            hr, mi, se, bs, pri, vol, trades, = struct.unpack("=BBBBIIH", body[offset:offset + 14])
                            offset += 14
                            print "\t[{0:02d}:{1:02d}:{2:02d}][{3}][{4}][{5}][{6}]".format(hr, mi, se, bs, pri, vol,
                                                                                           trades)
                    elif msgid == 6035:
                        num, = struct.unpack("=I", body[1:5])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}]'.format(num))
                        offset = 5
                        for i in range(0, num):
                            stype, mtype, ti, sid, stockid, msglen, = struct.unpack("=BIIIIH", body[offset:offset + 19])
                            offset += 19
                            msg, = struct.unpack("={0}s".format(msglen), body[offset:offset + msglen])
                            print "\t[{0}][{1}][{2}][{3}][{4}]".format(ti, stype, mtype, stockid, msg)
                            offset += (msglen + 1)
                    elif msgid == 6046:
                        timeoflastsale, = struct.unpack("=I", body[1:5])
                        self.make_a_tag(self.recvaction, msgid, bodylen,
                                        '[{0}]'.format(Convenience.time_to_string(timeoflastsale)))
                    elif msgid == 6055:
                        serverdate, servertime = struct.unpack("=II", body[:8])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0},{1}]'.format(serverdate, servertime))
                    elif msgid == 6062:
                        offset = 36
                        seq = 1
                        market, code, = struct.unpack("=B7s", body[:8])
                        pre_num, num, = struct.unpack("=HH", body[32:36])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}:{1}][{2},{3}]'.format(market, code,
                                                                                                     pre_num, num))
                        for i in range(0, num):
                            pri, vol, avgpri, = struct.unpack("=fIf", body[offset:offset + 12])
                            print "\t[{0}][{1:.2f}][{2}][{3:.2f}]".format(seq, pri, vol, avgpri)
                            offset += 12
                            seq += 1
                        seq = 1
                        for i in range(0, pre_num):
                            pri, vol, avgpri, = struct.unpack("=fIf", body[offset:offset + 12])
                            print "\t[pre{0}][{1:.2f}][{2}][{3:.2f}]".format(seq, pri, vol, avgpri)
                            offset += 12
                            seq += 1
                    elif msgid == 6071 or msgid == 6074:
                        incid, pshstat, num, dummy, datalen= struct.unpack("=IBBII", body[:14])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[{0}][{1}][{2}]'.format(incid, pshstat, num))
                        p = create_string_buffer(8192)
                        c_s = c_char_p(body[14:14 + datalen])
                        self.pylcm.uncompress_quoterec_new(c_s, datalen, p)
                        market, timeoflastsale, code, = struct.unpack('=BI7s', p[4:16])
                        print '----{0},{1},{2}'.format(market, timeoflastsale, code)
                        #f = open('e:\\scratch\\tmp.txt', 'wb')
                        #f.write(body[14:14+datalen])
                        #f.close()
                    elif msgid == 6072:
                        incid, pshstat, size, sid, price, type, datalen, = struct.unpack("=IBBIfBI", body[:19])
                        print "[%d][%s][msgid:%d][bodylen:%d][incid:%d][status:%d][size:%d][sid:%d][price:%f][type:%d]" \
                              "[datalen:%d]" % (self.recvseq, strtime, msgid, bodylen, incid, pshstat, size,
                                                sid, price, type, datalen)
                    elif msgid == 6077:
                        incid, pshstat, compresslen, = struct.unpack("=iBi", body[:9])
                        self.make_a_tag(self.recvaction, msgid, bodylen, '[increamentid:{0},pushstat:{1},'
                                                                         'compresslen:{2}]'.format(incid, pshstat,
                                                                                                   compresslen))

                        c_in_buf = c_char_p(body[9:9 + compresslen])
                        out_buf_len = c_int()
                        col_buf_len = c_int()
                        c_out_buf = c_char_p()
                        c_col_buf = c_char_p()
                        snap = c_bool()
                        self.pylcm.uncompress_qtlistrecjg(c_in_buf, compresslen, byref(c_out_buf), byref(out_buf_len),
                                                          byref(c_col_buf), byref(col_buf_len), byref(snap))

                        strcode = create_string_buffer(7)
                        data_buf = create_string_buffer(out_buf_len.value)
                        col_buf = create_string_buffer(col_buf_len.value)
                        memmove(data_buf, c_out_buf, out_buf_len.value)
                        memmove(col_buf, c_col_buf, col_buf_len.value)
                        # for i in range(0, col_buf_len.value, 2):
                        #     col, = struct.unpack('=H', col_buf[i:i+2])
                        #     print col,
                        j = 1
                        if snap.value:
                            self.stocks.clear()
                        listdata = []
                        for i in range(0, out_buf_len.value, 345):
                            stockid, = struct.unpack('=I', data_buf[i:i+4])
                            lastprice, = struct.unpack('=f', data_buf[i+20:i+24])
                            _52_week_high, _52_week_low, = struct.unpack('=ff', data_buf[i+173:i+181])
                            # print '[{0}]{1},{2:.2f},{3:.2f},{4:.2f}'.format(j, stockid, lastprice,
                            #                                                 _52_week_high, _52_week_low)
                            j += 1
                            code_buf = c_char_p()
                            code_len = c_int()
                            market = c_char()
                            self.pylcm.int2marketcode(stockid, strcode, byref(market))
                            self.stocks[stockid] = [market.value, strcode.value, lastprice, _52_week_high, _52_week_low]
                            listdata.append((stockid, market.value, strcode.value, lastprice, _52_week_high, _52_week_low))
                        self.quotelist.update(snap.value, listdata)
                        # print out_buf_len, col_buf_len, snap, out_buf_len.value, self.stocks
                        # market, timeoflastsale, code, = struct.unpack('=BI7s', c_out_buf[4:16])
                        # print '----{0},{1},{2}'.format(market, timeoflastsale, code)
                        # f = open('e:\\scratch\\tmp_{0}_{1}.txt'.format(msgid, self.recvseq), 'wb')
                        # f.write(body[9:])
                        # f.close()
                    else:
                        self.make_a_tag(self.recvaction, msgid, bodylen)
                self.recvbuf = self.recvbuf[self.headerlen + bodylen:]
            else:
                pass
        else:
            pass