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


Python Buffer.has方法代码示例

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


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

示例1: __init__

# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import has [as 别名]
class Session:
  """
  The session makes sure that all sent messages are queued until they receive a response.
  """
  headerframe = None
  default = None

  def __init__(self, peer):
    self.server = peer.server
    self.peer = peer
    
    ## queue containing all received data
    self.buffer = Buffer();
    
    self.tx = 0
    self.rx = 0
    
    ## just a small optimization for slow connections
    self.expectsize = None
    
    ## all running periodcalls
    self._periods = list();
    
    assert self.headerframe is not None, "headerframe is None"
    
    if self.default:
      self.switch(self.default)
    
    self.sessionInit();
  
  def period(self, time, cb, *args, **kw):
    period = task.LoopingCall(cb, *args, **kw)
    period.start(time)
    self._periods.append(period)

  def later(self, cb, *args, **kw):
    reactor.callLater(0, cb, *args, **kw)
  
  def check_recv(self):
    """
    Check receiver queue and pack the header frame when possible.
    """
    headersize = self.headerframe._size()
    
    while self.buffer.has(self.expectsize):
      # nothing to do since we have not received any header frames yet...
      if not self.buffer.has(headersize):
        self.debug("Buffer does not contain enough data for header (no payload)")
        return
      
      data = self.buffer.read(headersize, buffered=True)
      
      header = self.headerframe()._unpack(data)
      
      if not self.validateHeader(header):
        self.debug("Validation of header frame failed")
        return self.lose()

      totalsize = headersize + self.getPayloadSize(header)
      
      if not self.buffer.has(totalsize):
        ## do not read header if it has already been done, we know next header + payload
        self.expectsize = totalsize
        self.debug("Buffer does not contain enough data for header + payload")
        return
      
      # flush header, header and payload is usually sent in the same packet
      # this should be more optimized than sending seperately
      data = self.buffer.read(totalsize)[headersize:]
      self.rx += totalsize
      
      if not self.validatePayload(header, data):
        self.debug("Validation of frame failed, losing connection")
        return self.lose();
      
      self.receiveFrame(header, data)
      self.expectsize = None
    
    return
    
  def recv(self, data):
    self.buffer.write(data)
    self.check_recv();

  def send(self, frame):
    self.prepareFrame(frame)

    header = self.buildHeaderFrame(self.headerframe, frame)
    
    if header is NotImplemented:
      self.debug("Session's buildHeaderFrame method not implemented!")
      return self.lose()
    
    data = header + frame._pack()
    
    self.tx += len(data)
    self.sendMessage(data)
    return True

  def lose(self):
#.........这里部分代码省略.........
开发者ID:udoprog,项目名称:metap2p,代码行数:103,代码来源:session.py


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