Exemplo n.º 1
0
    def send_hello(self, ):
        # 与服务端建立socket连接
        with socket.create_connection((host, port)) as sock:
            #sock.send(bytes(testreq,"utf-8") )
            # print("sent",testreq)
            #reply = sock.recv(4096)
            # print(reply)
            # 将socket打包成SSL socket
            print("connect {}:{},as socket {}".format(host, port, sock))
            with context.wrap_socket(sock) as ssock:
                print(ssock)
                # 向服务端发送信息
                buffer = ChannelPack.pack_all(ChannelPack.TYPE_RPC, testreq)
                ssock.send(buffer)
                # 接收服务端返回的信息
                responsePack = None
                respbuffer = bytearray()
                for i in [0, 3]:
                    msg = ssock.recv(1024 * 10)
                    respbuffer += msg
                    (code, len,
                     responsePack) = ChannelPack.unpack(bytes(respbuffer))
                    i += 1
                    if code != 0:
                        continue
                    if len > 0:
                        respbuffer = respbuffer[len:]
                        if code == 0:
                            break
                print(responsePack.result, responsePack.data)

                ssock.close()
Exemplo n.º 2
0
    def read_channel(self):
        # 接收服务端返回的信息
        try:
            self.logger.debug("{} channelHandler.ssock.recv begin.".format(
                self.name))
            msg = self.channelHandler.ssock.recv(1024 * 1024 * 10)
            self.logger.debug("channelHandler.ssock.recv len:{}".format(
                len(msg)))
            if msg is None:
                return -1
            if len(msg) == 0:
                return 0
        except Exception as e:
            self.logger.error("{}:ssock read error {}".format(self.name, e))
            return -1
        self.respbuffer += msg
        # if no enough data even for header ,continue to read
        if len(self.respbuffer) < ChannelPack.getheaderlen():
            return len(msg)

        code = 0
        # decode all packs in buffer from node,maybe got N packs on one read
        # -1 means no enough bytes for decode, should break to  continue read and wait
        while code != -1:
            (code, decodelen,
             responsePack) = ChannelPack.unpack(bytes(self.respbuffer))
            # print("respbuffer:",self.respbuffer)
            if decodelen > 0:
                # cut the buffer from last decode  pos
                self.respbuffer = self.respbuffer[decodelen:]
            if code != -1 and responsePack is not None:  # got a pack
                # print("get a pack from node, put to queue {}".format(responsePack.detail()))
                self.logger.debug(
                    "{}:pack from node, put queue(qsize{}),detail {}".format(
                        self.name, self.recvQueue.qsize(),
                        responsePack.detail()))
                if self.recvQueue.full():
                    self.recvQueue.get(
                    )  # if queue full ,pop the head item ,!! the item LOST
                    self.logger.error(
                        "{}:queue {} FULL pop and LOST: {}".format(
                            self.name, responsePack.type,
                            responsePack.detail()))
                self.recvQueue.put(responsePack)
                # self.print_queue()

        return len(msg)