/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/medium.py

Merge 2.0 into 2.1 resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
bzrlib/transport/smart/__init__.py.
25
25
"""
26
26
 
 
27
import errno
27
28
import os
 
29
import socket
28
30
import sys
29
31
import urllib
30
32
 
31
33
from bzrlib.lazy_import import lazy_import
32
34
lazy_import(globals(), """
33
35
import atexit
34
 
import socket
35
36
import thread
36
37
import weakref
37
38
 
46
47
from bzrlib.smart import client, protocol, request, vfs
47
48
from bzrlib.transport import ssh
48
49
""")
 
50
#usually already imported, and getting IllegalScoperReplacer on it here.
49
51
from bzrlib import osutils
50
52
 
51
 
# Throughout this module buffer size parameters are either limited to be at
52
 
# most _MAX_READ_SIZE, or are ignored and _MAX_READ_SIZE is used instead.
53
 
# For this module's purposes, MAX_SOCKET_CHUNK is a reasonable size for reads
54
 
# from non-sockets as well.
55
 
_MAX_READ_SIZE = osutils.MAX_SOCKET_CHUNK
 
53
# We must not read any more than 64k at a time so we don't risk "no buffer
 
54
# space available" errors on some platforms.  Windows in particular is likely
 
55
# to give error 10053 or 10055 if we read more than 64k from a socket.
 
56
_MAX_READ_SIZE = 64 * 1024
 
57
 
56
58
 
57
59
def _get_protocol_factory_for_bytes(bytes):
58
60
    """Determine the right protocol factory for 'bytes'.
274
276
    def _serve_one_request_unguarded(self, protocol):
275
277
        while protocol.next_read_size():
276
278
            # We can safely try to read large chunks.  If there is less data
277
 
            # than MAX_SOCKET_CHUNK ready, the socket will just return a
278
 
            # short read immediately rather than block.
279
 
            bytes = self.read_bytes(osutils.MAX_SOCKET_CHUNK)
 
279
            # than _MAX_READ_SIZE ready, the socket wil just return a short
 
280
            # read immediately rather than block.
 
281
            bytes = self.read_bytes(_MAX_READ_SIZE)
280
282
            if bytes == '':
281
283
                self.finished = True
282
284
                return
285
287
        self._push_back(protocol.unused_data)
286
288
 
287
289
    def _read_bytes(self, desired_count):
288
 
        return osutils.read_bytes_from_socket(
289
 
            self.socket, self._report_activity)
 
290
        return _read_bytes_from_socket(
 
291
            self.socket.recv, desired_count, self._report_activity)
290
292
 
291
293
    def terminate_due_to_error(self):
292
294
        # TODO: This should log to a server log file, but no such thing
293
295
        # exists yet.  Andrew Bennetts 2006-09-29.
294
 
        self.socket.close()
 
296
        osutils.until_no_eintr(self.socket.close)
295
297
        self.finished = True
296
298
 
297
299
    def _write_out(self, bytes):
332
334
            bytes_to_read = protocol.next_read_size()
333
335
            if bytes_to_read == 0:
334
336
                # Finished serving this request.
335
 
                self._out.flush()
 
337
                osutils.until_no_eintr(self._out.flush)
336
338
                return
337
339
            bytes = self.read_bytes(bytes_to_read)
338
340
            if bytes == '':
339
341
                # Connection has been closed.
340
342
                self.finished = True
341
 
                self._out.flush()
 
343
                osutils.until_no_eintr(self._out.flush)
342
344
                return
343
345
            protocol.accept_bytes(bytes)
344
346
 
345
347
    def _read_bytes(self, desired_count):
346
 
        return self._in.read(desired_count)
 
348
        return osutils.until_no_eintr(self._in.read, desired_count)
347
349
 
348
350
    def terminate_due_to_error(self):
349
351
        # TODO: This should log to a server log file, but no such thing
350
352
        # exists yet.  Andrew Bennetts 2006-09-29.
351
 
        self._out.close()
 
353
        osutils.until_no_eintr(self._out.close)
352
354
        self.finished = True
353
355
 
354
356
    def _write_out(self, bytes):
355
 
        self._out.write(bytes)
 
357
        osutils.until_no_eintr(self._out.write, bytes)
356
358
 
357
359
 
358
360
class SmartClientMediumRequest(object):
715
717
    """A client medium using simple pipes.
716
718
 
717
719
    This client does not manage the pipes: it assumes they will always be open.
718
 
 
719
 
    Note that if readable_pipe.read might raise IOError or OSError with errno
720
 
    of EINTR, it must be safe to retry the read.  Plain CPython fileobjects
721
 
    (such as used for sys.stdin) are safe.
722
720
    """
723
721
 
724
722
    def __init__(self, readable_pipe, writeable_pipe, base):
728
726
 
729
727
    def _accept_bytes(self, bytes):
730
728
        """See SmartClientStreamMedium.accept_bytes."""
731
 
        self._writeable_pipe.write(bytes)
 
729
        osutils.until_no_eintr(self._writeable_pipe.write, bytes)
732
730
        self._report_activity(len(bytes), 'write')
733
731
 
734
732
    def _flush(self):
735
733
        """See SmartClientStreamMedium._flush()."""
736
 
        self._writeable_pipe.flush()
 
734
        osutils.until_no_eintr(self._writeable_pipe.flush)
737
735
 
738
736
    def _read_bytes(self, count):
739
737
        """See SmartClientStreamMedium._read_bytes."""
785
783
    def _accept_bytes(self, bytes):
786
784
        """See SmartClientStreamMedium.accept_bytes."""
787
785
        self._ensure_connection()
788
 
        self._write_to.write(bytes)
 
786
        osutils.until_no_eintr(self._write_to.write, bytes)
789
787
        self._report_activity(len(bytes), 'write')
790
788
 
791
789
    def disconnect(self):
792
790
        """See SmartClientMedium.disconnect()."""
793
791
        if not self._connected:
794
792
            return
795
 
        self._read_from.close()
796
 
        self._write_to.close()
 
793
        osutils.until_no_eintr(self._read_from.close)
 
794
        osutils.until_no_eintr(self._write_to.close)
797
795
        self._ssh_connection.close()
798
796
        self._connected = False
799
797
 
822
820
        if not self._connected:
823
821
            raise errors.MediumNotConnected(self)
824
822
        bytes_to_read = min(count, _MAX_READ_SIZE)
825
 
        bytes = self._read_from.read(bytes_to_read)
 
823
        bytes = osutils.until_no_eintr(self._read_from.read, bytes_to_read)
826
824
        self._report_activity(len(bytes), 'read')
827
825
        return bytes
828
826
 
852
850
        """See SmartClientMedium.disconnect()."""
853
851
        if not self._connected:
854
852
            return
855
 
        self._socket.close()
 
853
        osutils.until_no_eintr(self._socket.close)
856
854
        self._socket = None
857
855
        self._connected = False
858
856
 
906
904
        """See SmartClientMedium.read_bytes."""
907
905
        if not self._connected:
908
906
            raise errors.MediumNotConnected(self)
909
 
        return osutils.read_bytes_from_socket(
910
 
            self._socket, self._report_activity)
 
907
        return _read_bytes_from_socket(
 
908
            self._socket.recv, count, self._report_activity)
911
909
 
912
910
 
913
911
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
950
948
        self._medium._flush()
951
949
 
952
950
 
 
951
def _read_bytes_from_socket(sock, desired_count, report_activity):
 
952
    # We ignore the desired_count because on sockets it's more efficient to
 
953
    # read large chunks (of _MAX_READ_SIZE bytes) at a time.
 
954
    try:
 
955
        bytes = osutils.until_no_eintr(sock, _MAX_READ_SIZE)
 
956
    except socket.error, e:
 
957
        if len(e.args) and e.args[0] in (errno.ECONNRESET, 10054):
 
958
            # The connection was closed by the other side.  Callers expect an
 
959
            # empty string to signal end-of-stream.
 
960
            bytes = ''
 
961
        else:
 
962
            raise
 
963
    else:
 
964
        report_activity(len(bytes), 'read')
 
965
    return bytes
 
966