当前位置: 首页>>代码示例>>Python>>正文


Python Buffer.putData方法代码示例

本文整理汇总了Python中buffer.Buffer.putData方法的典型用法代码示例。如果您正苦于以下问题:Python Buffer.putData方法的具体用法?Python Buffer.putData怎么用?Python Buffer.putData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在buffer.Buffer的用法示例。


在下文中一共展示了Buffer.putData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import putData [as 别名]
class Connection:
    ''' Tcp connection '''
    def __init__(self, loop, socket, remote = None):
        self.__sock = socket
        self.__sock.setblocking(False)
        self.__peeraddr = remote

        self.__loop = loop

        self.__recvbuf = Buffer()
        self.__sendbuf = Buffer()
        self.__shouldPollWrite = False

        self.__onConnectCallback = None
        self.__onDisconnectCallback = None

        self.__msgCallback = None
        self.__futures = []

    def __str__(self):
        return "connection from:" + str(self.__peeraddr)

    @property
    def peerAddr(self):
        return str(self.__peeraddr)

    def setMsgCallback(self, callback):
        self.__msgCallback = callback

    def setOnConnectCallback(self, callback):
        self.__onConnectCallback = callback

    def setOnDisconnectCallback(self, callback):
        self.__onDisconnectCallback = callback

    def fileno(self):
        return self.__sock.fileno()

    def send(self, data):
        '''High level send data interface, deal with EWOULDBLOCK'''
        if self.__sock is None:
            return False

        if self.__shouldPollWrite:
            self.__sendbuf.putData(data)
            return True

        sent = self._rawSend(data)
        if sent < 0:
            return False

        if self.__shouldPollWrite:
            from ioloop import IOLoop
            self.__loop.modify(self, IOLoop.READ_EVENT | IOLoop.WRITE_EVENT)

        return True

    def _rawSend(self, data):
        '''A wrapper for send data on socket'''
        if len(data) == 0:
            return 0

        sent = 0
        try:
            sent = self.__sock.send(data)
            assert sent != 0

            if sent > 0:
                self.__shouldPollWrite = (sent < len(data))

        except (socket.error, IOError, OSError) as e:
            if e.args[0] in _ERRNO_WOULDBLOCK:
                self.__shouldPollWrite = True
                assert sent == -1
                sent = 0
            else:
                logging.error("_rawSend error %s", str(e.args[0]))
                sent = -1

        return sent


    def close(self):
        if self.__sock is None:
            return

        try:
            self.__sock.shutdown(socket.SHUT_WR)
            self.__sock.close()
            self.__sock = None

            if self.__onDisconnectCallback is not None:
                self.__onDisconnectCallback(self)

        except socket.error as serr:
            pass

    def onConnect(self):
        if self.__onConnectCallback is not None:
            self.__onConnectCallback(self)
#.........这里部分代码省略.........
开发者ID:loveyacper,项目名称:pyredis,代码行数:103,代码来源:connection.py


注:本文中的buffer.Buffer.putData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。