static void BeginSendToCallback (SocketAsyncResult sockares, int sent_so_far) { int total = 0; try { total = sockares.socket.SendTo_nochecks (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, sockares.EndPoint); if (sockares.error == 0) { sent_so_far += total; sockares.Offset += total; sockares.Size -= total; } if (sockares.Size > 0) { IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, sent_so_far), sockares)); return; // Have to finish writing everything. See bug #74475. } sockares.Total = sent_so_far; } catch (Exception e) { sockares.Complete (e); return; } sockares.Complete (); }
public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, SocketFlags socket_flags, ref EndPoint remote_end, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); ThrowIfBufferNull (buffer); ThrowIfBufferOutOfRange (buffer, offset, size); if (remote_end == null) throw new ArgumentNullException ("remote_end"); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveFrom) { Buffer = buffer, Offset = offset, Size = size, SockFlags = socket_flags, EndPoint = remote_end, }; QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveFromCallback, sockares)); return sockares; }
public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (buffers == null) throw new ArgumentNullException ("buffers"); if (!is_connected) throw new SocketException ((int)SocketError.NotConnected); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendGeneric) { Buffers = buffers, SockFlags = socketFlags, }; QueueIOSelectorJob (writeQ, sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginSendGenericCallback, sockares)); return sockares; }
internal IAsyncResult BeginMConnect (SocketAsyncResult sockares) { SocketAsyncResult ares = null; Exception exc = null; AsyncCallback callback; for (int i = sockares.CurrentAddress; i < sockares.Addresses.Length; i++) { try { sockares.CurrentAddress++; ares = (SocketAsyncResult) BeginConnect (new IPEndPoint (sockares.Addresses [i], sockares.Port), null, sockares); if (ares.IsCompleted && ares.CompletedSynchronously) { ares.CheckIfThrowDelayedException (); callback = ares.AsyncCallback; if (callback != null) ThreadPool.UnsafeQueueUserWorkItem (_ => callback (ares), null); } break; } catch (Exception e) { exc = e; ares = null; } } if (ares == null) throw exc; return sockares; }
public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); ThrowIfBufferNull (buffer); ThrowIfBufferOutOfRange (buffer, offset, size); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Receive) { Buffer = buffer, Offset = offset, Size = size, SockFlags = socket_flags, }; QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveCallback, sockares)); return sockares; }
public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (receiveSize < 0) throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero"); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) { Buffer = new byte [receiveSize], Offset = 0, Size = receiveSize, SockFlags = SocketFlags.None, }; QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptReceiveCallback, sockares)); return sockares; }
public IAsyncResult BeginConnect (EndPoint end_point, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (end_point == null) throw new ArgumentNullException ("end_point"); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) { EndPoint = end_point, }; // Bug #75154: Connect() should not succeed for .Any addresses. if (end_point is IPEndPoint) { IPEndPoint ep = (IPEndPoint) end_point; if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) { sockares.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true); return sockares; } } int error = 0; if (connect_in_progress) { // This could happen when multiple IPs are used // Calling connect() again will reset the connection attempt and cause // an error. Better to just close the socket and move on. connect_in_progress = false; safe_handle.Dispose (); safe_handle = new SafeSocketHandle (Socket_internal (address_family, socket_type, protocol_type, out error), true); if (error != 0) throw new SocketException (error); } bool blk = is_blocking; if (blk) Blocking = false; Connect_internal (safe_handle, end_point.Serialize (), out error); if (blk) Blocking = true; if (error == 0) { // succeeded synch is_connected = true; is_bound = true; sockares.Complete (true); return sockares; } if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) { // error synch is_connected = false; is_bound = false; sockares.Complete (new SocketException (error), true); return sockares; } // continue asynch is_connected = false; is_bound = false; connect_in_progress = true; IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginConnectCallback, sockares)); return sockares; }
static extern void socket_pool_queue (SocketAsyncCall d, SocketAsyncResult r);
public Worker (SocketAsyncEventArgs args) { this.args = args; result = new SocketAsyncResult (); }
static void DispatcherCB (SocketAsyncResult sar) { SocketOperation op = sar.operation; if (op == Socket.SocketOperation.Receive || op == Socket.SocketOperation.ReceiveGeneric || op == Socket.SocketOperation.RecvJustCallback) sar.Worker.Receive (); else if (op == Socket.SocketOperation.Send || op == Socket.SocketOperation.SendGeneric || op == Socket.SocketOperation.SendJustCallback) sar.Worker.Send (); else if (op == Socket.SocketOperation.ReceiveFrom) sar.Worker.ReceiveFrom (); else if (op == Socket.SocketOperation.SendTo) sar.Worker.SendTo (); else if (op == Socket.SocketOperation.Connect) sar.Worker.Connect (); else if (op == Socket.SocketOperation.Accept) sar.Worker.Accept (); else if (op == Socket.SocketOperation.AcceptReceive) sar.Worker.AcceptReceive (); else if (op == Socket.SocketOperation.Disconnect) sar.Worker.Disconnect (); // SendPackets and ReceiveMessageFrom are not implemented yet /* else if (op == Socket.SocketOperation.ReceiveMessageFrom) async_op = SocketAsyncOperation.ReceiveMessageFrom; else if (op == Socket.SocketOperation.SendPackets) async_op = SocketAsyncOperation.SendPackets; */ else throw new NotImplementedException (String.Format ("Operation {0} is not implemented", op)); }
public void Connect () { if (result.EndPoint == null) { result.Complete (new SocketException ((int)SocketError.AddressNotAvailable)); return; } SocketAsyncResult mconnect = result.AsyncState as SocketAsyncResult; bool is_mconnect = (mconnect != null && mconnect.Addresses != null); try { int error_code; EndPoint ep = result.EndPoint; error_code = (int) result.Sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error); if (error_code == 0) { if (is_mconnect) result = mconnect; result.Sock.seed_endpoint = ep; result.Sock.connected = true; result.Sock.isbound = true; result.Sock.connect_in_progress = false; result.error = 0; result.Complete (); if (is_mconnect) result.DoMConnectCallback (); return; } if (!is_mconnect) { result.Sock.connect_in_progress = false; result.Complete (new SocketException (error_code)); return; } if (mconnect.CurrentAddress >= mconnect.Addresses.Length) { mconnect.Complete (new SocketException (error_code)); if (is_mconnect) mconnect.DoMConnectCallback (); return; } mconnect.Sock.BeginMConnect (mconnect); } catch (Exception e) { result.Sock.connect_in_progress = false; if (is_mconnect) result = mconnect; result.Complete (e); if (is_mconnect) result.DoMConnectCallback (); return; } }
IAsyncResult BeginMConnect (SocketAsyncResult req) { IAsyncResult ares = null; Exception exc = null; for (int i = req.CurrentAddress; i < req.Addresses.Length; i++) { IPAddress addr = req.Addresses [i]; IPEndPoint ep = new IPEndPoint (addr, req.Port); try { req.CurrentAddress++; ares = BeginConnect (ep, null, req); if (ares.IsCompleted && ares.CompletedSynchronously) { ((SocketAsyncResult) ares).CheckIfThrowDelayedException (); req.DoMConnectCallback (); } break; } catch (Exception e) { exc = e; ares = null; } } if (ares == null) throw exc; return req; }
IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (addresses == null) throw new ArgumentNullException ("addresses"); if (addresses.Length == 0) throw new ArgumentException ("Empty addresses list"); if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6) throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families"); if (port <= 0 || port > 65535) throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536"); if (islistening) throw new InvalidOperationException (); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect); req.Addresses = addresses; req.Port = port; connected = false; return BeginMConnect (req); }
IAsyncResult BeginConnect(EndPoint end_point, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (end_point == null) throw new ArgumentNullException ("end_point"); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect); req.EndPoint = end_point; // Bug #75154: Connect() should not succeed for .Any addresses. if (end_point is IPEndPoint) { IPEndPoint ep = (IPEndPoint) end_point; if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) { req.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true); return req; } } int error = 0; if (connect_in_progress) { // This could happen when multiple IPs are used // Calling connect() again will reset the connection attempt and cause // an error. Better to just close the socket and move on. connect_in_progress = false; Close_internal (socket, out error); socket = Socket_internal (address_family, socket_type, protocol_type, out error); if (error != 0) throw new SocketException (error); } bool blk = blocking; if (blk) Blocking = false; SocketAddress serial = end_point.Serialize (); Connect_internal (socket, serial, out error); if (blk) Blocking = true; if (error == 0) { // succeeded synch connected = true; isbound = true; req.Complete (true); return req; } if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) { // error synch connected = false; isbound = false; req.Complete (new SocketException (error), true); return req; } // continue asynch connected = false; isbound = false; connect_in_progress = true; socket_pool_queue (Worker.Dispatcher, req); return req; }
static void socket_pool_queue(SocketAsyncCall d, SocketAsyncResult r) { throw new System.NotImplementedException(); }
public Worker (SocketAsyncResult ares) { this.result = ares; }
public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (buffers == null) throw new ArgumentNullException ("buffers"); if (!connected) throw new SocketException ((int)SocketError.NotConnected); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.SendGeneric); req.Buffers = buffers; req.SockFlags = socketFlags; int count; lock (writeQ) { writeQ.Enqueue (req.Worker); count = writeQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return req; }
public void Dispose () { if (result != null) { result.Dispose (); result = null; args = null; } }
public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (receiveSize < 0) throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero"); if (acceptSocket != null) { ThrowIfDisposedAndClosed (acceptSocket); if (acceptSocket.IsBound) throw new InvalidOperationException (); /* For some reason the MS runtime * barfs if the new socket is not TCP, * even though it's just about to blow * away all those parameters */ if (acceptSocket.ProtocolType != ProtocolType.Tcp) throw new SocketException ((int)SocketError.InvalidArgument); } SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) { Buffer = new byte [receiveSize], Offset = 0, Size = receiveSize, SockFlags = SocketFlags.None, AcceptSocket = acceptSocket, }; QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptReceiveCallback, sockares)); return sockares; }
public IAsyncResult BeginAccept(AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (!isbound || !islistening) throw new InvalidOperationException (); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Accept); int count; lock (readQ) { readQ.Enqueue (req.Worker); count = readQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return req; }
public IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (addresses == null) throw new ArgumentNullException ("addresses"); if (addresses.Length == 0) throw new ArgumentException ("Empty addresses list"); if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6) throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families"); if (port <= 0 || port > 65535) throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536"); if (is_listening) throw new InvalidOperationException (); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) { Addresses = addresses, Port = port, }; is_connected = false; return BeginMConnect (sockares); }
public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (receiveSize < 0) throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero"); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive); req.Buffer = new byte[receiveSize]; req.Offset = 0; req.Size = receiveSize; req.SockFlags = SocketFlags.None; int count; lock (readQ) { readQ.Enqueue (req.Worker); count = readQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return req; }
public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Disconnect) { ReuseSocket = reuseSocket, }; IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginDisconnectCallback, sockares)); return sockares; }
public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (receiveSize < 0) throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero"); if (acceptSocket != null) { if (acceptSocket.disposed && acceptSocket.closed) throw new ObjectDisposedException (acceptSocket.GetType ().ToString ()); if (acceptSocket.IsBound) throw new InvalidOperationException (); /* For some reason the MS runtime * barfs if the new socket is not TCP, * even though it's just about to blow * away all those parameters */ if (acceptSocket.ProtocolType != ProtocolType.Tcp) throw new SocketException ((int)SocketError.InvalidArgument); } SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive); req.Buffer = new byte[receiveSize]; req.Offset = 0; req.Size = receiveSize; req.SockFlags = SocketFlags.None; req.AcceptSocket = acceptSocket; int count; lock (readQ) { readQ.Enqueue (req.Worker); count = readQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return(req); }
public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (buffers == null) throw new ArgumentNullException ("buffers"); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveGeneric) { Buffers = buffers, SockFlags = socketFlags, }; QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveGenericCallback, sockares)); return sockares; }
public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Disconnect); req.ReuseSocket = reuseSocket; socket_pool_queue (Worker.Dispatcher, req); return(req); }
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); ThrowIfBufferNull (buffer); ThrowIfBufferOutOfRange (buffer, offset, size); if (!is_connected) throw new SocketException ((int)SocketError.NotConnected); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Send) { Buffer = buffer, Offset = offset, Size = size, SockFlags = socket_flags, }; QueueIOSelectorJob (writeQ, sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendCallback ((SocketAsyncResult) s, 0), sockares)); return sockares; }
public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socket_flags, ref EndPoint remote_end, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (buffer == null) throw new ArgumentNullException ("buffer"); if (remote_end == null) throw new ArgumentNullException ("remote_end"); CheckRange (buffer, offset, size); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveFrom); req.Buffer = buffer; req.Offset = offset; req.Size = size; req.SockFlags = socket_flags; req.EndPoint = remote_end; int count; lock (readQ) { readQ.Enqueue (req.Worker); count = readQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return req; }
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socket_flags, EndPoint remote_end, AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); ThrowIfBufferNull (buffer); ThrowIfBufferOutOfRange (buffer, offset, size); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendTo) { Buffer = buffer, Offset = offset, Size = size, SockFlags = socket_flags, EndPoint = remote_end, }; QueueIOSelectorJob (writeQ, sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, 0), sockares)); return sockares; }
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state) { if (disposed && closed) throw new ObjectDisposedException (GetType ().ToString ()); if (buffer == null) throw new ArgumentNullException ("buffer"); CheckRange (buffer, offset, size); if (!connected) throw new SocketException ((int)SocketError.NotConnected); SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Send); req.Buffer = buffer; req.Offset = offset; req.Size = size; req.SockFlags = socket_flags; int count; lock (writeQ) { writeQ.Enqueue (req.Worker); count = writeQ.Count; } if (count == 1) socket_pool_queue (Worker.Dispatcher, req); return req; }
public IAsyncResult BeginAccept(AsyncCallback callback, object state) { ThrowIfDisposedAndClosed (); if (!is_bound || !is_listening) throw new InvalidOperationException (); SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Accept); QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptCallback, sockares)); return sockares; }
static extern void socket_pool_queue(SocketAsyncCall d, SocketAsyncResult r);