private int Write(ByteBuffer buf) { Debug.Assert(_fd != null); if (AssemblyUtil.IsMono) { // // Mono on Android and iOS don't support the use of synchronous socket // operations on a non-blocking socket. Returning 0 here forces the caller to schedule // an asynchronous operation. // return(0); } int packetSize = buf.Remaining(); if (AssemblyUtil.IsWindows) { // // On Windows, limiting the buffer size is important to prevent // poor throughput performances when transfering large amount of // data. See Microsoft KB article KB823764. // if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize / 2) { packetSize = _maxSendPacketSize / 2; } } int sent = 0; while (buf.HasRemaining()) { try { int ret = _fd.Send(buf.RawBytes(), buf.Position(), packetSize, SocketFlags.None); Debug.Assert(ret > 0); sent += ret; buf.Position(buf.Position() + ret); if (packetSize > buf.Remaining()) { packetSize = buf.Remaining(); } } catch (SocketException ex) { if (Network.WouldBlock(ex)) { return(sent); } else if (Network.ConnectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(sent); }
private int Read(ByteBuffer buf) { Debug.Assert(_fd != null); if (AssemblyUtil.IsMono) { // // Mono on Android and iOS don't support the use of synchronous socket // operations on a non-blocking socket. Returning 0 here forces the caller to schedule // an asynchronous operation. // return(0); } int read = 0; while (buf.HasRemaining()) { try { int ret = _fd.Receive(buf.RawBytes(), buf.Position(), buf.Remaining(), SocketFlags.None); if (ret == 0) { throw new Ice.ConnectionLostException(); } read += ret; buf.Position(buf.Position() + ret); } catch (SocketException ex) { if (Network.WouldBlock(ex)) { return(read); } else if (Network.Interrupted(ex)) { continue; } else if (Network.ConnectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(read); }