1957
1962
return socket.gethostname().decode(get_user_encoding())
1960
def recv_all(socket, bytes):
1965
# We must not read/write any more than 64k at a time from/to a socket so we
1966
# don't risk "no buffer space available" errors on some platforms. Windows in
1967
# particular is likely to throw WSAECONNABORTED or WSAENOBUFS if given too much
1969
MAX_SOCKET_CHUNK = 64 * 1024
1971
def read_bytes_from_socket(sock, report_activity=None,
1972
max_read_size=MAX_SOCKET_CHUNK):
1973
"""Read up to max_read_size of bytes from sock and notify of progress.
1975
Translates "Connection reset by peer" into file-like EOF (return an
1976
empty string rather than raise an error), and repeats the recv if
1977
interrupted by a signal.
1981
bytes = sock.recv(max_read_size)
1982
except socket.error, e:
1984
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
1985
# The connection was closed by the other side. Callers expect
1986
# an empty string to signal end-of-stream.
1988
elif eno == errno.EINTR:
1989
# Retry the interrupted recv.
1993
if report_activity is not None:
1994
report_activity(len(bytes), 'read')
1998
def recv_all(socket, count):
1961
1999
"""Receive an exact number of bytes.
1963
2001
Regular Socket.recv() may return less than the requested number of bytes,
1964
dependning on what's in the OS buffer. MSG_WAITALL is not available
2002
depending on what's in the OS buffer. MSG_WAITALL is not available
1965
2003
on all platforms, but this should work everywhere. This will return
1966
2004
less than the requested amount if the remote end closes.
1968
2006
This isn't optimized and is intended mostly for use in testing.
1971
while len(b) < bytes:
1972
new = until_no_eintr(socket.recv, bytes - len(b))
2009
while len(b) < count:
2010
new = read_bytes_from_socket(socket, None, count - len(b))
1979
def send_all(socket, bytes, report_activity=None):
2017
def send_all(sock, bytes, report_activity=None):
1980
2018
"""Send all bytes on a socket.
1982
Regular socket.sendall() can give socket error 10053 on Windows. This
1983
implementation sends no more than 64k at a time, which avoids this problem.
2020
Breaks large blocks in smaller chunks to avoid buffering limitations on
2021
some platforms, and catches EINTR which may be thrown if the send is
2022
interrupted by a signal.
2024
This is preferred to socket.sendall(), because it avoids portability bugs
2025
and provides activity reporting.
1985
2027
:param report_activity: Call this as bytes are read, see
1986
2028
Transport._report_activity
1989
for pos in xrange(0, len(bytes), chunk_size):
1990
block = bytes[pos:pos+chunk_size]
1991
if report_activity is not None:
1992
report_activity(len(block), 'write')
1993
until_no_eintr(socket.sendall, block)
2031
byte_count = len(bytes)
2032
while sent_total < byte_count:
2034
sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
2035
except socket.error, e:
2036
if e.args[0] != errno.EINTR:
2040
report_activity(sent, 'write')
1996
2043
def dereference_path(path):
2069
2116
def until_no_eintr(f, *a, **kw):
2070
"""Run f(*a, **kw), retrying if an EINTR error occurs."""
2117
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2119
WARNING: you must be certain that it is safe to retry the call repeatedly
2120
if EINTR does occur. This is typically only true for low-level operations
2121
like os.read. If in any doubt, don't use this.
2123
Keep in mind that this is not a complete solution to EINTR. There is
2124
probably code in the Python standard library and other dependencies that
2125
may encounter EINTR if a signal arrives (and there is signal handler for
2126
that signal). So this function can reduce the impact for IO that bzrlib
2127
directly controls, but it is not a complete solution.
2071
2129
# Borrowed from Twisted's twisted.python.util.untilConcludes function.