Captures a message from given socket.

    :param socket.socket sock:
        The socket to read a message from.
    :param bool get_channel:
        Find out which channel the message comes from.

    :return: The received message, or None on failure.
493765
def capture_message(sock, get_channel=False):
    
    # Fetching the Arb ID, DLC and Data
    try:
        if get_channel:
            if HAS_NATIVE_SUPPORT:
                cf, addr = sock.recvfrom(CANFD_MTU)
                channel = addr[0] if isinstance(addr, tuple) else addr
            else:
                data = ctypes.create_string_buffer(CANFD_MTU)
                addr = ctypes.create_string_buffer(32)
                addrlen = ctypes.c_int(len(addr))
                received = libc.recvfrom(sock.fileno(), data, len(data), 0,
                                         addr, ctypes.byref(addrlen))
                cf = data.raw[:received]
                # Figure out the channel name
                family, ifindex = struct.unpack_from("Hi", addr.raw)
                assert family == AF_CAN
                data = struct.pack("16xi", ifindex)
                res = fcntl.ioctl(sock, SIOCGIFNAME, data)
                channel = ctypes.create_string_buffer(res).value.decode()
        else:
            cf = sock.recv(CANFD_MTU)
            channel = None
    except socket.error as exc:
        raise can.CanError("Error receiving: %s" % exc)

    can_id, can_dlc, flags, data = dissect_can_frame(cf)
    #log.debug('Received: can_id=%x, can_dlc=%x, data=%s', can_id, can_dlc, data)

    # Fetching the timestamp
    binary_structure = "@LL"
    res = fcntl.ioctl(sock, SIOCGSTAMP, struct.pack(binary_structure, 0, 0))

    seconds, microseconds = struct.unpack(binary_structure, res)
    timestamp = seconds + microseconds * 1e-6

    # EXT, RTR, ERR flags -> boolean attributes
    #   /* special address description flags for the CAN_ID */
    #   #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
    #   #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
    #   #define CAN_ERR_FLAG 0x20000000U /* error frame */
    is_extended_frame_format = bool(can_id & CAN_EFF_FLAG)
    is_remote_transmission_request = bool(can_id & CAN_RTR_FLAG)
    is_error_frame = bool(can_id & CAN_ERR_FLAG)
    is_fd = len(cf) == CANFD_MTU
    bitrate_switch = bool(flags & CANFD_BRS)
    error_state_indicator = bool(flags & CANFD_ESI)

    if is_extended_frame_format:
        #log.debug("CAN: Extended")
        # TODO does this depend on SFF or EFF?
        arbitration_id = can_id & 0x1FFFFFFF
    else:
        #log.debug("CAN: Standard")
        arbitration_id = can_id & 0x000007FF

    msg = Message(timestamp=timestamp,
                  channel=channel,
                  arbitration_id=arbitration_id,
                  is_extended_id=is_extended_frame_format,
                  is_remote_frame=is_remote_transmission_request,
                  is_error_frame=is_error_frame,
                  is_fd=is_fd,
                  bitrate_switch=bitrate_switch,
                  error_state_indicator=error_state_indicator,
                  dlc=can_dlc,
                  data=data)

    #log_rx.debug('Received: %s', msg)

    return msg
992023
def mock_server_receive(sock, length):
    """"""
    msg = b''
    while length:
        chunk = sock.recv(length)
        if chunk == b'':
            raise socket.error(errno.ECONNRESET, 'closed')

        length -= len(chunk)
        msg += chunk

    return msg
511992
def get_socket(host, port, timeout=None):
    
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
        af, socktype, proto, canonname, sa = res
        sock = None
        try:
            sock = socket(af, socktype, proto)
            if timeout is not None:
                sock.settimeout(timeout)
            sock.connect(sa)
            return sock

        except error:
            if sock is not None:
                sock.close()

    raise error
153941
def recvall(sock, size):
    
    data = b""
    while len(data) < size:
        s = sock.recv(size - len(data))
        data += s
        if not s:
            raise RuntimeError("did not receive data from socket "
                               "(size {}, got only {!r})".format(size, data))
    return data
1304183
def broadcast_channel(message, channel):
    
    try:
        socket = CLIENTS[CHANNELS.get(channel, [])[0]][1]
    except (IndexError, KeyError):
        raise NoSocket("There are no clients on the channel: " + channel)
    socket.send_and_broadcast_channel(message, channel)
714426
def recv_msg(self):
        ''''''
        try:
            while True:
                if HEADER_END_STR in self._recv_buf: break
                more = self._sock.recv(2048)
                if not more: break
                self._recv_buf += more
        except socket.error, e:
            PRINT('Receive data error: %s'%e,RED)
            sys.exit(-1)

        msg = ''
        if self._recv_buf:
            (msg,self._recv_buf) = self._recv_buf.split(HEADER_END_STR,1)
            content_length = self._get_content_length(msg)
            msg += HEADER_END_STR + self._recv_buf[:content_length]
            self._recv_buf = self._recv_buf[content_length:]
        return msg
1066063
def _receiving(self):
        
        while self._is_running:
            try:
                rlist, wlist, xlist = select.select(
                    self._listening, [], [],
                    self._select_timeout
                )
            except:
                self.exception("Failed to select socket")
                continue
            for sock in rlist:
                try:
                    self._get_packet(sock)
                except:
                    self.exception("Failed to receive packet")
887361
def client_socket(self, config):
		
		if self.__client_socket is None:
			self.__client_socket = self.create_client_socket(config)
		return self.__client_socket
866463
def receive_message(self):
		
		while not self.recv_message_queue:
			self.transport.fill_inbuffer()
		message = self.recv_message_queue.pop(0)
		assert message is not None
		if not message.verify(self.protocol.authenticators):
			self.close()
			raise OmapiError("bad omapi message signature")
		return message
726646
def recv(self):
        

        # See if we have a message to process...
        if self._recvbuf:
            return self._recvbuf_pop()

        # If it's closed, don't try to read more data
        if not self._sock:
            raise ConnectionClosed("Connection closed")

        # OK, get some data from the socket
        while True:
            try:
                data = self._sock.recv(4096)
            except socket.error:
                # We'll need to re-raise
                e_type, e_value, e_tb = sys.exc_info()

                # Make sure the socket is closed
                self.close()

                # Re-raise
                raise e_type, e_value, e_tb

            # Did the connection get closed?
            if not data:
                # There can never be anything in the buffer here
                self.close()
                raise ConnectionClosed("Connection closed")

            # Begin parsing the read-in data
            partial = self._recvbuf_partial + data
            self._recvbuf_partial = ''
            while partial:
                msg, sep, partial = partial.partition('\n')

                # If we have no sep, then it's not a complete message,
                # and the remainder is in msg
                if not sep:
                    self._recvbuf_partial = msg
                    break

                # Parse the message
                try:
                    self._recvbuf.append(json.loads(msg))
                except ValueError as exc:
                    # Error parsing the message; save the exception,
                    # which we will re-raise
                    self._recvbuf.append(exc)

            # Make sure we have a message to return
            if self._recvbuf:
                return self._recvbuf_pop()
427640
def recv(request_context=None, non_blocking=False):
    

    if non_blocking:
        result = uwsgi.websocket_recv_nb(request_context)

    else:
        result = uwsgi.websocket_recv(request_context)

    return result
108767
def socket_read(fp):
    
    response = ''
    oldlen = 0
    newlen = 0
    while True:
        response += fp.read(buffSize)
        newlen = len(response)
        if newlen - oldlen == 0:
            break
        else:
            oldlen = newlen
    return response
814005
def publish(self, channel, message):
        
        if self.cfg.jsonpickle:
            message = jsonpickle.encode(message)
        return self.conn.publish(channel, message)
106650
def nn_recv(socket, *args):
    ""
    if len(args) == 1:
        flags, = args
        pointer = ctypes.c_void_p()
        rtn = _nn_recv(socket, ctypes.byref(pointer), ctypes.c_size_t(-1),
                       flags)
        if rtn < 0:
            return rtn, None
        else:
            return rtn, _create_message(pointer.value, rtn)
    elif len(args) == 2:
        msg_buf, flags = args
        mv_buf = memoryview(msg_buf)
        if mv_buf.readonly:
            raise TypeError('Writable buffer is required')
        rtn = _nn_recv(socket, ctypes.addressof(msg_buf), len(mv_buf), flags)
        return rtn, msg_buf
1106714
def broadcast_channel(self, message, channel=None):
        
        if channel is None:
            channels = self.channels
        else:
            channels = [channel]
        for channel in channels:
            for subscriber in CHANNELS[channel]:
                if subscriber != self.socket.session.session_id:
                    session = self.socket.handler.server.sessions[subscriber]
                    self._write(message, session)
949068
def receive(self):
        '''
        
        '''
        try:
            msg, addr = self.skt.recvfrom(self.buffer_size)
        except socket.error as error:
            log.error('Received listener socket error: %s', error, exc_info=True)
            raise ListenerException(error)
        log.debug('[%s] Received %s from %s', msg, addr, time.time())
        return msg, addr[0]
133799
def _receive(self):
        
        result = self._talk.get()
        if not result:
            self._logger.error('Failed to receive')
        return result