Imported Upstream version 6.8.0.73

Former-commit-id: d18deab1b47cfd3ad8cba82b3f37d00eec2170af
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-12-10 18:00:56 +00:00
parent bceda29824
commit 73ee7591e8
1043 changed files with 16271 additions and 22080 deletions

View File

@ -608,15 +608,15 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.AcceptSocket = e.current_socket.EndAccept (ares);
e.AcceptSocket = e.CurrentSocket.EndAccept (ares);
} catch (SocketException ex) {
e.SocketError = ex.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
if (e.AcceptSocket == null)
e.AcceptSocket = new Socket (e.current_socket.AddressFamily, e.current_socket.SocketType, e.current_socket.ProtocolType, null);
e.Complete ();
e.AcceptSocket = new Socket (e.CurrentSocket.AddressFamily, e.CurrentSocket.SocketType, e.CurrentSocket.ProtocolType, null);
e.Complete_internal ();
}
});
@ -740,7 +740,7 @@ namespace System.Net.Sockets
sockares.CheckIfThrowDelayedException ();
buffer = sockares.Buffer;
buffer = sockares.Buffer.ToArray ();
bytesTransferred = sockares.Total;
return sockares.AcceptedSocket;
@ -959,7 +959,7 @@ namespace System.Net.Sockets
*
* Note that we're not calling `e.Complete ()` (or resetting `e.in_progress`) here.
*/
e.current_socket.EndConnect (ares);
e.CurrentSocket.EndConnect (ares);
}
return pending;
@ -979,7 +979,7 @@ namespace System.Net.Sockets
throw new ArgumentNullException("e");
if (e.in_progress != 0 && e.LastOperation == SocketAsyncOperation.Connect)
e.current_socket?.Close ();
e.CurrentSocket?.Close ();
}
static AsyncCallback ConnectAsyncCallback = new AsyncCallback (ares => {
@ -989,13 +989,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.current_socket.EndConnect (ares);
e.CurrentSocket.EndConnect (ares);
} catch (SocketException se) {
e.SocketError = se.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -1254,7 +1254,7 @@ namespace System.Net.Sockets
Array.Resize (ref addresses, last_valid);
return true;
} else {
e.ConnectByNameError = null;
e.SetConnectByNameError (null);
return false;
}
}
@ -1307,13 +1307,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.current_socket.EndDisconnect (ares);
e.CurrentSocket.EndDisconnect (ares);
} catch (SocketException ex) {
e.SocketError = ex.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -1399,6 +1399,29 @@ namespace System.Net.Sockets
return ret;
}
int Receive (Memory<byte> buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
{
ThrowIfDisposedAndClosed ();
int nativeError;
int ret;
unsafe {
using (var handle = buffer.Slice (offset, size).Pin ()) {
ret = Receive_internal (m_Handle, (byte*)handle.Pointer, size, socketFlags, out nativeError, is_blocking);
}
}
errorCode = (SocketError) nativeError;
if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
is_connected = false;
is_bound = false;
} else {
is_connected = true;
}
return ret;
}
[CLSCompliant (false)]
public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
{
@ -1477,10 +1500,10 @@ namespace System.Net.Sockets
// LAME SPEC: the ArgumentException is never thrown, instead an NRE is
// thrown when e.Buffer and e.BufferList are null (works fine when one is
// set to a valid object)
if (e.Buffer == null && e.BufferList == null)
if (e.MemoryBuffer.Equals (default) && e.BufferList == null)
throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
if (e.Buffer == null) {
if (e.BufferList != null) {
InitSocketAsyncEventArgs (e, ReceiveAsyncCallback, e, SocketOperation.ReceiveGeneric);
e.socket_async_result.Buffers = e.BufferList;
@ -1489,7 +1512,7 @@ namespace System.Net.Sockets
} else {
InitSocketAsyncEventArgs (e, ReceiveAsyncCallback, e, SocketOperation.Receive);
e.socket_async_result.Buffer = e.Buffer;
e.socket_async_result.Buffer = e.MemoryBuffer;
e.socket_async_result.Offset = e.Offset;
e.socket_async_result.Size = e.Count;
@ -1506,13 +1529,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.BytesTransferred = e.current_socket.EndReceive (ares);
e.SetBytesTransferred (e.CurrentSocket.EndReceive (ares));
} catch (SocketException se){
e.SocketError = se.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -1545,8 +1568,8 @@ namespace System.Net.Sockets
try {
unsafe {
fixed (byte* pbuffer = sockares.Buffer) {
total = Receive_internal (sockares.socket.m_Handle, &pbuffer[sockares.Offset], sockares.Size, sockares.SockFlags, out sockares.error, sockares.socket.is_blocking);
using (var pbuffer = sockares.Buffer.Slice (sockares.Offset, sockares.Size).Pin ()) {
total = Receive_internal (sockares.socket.m_Handle, (byte*)pbuffer.Pointer, sockares.Size, sockares.SockFlags, out sockares.error, sockares.socket.is_blocking);
}
}
} catch (Exception e) {
@ -1700,6 +1723,44 @@ namespace System.Net.Sockets
return cnt;
}
int ReceiveFrom (Memory<byte> buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, out SocketError errorCode)
{
SocketAddress sockaddr = remoteEP.Serialize();
int nativeError;
int cnt;
unsafe {
using (var handle = buffer.Slice (offset, size).Pin ()) {
cnt = ReceiveFrom_internal (m_Handle, (byte*)handle.Pointer, size, socketFlags, ref sockaddr, out nativeError, is_blocking);
}
}
errorCode = (SocketError) nativeError;
if (errorCode != SocketError.Success) {
if (errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
is_connected = false;
} else if (errorCode == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
errorCode = SocketError.TimedOut;
}
return 0;
}
is_connected = true;
is_bound = true;
/* If sockaddr is null then we're a connection oriented protocol and should ignore the
* remoteEP parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
if (sockaddr != null) {
/* Stupidly, EndPoint.Create() is an instance method */
remoteEP = remoteEP.Create (sockaddr);
}
seed_endpoint = remoteEP;
return cnt;
}
public bool ReceiveFromAsync (SocketAsyncEventArgs e)
{
ThrowIfDisposedAndClosed ();
@ -1730,13 +1791,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.BytesTransferred = e.current_socket.EndReceiveFrom (ares, ref e.remote_ep);
e.SetBytesTransferred (e.CurrentSocket.EndReceiveFrom_internal ((SocketAsyncResult)ares, e));
} catch (SocketException ex) {
e.SocketError = ex.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -1801,7 +1862,20 @@ namespace System.Net.Sockets
return sockares.Total;
}
int EndReceiveFrom_internal (SocketAsyncResult sockares, SocketAsyncEventArgs ares)
{
ThrowIfDisposedAndClosed ();
if (Interlocked.CompareExchange (ref sockares.EndCalled, 1, 0) == 1)
throw new InvalidOperationException ("EndReceiveFrom can only be called once per asynchronous operation");
if (!sockares.IsCompleted)
sockares.AsyncWaitHandle.WaitOne ();
sockares.CheckIfThrowDelayedException ();
ares.RemoteEndPoint = sockares.EndPoint;
return sockares.Total;
}
static unsafe int ReceiveFrom_internal (SafeSocketHandle safeHandle, byte* buffer, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error, bool blocking)
{
@ -1967,10 +2041,10 @@ namespace System.Net.Sockets
ThrowIfDisposedAndClosed ();
if (e.Buffer == null && e.BufferList == null)
if (e.MemoryBuffer.Equals (default) && e.BufferList == null)
throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
if (e.Buffer == null) {
if (e.BufferList != null) {
InitSocketAsyncEventArgs (e, SendAsyncCallback, e, SocketOperation.SendGeneric);
e.socket_async_result.Buffers = e.BufferList;
@ -1979,7 +2053,7 @@ namespace System.Net.Sockets
} else {
InitSocketAsyncEventArgs (e, SendAsyncCallback, e, SocketOperation.Send);
e.socket_async_result.Buffer = e.Buffer;
e.socket_async_result.Buffer = e.MemoryBuffer;
e.socket_async_result.Offset = e.Offset;
e.socket_async_result.Size = e.Count;
@ -1996,13 +2070,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.BytesTransferred = e.current_socket.EndSend (ares);
e.SetBytesTransferred (e.CurrentSocket.EndSend (ares));
} catch (SocketException se){
e.SocketError = se.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -2037,8 +2111,8 @@ namespace System.Net.Sockets
try {
unsafe {
fixed (byte *pbuffer = sockares.Buffer) {
total = Socket.Send_internal (sockares.socket.m_Handle, &pbuffer[sockares.Offset], sockares.Size, sockares.SockFlags, out sockares.error, false);
using (var pbuffer = sockares.Buffer.Slice (sockares.Offset, sockares.Size).Pin ()) {
total = Socket.Send_internal (sockares.socket.m_Handle, (byte*)pbuffer.Pointer, sockares.Size, sockares.SockFlags, out sockares.error, false);
}
}
} catch (Exception e) {
@ -2189,6 +2263,35 @@ namespace System.Net.Sockets
return ret;
}
int SendTo (Memory<byte> buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)
{
ThrowIfDisposedAndClosed ();
if (remoteEP == null)
throw new ArgumentNullException("remoteEP");
int error;
int ret;
unsafe {
using (var pbuffer = buffer.Slice (offset, size).Pin ()) {
ret = SendTo_internal (m_Handle, (byte*)pbuffer.Pointer, size, socketFlags, remoteEP.Serialize (), out error, is_blocking);
}
}
SocketError err = (SocketError) error;
if (err != 0) {
if (err != SocketError.WouldBlock && err != SocketError.InProgress)
is_connected = false;
throw new SocketException (error);
}
is_connected = true;
is_bound = true;
seed_endpoint = remoteEP;
return ret;
}
public bool SendToAsync (SocketAsyncEventArgs e)
{
// NO check is made whether e != null in MS.NET (NRE is thrown in such case)
@ -2220,13 +2323,13 @@ namespace System.Net.Sockets
throw new InvalidOperationException ("No operation in progress");
try {
e.BytesTransferred = e.current_socket.EndSendTo (ares);
e.SetBytesTransferred (e.CurrentSocket.EndSendTo (ares));
} catch (SocketException ex) {
e.SocketError = ex.SocketErrorCode;
} catch (ObjectDisposedException) {
e.SocketError = SocketError.OperationAborted;
} finally {
e.Complete ();
e.Complete_internal ();
}
});
@ -2857,10 +2960,10 @@ namespace System.Net.Sockets
if (e.AcceptSocket != null) {
e.socket_async_result.AcceptSocket = e.AcceptSocket;
}
e.current_socket = this;
e.SetCurrentSocket (this);
e.SetLastOperation (SocketOperationToSocketAsyncOperation (operation));
e.SocketError = SocketError.Success;
e.BytesTransferred = 0;
e.SetBytesTransferred (0);
}
SocketAsyncOperation SocketOperationToSocketAsyncOperation (SocketOperation op)

View File

@ -36,19 +36,19 @@ using System.Threading;
namespace System.Net.Sockets
{
public class SocketAsyncEventArgs : EventArgs, IDisposable
public partial class SocketAsyncEventArgs : EventArgs, IDisposable
{
bool disposed;
internal volatile int in_progress;
internal EndPoint remote_ep;
internal Socket current_socket;
EndPoint remote_ep;
Socket current_socket;
internal SocketAsyncResult socket_async_result = new SocketAsyncResult ();
public Exception ConnectByNameError {
get;
internal set;
private set;
}
public Socket AcceptSocket {
@ -56,31 +56,9 @@ namespace System.Net.Sockets
set;
}
public byte[] Buffer {
get;
private set;
}
public Memory<byte> MemoryBuffer => Buffer;
internal IList<ArraySegment<byte>> m_BufferList;
public IList<ArraySegment<byte>> BufferList {
get { return m_BufferList; }
set {
if (Buffer != null && value != null)
throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
m_BufferList = value;
}
}
public int BytesTransferred {
get;
internal set;
}
public int Count {
get;
internal set;
private set;
}
public bool DisconnectReuseSocket {
@ -93,11 +71,6 @@ namespace System.Net.Sockets
private set;
}
public int Offset {
get;
private set;
}
public EndPoint RemoteEndPoint {
get { return remote_ep; }
set { remote_ep = value; }
@ -150,24 +123,17 @@ namespace System.Net.Sockets
}
}
internal bool PolicyRestricted {
get;
private set;
}
public event EventHandler<SocketAsyncEventArgs> Completed;
internal SocketAsyncEventArgs (bool policy)
: this ()
{
PolicyRestricted = policy;
}
public SocketAsyncEventArgs ()
{
SendPacketsSendSize = -1;
}
internal SocketAsyncEventArgs (bool flowExecutionContext)
{
}
~SocketAsyncEventArgs ()
{
Dispose (false);
@ -187,6 +153,25 @@ namespace System.Net.Sockets
GC.SuppressFinalize (this);
}
internal void SetConnectByNameError (Exception error)
{
ConnectByNameError = error;
}
internal void SetBytesTransferred (int value)
{
BytesTransferred = value;
}
internal Socket CurrentSocket {
get { return current_socket; }
}
internal void SetCurrentSocket (Socket socket)
{
current_socket = socket;
}
internal void SetLastOperation (SocketAsyncOperation op)
{
if (disposed)
@ -197,7 +182,7 @@ namespace System.Net.Sockets
LastOperation = op;
}
internal void Complete ()
internal void Complete_internal ()
{
in_progress = 0;
OnCompleted (this);
@ -213,43 +198,6 @@ namespace System.Net.Sockets
handler (e.current_socket, e);
}
internal void CopyBufferFrom (SocketAsyncEventArgs source)
{
Buffer = source.Buffer;
Offset = source.Offset;
Count = source.Count;
}
public void SetBuffer (int offset, int count)
{
SetBuffer (Buffer, offset, count);
}
public void SetBuffer (byte[] buffer, int offset, int count)
{
if (buffer != null) {
if (BufferList != null)
throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
int buflen = buffer.Length;
if (offset < 0 || (offset != 0 && offset >= buflen))
throw new ArgumentOutOfRangeException ("offset");
if (count < 0 || count > buflen - offset)
throw new ArgumentOutOfRangeException ("count");
Count = count;
Offset = offset;
}
Buffer = buffer;
}
public void SetBuffer(Memory<byte> buffer)
{
SetBuffer(buffer.ToArray(), 0, buffer.Length);
}
internal void StartOperationCommon (Socket socket)
{
current_socket = socket;
@ -269,7 +217,7 @@ namespace System.Net.Sockets
if (current_socket != null)
current_socket.is_connected = false;
Complete ();
Complete_internal ();
}
internal void FinishOperationAsyncFailure (Exception exception, int bytesTransferred, SocketFlags flags)
@ -279,7 +227,7 @@ namespace System.Net.Sockets
if (current_socket != null)
current_socket.is_connected = false;
Complete ();
Complete_internal ();
}
internal void FinishWrapperConnectSuccess (Socket connectSocket, int bytesTransferred, SocketFlags flags)
@ -287,7 +235,7 @@ namespace System.Net.Sockets
SetResults(SocketError.Success, bytesTransferred, flags);
current_socket = connectSocket;
Complete ();
Complete_internal ();
}
internal void SetResults (SocketError socketError, int bytesTransferred, SocketFlags flags)

View File

@ -43,7 +43,7 @@ namespace System.Net.Sockets
Exception DelayedException;
public EndPoint EndPoint; // Connect,ReceiveFrom,SendTo
public byte [] Buffer; // Receive,ReceiveFrom,Send,SendTo
public Memory<byte> Buffer; // Receive,ReceiveFrom,Send,SendTo
public int Offset; // Receive,ReceiveFrom,Send,SendTo
public int Size; // Receive,ReceiveFrom,Send,SendTo
public SocketFlags SockFlags; // Receive,ReceiveFrom,Send,SendTo