1893
1893
return socket.gethostname().decode(get_user_encoding())
1896
@deprecated_function(deprecated_in((2, 2, 0)))
1897
def recv_all(socket, bytes):
1898
"""See bzrlib.tests.test_smart_transport._recv_all"""
1899
return test_smart_transport._recv_all(socket, bytes)
1902
_MAX_SOCKET_CHUNK = 64 * 1024
1896
# We must not read/write any more than 64k at a time from/to a socket so we
1897
# don't risk "no buffer space available" errors on some platforms. Windows in
1898
# particular is likely to throw WSAECONNABORTED or WSAENOBUFS if given too much
1900
MAX_SOCKET_CHUNK = 64 * 1024
1902
def read_bytes_from_socket(sock, report_activity,
1903
max_read_size=MAX_SOCKET_CHUNK):
1904
"""Read up to max_read_size of bytes from sock and notify of progress.
1906
Translates "Connection reset by peer" into file-like EOF (return an
1907
empty string rather than raise an error), and repeats the recv if
1908
interrupted by a signal.
1912
bytes = sock.recv(max_read_size)
1913
except socket.error, e:
1915
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
1916
# The connection was closed by the other side. Callers expect
1917
# an empty string to signal end-of-stream.
1919
elif eno == errno.EINTR:
1920
# Retry the interrupted recv.
1924
report_activity(len(bytes), 'read')
1928
def recv_all(socket, count):
1929
"""Receive an exact number of bytes.
1931
Regular Socket.recv() may return less than the requested number of bytes,
1932
depending on what's in the OS buffer. MSG_WAITALL is not available
1933
on all platforms, but this should work everywhere. This will return
1934
less than the requested amount if the remote end closes.
1936
This isn't optimized and is intended mostly for use in testing.
1939
reporter = lambda n, rw: None
1940
while len(b) < count:
1941
new = read_bytes_from_socket(socket, reporter, count - len(b))
1904
1948
def send_all(sock, bytes, report_activity=None):
1905
1949
"""Send all bytes on a socket.
1908
1952
some platforms, and catches EINTR which may be thrown if the send is
1909
1953
interrupted by a signal.
1911
This is preferred to socket.sendall(), because it avoids bugs and provides
1912
reasonable activity reporting.
1955
This is preferred to socket.sendall(), because it avoids portability bugs
1956
and provides activity reporting.
1914
1958
:param report_activity: Call this as bytes are read, see
1915
1959
Transport._report_activity
1918
1962
byte_count = len(bytes)
1919
1963
while sent_total < byte_count:
1921
sent = sock.send(buffer(bytes, sent_total, _MAX_SOCKET_CHUNK))
1965
sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
1922
1966
except socket.error, e:
1923
1967
if e.args[0] != errno.EINTR: