Imported Upstream version 4.3.2.467

Former-commit-id: 9c2cb47f45fa221e661ab616387c9cda183f283d
This commit is contained in:
Xamarin Public Jenkins
2016-02-22 11:00:01 -05:00
parent f302175246
commit f3e3aab35a
4097 changed files with 122406 additions and 82300 deletions

View File

@ -35,19 +35,12 @@ using System.Threading;
namespace System.Net.Sockets
{
[StructLayout (LayoutKind.Sequential)]
internal sealed class SocketAsyncResult: IAsyncResult, IThreadPoolWorkItem
internal sealed class SocketAsyncResult: IOAsyncResult
{
/* Same structure in the runtime. Keep this in sync with
* MonoSocketAsyncResult in metadata/socket-io.h and
* ProcessAsyncReader in System.Diagnostics/Process.cs. */
public Socket socket;
IntPtr handle;
object state;
AsyncCallback callback; // used from the runtime
WaitHandle wait_handle;
public SocketOperation operation;
Exception delayed_exception;
Exception DelayedException;
public EndPoint EndPoint; // Connect,ReceiveFrom,SendTo
public byte [] Buffer; // Receive,ReceiveFrom,Send,SendTo
@ -59,108 +52,33 @@ namespace System.Net.Sockets
public int Port; // Connect
public IList<ArraySegment<byte>> Buffers; // Receive, Send
public bool ReuseSocket; // Disconnect
// Return values
Socket accept_socket;
int total;
bool completed_synchronously;
bool completed;
bool is_blocking;
internal int error;
public SocketOperation operation;
AsyncResult async_result;
public int EndCalled;
/* These fields are not in MonoSocketAsyncResult */
public SocketAsyncWorker Worker;
public int CurrentAddress; // Connect
public Socket AcceptedSocket;
public int Total;
internal int error;
public int EndCalled;
public IntPtr Handle {
get { return socket != null ? socket.Handle : IntPtr.Zero; }
}
/* Used by SocketAsyncEventArgs */
public SocketAsyncResult ()
: base ()
{
}
public SocketAsyncResult (Socket socket, object state, AsyncCallback callback, SocketOperation operation)
public void Init (Socket socket, AsyncCallback callback, object state, SocketOperation operation)
{
Init (socket, state, callback, operation, new SocketAsyncWorker (this));
}
base.Init (callback, state);
public object AsyncState {
get {
return state;
}
}
public WaitHandle AsyncWaitHandle {
get {
lock (this) {
if (wait_handle == null)
wait_handle = new ManualResetEvent (completed);
}
return wait_handle;
}
set {
wait_handle = value;
}
}
public bool CompletedSynchronously {
get {
return completed_synchronously;
}
}
public bool IsCompleted {
get {
return completed;
}
set {
completed = value;
lock (this) {
if (wait_handle != null && value)
((ManualResetEvent) wait_handle).Set ();
}
}
}
public Socket Socket {
get {
return accept_socket;
}
}
public int Total {
get { return total; }
set { total = value; }
}
public SocketError ErrorCode {
get {
SocketException ex = delayed_exception as SocketException;
if (ex != null)
return ex.SocketErrorCode;
if (error != 0)
return (SocketError) error;
return SocketError.Success;
}
}
public void Init (Socket socket, object state, AsyncCallback callback, SocketOperation operation, SocketAsyncWorker worker)
{
this.socket = socket;
this.is_blocking = socket != null ? socket.is_blocking : true;
this.handle = socket != null ? socket.Handle : IntPtr.Zero;
this.state = state;
this.callback = callback;
this.operation = operation;
if (wait_handle != null)
((ManualResetEvent) wait_handle).Reset ();
delayed_exception = null;
DelayedException = null;
EndPoint = null;
Buffer = null;
@ -172,39 +90,41 @@ namespace System.Net.Sockets
Port = 0;
Buffers = null;
ReuseSocket = false;
accept_socket = null;
total = 0;
CurrentAddress = 0;
AcceptedSocket = null;
Total = 0;
completed_synchronously = false;
completed = false;
is_blocking = false;
error = 0;
async_result = null;
EndCalled = 0;
Worker = worker;
}
public void DoMConnectCallback ()
public SocketAsyncResult (Socket socket, AsyncCallback callback, object state, SocketOperation operation)
: base (callback, state)
{
if (callback == null)
return;
ThreadPool.UnsafeQueueUserWorkItem (_ => callback (this), null);
this.socket = socket;
this.operation = operation;
}
public void Dispose ()
{
Init (null, null, null, 0, Worker);
if (wait_handle != null) {
wait_handle.Close ();
wait_handle = null;
public SocketError ErrorCode {
get {
SocketException ex = DelayedException as SocketException;
if (ex != null)
return ex.SocketErrorCode;
if (error != 0)
return (SocketError) error;
return SocketError.Success;
}
}
public void CheckIfThrowDelayedException ()
{
if (delayed_exception != null) {
if (DelayedException != null) {
socket.is_connected = false;
throw delayed_exception;
throw DelayedException;
}
if (error != 0) {
@ -213,7 +133,7 @@ namespace System.Net.Sockets
}
}
void CompleteDisposed (object unused)
internal override void CompleteDisposed ()
{
Complete ();
}
@ -221,11 +141,16 @@ namespace System.Net.Sockets
public void Complete ()
{
if (operation != SocketOperation.Receive && socket.is_disposed)
delayed_exception = new ObjectDisposedException (socket.GetType ().ToString ());
DelayedException = new ObjectDisposedException (socket.GetType ().ToString ());
IsCompleted = true;
Queue<SocketAsyncWorker> queue = null;
AsyncCallback callback = AsyncCallback;
if (callback != null) {
ThreadPool.UnsafeQueueUserWorkItem (_ => callback (this), null);
}
Queue<KeyValuePair<IntPtr, IOSelectorJob>> queue = null;
switch (operation) {
case SocketOperation.Receive:
case SocketOperation.ReceiveFrom:
@ -249,12 +174,12 @@ namespace System.Net.Sockets
queue.Dequeue (); /* remove ourselves */
if (queue.Count > 0) {
if (!socket.is_disposed) {
Socket.socket_pool_queue (SocketAsyncWorker.Dispatcher, (queue.Peek ()).result);
IOSelector.Add (queue.Peek ().Key, queue.Peek ().Value);
} else {
/* CompleteAllOnDispose */
SocketAsyncWorker [] workers = queue.ToArray ();
for (int i = 0; i < workers.Length; i++)
ThreadPool.UnsafeQueueUserWorkItem (workers [i].result.CompleteDisposed, null);
KeyValuePair<IntPtr, IOSelectorJob> [] jobs = queue.ToArray ();
for (int i = 0; i < jobs.Length; i++)
ThreadPool.QueueUserWorkItem (j => ((IOSelectorJob) j).MarkDisposed (), jobs [i].Value);
queue.Clear ();
}
}
@ -266,53 +191,40 @@ namespace System.Net.Sockets
public void Complete (bool synch)
{
this.completed_synchronously = synch;
CompletedSynchronously = synch;
Complete ();
}
public void Complete (int total)
{
this.total = total;
Total = total;
Complete ();
}
public void Complete (Exception e, bool synch)
{
this.completed_synchronously = synch;
this.delayed_exception = e;
DelayedException = e;
CompletedSynchronously = synch;
Complete ();
}
public void Complete (Exception e)
{
this.delayed_exception = e;
DelayedException = e;
Complete ();
}
public void Complete (Socket s)
{
this.accept_socket = s;
AcceptedSocket = s;
Complete ();
}
public void Complete (Socket s, int total)
{
this.accept_socket = s;
this.total = total;
AcceptedSocket = s;
Total = total;
Complete ();
}
void IThreadPoolWorkItem.ExecuteWorkItem()
{
async_result.Invoke ();
if (completed && callback != null) {
ThreadPool.UnsafeQueueCustomWorkItem (new AsyncResult (state => callback ((IAsyncResult) state), this, false), false);
}
}
void IThreadPoolWorkItem.MarkAborted(ThreadAbortException tae)
{
}
}
}